|
| 1 | +#include "tommath_private.h" |
| 2 | +#ifdef S_MP_FP_EXP2_C |
| 3 | +/* LibTomMath, multiple-precision integer library -- Tom St Denis */ |
| 4 | +/* SPDX-License-Identifier: Unlicense */ |
| 5 | + |
| 6 | +static MP_SET_UNSIGNED(mp_set_word, mp_word) |
| 7 | + |
| 8 | +static mp_err s_mp_fp_exp2_fract(const mp_int *a, mp_int *c) |
| 9 | +{ |
| 10 | + mp_err err = MP_OKAY; |
| 11 | + mp_int u, tmp; |
| 12 | + /* |
| 13 | + Generated with Sam Hocevar's LolRemez at https://github.com/samhocevar/lolremez |
| 14 | +
|
| 15 | + lolremez --double -d 10 -r "0:1" "2**(x)" |
| 16 | + and |
| 17 | + lolremez --double -d 5 -r "0:1" "2**(x)" |
| 18 | +
|
| 19 | + */ |
| 20 | +#if (MP_DIGIT_BIT == 15) |
| 21 | + mp_word coefficients[] = { |
| 22 | + 0x3e, |
| 23 | + 0x125, |
| 24 | + 0x726, |
| 25 | + 0x1ebc, |
| 26 | + 0x58b9, |
| 27 | + 0x7fff |
| 28 | + }; |
| 29 | +#elif ((MP_DIGIT_BIT == 28) || (MP_DIGIT_BIT == 31)) |
| 30 | + mp_word coefficients[] = { |
| 31 | + 0x15, |
| 32 | + 0xca, |
| 33 | + 0xb2c, |
| 34 | + 0x7fe0, |
| 35 | + 0x50c2e, |
| 36 | + 0x2bb0fc, |
| 37 | + 0x13b2ab7, |
| 38 | + 0x71ac235, |
| 39 | + 0x1ebfbdff, |
| 40 | + 0x58b90bfb, |
| 41 | + 0x80000000 |
| 42 | + }; |
| 43 | +#elif (MP_DIGIT_BIT == 60) |
| 44 | + mp_word coefficients[] = { |
| 45 | + 0x157d3d6ee0, |
| 46 | + 0xca97869248, |
| 47 | + 0xb2c1b72cee4, |
| 48 | + 0x7fe03bc5603e, |
| 49 | + 0x50c2e781f2028, |
| 50 | + 0x2bb0fc474348da, |
| 51 | + 0x13b2ab7bece61d7, |
| 52 | + 0x71ac235a89a9728, |
| 53 | + 0x1ebfbdff845aca73, |
| 54 | + 0x58b90bfbe8dd8d73, |
| 55 | + 0x8000000000000acf |
| 56 | + }; |
| 57 | +#endif |
| 58 | + int coeff_size = (int)(sizeof(coefficients)/sizeof(coefficients[0])); |
| 59 | + int i; |
| 60 | + |
| 61 | + if ((err = mp_init_multi(&u, &tmp, NULL)) != MP_OKAY) { |
| 62 | + return err; |
| 63 | + } |
| 64 | + mp_set_word(&tmp, coefficients[0]); |
| 65 | + for (i = 1; i < coeff_size; i++) { |
| 66 | + /* u = ((u * x) >> 63) + coefficients[i] */ |
| 67 | + if ((err = mp_mul(&u, a, &u)) != MP_OKAY) goto LBL_ERR; |
| 68 | + if ((err = mp_div_2d(&u, MP_PRECISION_FIXED_LOG, &u, NULL)) != MP_OKAY) goto LBL_ERR; |
| 69 | + mp_set_word(&tmp, coefficients[i]); |
| 70 | + if ((err = mp_add(&u, &tmp, &u)) != MP_OKAY) goto LBL_ERR; |
| 71 | + } |
| 72 | + |
| 73 | + if (c != NULL) { |
| 74 | + mp_exch(&u, c); |
| 75 | + } |
| 76 | + |
| 77 | +LBL_ERR: |
| 78 | + mp_clear_multi(&u, &tmp, NULL); |
| 79 | + return err; |
| 80 | +} |
| 81 | + |
| 82 | +/* input a is the output from s_mp_fp_log(), a fixed point log base 2 */ |
| 83 | +mp_err s_mp_fp_exp2(const mp_int *a, mp_int *c) |
| 84 | +{ |
| 85 | + mp_err err = MP_OKAY; |
| 86 | + mp_int tmp, int_part, fract_part; |
| 87 | + |
| 88 | + if ((err = mp_init_multi(&tmp, &int_part, &fract_part, NULL)) != MP_OKAY) { |
| 89 | + return err; |
| 90 | + } |
| 91 | + /* Cut fore and aft to get the integer part */ |
| 92 | + if ((err = mp_div_2d(a, MP_PRECISION_FIXED_LOG, &int_part, NULL)) != MP_OKAY) goto LBL_ERR; |
| 93 | + if ((err = mp_mul_2d(&int_part, MP_PRECISION_FIXED_LOG, &int_part)) != MP_OKAY) goto LBL_ERR; |
| 94 | + /* Fractional part */ |
| 95 | + if ((err = mp_sub(a, &int_part, &fract_part)) != MP_OKAY) goto LBL_ERR; |
| 96 | + /* 2^{fractional part} */ |
| 97 | + if ((err = s_mp_fp_exp2_fract(&fract_part, &fract_part)) != MP_OKAY) goto LBL_ERR; |
| 98 | + /* A = 1 << normalized integer part */ |
| 99 | + mp_set(&tmp,1); |
| 100 | + if ((err = mp_div_2d(&int_part, MP_PRECISION_FIXED_LOG, &int_part, NULL)) != MP_OKAY) goto LBL_ERR; |
| 101 | + if ((err = mp_mul_2d(&tmp, (int)mp_get_l(&int_part), &int_part)) != MP_OKAY) goto LBL_ERR; |
| 102 | + /* B = A * fractional part */ |
| 103 | + if ((err = mp_mul(&int_part, &fract_part, &tmp)) != MP_OKAY) goto LBL_ERR; |
| 104 | + /* normalize B */ |
| 105 | + if ((err = mp_div_2d(&tmp, MP_PRECISION_FIXED_LOG, &tmp, NULL)) != MP_OKAY) goto LBL_ERR; |
| 106 | + /* C = 2^a (Roughly. Very, very roughly) */ |
| 107 | + if (c != NULL) { |
| 108 | + mp_exch(&tmp, c); |
| 109 | + } |
| 110 | + |
| 111 | +LBL_ERR: |
| 112 | + mp_clear_multi(&tmp, &int_part, &fract_part, NULL); |
| 113 | + return err; |
| 114 | +} |
| 115 | + |
| 116 | +#endif |
0 commit comments