1 /* TomsFastMath, a fast ISO C bignum library.
3 * This project is meant to fill in where LibTomMath
4 * falls short. That is speed ;-)
6 * This project is public domain and free for all purposes.
8 * Tom St Denis, tomstdenis@gmail.com
10 #include <tfm_private.h>
12 static int s_is_power_of_two(fp_digit b, int *p)
16 /* fast return if no power of two */
17 if ((b==0) || (b & (b-1))) {
21 for (x = 0; x < DIGIT_BIT; x++) {
22 if (b == (((fp_digit)1)<<x)) {
30 /* a/b => cb + d == a */
31 int fp_div_d(fp_int *a, fp_digit b, fp_int *c, fp_digit *d)
38 /* cannot divide by zero */
44 if (b == 1 || fp_iszero(a) == 1) {
55 if (s_is_power_of_two(b, &ix) == 1) {
57 *d = a->dp[0] & ((((fp_digit)1)<<ix) - 1);
60 fp_div_2d(a, ix, c, NULL);
65 /* no easy answer [c'est la vie]. Just division */
71 for (ix = a->used - 1; ix >= 0; ix--) {
72 w = (w << ((fp_word)DIGIT_BIT)) | ((fp_word)a->dp[ix]);
75 t = (fp_digit)(w / b);
76 w -= ((fp_word)t) * ((fp_word)b);
80 q.dp[ix] = (fp_digit)t;