change some parameters in the library to meet the needs of the website httpbin.org
Fork of MiniTLS-GPL by
math/divide/fp_div_d.c@5:95f70ebfe61f, 2015-02-06 (annotated)
- Committer:
- shiyilei
- Date:
- Fri Feb 06 06:17:33 2015 +0000
- Revision:
- 5:95f70ebfe61f
- Parent:
- 0:35aa5be3b78d
change some parameters in the library to meet the needs of httpbin.org
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MiniTLS | 0:35aa5be3b78d | 1 | /* TomsFastMath, a fast ISO C bignum library. |
MiniTLS | 0:35aa5be3b78d | 2 | * |
MiniTLS | 0:35aa5be3b78d | 3 | * This project is meant to fill in where LibTomMath |
MiniTLS | 0:35aa5be3b78d | 4 | * falls short. That is speed ;-) |
MiniTLS | 0:35aa5be3b78d | 5 | * |
MiniTLS | 0:35aa5be3b78d | 6 | * This project is public domain and free for all purposes. |
MiniTLS | 0:35aa5be3b78d | 7 | * |
MiniTLS | 0:35aa5be3b78d | 8 | * Tom St Denis, tomstdenis@gmail.com |
MiniTLS | 0:35aa5be3b78d | 9 | */ |
MiniTLS | 0:35aa5be3b78d | 10 | #include <tfm.h> |
MiniTLS | 0:35aa5be3b78d | 11 | |
MiniTLS | 0:35aa5be3b78d | 12 | static int s_is_power_of_two(fp_digit b, int *p) |
MiniTLS | 0:35aa5be3b78d | 13 | { |
MiniTLS | 0:35aa5be3b78d | 14 | int x; |
MiniTLS | 0:35aa5be3b78d | 15 | |
MiniTLS | 0:35aa5be3b78d | 16 | /* fast return if no power of two */ |
MiniTLS | 0:35aa5be3b78d | 17 | if ((b==0) || (b & (b-1))) { |
MiniTLS | 0:35aa5be3b78d | 18 | return 0; |
MiniTLS | 0:35aa5be3b78d | 19 | } |
MiniTLS | 0:35aa5be3b78d | 20 | |
MiniTLS | 0:35aa5be3b78d | 21 | for (x = 0; x < DIGIT_BIT; x++) { |
MiniTLS | 0:35aa5be3b78d | 22 | if (b == (((fp_digit)1)<<x)) { |
MiniTLS | 0:35aa5be3b78d | 23 | *p = x; |
MiniTLS | 0:35aa5be3b78d | 24 | return 1; |
MiniTLS | 0:35aa5be3b78d | 25 | } |
MiniTLS | 0:35aa5be3b78d | 26 | } |
MiniTLS | 0:35aa5be3b78d | 27 | return 0; |
MiniTLS | 0:35aa5be3b78d | 28 | } |
MiniTLS | 0:35aa5be3b78d | 29 | |
MiniTLS | 0:35aa5be3b78d | 30 | /* a/b => cb + d == a */ |
MiniTLS | 0:35aa5be3b78d | 31 | int fp_div_d(fp_int *a, fp_digit b, fp_int *c, fp_digit *d) |
MiniTLS | 0:35aa5be3b78d | 32 | { |
MiniTLS | 0:35aa5be3b78d | 33 | fp_int q; |
MiniTLS | 0:35aa5be3b78d | 34 | fp_word w; |
MiniTLS | 0:35aa5be3b78d | 35 | fp_digit t; |
MiniTLS | 0:35aa5be3b78d | 36 | int ix; |
MiniTLS | 0:35aa5be3b78d | 37 | |
MiniTLS | 0:35aa5be3b78d | 38 | /* cannot divide by zero */ |
MiniTLS | 0:35aa5be3b78d | 39 | if (b == 0) { |
MiniTLS | 0:35aa5be3b78d | 40 | return FP_VAL; |
MiniTLS | 0:35aa5be3b78d | 41 | } |
MiniTLS | 0:35aa5be3b78d | 42 | |
MiniTLS | 0:35aa5be3b78d | 43 | /* quick outs */ |
MiniTLS | 0:35aa5be3b78d | 44 | if (b == 1 || fp_iszero(a) == 1) { |
MiniTLS | 0:35aa5be3b78d | 45 | if (d != NULL) { |
MiniTLS | 0:35aa5be3b78d | 46 | *d = 0; |
MiniTLS | 0:35aa5be3b78d | 47 | } |
MiniTLS | 0:35aa5be3b78d | 48 | if (c != NULL) { |
MiniTLS | 0:35aa5be3b78d | 49 | fp_copy(a, c); |
MiniTLS | 0:35aa5be3b78d | 50 | } |
MiniTLS | 0:35aa5be3b78d | 51 | return FP_OKAY; |
MiniTLS | 0:35aa5be3b78d | 52 | } |
MiniTLS | 0:35aa5be3b78d | 53 | |
MiniTLS | 0:35aa5be3b78d | 54 | /* power of two ? */ |
MiniTLS | 0:35aa5be3b78d | 55 | if (s_is_power_of_two(b, &ix) == 1) { |
MiniTLS | 0:35aa5be3b78d | 56 | if (d != NULL) { |
MiniTLS | 0:35aa5be3b78d | 57 | *d = a->dp[0] & ((((fp_digit)1)<<ix) - 1); |
MiniTLS | 0:35aa5be3b78d | 58 | } |
MiniTLS | 0:35aa5be3b78d | 59 | if (c != NULL) { |
MiniTLS | 0:35aa5be3b78d | 60 | fp_div_2d(a, ix, c, NULL); |
MiniTLS | 0:35aa5be3b78d | 61 | } |
MiniTLS | 0:35aa5be3b78d | 62 | return FP_OKAY; |
MiniTLS | 0:35aa5be3b78d | 63 | } |
MiniTLS | 0:35aa5be3b78d | 64 | |
MiniTLS | 0:35aa5be3b78d | 65 | /* no easy answer [c'est la vie]. Just division */ |
MiniTLS | 0:35aa5be3b78d | 66 | fp_init(&q); |
MiniTLS | 0:35aa5be3b78d | 67 | |
MiniTLS | 0:35aa5be3b78d | 68 | q.used = a->used; |
MiniTLS | 0:35aa5be3b78d | 69 | q.sign = a->sign; |
MiniTLS | 0:35aa5be3b78d | 70 | w = 0; |
MiniTLS | 0:35aa5be3b78d | 71 | for (ix = a->used - 1; ix >= 0; ix--) { |
MiniTLS | 0:35aa5be3b78d | 72 | w = (w << ((fp_word)DIGIT_BIT)) | ((fp_word)a->dp[ix]); |
MiniTLS | 0:35aa5be3b78d | 73 | |
MiniTLS | 0:35aa5be3b78d | 74 | if (w >= b) { |
MiniTLS | 0:35aa5be3b78d | 75 | t = (fp_digit)(w / b); |
MiniTLS | 0:35aa5be3b78d | 76 | w -= ((fp_word)t) * ((fp_word)b); |
MiniTLS | 0:35aa5be3b78d | 77 | } else { |
MiniTLS | 0:35aa5be3b78d | 78 | t = 0; |
MiniTLS | 0:35aa5be3b78d | 79 | } |
MiniTLS | 0:35aa5be3b78d | 80 | q.dp[ix] = (fp_digit)t; |
MiniTLS | 0:35aa5be3b78d | 81 | } |
MiniTLS | 0:35aa5be3b78d | 82 | |
MiniTLS | 0:35aa5be3b78d | 83 | if (d != NULL) { |
MiniTLS | 0:35aa5be3b78d | 84 | *d = (fp_digit)w; |
MiniTLS | 0:35aa5be3b78d | 85 | } |
MiniTLS | 0:35aa5be3b78d | 86 | |
MiniTLS | 0:35aa5be3b78d | 87 | if (c != NULL) { |
MiniTLS | 0:35aa5be3b78d | 88 | fp_clamp(&q); |
MiniTLS | 0:35aa5be3b78d | 89 | fp_copy(&q, c); |
MiniTLS | 0:35aa5be3b78d | 90 | } |
MiniTLS | 0:35aa5be3b78d | 91 | |
MiniTLS | 0:35aa5be3b78d | 92 | return FP_OKAY; |
MiniTLS | 0:35aa5be3b78d | 93 | } |
MiniTLS | 0:35aa5be3b78d | 94 | |
MiniTLS | 0:35aa5be3b78d | 95 | |
MiniTLS | 0:35aa5be3b78d | 96 | /* $Source: /cvs/libtom/tomsfastmath/src/divide/fp_div_d.c,v $ */ |
MiniTLS | 0:35aa5be3b78d | 97 | /* $Revision: 1.2 $ */ |
MiniTLS | 0:35aa5be3b78d | 98 | /* $Date: 2007/01/12 15:13:54 $ */ |