History

remove unused code

Sigrid Solveig Haflínudóttir committed 1 year ago0.5

Commit 04522871 parent c36900426 files · +2 −1193Browse files

Unified · Split

26 files
  1. Makefile +2 −16
  2. libc/dorfmt.c +0 −46
  3. libc/encodefmt.c +0 −78
  4. libc/fmt.c +0 −7
  5. libc/fmtlock.c +0 −16
  6. libc/fmtquote.c +0 −249
  7. libc/fmtstr.c +0 −12
  8. libc/nan64.c +0 −12
  9. libc/rune.c +0 −22
  10. libc/snprint.c +0 −16
  11. libc/u16.c +0 −44
  12. libmp/cnfield.c +0 −114
  13. libmp/gmfield.c +0 −173
  14. libmp/letomp.c +0 −31
  15. libmp/mpaux.c +0 −40
  16. libmp/mpfield.c +0 −21
  17. libmp/mpinvert.c +0 −17
  18. libmp/mplogic.c +0 −6
  19. libmp/mpmodop.c +0 −61
  20. libmp/mpsel.c +0 −42
  21. libmp/mptole.c +0 −28
  22. libmp/mptolel.c +0 −33
  23. libmp/mptoui.c +0 −13
  24. libmp/mptouv.c +0 −47
  25. libmp/mptov.c +0 −33
  26. libmp/reduce +0 −16
