PERFORCE change 88224 for review
Matthew N. Dodd
mdodd at FreeBSD.org
Wed Dec 14 21:29:30 PST 2005
http://perforce.freebsd.org/chv.cgi?CH=88224
Change 88224 by mdodd at mdodd_ibmtp600e on 2005/12/15 05:28:52
Checkpoint some potentially questionable changes from my
local CVS repo.
I believe this fakes up the addition of bc(1) like 'ibase'
and 'obase' to the test harness.
Affected files ...
.. //depot/projects/bignum/src/bignum.c#2 edit
.. //depot/projects/bignum/src/bn.h#2 edit
.. //depot/projects/bignum/test/bn_parse.y#2 edit
.. //depot/projects/bignum/test/bn_token.l#2 edit
Differences ...
==== //depot/projects/bignum/src/bignum.c#2 (text+ko) ====
@@ -235,6 +235,28 @@
return (n->neg != 0 ? 1 : 0);
}
+int
+bn_is_odd (bignum_t n)
+{
+
+ if (n->len == 0)
+ return (0);
+ if (n->num[0] & 1)
+ return (1);
+ return (0);
+}
+
+int
+bn_is_even (bignum_t n)
+{
+
+ if (n->len == 0)
+ return (1);
+ if ((n->num[0] & 1) == 0)
+ return (1);
+ return (0);
+}
+
#if 0
int
bn_bin2bn (bignum_t r, const u_char *buf, int len)
@@ -279,7 +301,7 @@
goto out;
}
- error = bn_divide_word(q, r, q, base);
+ error = bn_udivide_word(q, r, q, base);
if (error)
goto out;
@@ -373,7 +395,7 @@
return (0);
s = b % 32;
- return ((r->num[i] & (1 << s)) ? 1 : 0);
+ return ((r->num[i] & (((uint32_t)1) << s)) ? 1 : 0);
}
/*
@@ -856,8 +878,8 @@
if (error)
goto out;
}
- r->num[k] += l;
-
+ r->num[k] += (uint32_t)l;
+#if 0
/* Carry things out */
for (;;) {
c = (l > r->num[k]);
@@ -869,8 +891,10 @@
if (error)
goto out;
}
+ l = r->num[k];
r->num[k] += c;
}
+#endif
}
}
bn_compact(r);
@@ -964,7 +988,7 @@
* remainder and the divisor and perform a divide. If the result
* is non-zero we shift by the place of the divisor. This number
* is added to the quotient and multiplied by the whole divisor
- * and subtracted (what?) the result from the remainder.
+ * and the result is subtracted from the remainder.
* If the result above is zero and we are at the lowest digit the
* division is over. If we are at any non-lowest digit we divide
* the maximum digit value by the divisor and multiply by the
@@ -981,7 +1005,7 @@
if (l == 0) {
if (i == 0)
break;
- l = (0xffffffff / D->num[D->wid]) + 1;
+ l = 0xffffffff / D->num[D->wid];
error = bn_uadd_word(t, t, l);
if (error)
goto out;
@@ -996,15 +1020,17 @@
}
shift = (i - D->wid) * 32;
- if (shift)
+ if (shift > 0) {
error = bn_shl(t, t, shift);
if (error)
goto out;
+ }
error = bn_uadd(q, q, t);
if (error)
goto out;
+ /* Scale and subtract from the remainder */
error = bn_umul(t, t, D);
if (error)
goto out;
@@ -1146,9 +1172,15 @@
r = bn_new();
if (r == NULL)
return (ENOMEM);
- error = bn_add_word(r, r, 1);
- if (error)
- goto out;
+ if (bn_is_odd(P)) {
+ error = bn_copy(r, N);
+ if (error)
+ goto out;
+ } else {
+ error = bn_add_word(r, r, 1);
+ if (error)
+ goto out;
+ }
v = bn_dup(N);
if (v == NULL) {
==== //depot/projects/bignum/src/bn.h#2 (text+ko) ====
@@ -48,6 +48,8 @@
int bn_is_zero (bignum_t n);
void bn_neg (bignum_t n);
int bn_is_neg (bignum_t n);
+int bn_is_odd (bignum_t n);
+int bn_is_even (bignum_t n);
bignum_t bn_strtobn (const char * __restrict nptr, char ** __restrict endptr, int base);
int bn_bin2bn (bignum_t r, const u_char *, int len);
==== //depot/projects/bignum/test/bn_parse.y#2 (text+ko) ====
@@ -35,6 +35,7 @@
;
line : expr BREAK {
+ /* bn_print($1); */
memset(buf, 0, BUFLEN);
bn_bntoa($1, buf, BUFLEN, 10);
if (bn_is_neg($1))
==== //depot/projects/bignum/test/bn_token.l#2 (text+ko) ====
@@ -7,6 +7,8 @@
#include "bn.h"
#include "bn_parse.h"
+int ibase=0;
+
int yylex (void);
void yyerror (const char *);
#define YY_NO_UNPUT
@@ -21,15 +23,22 @@
return BIGNUM;
}
[0-9]+ { /* dec */
- yylval.bn = bn_strtobn(yytext, NULL, 10);
+ if (ibase)
+ yylval.bn = bn_strtobn(yytext, NULL, ibase);
+ else
+ yylval.bn = bn_strtobn(yytext, NULL, 10);
return BIGNUM;
}
-0[xX][0-9a-fA-F]+ { /* hex */
+(0[xX])?[0-9a-fA-F]+ { /* hex */
yylval.bn = bn_strtobn(yytext, NULL, 16);
return BIGNUM;
}
[ \t\r] ;
+"ibase=16" { ibase=16; }
+"ibase=10" { ibase=10; }
+"ibase=0" { ibase=0; }
+"obase=16" ;
"=" { return EQUAL; }
"+" { return ADD; }
"-" { return SUB; }
More information about the p4-projects
mailing list