1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
|
/***********************************************************************/
/* */
/* Objective Caml */
/* */
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
/* */
/* Copyright 2003 Institut National de Recherche en Informatique et */
/* en Automatique. All rights reserved. This file is distributed */
/* under the terms of the GNU Library General Public License, with */
/* the special exception on linking described in file ../../LICENSE. */
/* */
/***********************************************************************/
/* $Id: bng.c 10450 2010-05-21 12:00:49Z doligez $ */
#include "bng.h"
#include "config.h"
#if defined(__GNUC__) && BNG_ASM_LEVEL > 0
#if defined(BNG_ARCH_ia32)
#include "bng_ia32.c"
#elif defined(BNG_ARCH_amd64)
#include "bng_amd64.c"
#elif defined(BNG_ARCH_ppc)
#include "bng_ppc.c"
#elif defined (BNG_ARCH_alpha)
#include "bng_alpha.c"
#elif defined (BNG_ARCH_sparc)
#include "bng_sparc.c"
#elif defined (BNG_ARCH_mips)
#include "bng_mips.c"
#endif
#endif
#include "bng_digit.c"
/**** Operations that cannot be overridden ****/
/* Return number of leading zero bits in d */
int bng_leading_zero_bits(bngdigit d)
{
int n = BNG_BITS_PER_DIGIT;
#ifdef ARCH_SIXTYFOUR
if ((d & 0xFFFFFFFF00000000L) != 0) { n -= 32; d = d >> 32; }
#endif
if ((d & 0xFFFF0000) != 0) { n -= 16; d = d >> 16; }
if ((d & 0xFF00) != 0) { n -= 8; d = d >> 8; }
if ((d & 0xF0) != 0) { n -= 4; d = d >> 4; }
if ((d & 0xC) != 0) { n -= 2; d = d >> 2; }
if ((d & 2) != 0) { n -= 1; d = d >> 1; }
return n - d;
}
/* Complement the digits of {a,len} */
void bng_complement(bng a/*[alen]*/, bngsize alen)
{
for (/**/; alen > 0; alen--, a++) *a = ~*a;
}
/* Return number of significant digits in {a,alen}. */
bngsize bng_num_digits(bng a/*[alen]*/, bngsize alen)
{
while (1) {
if (alen == 0) return 1;
if (a[alen - 1] != 0) return alen;
alen--;
}
}
/* Return 0 if {a,alen} = {b,blen}
-1 if {a,alen} < {b,blen}
1 if {a,alen} > {b,blen}. */
int bng_compare(bng a/*[alen]*/, bngsize alen,
bng b/*[blen]*/, bngsize blen)
{
bngdigit da, db;
while (alen > 0 && a[alen-1] == 0) alen--;
while (blen > 0 && b[blen-1] == 0) blen--;
if (alen > blen) return 1;
if (alen < blen) return -1;
while (alen > 0) {
alen--;
da = a[alen];
db = b[alen];
if (da > db) return 1;
if (da < db) return -1;
}
return 0;
}
/**** Generic definitions of the overridable operations ****/
/* {a,alen} := {a, alen} + carry. Return carry out. */
static bngcarry bng_generic_add_carry
(bng a/*[alen]*/, bngsize alen, bngcarry carry)
{
if (carry == 0 || alen == 0) return carry;
do {
if (++(*a) != 0) return 0;
a++;
} while (--alen);
return 1;
}
/* {a,alen} := {a,alen} + {b,blen} + carry. Return carry out.
Require alen >= blen. */
static bngcarry bng_generic_add
(bng a/*[alen]*/, bngsize alen,
bng b/*[blen]*/, bngsize blen,
bngcarry carry)
{
alen -= blen;
for (/**/; blen > 0; blen--, a++, b++) {
BngAdd2Carry(*a, carry, *a, *b, carry);
}
if (carry == 0 || alen == 0) return carry;
do {
if (++(*a) != 0) return 0;
a++;
} while (--alen);
return 1;
}
/* {a,alen} := {a, alen} - carry. Return carry out. */
static bngcarry bng_generic_sub_carry
(bng a/*[alen]*/, bngsize alen, bngcarry carry)
{
if (carry == 0 || alen == 0) return carry;
do {
if ((*a)-- != 0) return 0;
a++;
} while (--alen);
return 1;
}
/* {a,alen} := {a,alen} - {b,blen} - carry. Return carry out.
Require alen >= blen. */
static bngcarry bng_generic_sub
(bng a/*[alen]*/, bngsize alen,
bng b/*[blen]*/, bngsize blen,
bngcarry carry)
{
alen -= blen;
for (/**/; blen > 0; blen--, a++, b++) {
BngSub2Carry(*a, carry, *a, *b, carry);
}
if (carry == 0 || alen == 0) return carry;
do {
if ((*a)-- != 0) return 0;
a++;
} while (--alen);
return 1;
}
/* {a,alen} := {a,alen} << shift.
Return the bits shifted out of the most significant digit of a.
Require 0 <= shift < BITS_PER_BNGDIGIT. */
static bngdigit bng_generic_shift_left
(bng a/*[alen]*/, bngsize alen,
int shift)
{
int shift2 = BNG_BITS_PER_DIGIT - shift;
bngdigit carry = 0;
if (shift > 0) {
for (/**/; alen > 0; alen--, a++) {
bngdigit d = *a;
*a = (d << shift) | carry;
carry = d >> shift2;
}
}
return carry;
}
/* {a,alen} := {a,alen} >> shift.
Return the bits shifted out of the least significant digit of a.
Require 0 <= shift < BITS_PER_BNGDIGIT. */
static bngdigit bng_generic_shift_right
(bng a/*[alen]*/, bngsize alen,
int shift)
{
int shift2 = BNG_BITS_PER_DIGIT - shift;
bngdigit carry = 0;
if (shift > 0) {
for (a = a + alen - 1; alen > 0; alen--, a--) {
bngdigit d = *a;
*a = (d >> shift) | carry;
carry = d << shift2;
}
}
return carry;
}
/* {a,alen} := {a,alen} + d * {b,blen}. Return carry out.
Require alen >= blen. */
static bngdigit bng_generic_mult_add_digit
(bng a/*[alen]*/, bngsize alen,
bng b/*[blen]*/, bngsize blen,
bngdigit d)
{
bngdigit out, ph, pl;
bngcarry carry;
alen -= blen;
for (out = 0; blen > 0; blen--, a++, b++) {
bngdigit bd = *b;
/* ph:pl = double-digit product of b's current digit and d */
BngMult(ph, pl, bd, d);
/* current digit of a += pl + out. Accumulate carries in ph. */
BngAdd3(*a, ph, *a, pl, out);
/* prepare out for next iteration */
out = ph;
}
if (alen == 0) return out;
/* current digit of a += out */
BngAdd2(*a, carry, *a, out);
a++;
alen--;
/* Propagate carry */
if (carry == 0 || alen == 0) return carry;
do {
if (++(*a) != 0) return 0;
a++;
} while (--alen);
return 1;
}
/* {a,alen} := {a,alen} - d * {b,blen}. Return carry out.
Require alen >= blen. */
static bngdigit bng_generic_mult_sub_digit
(bng a/*[alen]*/, bngsize alen,
bng b/*[blen]*/, bngsize blen,
bngdigit d)
{
bngdigit out, ph, pl;
bngcarry carry;
alen -= blen;
for (out = 0; blen > 0; blen--, a++, b++) {
bngdigit bd = *b;
/* ph:pl = double-digit product of b's current digit and d */
BngMult(ph, pl, bd, d);
/* current digit of a -= pl + out. Accumulate carrys in ph. */
BngSub3(*a, ph, *a, pl, out);
/* prepare out for next iteration */
out = ph;
}
if (alen == 0) return out;
/* current digit of a -= out */
BngSub2(*a, carry, *a, out);
a++;
alen--;
/* Propagate carry */
if (carry == 0 || alen == 0) return carry;
do {
if ((*a)-- != 0) return 0;
a++;
} while (--alen);
return 1;
}
/* {a,alen} := {a,alen} + {b,blen} * {c,clen}. Return carry out.
Require alen >= blen + clen. */
static bngcarry bng_generic_mult_add
(bng a/*[alen]*/, bngsize alen,
bng b/*[blen]*/, bngsize blen,
bng c/*[clen]*/, bngsize clen)
{
bngcarry carry;
for (carry = 0; clen > 0; clen--, c++, alen--, a++)
carry += bng_mult_add_digit(a, alen, b, blen, *c);
return carry;
}
/* {a,alen} := 2 * {a,alen} + {b,blen}^2. Return carry out.
Require alen >= 2 * blen. */
static bngcarry bng_generic_square_add
(bng a/*[alen]*/, bngsize alen,
bng b/*[blen]*/, bngsize blen)
{
bngcarry carry1, carry2;
bngsize i, aofs;
bngdigit ph, pl, d;
/* Double products */
for (carry1 = 0, i = 1; i < blen; i++) {
aofs = 2 * i - 1;
carry1 += bng_mult_add_digit(a + aofs, alen - aofs,
b + i, blen - i, b[i - 1]);
}
/* Multiply by two */
carry1 = (carry1 << 1) | bng_shift_left(a, alen, 1);
/* Add square of digits */
carry2 = 0;
for (i = 0; i < blen; i++) {
d = b[i];
BngMult(ph, pl, d, d);
BngAdd2Carry(*a, carry2, *a, pl, carry2);
a++;
BngAdd2Carry(*a, carry2, *a, ph, carry2);
a++;
}
alen -= 2 * blen;
if (alen > 0 && carry2 != 0) {
do {
if (++(*a) != 0) { carry2 = 0; break; }
a++;
} while (--alen);
}
return carry1 + carry2;
}
/* {a,len-1} := {b,len} / d. Return {b,len} modulo d.
Require MSD of b < d.
If BngDivNeedsNormalization is defined, require d normalized. */
static bngdigit bng_generic_div_rem_norm_digit
(bng a/*[len-1]*/, bng b/*[len]*/, bngsize len, bngdigit d)
{
bngdigit topdigit, quo, rem;
intnat i;
topdigit = b[len - 1];
for (i = len - 2; i >= 0; i--) {
/* Divide topdigit:current digit of numerator by d */
BngDiv(quo, rem, topdigit, b[i], d);
/* Quotient is current digit of result */
a[i] = quo;
/* Iterate with topdigit = remainder */
topdigit = rem;
}
return topdigit;
}
#ifdef BngDivNeedsNormalization
/* {a,len-1} := {b,len} / d. Return {b,len} modulo d.
Require MSD of b < d. */
static bngdigit bng_generic_div_rem_digit
(bng a/*[len-1]*/, bng b/*[len]*/, bngsize len, bngdigit d)
{
bngdigit rem;
int shift;
/* Normalize d and b */
shift = bng_leading_zero_bits(d);
d <<= shift;
bng_shift_left(b, len, shift);
/* Do the division */
rem = bng_div_rem_norm_digit(a, b, len, d);
/* Undo normalization on b and remainder */
bng_shift_right(b, len, shift);
return rem >> shift;
}
#endif
/* {n+dlen, nlen-dlen} := {n,nlen} / {d, dlen}.
{n, dlen} := {n,nlen} modulo {d, dlen}.
Require nlen > dlen and MSD of n < MSD of d.
(This implies MSD of d > 0). */
static void bng_generic_div_rem
(bng n/*[nlen]*/, bngsize nlen,
bng d/*[dlen]*/, bngsize dlen)
{
bngdigit topden, quo, rem;
int shift;
bngsize i, j;
/* Normalize d */
shift = bng_leading_zero_bits(d[dlen - 1]);
/* Note that no bits of n are lost by the following shift,
since n[nlen-1] < d[dlen-1] */
bng_shift_left(n, nlen, shift);
bng_shift_left(d, dlen, shift);
/* Special case if d is just one digit */
if (dlen == 1) {
*n = bng_div_rem_norm_digit(n + 1, n, nlen, *d);
} else {
topden = d[dlen - 1];
/* Long division */
for (j = nlen - 1; j >= dlen; j--) {
i = j - dlen;
/* At this point:
- the current numerator is n[j] : ...................... : n[0]
- to be subtracted quo times: d[dlen-1] : ... : d[0] : 0... : 0
(there are i zeroes at the end) */
/* Under-estimate the next digit of the quotient (quo) */
if (topden + 1 == 0)
quo = n[j];
else
BngDiv(quo, rem, n[j], n[j - 1], topden + 1);
/* Subtract d * quo (shifted i places) from numerator */
n[j] -= bng_mult_sub_digit(n + i, dlen, d, dlen, quo);
/* Adjust if necessary */
while (n[j] != 0 || bng_compare(n + i, dlen, d, dlen) >= 0) {
/* Numerator is still bigger than shifted divisor.
Increment quotient and subtract shifted divisor. */
quo++;
n[j] -= bng_sub(n + i, dlen, d, dlen, 0);
}
/* Store quotient digit */
n[j] = quo;
}
}
/* Undo normalization on remainder and divisor */
bng_shift_right(n, dlen, shift);
bng_shift_right(d, dlen, shift);
}
/**** Construction of the table of operations ****/
struct bng_operations bng_ops = {
bng_generic_add_carry,
bng_generic_add,
bng_generic_sub_carry,
bng_generic_sub,
bng_generic_shift_left,
bng_generic_shift_right,
bng_generic_mult_add_digit,
bng_generic_mult_sub_digit,
bng_generic_mult_add,
bng_generic_square_add,
bng_generic_div_rem_norm_digit,
#ifdef BngDivNeedsNormalization
bng_generic_div_rem_digit,
#else
bng_generic_div_rem_norm_digit,
#endif
bng_generic_div_rem
};
void bng_init(void)
{
#ifdef BNG_SETUP_OPS
BNG_SETUP_OPS;
#endif
}
|