Makefile+2−16
@@ -5,20 +5,16 @@ BIN=${DESTDIR}${PREFIX}/bin · 4 unmodified lines
5MAN=${DESTDIR}${PREFIX}/share/man/man15MAN=${DESTDIR}${PREFIX}/share/man/man1
6CFLAGS?=-g -O26CFLAGS?=-g -O2
7CFLAGS+=-Iinclude -Wall -Wno-missing-braces -Wno-parentheses -Wno-incompatible-pointer-types7CFLAGS+=-Iinclude -Wall -Wno-missing-braces -Wno-parentheses -Wno-incompatible-pointer-types
8LDFLAGS?=
8 9
9OBJS=\10OBJS=\
10 libc/dofmt.o\11 libc/dofmt.o\
11 libc/dorfmt.o\
12 libc/encodefmt.o\
13 libc/fltfmt.o\12 libc/fltfmt.o\
14 libc/fmt.o\13 libc/fmt.o\
15 libc/fmtfd.o\14 libc/fmtfd.o\
16 libc/fmtfdflush.o\15 libc/fmtfdflush.o\
17 libc/fmtlock.o\
18 libc/fmtprint.o\16 libc/fmtprint.o\
19 libc/fmtquote.o\
20 libc/fmtrune.o\17 libc/fmtrune.o\
21 libc/fmtstr.o\
22 libc/fmtvprint.o\18 libc/fmtvprint.o\
23 libc/fprint.o\19 libc/fprint.o\
24 libc/genrandom.o\20 libc/genrandom.o\
@@ -26,7 +22,6 @@ OBJS=\ · 1 unmodified line
26 libc/nan64.o\22 libc/nan64.o\
27 libc/print.o\23 libc/print.o\
28 libc/rune.o\24 libc/rune.o\
29 libc/snprint.o\
30 libc/sprint.o\25 libc/sprint.o\
31 libc/strtod.o\26 libc/strtod.o\
32 libc/sysfatal.o\27 libc/sysfatal.o\
@@ -37,9 +32,6 @@ OBJS=\ · 4 unmodified lines
37 libc/vfprint.o\32 libc/vfprint.o\
38 libc/vsnprint.o\33 libc/vsnprint.o\
39 libmp/betomp.o\34 libmp/betomp.o\
40 libmp/cnfield.o\
41 libmp/gmfield.o\
42 libmp/letomp.o\
43 libmp/mpadd.o\35 libmp/mpadd.o\
44 libmp/mpaux.o\36 libmp/mpaux.o\
45 libmp/mpcmp.o\37 libmp/mpcmp.o\
@@ -47,9 +39,7 @@ OBJS=\ · 1 unmodified line
47 libmp/mpdiv.o\39 libmp/mpdiv.o\
48 libmp/mpexp.o\40 libmp/mpexp.o\
49 libmp/mpextendedgcd.o\41 libmp/mpextendedgcd.o\
50 libmp/mpfield.o\
51 libmp/mpfmt.o\42 libmp/mpfmt.o\
52 libmp/mpinvert.o\
53 libmp/mpleft.o\43 libmp/mpleft.o\
54 libmp/mplogic.o\44 libmp/mplogic.o\
55 libmp/mpmod.o\45 libmp/mpmod.o\
@@ -58,15 +48,11 @@ OBJS=\ · 2 unmodified lines
58 libmp/mpnrand.o\48 libmp/mpnrand.o\
59 libmp/mprand.o\49 libmp/mprand.o\
60 libmp/mpright.o\50 libmp/mpright.o\
61 libmp/mpsel.o\
62 libmp/mpsub.o\51 libmp/mpsub.o\
63 libmp/mptobe.o\52 libmp/mptobe.o\
64 libmp/mptober.o\53 libmp/mptober.o\
65 libmp/mptoi.o\54 libmp/mptoi.o\
66 libmp/mptole.o\
67 libmp/mptolel.o\
68 libmp/mptoui.o\55 libmp/mptoui.o\
69 libmp/mptouv.o\
70 libmp/mptov.o\56 libmp/mptov.o\
71 libmp/mpvecadd.o\57 libmp/mpvecadd.o\
72 libmp/mpveccmp.o\58 libmp/mpveccmp.o\
@@ -95,7 +81,7 @@ clean: · 22 unmodified lines
95 rm -f ${TARGET} ${OBJS} ${TARGET}.c81 rm -f ${TARGET} ${OBJS} ${TARGET}.c
96 82
97${TARGET}: ${OBJS}83${TARGET}: ${OBJS}
98 ${CC} -o $@ ${OBJS}84 ${CC} -o $@ ${OBJS} ${LDFLAGS}
99 85
100pc.c: pc.y86pc.c: pc.y
101 yacc -o $@ $<87 yacc -o $@ $<
libc/dorfmt.c+0−46
@@ -1,46 +0,0 @@
1#include <u.h>
2#include <libc.h>
3#include "fmtdef.h"
4
5/* format the output into f->to and return the number of characters fmted */
6
7int
8dorfmt(Fmt *f, const Rune *fmt)
9{
10 Rune *rt, *rs;
11 int r;
12 char *t, *s;
13 int nfmt;
14
15 nfmt = f->nfmt;
16 for(;;){
17 if(f->runes){
18 rt = f->to;
19 rs = f->stop;
20 while((r = *fmt++) && r != '%'){
21 FMTRCHAR(f, rt, rs, r);
22 }
23 f->nfmt += rt - (Rune *)f->to;
24 f->to = rt;
25 if(!r)
26 return f->nfmt - nfmt;
27 f->stop = rs;
28 }else{
29 t = f->to;
30 s = f->stop;
31 while((r = *fmt++) && r != '%'){
32 FMTRUNE(f, t, f->stop, r);
33 }
34 f->nfmt += t - (char *)f->to;
35 f->to = t;
36 if(!r)
37 return f->nfmt - nfmt;
38 f->stop = s;
39 }
40
41 fmt = __fmtdispatch(f, (Rune*)fmt, 1);
42 if(fmt == nil)
43 return -1;
44 }
45 return 0; /* not reached */
46}
libc/encodefmt.c+0−78
@@ -1,78 +0,0 @@
1#include <u.h>
2#include <libc.h>
3#include <ctype.h>
4
5int
6encodefmt(Fmt *f)
7{
8 char *out;
9 char *buf;
10 int len;
11 int ilen;
12 int rv;
13 uchar *b;
14 char *p;
15 char obuf[64]; // rsc optimization
16
17 if(!(f->flags&FmtPrec) || f->prec < 1)
18 goto error;
19
20 b = va_arg(f->args, uchar*);
21 if(b == 0)
22 return fmtstrcpy(f, "<nil>");
23
24 ilen = f->prec;
25 f->prec = 0;
26 f->flags &= ~FmtPrec;
27 switch(f->r){
28 case '<':
29 len = (8*ilen+4)/5 + 3;
30 break;
31 case '[':
32 len = (8*ilen+5)/6 + 4;
33 break;
34 case 'H':
35 len = 2*ilen + 1;
36 break;
37 default:
38 goto error;
39 }
40
41 if(len > sizeof(obuf)){
42 buf = malloc(len);
43 if(buf == nil)
44 goto error;
45 } else
46 buf = obuf;
47
48 // convert
49 out = buf;
50 switch(f->r){
51 case '[':
52 rv = enc64(out, len, b, ilen);
53 break;
54 case '<':
55 rv = enc32(out, len, b, ilen);
56 break;
57 case 'H':
58 rv = enc16(out, len, b, ilen);
59 break;
60 default:
61 rv = -1;
62 break;
63 }
64 if(rv < 0)
65 goto error;
66
67 if((f->flags & FmtLong) != 0 && f->r != '[')
68 for(p = buf; *p; p++)
69 *p = tolower(*p);
70
71 fmtstrcpy(f, buf);
72 if(buf != obuf)
73 free(buf);
74 return 0;
75
76error:
77 return fmtstrcpy(f, "<encodefmt>");
78}
libc/fmt.c+0−7
@@ -65,8 +65,6 @@ static Convfmt knownfmt[] = { · 64 unmodified lines
65};65};
66 66
67 67
68int (*fmtdoquote)(int);
69
70/*68/*
71 * __fmtlock() must be set69 * __fmtlock() must be set
72 */70 */
@@ -102,9 +100,7 @@ fmtinstall(int c, int (*f)(Fmt*)) · 29 unmodified lines
102{100{
103 int ret;101 int ret;
104 102
105 __fmtlock();
106 ret = __fmtinstall(c, f);103 ret = __fmtinstall(c, f);
107 __fmtunlock();
108 return ret;104 return ret;
109}105}
110 106
@@ -122,14 +118,11 @@ fmtfmt(int c) · 11 unmodified lines
122 }118 }
123 119
124 /* is this a predefined format char? */120 /* is this a predefined format char? */
125 __fmtlock();
126 for(p=knownfmt; p->c; p++)121 for(p=knownfmt; p->c; p++)
127 if(p->c == c){122 if(p->c == c){
128 __fmtinstall(p->c, p->fmt);123 __fmtinstall(p->c, p->fmt);
129 __fmtunlock();
130 return p->fmt;124 return p->fmt;
131 }125 }
132 __fmtunlock();
133 126
134 return __badfmt;127 return __badfmt;
135}128}
libc/fmtlock.c+0−16
@@ -1,16 +0,0 @@
1#include <u.h>
2#include <libc.h>
3
4static Lock fmtl;
5
6void
7__fmtlock(void)
8{
9 lock(&fmtl);
10}
11
12void
13__fmtunlock(void)
14{
15 unlock(&fmtl);
16}
libc/fmtquote.c+0−249
@@ -1,249 +0,0 @@
1#include <u.h>
2#include <libc.h>
3#include "fmtdef.h"
4
5/*
6 * How many bytes of output UTF will be produced by quoting (if necessary) this string?
7 * How many runes? How much of the input will be consumed?
8 * The parameter q is filled in by __quotesetup.
9 * The string may be UTF or Runes (s or r).
10 * Return count does not include NUL.
11 * Terminate the scan at the first of:
12 * NUL in input
13 * count exceeded in input
14 * count exceeded on output
15 * *ninp is set to number of input bytes accepted.
16 * nin may be <0 initially, to avoid checking input by count.
17 */
18void
19__quotesetup(char *s, Rune *r, int nin, int nout, Quoteinfo *q, int sharp, int runesout)
20{
21 int w;
22 Rune c;
23
24 q->quoted = 0;
25 q->nbytesout = 0;
26 q->nrunesout = 0;
27 q->nbytesin = 0;
28 q->nrunesin = 0;
29 if(sharp || nin==0 || (s && *s=='\0') || (r && *r=='\0')){
30 if(nout < 2)
31 return;
32 q->quoted = 1;
33 q->nbytesout = 2;
34 q->nrunesout = 2;
35 }
36 for(; nin!=0; nin--){
37 if(s)
38 w = chartorune(&c, s);
39 else{
40 c = *r;
41 w = runelen(c);
42 }
43
44 if(c == '\0')
45 break;
46 if(runesout){
47 if(q->nrunesout+1 > nout)
48 break;
49 }else{
50 if(q->nbytesout+w > nout)
51 break;
52 }
53
54 if((c <= L' ') || (c == L'\'') || (fmtdoquote!=0 && fmtdoquote(c))){
55 if(!q->quoted){
56 if(runesout){
57 if(1+q->nrunesout+1+1 > nout) /* no room for quotes */
58 break;
59 }else{
60 if(1+q->nbytesout+w+1 > nout) /* no room for quotes */
61 break;
62 }
63 q->nrunesout += 2; /* include quotes */
64 q->nbytesout += 2; /* include quotes */
65 q->quoted = 1;
66 }
67 if(c == '\'') {
68 if(runesout){
69 if(1+q->nrunesout+1 > nout) /* no room for quotes */
70 break;
71 }else{
72 if(1+q->nbytesout+w > nout) /* no room for quotes */
73 break;
74 }
75 q->nbytesout++;
76 q->nrunesout++; /* quotes reproduce as two characters */
77 }
78 }
79
80 /* advance input */
81 if(s)
82 s += w;
83 else
84 r++;
85 q->nbytesin += w;
86 q->nrunesin++;
87
88 /* advance output */
89 q->nbytesout += w;
90 q->nrunesout++;
91 }
92}
93
94static int
95qstrfmt(char *sin, Rune *rin, Quoteinfo *q, Fmt *f)
96{
97 Rune r, *rm, *rme;
98 char *t, *s, *m, *me;
99 Rune *rt, *rs;
100 ulong fl;
101 int nc, w;
102
103 m = sin;
104 me = m + q->nbytesin;
105 rm = rin;
106 rme = rm + q->nrunesin;
107
108 w = f->width;
109 fl = f->flags;
110 if(f->runes){
111 if(!(fl & FmtLeft) && __rfmtpad(f, w - q->nrunesout) < 0)
112 return -1;
113 }else{
114 if(!(fl & FmtLeft) && __fmtpad(f, w - q->nbytesout) < 0)
115 return -1;
116 }
117 t = (char*)f->to;
118 s = (char*)f->stop;
119 rt = (Rune*)f->to;
120 rs = (Rune*)f->stop;
121 if(f->runes)
122 FMTRCHAR(f, rt, rs, '\'');
123 else
124 FMTRUNE(f, t, s, '\'');
125 for(nc = q->nrunesin; nc > 0; nc--){
126 if(sin){
127 r = *(uchar*)m;
128 if(r < Runeself)
129 m++;
130 else if((me - m) >= UTFmax || fullrune(m, me-m))
131 m += chartorune(&r, m);
132 else
133 break;
134 }else{
135 if(rm >= rme)
136 break;
137 r = *(uchar*)rm++;
138 }
139 if(f->runes){
140 FMTRCHAR(f, rt, rs, r);
141 if(r == '\'')
142 FMTRCHAR(f, rt, rs, r);
143 }else{
144 FMTRUNE(f, t, s, r);
145 if(r == '\'')
146 FMTRUNE(f, t, s, r);
147 }
148 }
149
150 if(f->runes){
151 FMTRCHAR(f, rt, rs, '\'');
152 USED(rs);
153 f->nfmt += rt - (Rune *)f->to;
154 f->to = rt;
155 if(fl & FmtLeft && __rfmtpad(f, w - q->nrunesout) < 0)
156 return -1;
157 }else{
158 FMTRUNE(f, t, s, '\'');
159 USED(s);
160 f->nfmt += t - (char *)f->to;
161 f->to = t;
162 if(fl & FmtLeft && __fmtpad(f, w - q->nbytesout) < 0)
163 return -1;
164 }
165 return 0;
166}
167
168int
169__quotestrfmt(int runesin, Fmt *f)
170{
171 int nin, outlen;
172 Rune *r;
173 char *s;
174 Quoteinfo q;
175
176 nin = -1;
177 if(f->flags&FmtPrec)
178 nin = f->prec;
179 if(runesin){
180 r = va_arg(f->args, Rune *);
181 s = nil;
182 }else{
183 s = va_arg(f->args, char *);
184 r = nil;
185 }
186 if(!s && !r)
187 return __fmtcpy(f, (void*)"<nil>", 5, 5);
188
189 if(f->flush)
190 outlen = 0x7FFFFFFF; /* if we can flush, no output limit */
191 else if(f->runes)
192 outlen = (Rune*)f->stop - (Rune*)f->to;
193 else
194 outlen = (char*)f->stop - (char*)f->to;
195
196 __quotesetup(s, r, nin, outlen, &q, f->flags&FmtSharp, f->runes);
197//print("bytes in %d bytes out %d runes in %d runesout %d\n", q.nbytesin, q.nbytesout, q.nrunesin, q.nrunesout);
198
199 if(runesin){
200 if(!q.quoted)
201 return __fmtrcpy(f, r, q.nrunesin);
202 return qstrfmt(nil, r, &q, f);
203 }
204
205 if(!q.quoted)
206 return __fmtcpy(f, s, q.nrunesin, q.nbytesin);
207 return qstrfmt(s, nil, &q, f);
208}
209
210int
211quotestrfmt(Fmt *f)
212{
213 return __quotestrfmt(0, f);
214}
215
216int
217quoterunestrfmt(Fmt *f)
218{
219 return __quotestrfmt(1, f);
220}
221
222void
223quotefmtinstall(void)
224{
225 fmtinstall('q', quotestrfmt);
226 fmtinstall('Q', quoterunestrfmt);
227}
228
229int
230__needsquotes(char *s, int *quotelenp)
231{
232 Quoteinfo q;
233
234 __quotesetup(s, nil, -1, 0x7FFFFFFF, &q, 0, 0);
235 *quotelenp = q.nbytesout;
236
237 return q.quoted;
238}
239
240int
241__runeneedsquotes(Rune *r, int *quotelenp)
242{
243 Quoteinfo q;
244
245 __quotesetup(nil, r, -1, 0x7FFFFFFF, &q, 0, 0);
246 *quotelenp = q.nrunesout;
247
248 return q.quoted;
249}
libc/fmtstr.c+0−12
@@ -1,12 +0,0 @@
1#include <u.h>
2#include <libc.h>
3#include "fmtdef.h"
4
5char*
6fmtstrflush(Fmt *f)
7{
8 if(f->start == nil)
9 return nil;
10 *(char*)f->to = '\0';
11 return (char*)f->start;
12}
libc/nan64.c+0−12
@@ -38,18 +38,6 @@ __isNaN(double d) · 37 unmodified lines
38 return (ulong)(x>>32)==0x7FF00000 && !__isInf(d, 0);38 return (ulong)(x>>32)==0x7FF00000 && !__isInf(d, 0);
39}39}
40 40
41double
42__Inf(int sign)
43{
44 uvlong *p;
45
46 if(sign < 0)
47 p = &uvinf;
48 else
49 p = &uvneginf;
50 return *(double*)p;
51}
52
53int41int
54__isInf(double d, int sign)42__isInf(double d, int sign)
55{43{
libc/rune.c+0−22
@@ -163,28 +163,6 @@ runelen(long c) · 162 unmodified lines
163 return runetochar(str, &rune);163 return runetochar(str, &rune);
164}164}
165 165
166int
167runenlen(Rune *r, int nrune)
168{
169 int nb, c;
170
171 nb = 0;
172 while(nrune--) {
173 c = *r++;
174 if(c <= Rune1)
175 nb++;
176 else
177 if(c <= Rune2)
178 nb += 2;
179 else
180 if(c <= Rune3 || c > Runemax)
181 nb += 3;
182 else
183 nb += 4;
184 }
185 return nb;
186}
187
188int166int
189fullrune(char *str, int n)167fullrune(char *str, int n)
190{168{
libc/snprint.c+0−16
@@ -1,16 +0,0 @@
1#include <u.h>
2#include <libc.h>
3#include "fmtdef.h"
4
5int
6snprint(char *buf, int len, char *fmt, ...)
7{
8 int n;
9 va_list args;
10
11 va_start(args, fmt);
12 n = vsnprint(buf, len, fmt, args);
13 va_end(args);
14 return n;
15}
16
libc/u16.c+0−44
@@ -23,47 +23,3 @@ dec16chr(int c) · 22 unmodified lines
23 o |= between(c, 'a', 'f') & (1+10+(c-'a'));23 o |= between(c, 'a', 'f') & (1+10+(c-'a'));
24 return o-1;24 return o-1;
25}25}
26
27int
28dec16(uchar *out, int lim, char *in, int n)
29{
30 int c, w = 0, i = 0;
31 uchar *start = out;
32 uchar *eout = out + lim;
33
34 while(n-- > 0){
35 c = dec16chr(*in++);
36 if(c < 0)
37 continue;
38 w = (w<<4) + c;
39 i++;
40 if(i == 2){
41 if(out + 1 > eout)
42 goto exhausted;
43 *out++ = w;
44 w = 0;
45 i = 0;
46 }
47 }
48exhausted:
49 return out - start;
50}
51
52int
53enc16(char *out, int lim, uchar *in, int n)
54{
55 uint c;
56 char *eout = out + lim;
57 char *start = out;
58
59 while(n-- > 0){
60 c = *in++;
61 if(out + 2 >= eout)
62 goto exhausted;
63 *out++ = enc16chr(c>>4);
64 *out++ = enc16chr(c&15);
65 }
66exhausted:
67 *out = 0;
68 return out - start;
69}
libmp/cnfield.c+0−114
@@ -1,114 +0,0 @@
1#include "os.h"
2#include <mp.h>
3#include "dat.h"
4
5/*
6 * fast reduction for crandall numbers of the form: 2^n - c
7 */
8
9enum {
10 MAXDIG = 1024 / Dbits,
11};
12
13typedef struct CNfield CNfield;
14struct CNfield
15{
16 Mfield f;
17
18 mpint m[1];
19
20 int s;
21 mpdigit c;
22};
23
24static int
25cnreduce(Mfield *m, mpint *a, mpint *r)
26{
27 mpdigit q[MAXDIG-1], t[MAXDIG], d;
28 CNfield *f = (CNfield*)m;
29 int qn, tn, k;
30
31 k = f->f.m.top;
32 if((a->top - k) >= MAXDIG)
33 return -1;
34
35 mpleft(a, f->s, r);
36 if(r->top <= k)
37 mpbits(r, (k+1)*Dbits);
38
39 /* q = hi(r) */
40 qn = r->top - k;
41 memmove(q, r->p+k, qn*Dbytes);
42
43 /* r = lo(r) */
44 r->top = k;
45 r->sign = 1;
46
47 do {
48 /* t = q*c */
49 tn = qn+1;
50 memset(t, 0, tn*Dbytes);
51 mpvecdigmuladd(q, qn, f->c, t);
52
53 /* q = hi(t) */
54 qn = tn - k;
55 if(qn <= 0) qn = 0;
56 else memmove(q, t+k, qn*Dbytes);
57
58 /* r += lo(t) */
59 if(tn > k)
60 tn = k;
61 mpvecadd(r->p, k, t, tn, r->p);
62
63 /* if(r >= m) r -= m */
64 mpvecsub(r->p, k+1, f->m->p, k, t);
65 d = t[k];
66 for(tn = 0; tn < k; tn++)
67 r->p[tn] = (r->p[tn] & d) | (t[tn] & ~d);
68 } while(qn > 0);
69
70 if(f->s != 0)
71 mpright(r, f->s, r);
72 mpnorm(r);
73
74 return 0;
75}
76
77Mfield*
78cnfield(mpint *N)
79{
80 mpint *M, *C;
81 CNfield *f;
82 mpdigit d;
83 int s;
84
85 if(N->top <= 2 || N->top >= MAXDIG)
86 return nil;
87 f = nil;
88 d = N->p[N->top-1];
89 for(s = 0; (d & (mpdigit)1<<Dbits-1) == 0; s++)
90 d <<= 1;
91 C = mpnew(0);
92 M = mpcopy(N);
93 mpleft(N, s, M);
94 mpleft(mpone, M->top*Dbits, C);
95 mpsub(C, M, C);
96 if(C->top != 1)
97 goto out;
98 f = mallocz(sizeof(CNfield) + M->top*sizeof(mpdigit), 1);
99 if(f == nil)
100 goto out;
101 f->s = s;
102 f->c = C->p[0];
103 f->m->size = M->top;
104 f->m->p = (mpdigit*)&f[1];
105 mpassign(M, f->m);
106 mpassign(N, (mpint*)f);
107 f->f.reduce = cnreduce;
108 f->f.m.flags |= MPfield;
109out:
110 mpfree(M);
111 mpfree(C);
112
113 return (Mfield*)f;
114}
libmp/gmfield.c+0−173
@@ -1,173 +0,0 @@
1#include "os.h"
2#include <mp.h>
3#include "dat.h"
4
5/*
6 * fast reduction for generalized mersenne numbers (GM)
7 * using a series of additions and subtractions.
8 */
9
10enum {
11 MAXDIG = 1024/Dbits,
12};
13
14typedef struct GMfield GMfield;
15struct GMfield
16{
17 Mfield f;
18
19 mpint m2[1];
20
21 int nadd;
22 int nsub;
23 int indx[256];
24};
25
26static int
27gmreduce(Mfield *m, mpint *a, mpint *r)
28{
29 GMfield *g = (GMfield*)m;
30 mpdigit d0, t[MAXDIG];
31 int i, j, d, *x;
32
33 if(mpmagcmp(a, g->m2) >= 0)
34 return -1;
35
36 if(a != r)
37 mpassign(a, r);
38
39 d = g->f.m.top;
40 mpbits(r, (d+1)*Dbits*2);
41 memmove(t+d, r->p+d, d*Dbytes);
42
43 r->sign = 1;
44 r->top = d;
45 r->p[d] = 0;
46
47 if(g->nsub > 0)
48 mpvecdigmuladd(g->f.m.p, d, g->nsub, r->p);
49
50 x = g->indx;
51 for(i=0; i<g->nadd; i++){
52 t[0] = 0;
53 d0 = t[*x++];
54 for(j=1; j<d; j++)
55 t[j] = t[*x++];
56 t[0] = d0;
57
58 mpvecadd(r->p, d+1, t, d, r->p);
59 }
60
61 for(i=0; i<g->nsub; i++){
62 t[0] = 0;
63 d0 = t[*x++];
64 for(j=1; j<d; j++)
65 t[j] = t[*x++];
66 t[0] = d0;
67
68 mpvecsub(r->p, d+1, t, d, r->p);
69 }
70
71 mpvecdigmulsub(g->f.m.p, d, r->p[d], r->p);
72 r->p[d] = 0;
73
74 mpvecsub(r->p, d+1, g->f.m.p, d, r->p+d+1);
75 d0 = r->p[2*d+1];
76 for(j=0; j<d; j++)
77 r->p[j] = (r->p[j] & d0) | (r->p[j+d+1] & ~d0);
78
79 mpnorm(r);
80
81 return 0;
82}
83
84Mfield*
85gmfield(mpint *N)
86{
87 int i,j,d, s, *C, *X, *x, *e;
88 mpint *M, *T;
89 GMfield *g;
90
91 d = N->top;
92 if(d <= 2 || d > MAXDIG/2 || (mpsignif(N) % Dbits) != 0)
93 return nil;
94 g = nil;
95 T = mpnew(0);
96 M = mpcopy(N);
97 C = malloc(sizeof(int)*(d+1));
98 X = malloc(sizeof(int)*(d*d));
99 if(C == nil || X == nil)
100 goto out;
101
102 for(i=0; i<=d; i++){
103 if((M->p[i]>>8) != 0 && (~M->p[i]>>8) != 0)
104 goto out;
105 j = M->p[i];
106 C[d - i] = -j;
107 itomp(j, T);
108 mpleft(T, i*Dbits, T);
109 mpsub(M, T, M);
110 }
111 for(j=0; j<d; j++)
112 X[j] = C[d-j];
113 for(i=1; i<d; i++){
114 X[d*i] = X[d*(i-1) + d-1]*C[d];
115 for(j=1; j<d; j++)
116 X[d*i + j] = X[d*(i-1) + j-1] + X[d*(i-1) + d-1]*C[d-j];
117 }
118 g = mallocz(sizeof(GMfield) + (d+1)*sizeof(mpdigit)*2, 1);
119 if(g == nil)
120 goto out;
121
122 g->m2->p = (mpdigit*)&g[1];
123 g->m2->size = d*2+1;
124 mpmul(N, N, g->m2);
125 mpassign(N, (mpint*)g);
126 g->f.reduce = gmreduce;
127 g->f.m.flags |= MPfield;
128
129 s = 0;
130 x = g->indx;
131 e = x + nelem(g->indx) - d;
132 for(g->nadd=0; x <= e; x += d, g->nadd++){
133 s = 0;
134 for(i=0; i<d; i++){
135 for(j=0; j<d; j++){
136 if(X[d*i+j] > 0 && x[j] == 0){
137 X[d*i+j]--;
138 x[j] = d+i;
139 s = 1;
140 break;
141 }
142 }
143 }
144 if(s == 0)
145 break;
146 }
147 for(g->nsub=0; x <= e; x += d, g->nsub++){
148 s = 0;
149 for(i=0; i<d; i++){
150 for(j=0; j<d; j++){
151 if(X[d*i+j] < 0 && x[j] == 0){
152 X[d*i+j]++;
153 x[j] = d+i;
154 s = 1;
155 break;
156 }
157 }
158 }
159 if(s == 0)
160 break;
161 }
162 if(s != 0){
163 mpfree((mpint*)g);
164 g = nil;
165 }
166out:
167 free(C);
168 free(X);
169 mpfree(M);
170 mpfree(T);
171 return (Mfield*)g;
172}
173
libmp/letomp.c+0−31
@@ -1,31 +0,0 @@
1#include "os.h"
2#include <mp.h>
3#include "dat.h"
4
5// convert a little endian byte array (least significant byte first) to an mpint
6mpint*
7letomp(uchar *s, uint n, mpint *b)
8{
9 int i=0, m = 0;
10 mpdigit x=0;
11
12 if(b == nil){
13 b = mpnew(0);
14 setmalloctag(b, getcallerpc(&s));
15 }
16 mpbits(b, 8*n);
17 for(; n > 0; n--){
18 x |= ((mpdigit)(*s++)) << i;
19 i += 8;
20 if(i == Dbits){
21 b->p[m++] = x;
22 i = 0;
23 x = 0;
24 }
25 }
26 if(i > 0)
27 b->p[m++] = x;
28 b->top = m;
29 b->sign = 1;
30 return mpnorm(b);
31}
libmp/mpaux.c+0−40
@@ -31,17 +31,6 @@ mpint *mpzero = &_mpzero; · 30 unmodified lines
31 31
32static int mpmindigits = 33;32static int mpmindigits = 33;
33 33
34// set minimum digit allocation
35void
36mpsetminbits(int n)
37{
38 if(n < 0)
39 sysfatal("mpsetminbits: n < 0");
40 if(n == 0)
41 n = 1;
42 mpmindigits = DIGITS(n);
43}
44
45// allocate an n bit 0'd number 34// allocate an n bit 0'd number
46mpint*35mpint*
47mpnew(int n)36mpnew(int n)
@@ -174,32 +163,3 @@ mpsignif(mpint *n) · 126 unmodified lines
174 }163 }
175 return 0;164 return 0;
176}165}
177
178// k, where n = 2**k * q for odd q
179int
180mplowbits0(mpint *n)
181{
182 int k, bit, digit;
183 mpdigit d;
184
185 assert(n->flags & MPnorm);
186 if(n->top==0)
187 return 0;
188 k = 0;
189 bit = 0;
190 digit = 0;
191 d = n->p[0];
192 for(;;){
193 if(d & (1<<bit))
194 break;
195 k++;
196 bit++;
197 if(bit==Dbits){
198 if(++digit >= n->top)
199 return 0;
200 d = n->p[digit];
201 bit = 0;
202 }
203 }
204 return k;
205}
libmp/mpfield.c+0−21
@@ -1,21 +0,0 @@
1#include "os.h"
2#include <mp.h>
3#include "dat.h"
4
5mpint*
6mpfield(mpint *N)
7{
8 Mfield *f;
9
10 if(N == nil || N->flags & (MPfield|MPstatic))
11 return N;
12 if((f = cnfield(N)) != nil)
13 goto Exchange;
14 if((f = gmfield(N)) != nil)
15 goto Exchange;
16 return N;
17Exchange:
18 setmalloctag(f, getcallerpc(&N));
19 mpfree(N);
20 return (mpint*)f;
21}
libmp/mpinvert.c+0−17
@@ -1,17 +0,0 @@
1#include "os.h"
2#include <mp.h>
3
4// use extended gcd to find the multiplicative inverse
5// res = b**-1 mod m
6void
7mpinvert(mpint *b, mpint *m, mpint *res)
8{
9 mpint *v;
10
11 v = mpnew(0);
12 mpextendedgcd(b, m, v, res, nil);
13 if(mpcmp(v, mpone) != 0)
14 abort();
15 mpfree(v);
16 mpmod(res, m, res);
17}
libmp/mplogic.c+0−6
@@ -77,12 +77,6 @@ mpand(mpint *b1, mpint *b2, mpint *sum) · 76 unmodified lines
77 mplogic(b1, b2, sum, 0x2a);77 mplogic(b1, b2, sum, 0x2a);
78}78}
79 79
80void
81mpbic(mpint *b1, mpint *b2, mpint *sum)
82{
83 mplogic(b1, b2, sum, 0x22);
84}
85
86void80void
87mpnot(mpint *b, mpint *r)81mpnot(mpint *b, mpint *r)
88{82{
libmp/mpmodop.c+0−61
@@ -16,67 +16,6 @@ modarg(mpint *a, mpint *m) · 15 unmodified lines
16 return a;16 return a;
17}17}
18 18
19void
20mpmodadd(mpint *b1, mpint *b2, mpint *m, mpint *sum)
21{
22 mpint *a, *b;
23 mpdigit d;
24 int i, j;
25
26 a = modarg(b1, m);
27 b = modarg(b2, m);
28
29 sum->flags |= (a->flags | b->flags) & MPtimesafe;
30 mpbits(sum, Dbits*2*(m->top+1));
31
32 mpvecadd(a->p, m->top, b->p, m->top, sum->p);
33 mpvecsub(sum->p, m->top+1, m->p, m->top, sum->p+m->top+1);
34
35 d = sum->p[2*m->top+1];
36 for(i = 0, j = m->top+1; i < m->top; i++, j++)
37 sum->p[i] = (sum->p[i] & d) | (sum->p[j] & ~d);
38
39 sum->top = m->top;
40 sum->sign = 1;
41 mpnorm(sum);
42
43 if(a != b1)
44 mpfree(a);
45 if(b != b2)
46 mpfree(b);
47}
48
49void
50mpmodsub(mpint *b1, mpint *b2, mpint *m, mpint *diff)
51{
52 mpint *a, *b;
53 mpdigit d;
54 int i, j;
55
56 a = modarg(b1, m);
57 b = modarg(b2, m);
58
59 diff->flags |= (a->flags | b->flags) & MPtimesafe;
60 mpbits(diff, Dbits*2*(m->top+1));
61
62 a->p[m->top] = 0;
63 mpvecsub(a->p, m->top+1, b->p, m->top, diff->p);
64 mpvecadd(diff->p, m->top, m->p, m->top, diff->p+m->top+1);
65
66 d = ~diff->p[m->top];
67 for(i = 0, j = m->top+1; i < m->top; i++, j++)
68 diff->p[i] = (diff->p[i] & d) | (diff->p[j] & ~d);
69
70 diff->top = m->top;
71 diff->sign = 1;
72 mpnorm(diff);
73
74 if(a != b1)
75 mpfree(a);
76 if(b != b2)
77 mpfree(b);
78}
79
80void19void
81mpmodmul(mpint *b1, mpint *b2, mpint *m, mpint *prod)20mpmodmul(mpint *b1, mpint *b2, mpint *m, mpint *prod)
82{21{
libmp/mpsel.c+0−42
@@ -1,42 +0,0 @@
1#include "os.h"
2#include <mp.h>
3#include "dat.h"
4
5// res = s != 0 ? b1 : b2
6void
7mpsel(int s, mpint *b1, mpint *b2, mpint *res)
8{
9 mpdigit d;
10 int n, m, i;
11
12 res->flags |= (b1->flags | b2->flags) & MPtimesafe;
13 if((res->flags & MPtimesafe) == 0){
14 mpassign(s ? b1 : b2, res);
15 return;
16 }
17 res->flags &= ~MPnorm;
18
19 n = b1->top;
20 m = b2->top;
21 mpbits(res, Dbits*(n >= m ? n : m));
22 res->top = n >= m ? n : m;
23
24 s = (-s^s|s)>>(sizeof(s)*8-1);
25 res->sign = (b1->sign & s) | (b2->sign & ~s);
26
27 d = -((mpdigit)s & 1);
28
29 i = 0;
30 while(i < n && i < m){
31 res->p[i] = (b1->p[i] & d) | (b2->p[i] & ~d);
32 i++;
33 }
34 while(i < n){
35 res->p[i] = b1->p[i] & d;
36 i++;
37 }
38 while(i < m){
39 res->p[i] = b2->p[i] & ~d;
40 i++;
41 }
42}
libmp/mptole.c+0−28
@@ -1,28 +0,0 @@
1#include "os.h"
2#include <mp.h>
3#include "dat.h"
4
5// convert an mpint into a little endian byte array (least significant byte first)
6// return number of bytes converted
7// if p == nil, allocate and result array
8int
9mptole(mpint *b, uchar *p, uint n, uchar **pp)
10{
11 int m;
12
13 m = (mpsignif(b)+7)/8;
14 if(m == 0)
15 m++;
16 if(p == nil){
17 n = m;
18 p = malloc(n);
19 if(p == nil)
20 sysfatal("mptole: %r");
21 setmalloctag(p, getcallerpc(&b));
22 } else if(n < m)
23 return -1;
24 if(pp != nil)
25 *pp = p;
26 mptolel(b, p, n);
27 return m;
28}
libmp/mptolel.c+0−33
@@ -1,33 +0,0 @@
1#include "os.h"
2#include <mp.h>
3#include "dat.h"
4
5void
6mptolel(mpint *b, uchar *p, int n)
7{
8 int i, j, m;
9 mpdigit x;
10
11 memset(p, 0, n);
12
13 m = b->top*Dbytes;
14 if(m < n)
15 n = m;
16
17 i = 0;
18 while(n >= Dbytes){
19 n -= Dbytes;
20 x = b->p[i++];
21 for(j = 0; j < Dbytes; j++){
22 *p++ = x;
23 x >>= 8;
24 }
25 }
26 if(n > 0){
27 x = b->p[i];
28 for(j = 0; j < n; j++){
29 *p++ = x;
30 x >>= 8;
31 }
32 }
33}
libmp/mptoui.c+0−13
@@ -19,16 +19,3 @@ uitomp(uint i, mpint *b) · 18 unmodified lines
19 b->sign = 1;19 b->sign = 1;
20 return mpnorm(b);20 return mpnorm(b);
21}21}
22
23uint
24mptoui(mpint *b)
25{
26 uint x;
27
28 x = *b->p;
29 if(b->sign < 0)
30 x = 0;
31 else if(b->top > 1 || (sizeof(mpdigit) > sizeof(uint) && x > MAXUINT))
32 x = MAXUINT;
33 return x;
34}
libmp/mptouv.c+0−47
@@ -1,47 +0,0 @@
1#include "os.h"
2#include <mp.h>
3#include "dat.h"
4
5#define VLDIGITS (sizeof(vlong)/sizeof(mpdigit))
6
7/*
8 * this code assumes that a vlong is an integral number of
9 * mpdigits long.
10 */
11mpint*
12uvtomp(uvlong v, mpint *b)
13{
14 int s;
15
16 if(b == nil){
17 b = mpnew(VLDIGITS*Dbits);
18 setmalloctag(b, getcallerpc(&v));
19 }else
20 mpbits(b, VLDIGITS*Dbits);
21 b->sign = 1;
22 for(s = 0; s < VLDIGITS; s++){
23 b->p[s] = v;
24 v >>= sizeof(mpdigit)*8;
25 }
26 b->top = s;
27 return mpnorm(b);
28}
29
30uvlong
31mptouv(mpint *b)
32{
33 uvlong v;
34 int s;
35
36 if(b->top == 0 || b->sign < 0)
37 return 0LL;
38
39 if(b->top > VLDIGITS)
40 return -1LL;
41
42 v = 0ULL;
43 for(s = 0; s < b->top; s++)
44 v |= (uvlong)b->p[s]<<(s*sizeof(mpdigit)*8);
45
46 return v;
47}
libmp/mptov.c+0−33
@@ -28,36 +28,3 @@ vtomp(vlong v, mpint *b) · 27 unmodified lines
28 b->top = s;28 b->top = s;
29 return mpnorm(b);29 return mpnorm(b);
30}30}
31
32vlong
33mptov(mpint *b)
34{
35 uvlong v;
36 int s;
37
38 if(b->top == 0)
39 return 0LL;
40
41 if(b->top > VLDIGITS){
42 if(b->sign > 0)
43 return (vlong)MAXVLONG;
44 else
45 return (vlong)MINVLONG;
46 }
47
48 v = 0ULL;
49 for(s = 0; s < b->top; s++)
50 v |= (uvlong)b->p[s]<<(s*sizeof(mpdigit)*8);
51
52 if(b->sign > 0){
53 if(v > MAXVLONG)
54 v = MAXVLONG;
55 } else {
56 if(v > MINVLONG)
57 v = MINVLONG;
58 else
59 v = -(vlong)v;
60 }
61
62 return (vlong)v;
63}
libmp/reduce+0−16
@@ -1,16 +0,0 @@
1O=$1
2shift
3objtype=$1
4shift
5
6ls -p ../$objtype/*.[cs] >[2]/dev/null | sed 's/..$//' > /tmp/reduce.$pid
7#
8# if empty directory, just return the input files
9#
10if (! ~ $status '|') {
11 echo $*
12 rm /tmp/reduce.$pid
13 exit 0
14}
15echo $* | tr ' ' \012 | grep -v -f /tmp/reduce.$pid | tr \012 ' '
16rm /tmp/reduce.$pid

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t