diff options
author | Marco Bodrato <bodrato@mail.dm.unipi.it> | 2015-05-30 05:45:29 +0200 |
---|---|---|
committer | Marco Bodrato <bodrato@mail.dm.unipi.it> | 2015-05-30 05:45:29 +0200 |
commit | cafad50c95118250dd52a9ed9e8bb4b83f30b125 (patch) | |
tree | fa1a5ef2a279a6232c7b01ef784444ff9e41d049 /mpf | |
parent | a8109aae2ba6e046acdd24dc26ea3407b1e919b3 (diff) | |
download | gmp-cafad50c95118250dd52a9ed9e8bb4b83f30b125.tar.gz |
mpf/cmp_[su]i.c: Use macros, remove branches, correct nails.
Diffstat (limited to 'mpf')
-rw-r--r-- | mpf/cmp_si.c | 43 | ||||
-rw-r--r-- | mpf/cmp_ui.c | 48 |
2 files changed, 35 insertions, 56 deletions
diff --git a/mpf/cmp_si.c b/mpf/cmp_si.c index eaa8b87da..14923da96 100644 --- a/mpf/cmp_si.c +++ b/mpf/cmp_si.c @@ -41,8 +41,7 @@ mpf_cmp_si (mpf_srcptr u, long int vval) __GMP_NOTHROW int usign; unsigned long abs_vval; - uexp = u->_mp_exp; - usize = u->_mp_size; + usize = SIZ (u); /* 1. Are the signs different? */ if ((usize < 0) == (vval < 0)) /* don't use xor, type size may differ */ @@ -64,49 +63,41 @@ mpf_cmp_si (mpf_srcptr u, long int vval) __GMP_NOTHROW /* U and V have the same sign and are both non-zero. */ + /* 2. Are the exponents different (V's exponent == 1)? */ + uexp = EXP (u); usign = usize >= 0 ? 1 : -1; usize = ABS (usize); abs_vval = ABS_CAST (unsigned long, vval); - /* 2. Are the exponents different (V's exponent == 1)? */ #if GMP_NAIL_BITS != 0 - if (uexp > 1 + (abs_vval > GMP_NUMB_MAX)) - return usign; - if (uexp < 1 + (abs_vval > GMP_NUMB_MAX)) - return -usign; + if (uexp != 1 + (abs_vval > GMP_NUMB_MAX)) + return (uexp < 1 + (abs_vval > GMP_NUMB_MAX)) ? -usign : usign; #else - if (uexp > 1) - return usign; - if (uexp < 1) - return -usign; + if (uexp != 1) + return (uexp < 1) ? -usign : usign; #endif - up = u->_mp_d; + up = PTR (u); - ulimb = up[usize - 1]; + ASSERT (usize > 0); + ulimb = up[--usize]; #if GMP_NAIL_BITS != 0 - if (usize >= 2 && uexp == 2) + if (uexp == 2) { if ((ulimb >> GMP_NAIL_BITS) != 0) return usign; - ulimb = (ulimb << GMP_NUMB_BITS) | up[usize - 2]; - usize--; + ulimb = (ulimb << GMP_NUMB_BITS); + if (usize != 0) ulimb |= up[--usize]; } #endif - usize--; /* 3. Compare the most significant mantissa limb with V. */ - if (ulimb > abs_vval) - return usign; - else if (ulimb < abs_vval) - return -usign; + if (ulimb != abs_vval) + return (ulimb < abs_vval) ? -usign : usign; /* Ignore zeroes at the low end of U. */ - while (*up == 0) - { - up++; - usize--; - } + for (; *up == 0; ++up) + --usize; /* 4. Now, if the number of limbs are different, we have a difference since we have made sure the trailing limbs are not zero. */ diff --git a/mpf/cmp_ui.c b/mpf/cmp_ui.c index ccb76c6ce..1d9755083 100644 --- a/mpf/cmp_ui.c +++ b/mpf/cmp_ui.c @@ -39,8 +39,7 @@ mpf_cmp_ui (mpf_srcptr u, unsigned long int vval) __GMP_NOTHROW mp_exp_t uexp; mp_limb_t ulimb; - uexp = u->_mp_exp; - usize = u->_mp_size; + usize = SIZ (u); /* 1. Is U negative? */ if (usize < 0) @@ -51,50 +50,39 @@ mpf_cmp_ui (mpf_srcptr u, unsigned long int vval) __GMP_NOTHROW return usize != 0; /* 2. Are the exponents different (V's exponent == 1)? */ + uexp = EXP (u); + #if GMP_NAIL_BITS != 0 - if (uexp > 1 + (vval > GMP_NUMB_MAX)) - return 1; - if (uexp < 1 + (vval > GMP_NUMB_MAX)) - return -1; + if (uexp != 1 + (vval > GMP_NUMB_MAX)) + return (uexp < 1 + (vval > GMP_NUMB_MAX)) ? -1 : 1; #else - if (uexp > 1) - return 1; - if (uexp < 1) - return -1; + if (uexp != 1) + return (uexp < 1) ? -1 : 1; #endif - up = u->_mp_d; + up = PTR (u); - ulimb = up[usize - 1]; + ASSERT (usize > 0); + ulimb = up[--usize]; #if GMP_NAIL_BITS != 0 - if (usize >= 2 && uexp == 2) + if (uexp == 2) { if ((ulimb >> GMP_NAIL_BITS) != 0) return 1; - ulimb = (ulimb << GMP_NUMB_BITS) | up[usize - 2]; - usize--; + ulimb = (ulimb << GMP_NUMB_BITS); + if (usize != 0) ulimb |= up[--usize]; } #endif - usize--; /* 3. Compare the most significant mantissa limb with V. */ - if (ulimb > vval) - return 1; - else if (ulimb < vval) - return -1; + if (ulimb != vval) + return (ulimb < vval) ? -1 : 1; /* Ignore zeroes at the low end of U. */ - while (*up == 0) - { - up++; - usize--; - } + for (; *up == 0; ++up) + --usize; /* 4. Now, if the number of limbs are different, we have a difference since we have made sure the trailing limbs are not zero. */ - if (usize > 0) - return 1; - - /* Wow, we got zero even if we tried hard to avoid it. */ - return 0; + return (usize > 0); } |