From owner-p4-projects@FreeBSD.ORG Thu Dec 15 05:29:29 2005 Return-Path: X-Original-To: p4-projects@freebsd.org Delivered-To: p4-projects@freebsd.org Received: by hub.freebsd.org (Postfix, from userid 32767) id 4ABCE16A422; Thu, 15 Dec 2005 05:29:29 +0000 (GMT) X-Original-To: perforce@freebsd.org Delivered-To: perforce@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 0C59816A41F for ; Thu, 15 Dec 2005 05:29:29 +0000 (GMT) (envelope-from mdodd@freebsd.org) Received: from repoman.freebsd.org (repoman.freebsd.org [216.136.204.115]) by mx1.FreeBSD.org (Postfix) with ESMTP id 43EF243D58 for ; Thu, 15 Dec 2005 05:29:28 +0000 (GMT) (envelope-from mdodd@freebsd.org) Received: from repoman.freebsd.org (localhost [127.0.0.1]) by repoman.freebsd.org (8.13.1/8.13.1) with ESMTP id jBF5TSWa036152 for ; Thu, 15 Dec 2005 05:29:28 GMT (envelope-from mdodd@freebsd.org) Received: (from perforce@localhost) by repoman.freebsd.org (8.13.1/8.13.1/Submit) id jBF5TRvM036149 for perforce@freebsd.org; Thu, 15 Dec 2005 05:29:27 GMT (envelope-from mdodd@freebsd.org) Date: Thu, 15 Dec 2005 05:29:27 GMT Message-Id: <200512150529.jBF5TRvM036149@repoman.freebsd.org> X-Authentication-Warning: repoman.freebsd.org: perforce set sender to mdodd@freebsd.org using -f From: "Matthew N. Dodd" To: Perforce Change Reviews Cc: Subject: PERFORCE change 88224 for review X-BeenThere: p4-projects@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: p4 projects tree changes List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 15 Dec 2005 05:29:30 -0000 http://perforce.freebsd.org/chv.cgi?CH=88224 Change 88224 by mdodd@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; }