From 35b2657f53bbec850bafc1ecb60489d098457663 Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Wed, 23 Sep 2009 18:47:58 +0100 Subject: [PATCH] bignum indentation cleanup --- vm/bignum.cpp | 908 +++++++++++++++++++++++++------------------------- 1 file changed, 454 insertions(+), 454 deletions(-) diff --git a/vm/bignum.cpp b/vm/bignum.cpp index b7d51c3bfb..934b510a4b 100755 --- a/vm/bignum.cpp +++ b/vm/bignum.cpp @@ -145,21 +145,21 @@ bignum *factorvm::bignum_multiply(bignum * x, bignum * y) if (BIGNUM_ZERO_P (y)) return (y); if (x_length == 1) - { - bignum_digit_type digit = (BIGNUM_REF (x, 0)); - if (digit == 1) - return (bignum_maybe_new_sign (y, negative_p)); - if (digit < BIGNUM_RADIX_ROOT) - return (bignum_multiply_unsigned_small_factor (y, digit, negative_p)); - } + { + bignum_digit_type digit = (BIGNUM_REF (x, 0)); + if (digit == 1) + return (bignum_maybe_new_sign (y, negative_p)); + if (digit < BIGNUM_RADIX_ROOT) + return (bignum_multiply_unsigned_small_factor (y, digit, negative_p)); + } if (y_length == 1) - { - bignum_digit_type digit = (BIGNUM_REF (y, 0)); - if (digit == 1) - return (bignum_maybe_new_sign (x, negative_p)); - if (digit < BIGNUM_RADIX_ROOT) - return (bignum_multiply_unsigned_small_factor (x, digit, negative_p)); - } + { + bignum_digit_type digit = (BIGNUM_REF (y, 0)); + if (digit == 1) + return (bignum_maybe_new_sign (x, negative_p)); + if (digit < BIGNUM_RADIX_ROOT) + return (bignum_multiply_unsigned_small_factor (x, digit, negative_p)); + } return (bignum_multiply_unsigned (x, y, negative_p)); } @@ -167,81 +167,81 @@ bignum *factorvm::bignum_multiply(bignum * x, bignum * y) void factorvm::bignum_divide(bignum * numerator, bignum * denominator, bignum * * quotient, bignum * * remainder) { if (BIGNUM_ZERO_P (denominator)) - { - divide_by_zero_error(); - return; - } + { + divide_by_zero_error(); + return; + } if (BIGNUM_ZERO_P (numerator)) - { - (*quotient) = numerator; - (*remainder) = numerator; - } + { + (*quotient) = numerator; + (*remainder) = numerator; + } else + { + int r_negative_p = (BIGNUM_NEGATIVE_P (numerator)); + int q_negative_p = + ((BIGNUM_NEGATIVE_P (denominator)) ? (! r_negative_p) : r_negative_p); + switch (bignum_compare_unsigned (numerator, denominator)) { - int r_negative_p = (BIGNUM_NEGATIVE_P (numerator)); - int q_negative_p = - ((BIGNUM_NEGATIVE_P (denominator)) ? (! r_negative_p) : r_negative_p); - switch (bignum_compare_unsigned (numerator, denominator)) + case bignum_comparison_equal: + { + (*quotient) = (BIGNUM_ONE (q_negative_p)); + (*remainder) = (BIGNUM_ZERO ()); + break; + } + case bignum_comparison_less: + { + (*quotient) = (BIGNUM_ZERO ()); + (*remainder) = numerator; + break; + } + case bignum_comparison_greater: + { + if ((BIGNUM_LENGTH (denominator)) == 1) { - case bignum_comparison_equal: + bignum_digit_type digit = (BIGNUM_REF (denominator, 0)); + if (digit == 1) { - (*quotient) = (BIGNUM_ONE (q_negative_p)); + (*quotient) = + (bignum_maybe_new_sign (numerator, q_negative_p)); (*remainder) = (BIGNUM_ZERO ()); break; } - case bignum_comparison_less: + else if (digit < BIGNUM_RADIX_ROOT) { - (*quotient) = (BIGNUM_ZERO ()); - (*remainder) = numerator; + bignum_divide_unsigned_small_denominator + (numerator, digit, + quotient, remainder, + q_negative_p, r_negative_p); break; } - case bignum_comparison_greater: + else { - if ((BIGNUM_LENGTH (denominator)) == 1) - { - bignum_digit_type digit = (BIGNUM_REF (denominator, 0)); - if (digit == 1) - { - (*quotient) = - (bignum_maybe_new_sign (numerator, q_negative_p)); - (*remainder) = (BIGNUM_ZERO ()); - break; - } - else if (digit < BIGNUM_RADIX_ROOT) - { - bignum_divide_unsigned_small_denominator - (numerator, digit, - quotient, remainder, - q_negative_p, r_negative_p); - break; - } - else - { - bignum_divide_unsigned_medium_denominator - (numerator, digit, - quotient, remainder, - q_negative_p, r_negative_p); - break; - } - } - bignum_divide_unsigned_large_denominator - (numerator, denominator, + bignum_divide_unsigned_medium_denominator + (numerator, digit, quotient, remainder, q_negative_p, r_negative_p); break; } } + bignum_divide_unsigned_large_denominator + (numerator, denominator, + quotient, remainder, + q_negative_p, r_negative_p); + break; + } } + } } /* allocates memory */ bignum *factorvm::bignum_quotient(bignum * numerator, bignum * denominator) { if (BIGNUM_ZERO_P (denominator)) - { - divide_by_zero_error(); - return (BIGNUM_OUT_OF_BAND); - } + { + divide_by_zero_error(); + return (BIGNUM_OUT_OF_BAND); + } if (BIGNUM_ZERO_P (numerator)) return numerator; { @@ -250,39 +250,39 @@ bignum *factorvm::bignum_quotient(bignum * numerator, bignum * denominator) ? (! (BIGNUM_NEGATIVE_P (numerator))) : (BIGNUM_NEGATIVE_P (numerator))); switch (bignum_compare_unsigned (numerator, denominator)) + { + case bignum_comparison_equal: + return (BIGNUM_ONE (q_negative_p)); + case bignum_comparison_less: + return (BIGNUM_ZERO ()); + case bignum_comparison_greater: + default: /* to appease gcc -Wall */ { - case bignum_comparison_equal: - return (BIGNUM_ONE (q_negative_p)); - case bignum_comparison_less: - return (BIGNUM_ZERO ()); - case bignum_comparison_greater: - default: /* to appease gcc -Wall */ + bignum * quotient; + if ((BIGNUM_LENGTH (denominator)) == 1) { - bignum * quotient; - if ((BIGNUM_LENGTH (denominator)) == 1) - { - bignum_digit_type digit = (BIGNUM_REF (denominator, 0)); - if (digit == 1) - return (bignum_maybe_new_sign (numerator, q_negative_p)); - if (digit < BIGNUM_RADIX_ROOT) - bignum_divide_unsigned_small_denominator - (numerator, digit, - ("ient), ((bignum * *) 0), - q_negative_p, 0); - else - bignum_divide_unsigned_medium_denominator - (numerator, digit, - ("ient), ((bignum * *) 0), - q_negative_p, 0); - } + bignum_digit_type digit = (BIGNUM_REF (denominator, 0)); + if (digit == 1) + return (bignum_maybe_new_sign (numerator, q_negative_p)); + if (digit < BIGNUM_RADIX_ROOT) + bignum_divide_unsigned_small_denominator + (numerator, digit, + ("ient), ((bignum * *) 0), + q_negative_p, 0); else - bignum_divide_unsigned_large_denominator - (numerator, denominator, + bignum_divide_unsigned_medium_denominator + (numerator, digit, ("ient), ((bignum * *) 0), q_negative_p, 0); - return (quotient); } + else + bignum_divide_unsigned_large_denominator + (numerator, denominator, + ("ient), ((bignum * *) 0), + q_negative_p, 0); + return (quotient); } + } } } @@ -430,15 +430,15 @@ bignum *factorvm::double_to_bignum(double x) if (odd_bits > 0) DTB_WRITE_DIGIT ((fixnum)1 << odd_bits); while (start < scan) + { + if (significand == 0) { - if (significand == 0) - { - while (start < scan) - (*--scan) = 0; - break; - } - DTB_WRITE_DIGIT (BIGNUM_RADIX); + while (start < scan) + (*--scan) = 0; + break; } + DTB_WRITE_DIGIT (BIGNUM_RADIX); + } return (result); } } @@ -453,15 +453,15 @@ int factorvm::bignum_equal_p_unsigned(bignum * x, bignum * y) if (length != (BIGNUM_LENGTH (y))) return (0); else - { - bignum_digit_type * scan_x = (BIGNUM_START_PTR (x)); - bignum_digit_type * scan_y = (BIGNUM_START_PTR (y)); - bignum_digit_type * end_x = (scan_x + length); - while (scan_x < end_x) - if ((*scan_x++) != (*scan_y++)) - return (0); - return (1); - } + { + bignum_digit_type * scan_x = (BIGNUM_START_PTR (x)); + bignum_digit_type * scan_y = (BIGNUM_START_PTR (y)); + bignum_digit_type * end_x = (scan_x + length); + while (scan_x < end_x) + if ((*scan_x++) != (*scan_y++)) + return (0); + return (1); + } } enum bignum_comparison factorvm::bignum_compare_unsigned(bignum * x, bignum * y) @@ -477,14 +477,14 @@ enum bignum_comparison factorvm::bignum_compare_unsigned(bignum * x, bignum * y) bignum_digit_type * scan_x = (start_x + x_length); bignum_digit_type * scan_y = ((BIGNUM_START_PTR (y)) + y_length); while (start_x < scan_x) - { - bignum_digit_type digit_x = (*--scan_x); - bignum_digit_type digit_y = (*--scan_y); - if (digit_x < digit_y) - return (bignum_comparison_less); - if (digit_x > digit_y) - return (bignum_comparison_greater); - } + { + bignum_digit_type digit_x = (*--scan_x); + bignum_digit_type digit_y = (*--scan_y); + if (digit_x < digit_y) + return (bignum_comparison_less); + if (digit_x > digit_y) + return (bignum_comparison_greater); + } } return (bignum_comparison_equal); } @@ -497,11 +497,11 @@ bignum *factorvm::bignum_add_unsigned(bignum * x, bignum * y, int negative_p) GC_BIGNUM(x,this); GC_BIGNUM(y,this); if ((BIGNUM_LENGTH (y)) > (BIGNUM_LENGTH (x))) - { - bignum * z = x; - x = y; - y = z; - } + { + bignum * z = x; + x = y; + y = z; + } { bignum_length_type x_length = (BIGNUM_LENGTH (x)); @@ -533,25 +533,25 @@ bignum *factorvm::bignum_add_unsigned(bignum * x, bignum * y, int negative_p) bignum_digit_type * end_x = ((BIGNUM_START_PTR (x)) + x_length); if (carry != 0) while (scan_x < end_x) + { + sum = ((*scan_x++) + 1); + if (sum < BIGNUM_RADIX) { - sum = ((*scan_x++) + 1); - if (sum < BIGNUM_RADIX) - { - (*scan_r++) = sum; - carry = 0; - break; - } - else - (*scan_r++) = (sum - BIGNUM_RADIX); + (*scan_r++) = sum; + carry = 0; + break; } + else + (*scan_r++) = (sum - BIGNUM_RADIX); + } while (scan_x < end_x) (*scan_r++) = (*scan_x++); } if (carry != 0) - { - (*scan_r) = 1; - return (r); - } + { + (*scan_r) = 1; + return (r); + } return (bignum_shorten_length (r, x_length)); } } @@ -565,21 +565,21 @@ bignum *factorvm::bignum_subtract_unsigned(bignum * x, bignum * y) int negative_p = 0; switch (bignum_compare_unsigned (x, y)) + { + case bignum_comparison_equal: + return (BIGNUM_ZERO ()); + case bignum_comparison_less: { - case bignum_comparison_equal: - return (BIGNUM_ZERO ()); - case bignum_comparison_less: - { - bignum * z = x; - x = y; - y = z; - } - negative_p = 1; - break; - case bignum_comparison_greater: - negative_p = 0; - break; + bignum * z = x; + x = y; + y = z; } + negative_p = 1; + break; + case bignum_comparison_greater: + negative_p = 0; + break; + } { bignum_length_type x_length = (BIGNUM_LENGTH (x)); @@ -593,35 +593,35 @@ bignum *factorvm::bignum_subtract_unsigned(bignum * x, bignum * y) bignum_digit_type * scan_y = (BIGNUM_START_PTR (y)); bignum_digit_type * end_y = (scan_y + (BIGNUM_LENGTH (y))); while (scan_y < end_y) + { + difference = (((*scan_x++) - (*scan_y++)) - borrow); + if (difference < 0) { - difference = (((*scan_x++) - (*scan_y++)) - borrow); - if (difference < 0) - { - (*scan_r++) = (difference + BIGNUM_RADIX); - borrow = 1; - } - else - { - (*scan_r++) = difference; - borrow = 0; - } + (*scan_r++) = (difference + BIGNUM_RADIX); + borrow = 1; + } + else + { + (*scan_r++) = difference; + borrow = 0; } + } } { bignum_digit_type * end_x = ((BIGNUM_START_PTR (x)) + x_length); if (borrow != 0) while (scan_x < end_x) + { + difference = ((*scan_x++) - borrow); + if (difference < 0) + (*scan_r++) = (difference + BIGNUM_RADIX); + else { - difference = ((*scan_x++) - borrow); - if (difference < 0) - (*scan_r++) = (difference + BIGNUM_RADIX); - else - { - (*scan_r++) = difference; - borrow = 0; - break; - } + (*scan_r++) = difference; + borrow = 0; + break; } + } BIGNUM_ASSERT (borrow == 0); while (scan_x < end_x) (*scan_r++) = (*scan_x++); @@ -642,11 +642,11 @@ bignum *factorvm::bignum_multiply_unsigned(bignum * x, bignum * y, int negative_ GC_BIGNUM(x,this); GC_BIGNUM(y,this); if ((BIGNUM_LENGTH (y)) > (BIGNUM_LENGTH (x))) - { - bignum * z = x; - x = y; - y = z; - } + { + bignum * z = x; + x = y; + y = z; + } { bignum_digit_type carry; bignum_digit_type y_digit_low; @@ -671,35 +671,35 @@ bignum *factorvm::bignum_multiply_unsigned(bignum * x, bignum * y, int negative_ #define y_digit y_digit_high #define product_high carry while (scan_x < end_x) + { + x_digit = (*scan_x++); + x_digit_low = (HD_LOW (x_digit)); + x_digit_high = (HD_HIGH (x_digit)); + carry = 0; + scan_y = start_y; + scan_r = (start_r++); + while (scan_y < end_y) { - x_digit = (*scan_x++); - x_digit_low = (HD_LOW (x_digit)); - x_digit_high = (HD_HIGH (x_digit)); - carry = 0; - scan_y = start_y; - scan_r = (start_r++); - while (scan_y < end_y) - { - y_digit = (*scan_y++); - y_digit_low = (HD_LOW (y_digit)); - y_digit_high = (HD_HIGH (y_digit)); - product_low = - ((*scan_r) + - (x_digit_low * y_digit_low) + - (HD_LOW (carry))); - product_high = - ((x_digit_high * y_digit_low) + - (x_digit_low * y_digit_high) + - (HD_HIGH (product_low)) + - (HD_HIGH (carry))); - (*scan_r++) = - (HD_CONS ((HD_LOW (product_high)), (HD_LOW (product_low)))); - carry = - ((x_digit_high * y_digit_high) + - (HD_HIGH (product_high))); - } - (*scan_r) += carry; + y_digit = (*scan_y++); + y_digit_low = (HD_LOW (y_digit)); + y_digit_high = (HD_HIGH (y_digit)); + product_low = + ((*scan_r) + + (x_digit_low * y_digit_low) + + (HD_LOW (carry))); + product_high = + ((x_digit_high * y_digit_low) + + (x_digit_low * y_digit_high) + + (HD_HIGH (product_low)) + + (HD_HIGH (carry))); + (*scan_r++) = + (HD_CONS ((HD_LOW (product_high)), (HD_LOW (product_low)))); + carry = + ((x_digit_high * y_digit_high) + + (HD_HIGH (product_high))); } + (*scan_r) += carry; + } return (bignum_trim (r)); #undef x_digit #undef y_digit @@ -728,21 +728,21 @@ void factorvm::bignum_destructive_add(bignum * bignum, bignum_digit_type n) bignum_digit_type digit; digit = ((*scan) + n); if (digit < BIGNUM_RADIX) - { - (*scan) = digit; - return; - } + { + (*scan) = digit; + return; + } (*scan++) = (digit - BIGNUM_RADIX); while (1) + { + digit = ((*scan) + 1); + if (digit < BIGNUM_RADIX) { - digit = ((*scan) + 1); - if (digit < BIGNUM_RADIX) - { - (*scan) = digit; - return; - } - (*scan++) = (digit - BIGNUM_RADIX); + (*scan) = digit; + return; } + (*scan++) = (digit - BIGNUM_RADIX); + } } void factorvm::bignum_destructive_scale_up(bignum * bignum, bignum_digit_type factor) @@ -755,16 +755,16 @@ void factorvm::bignum_destructive_scale_up(bignum * bignum, bignum_digit_type fa bignum_digit_type * end = (scan + (BIGNUM_LENGTH (bignum))); BIGNUM_ASSERT ((factor > 1) && (factor < BIGNUM_RADIX_ROOT)); while (scan < end) - { - two_digits = (*scan); - product_low = ((factor * (HD_LOW (two_digits))) + (HD_LOW (carry))); - product_high = - ((factor * (HD_HIGH (two_digits))) + - (HD_HIGH (product_low)) + - (HD_HIGH (carry))); - (*scan++) = (HD_CONS ((HD_LOW (product_high)), (HD_LOW (product_low)))); - carry = (HD_HIGH (product_high)); - } + { + two_digits = (*scan); + product_low = ((factor * (HD_LOW (two_digits))) + (HD_LOW (carry))); + product_high = + ((factor * (HD_HIGH (two_digits))) + + (HD_HIGH (product_low)) + + (HD_HIGH (carry))); + (*scan++) = (HD_CONS ((HD_LOW (product_high)), (HD_LOW (product_low)))); + carry = (HD_HIGH (product_high)); + } /* A carry here would be an overflow, i.e. it would not fit. Hopefully the callers allocate enough space that this will never happen. @@ -803,27 +803,27 @@ void factorvm::bignum_divide_unsigned_large_denominator(bignum * numerator, bign { bignum_digit_type v1 = (BIGNUM_REF ((denominator), (length_d - 1))); while (v1 < (BIGNUM_RADIX / 2)) - { - v1 <<= 1; - shift += 1; - } - } - if (shift == 0) { - bignum_destructive_copy (numerator, u); - (BIGNUM_REF (u, (length_n - 1))) = 0; - bignum_divide_unsigned_normalized (u, denominator, q); + v1 <<= 1; + shift += 1; } + } + if (shift == 0) + { + bignum_destructive_copy (numerator, u); + (BIGNUM_REF (u, (length_n - 1))) = 0; + bignum_divide_unsigned_normalized (u, denominator, q); + } else - { - bignum * v = (allot_bignum (length_d, 0)); + { + bignum * v = (allot_bignum (length_d, 0)); - bignum_destructive_normalization (numerator, u, shift); - bignum_destructive_normalization (denominator, v, shift); - bignum_divide_unsigned_normalized (u, v, q); - if (remainder != ((bignum * *) 0)) - bignum_destructive_unnormalization (u, shift); - } + bignum_destructive_normalization (numerator, u, shift); + bignum_destructive_normalization (denominator, v, shift); + bignum_divide_unsigned_normalized (u, v, q); + if (remainder != ((bignum * *) 0)) + bignum_destructive_unnormalization (u, shift); + } if(q) q = bignum_trim (q); @@ -867,46 +867,46 @@ void factorvm::bignum_divide_unsigned_normalized(bignum * u, bignum * v, bignum if (q != BIGNUM_OUT_OF_BAND) q_scan = ((BIGNUM_START_PTR (q)) + (BIGNUM_LENGTH (q))); while (u_scan_limit < u_scan) + { + uj = (*--u_scan); + if (uj != v1) { - uj = (*--u_scan); - if (uj != v1) - { - /* comparand = - (((((uj * BIGNUM_RADIX) + uj1) % v1) * BIGNUM_RADIX) + uj2); - guess = (((uj * BIGNUM_RADIX) + uj1) / v1); */ - cl = (u_scan[-2]); - ch = (bignum_digit_divide (uj, (u_scan[-1]), v1, (&gm))); - guess = gm; - } - else - { - cl = (u_scan[-2]); - ch = ((u_scan[-1]) + v1); - guess = (BIGNUM_RADIX - 1); - } - while (1) - { - /* product = (guess * v2); */ - gl = (HD_LOW (guess)); - gh = (HD_HIGH (guess)); - pl = (v2l * gl); - ph = ((v2l * gh) + (v2h * gl) + (HD_HIGH (pl))); - pl = (HD_CONS ((HD_LOW (ph)), (HD_LOW (pl)))); - ph = ((v2h * gh) + (HD_HIGH (ph))); - /* if (comparand >= product) */ - if ((ch > ph) || ((ch == ph) && (cl >= pl))) - break; - guess -= 1; - /* comparand += (v1 << BIGNUM_DIGIT_LENGTH) */ - ch += v1; - /* if (comparand >= (BIGNUM_RADIX * BIGNUM_RADIX)) */ - if (ch >= BIGNUM_RADIX) - break; - } - qj = (bignum_divide_subtract (v_start, v_end, guess, (--u_scan_start))); - if (q != BIGNUM_OUT_OF_BAND) - (*--q_scan) = qj; + /* comparand = + (((((uj * BIGNUM_RADIX) + uj1) % v1) * BIGNUM_RADIX) + uj2); + guess = (((uj * BIGNUM_RADIX) + uj1) / v1); */ + cl = (u_scan[-2]); + ch = (bignum_digit_divide (uj, (u_scan[-1]), v1, (&gm))); + guess = gm; + } + else + { + cl = (u_scan[-2]); + ch = ((u_scan[-1]) + v1); + guess = (BIGNUM_RADIX - 1); } + while (1) + { + /* product = (guess * v2); */ + gl = (HD_LOW (guess)); + gh = (HD_HIGH (guess)); + pl = (v2l * gl); + ph = ((v2l * gh) + (v2h * gl) + (HD_HIGH (pl))); + pl = (HD_CONS ((HD_LOW (ph)), (HD_LOW (pl)))); + ph = ((v2h * gh) + (HD_HIGH (ph))); + /* if (comparand >= product) */ + if ((ch > ph) || ((ch == ph) && (cl >= pl))) + break; + guess -= 1; + /* comparand += (v1 << BIGNUM_DIGIT_LENGTH) */ + ch += v1; + /* if (comparand >= (BIGNUM_RADIX * BIGNUM_RADIX)) */ + if (ch >= BIGNUM_RADIX) + break; + } + qj = (bignum_divide_subtract (v_start, v_end, guess, (--u_scan_start))); + if (q != BIGNUM_OUT_OF_BAND) + (*--q_scan) = qj; + } return; #undef gl #undef uj @@ -929,34 +929,34 @@ bignum_digit_type factorvm::bignum_divide_subtract(bignum_digit_type * v_start, #define ph carry #define diff pl while (v_scan < v_end) + { + v = (*v_scan++); + vl = (HD_LOW (v)); + vh = (HD_HIGH (v)); + pl = ((vl * gl) + (HD_LOW (carry))); + ph = ((vl * gh) + (vh * gl) + (HD_HIGH (pl)) + (HD_HIGH (carry))); + diff = ((*u_scan) - (HD_CONS ((HD_LOW (ph)), (HD_LOW (pl))))); + if (diff < 0) { - v = (*v_scan++); - vl = (HD_LOW (v)); - vh = (HD_HIGH (v)); - pl = ((vl * gl) + (HD_LOW (carry))); - ph = ((vl * gh) + (vh * gl) + (HD_HIGH (pl)) + (HD_HIGH (carry))); - diff = ((*u_scan) - (HD_CONS ((HD_LOW (ph)), (HD_LOW (pl))))); - if (diff < 0) - { - (*u_scan++) = (diff + BIGNUM_RADIX); - carry = ((vh * gh) + (HD_HIGH (ph)) + 1); - } - else - { - (*u_scan++) = diff; - carry = ((vh * gh) + (HD_HIGH (ph))); - } + (*u_scan++) = (diff + BIGNUM_RADIX); + carry = ((vh * gh) + (HD_HIGH (ph)) + 1); } + else + { + (*u_scan++) = diff; + carry = ((vh * gh) + (HD_HIGH (ph))); + } + } if (carry == 0) return (guess); diff = ((*u_scan) - carry); if (diff < 0) (*u_scan) = (diff + BIGNUM_RADIX); else - { - (*u_scan) = diff; - return (guess); - } + { + (*u_scan) = diff; + return (guess); + } #undef vh #undef ph #undef diff @@ -967,24 +967,24 @@ bignum_digit_type factorvm::bignum_divide_subtract(bignum_digit_type * v_start, u_scan = u_start; carry = 0; while (v_scan < v_end) + { + bignum_digit_type sum = ((*v_scan++) + (*u_scan) + carry); + if (sum < BIGNUM_RADIX) { - bignum_digit_type sum = ((*v_scan++) + (*u_scan) + carry); - if (sum < BIGNUM_RADIX) - { - (*u_scan++) = sum; - carry = 0; - } - else - { - (*u_scan++) = (sum - BIGNUM_RADIX); - carry = 1; - } + (*u_scan++) = sum; + carry = 0; } - if (carry == 1) + else { - bignum_digit_type sum = ((*u_scan) + carry); - (*u_scan) = ((sum < BIGNUM_RADIX) ? sum : (sum - BIGNUM_RADIX)); + (*u_scan++) = (sum - BIGNUM_RADIX); + carry = 1; } + } + if (carry == 1) + { + bignum_digit_type sum = ((*u_scan) + carry); + (*u_scan) = ((sum < BIGNUM_RADIX) ? sum : (sum - BIGNUM_RADIX)); + } return (guess - 1); } @@ -1001,24 +1001,24 @@ void factorvm::bignum_divide_unsigned_medium_denominator(bignum * numerator,bign int shift = 0; /* Because `bignum_digit_divide' requires a normalized denominator. */ while (denominator < (BIGNUM_RADIX / 2)) - { - denominator <<= 1; - shift += 1; - } + { + denominator <<= 1; + shift += 1; + } if (shift == 0) - { - length_q = length_n; + { + length_q = length_n; - q = (allot_bignum (length_q, q_negative_p)); - bignum_destructive_copy (numerator, q); - } + q = (allot_bignum (length_q, q_negative_p)); + bignum_destructive_copy (numerator, q); + } else - { - length_q = (length_n + 1); + { + length_q = (length_n + 1); - q = (allot_bignum (length_q, q_negative_p)); - bignum_destructive_normalization (numerator, q, shift); - } + q = (allot_bignum (length_q, q_negative_p)); + bignum_destructive_normalization (numerator, q, shift); + } { bignum_digit_type r = 0; bignum_digit_type * start = (BIGNUM_START_PTR (q)); @@ -1026,20 +1026,20 @@ void factorvm::bignum_divide_unsigned_medium_denominator(bignum * numerator,bign bignum_digit_type qj; while (start < scan) - { - r = (bignum_digit_divide (r, (*--scan), denominator, (&qj))); - (*scan) = qj; - } + { + r = (bignum_digit_divide (r, (*--scan), denominator, (&qj))); + (*scan) = qj; + } q = bignum_trim (q); if (remainder != ((bignum * *) 0)) - { - if (shift != 0) - r >>= shift; + { + if (shift != 0) + r >>= shift; - (*remainder) = (bignum_digit_to_bignum (r, r_negative_p)); - } + (*remainder) = (bignum_digit_to_bignum (r, r_negative_p)); + } if (quotient != ((bignum * *) 0)) (*quotient) = q; @@ -1058,11 +1058,11 @@ void factorvm::bignum_destructive_normalization(bignum * source, bignum * target int shift_right = (BIGNUM_DIGIT_LENGTH - shift_left); bignum_digit_type mask = (((cell)1 << shift_right) - 1); while (scan_source < end_source) - { - digit = (*scan_source++); - (*scan_target++) = (((digit & mask) << shift_left) | carry); - carry = (digit >> shift_right); - } + { + digit = (*scan_source++); + (*scan_target++) = (((digit & mask) << shift_left) | carry); + carry = (digit >> shift_right); + } if (scan_target < end_target) (*scan_target) = carry; else @@ -1079,11 +1079,11 @@ void factorvm::bignum_destructive_unnormalization(bignum * bignum, int shift_rig int shift_left = (BIGNUM_DIGIT_LENGTH - shift_right); bignum_digit_type mask = (((fixnum)1 << shift_right) - 1); while (start < scan) - { - digit = (*--scan); - (*scan) = ((digit >> shift_right) | carry); - carry = ((digit & mask) << shift_left); - } + { + digit = (*--scan); + (*scan) = ((digit >> shift_right) | carry); + carry = ((digit & mask) << shift_left); + } BIGNUM_ASSERT (carry == 0); return; } @@ -1096,23 +1096,23 @@ void factorvm::bignum_destructive_unnormalization(bignum * bignum, int shift_rig { \ uj = (u[j]); \ if (uj != v1) \ - { \ - uj_uj1 = (HD_CONS (uj, (u[j + 1]))); \ - guess = (uj_uj1 / v1); \ - comparand = (HD_CONS ((uj_uj1 % v1), (u[j + 2]))); \ - } \ + { \ + uj_uj1 = (HD_CONS (uj, (u[j + 1]))); \ + guess = (uj_uj1 / v1); \ + comparand = (HD_CONS ((uj_uj1 % v1), (u[j + 2]))); \ + } \ else \ - { \ - guess = (BIGNUM_RADIX_ROOT - 1); \ - comparand = (HD_CONS (((u[j + 1]) + v1), (u[j + 2]))); \ - } \ + { \ + guess = (BIGNUM_RADIX_ROOT - 1); \ + comparand = (HD_CONS (((u[j + 1]) + v1), (u[j + 2]))); \ + } \ while ((guess * v2) > comparand) \ - { \ - guess -= 1; \ - comparand += (v1 << BIGNUM_HALF_DIGIT_LENGTH); \ - if (comparand >= BIGNUM_RADIX) \ - break; \ - } \ + { \ + guess -= 1; \ + comparand += (v1 << BIGNUM_HALF_DIGIT_LENGTH); \ + if (comparand >= BIGNUM_RADIX) \ + break; \ + } \ qn = (bignum_digit_divide_subtract (v1, v2, guess, (&u[j]))); \ } @@ -1128,18 +1128,18 @@ bignum_digit_type factorvm::bignum_digit_divide(bignum_digit_type uh, bignum_dig bignum_digit_type q2; bignum_digit_type u [4]; if (uh == 0) + { + if (ul < v) { - if (ul < v) - { - (*q) = 0; - return (ul); - } - else if (ul == v) - { - (*q) = 1; - return (0); - } + (*q) = 0; + return (ul); } + else if (ul == v) + { + (*q) = 1; + return (0); + } + } (u[0]) = (HD_HIGH (uh)); (u[1]) = (HD_LOW (uh)); (u[2]) = (HD_HIGH (ul)); @@ -1159,30 +1159,30 @@ bignum_digit_type factorvm::bignum_digit_divide(bignum_digit_type uh, bignum_dig product = ((vn * guess) + carry_in); \ diff = (un - (HD_LOW (product))); \ if (diff < 0) \ - { \ - un = (diff + BIGNUM_RADIX_ROOT); \ - carry = ((HD_HIGH (product)) + 1); \ - } \ + { \ + un = (diff + BIGNUM_RADIX_ROOT); \ + carry = ((HD_HIGH (product)) + 1); \ + } \ else \ - { \ - un = diff; \ - carry = (HD_HIGH (product)); \ - } \ + { \ + un = diff; \ + carry = (HD_HIGH (product)); \ + } \ } #define BDDS_ADD(vn, un, carry_in) \ { \ sum = (vn + un + carry_in); \ if (sum < BIGNUM_RADIX_ROOT) \ - { \ - un = sum; \ - carry = 0; \ - } \ + { \ + un = sum; \ + carry = 0; \ + } \ else \ - { \ - un = (sum - BIGNUM_RADIX_ROOT); \ - carry = 1; \ - } \ + { \ + un = (sum - BIGNUM_RADIX_ROOT); \ + carry = 1; \ + } \ } bignum_digit_type factorvm::bignum_digit_divide_subtract(bignum_digit_type v1, bignum_digit_type v2, bignum_digit_type guess, bignum_digit_type * u) @@ -1199,10 +1199,10 @@ bignum_digit_type factorvm::bignum_digit_divide_subtract(bignum_digit_type v1, b if (diff < 0) (u[0]) = (diff + BIGNUM_RADIX); else - { - (u[0]) = diff; - return (guess); - } + { + (u[0]) = diff; + return (guess); + } } { bignum_digit_type sum; @@ -1252,14 +1252,14 @@ bignum_digit_type factorvm::bignum_destructive_scale_down(bignum * bignum, bignu bignum_digit_type * scan = (start + (BIGNUM_LENGTH (bignum))); BIGNUM_ASSERT ((denominator > 1) && (denominator < BIGNUM_RADIX_ROOT)); while (start < scan) - { - two_digits = (*--scan); - numerator = (HD_CONS (remainder, (HD_HIGH (two_digits)))); - quotient_high = (numerator / denominator); - numerator = (HD_CONS ((numerator % denominator), (HD_LOW (two_digits)))); - (*scan) = (HD_CONS (quotient_high, (numerator / denominator))); - remainder = (numerator % denominator); - } + { + two_digits = (*--scan); + numerator = (HD_CONS (remainder, (HD_HIGH (two_digits)))); + quotient_high = (numerator / denominator); + numerator = (HD_CONS ((numerator % denominator), (HD_LOW (two_digits)))); + (*scan) = (HD_CONS (quotient_high, (numerator / denominator))); + remainder = (numerator % denominator); + } return (remainder); #undef quotient_high } @@ -1273,13 +1273,13 @@ bignum * factorvm::bignum_remainder_unsigned_small_denominator(bignum * n, bignu bignum_digit_type r = 0; BIGNUM_ASSERT ((d > 1) && (d < BIGNUM_RADIX_ROOT)); while (start < scan) - { - two_digits = (*--scan); - r = - ((HD_CONS (((HD_CONS (r, (HD_HIGH (two_digits)))) % d), - (HD_LOW (two_digits)))) - % d); - } + { + two_digits = (*--scan); + r = + ((HD_CONS (((HD_CONS (r, (HD_HIGH (two_digits)))) % d), + (HD_LOW (two_digits)))) + % d); + } return (bignum_digit_to_bignum (r, negative_p)); } @@ -1289,11 +1289,11 @@ bignum *factorvm::bignum_digit_to_bignum(bignum_digit_type digit, int negative_p if (digit == 0) return (BIGNUM_ZERO ()); else - { - bignum * result = (allot_bignum (1, negative_p)); - (BIGNUM_REF (result, 0)) = digit; - return (result); - } + { + bignum * result = (allot_bignum (1, negative_p)); + (BIGNUM_REF (result, 0)) = digit; + return (result); + } } /* allocates memory */ @@ -1325,10 +1325,10 @@ bignum *factorvm::bignum_shorten_length(bignum * bignum, bignum_length_type leng bignum_length_type current_length = (BIGNUM_LENGTH (bignum)); BIGNUM_ASSERT ((length >= 0) || (length <= current_length)); if (length < current_length) - { - BIGNUM_REDUCE_LENGTH (bignum, length); - BIGNUM_SET_NEGATIVE_P (bignum, (length != 0) && (BIGNUM_NEGATIVE_P (bignum))); - } + { + BIGNUM_REDUCE_LENGTH (bignum, length); + BIGNUM_SET_NEGATIVE_P (bignum, (length != 0) && (BIGNUM_NEGATIVE_P (bignum))); + } return (bignum); } @@ -1342,11 +1342,11 @@ bignum *factorvm::bignum_trim(bignum * bignum) ; scan += 1; if (scan < end) - { - bignum_length_type length = (scan - start); - BIGNUM_REDUCE_LENGTH (bignum, length); - BIGNUM_SET_NEGATIVE_P (bignum, (length != 0) && (BIGNUM_NEGATIVE_P (bignum))); - } + { + bignum_length_type length = (scan - start); + BIGNUM_REDUCE_LENGTH (bignum, length); + BIGNUM_SET_NEGATIVE_P (bignum, (length != 0) && (BIGNUM_NEGATIVE_P (bignum))); + } return (bignum); } @@ -1368,12 +1368,12 @@ bignum *factorvm::bignum_maybe_new_sign(bignum * x, int negative_p) if ((BIGNUM_NEGATIVE_P (x)) ? negative_p : (! negative_p)) return (x); else - { - bignum * result = - (allot_bignum ((BIGNUM_LENGTH (x)), negative_p)); - bignum_destructive_copy (x, result); - return (result); - } + { + bignum * result = + (allot_bignum ((BIGNUM_LENGTH (x)), negative_p)); + bignum_destructive_copy (x, result); + return (result); + } } void factorvm::bignum_destructive_copy(bignum * source, bignum * target) @@ -1585,10 +1585,10 @@ bignum *factorvm::bignum_posneg_bitwise_op(int op, bignum * arg1, bignum * arg2) if (digit2 < BIGNUM_RADIX) carry2 = 0; else - { - digit2 = (digit2 - BIGNUM_RADIX); - carry2 = 1; - } + { + digit2 = (digit2 - BIGNUM_RADIX); + carry2 = 1; + } *scanr++ = (op == AND_OP) ? digit1 & digit2 : (op == IOR_OP) ? digit1 | digit2 : @@ -1637,18 +1637,18 @@ bignum *factorvm::bignum_negneg_bitwise_op(int op, bignum * arg1, bignum * arg2) if (digit1 < BIGNUM_RADIX) carry1 = 0; else - { - digit1 = (digit1 - BIGNUM_RADIX); - carry1 = 1; - } + { + digit1 = (digit1 - BIGNUM_RADIX); + carry1 = 1; + } if (digit2 < BIGNUM_RADIX) carry2 = 0; else - { - digit2 = (digit2 - BIGNUM_RADIX); - carry2 = 1; - } + { + digit2 = (digit2 - BIGNUM_RADIX); + carry2 = 1; + } *scanr++ = (op == AND_OP) ? digit1 & digit2 : (op == IOR_OP) ? digit1 | digit2 : @@ -1679,10 +1679,10 @@ void factorvm::bignum_negate_magnitude(bignum * arg) if (digit < BIGNUM_RADIX) carry = 0; else - { - digit = (digit - BIGNUM_RADIX); - carry = 1; - } + { + digit = (digit - BIGNUM_RADIX); + carry = 1; + } *scan++ = digit; } @@ -1702,10 +1702,10 @@ bignum *factorvm::bignum_integer_length(bignum * x) (BIGNUM_REF (result, 1)) = 0; bignum_destructive_scale_up (result, BIGNUM_DIGIT_LENGTH); while (digit > 1) - { - bignum_destructive_add (result, ((bignum_digit_type) 1)); - digit >>= 1; - } + { + bignum_destructive_add (result, ((bignum_digit_type) 1)); + digit >>= 1; + } return (bignum_trim (result)); } @@ -1736,31 +1736,31 @@ bignum *factorvm::digit_stream_to_bignum(unsigned int n_digits, unsigned int (*p if (n_digits == 0) return (BIGNUM_ZERO ()); if (n_digits == 1) - { - fixnum digit = ((fixnum) ((*producer) (0,this))); - return (fixnum_to_bignum (negative_p ? (- digit) : digit)); - } + { + fixnum digit = ((fixnum) ((*producer) (0,this))); + return (fixnum_to_bignum (negative_p ? (- digit) : digit)); + } { bignum_length_type length; { unsigned int radix_copy = radix; unsigned int log_radix = 0; while (radix_copy > 0) - { - radix_copy >>= 1; - log_radix += 1; - } + { + radix_copy >>= 1; + log_radix += 1; + } /* This length will be at least as large as needed. */ length = (BIGNUM_BITS_TO_DIGITS (n_digits * log_radix)); } { bignum * result = (allot_bignum_zeroed (length, negative_p)); while ((n_digits--) > 0) - { - bignum_destructive_scale_up (result, ((bignum_digit_type) radix)); - bignum_destructive_add - (result, ((bignum_digit_type) ((*producer) (n_digits,this)))); - } + { + bignum_destructive_scale_up (result, ((bignum_digit_type) radix)); + bignum_destructive_add + (result, ((bignum_digit_type) ((*producer) (n_digits,this)))); + } return (bignum_trim (result)); } } -- 2.34.1