10 #ifndef MOD_ARITH_32BIT_H 11 #define MOD_ARITH_32BIT_H 20 static inline uint_fast32_t mod_add(uint_fast32_t a, uint_fast32_t b) {
22 assert (a <= 0x7FFFFFFE);
23 assert (b <= 0x7FFFFFFE);
25 return (a + b) % 0x7FFFFFFF;
28 x = (x >= 0x7FFFFFFF) ? (x - 0x7FFFFFFF) : x;
33 static inline uint_fast32_t mod_mul(uint_fast32_t a, uint_fast32_t b) {
36 assert (a <= 0x7FFFFFFE);
37 assert (b <= 0x7FFFFFFE);
39 return (uint_fast32_t)((uint_fast64_t)a * b % 0x7FFFFFFF);
41 temp = (uint_fast64_t)a * b;
42 temp2 = (uint_fast32_t)(temp & 0x7FFFFFFF) + (uint_fast32_t)(temp >> 31);
43 return (temp2 >= 0x7FFFFFFF) ? (temp2 - 0x7FFFFFFF) : temp2;
47 static inline uint_fast32_t mod_mac(uint_fast32_t sum, uint_fast32_t a, uint_fast32_t b) {
50 assert (sum <= 0x7FFFFFFE);
51 assert (a <= 0x7FFFFFFE);
52 assert (b <= 0x7FFFFFFE);
54 return (uint_fast32_t)(((uint_fast64_t)a * b + sum) % 0x7FFFFFFF);
56 temp = (uint_fast64_t)a * b + sum;
57 temp2 = (uint_fast32_t)(temp & 0x7FFFFFFF) + (uint_fast32_t)(temp >> 31);
58 return (temp2 >= 0x7FFFFFFF) ? (temp2 - 0x7FFFFFFF) : temp2;
62 static inline uint_fast32_t mod_mac2(uint_fast32_t sum, uint_fast32_t a, uint_fast32_t b, uint_fast32_t c, uint_fast32_t d) {
63 assert (sum <= 0x7FFFFFFE);
64 assert (a <= 0x7FFFFFFE);
65 assert (b <= 0x7FFFFFFE);
66 assert (c <= 0x7FFFFFFE);
67 assert (d <= 0x7FFFFFFE);
68 return mod_mac(mod_mac(sum, a, b), c, d);
71 static inline uint_fast32_t mod_mac3(uint_fast32_t sum, uint_fast32_t a, uint_fast32_t b, uint_fast32_t c, uint_fast32_t d, uint_fast32_t e, uint_fast32_t f) {
73 assert (sum <= 0x7FFFFFFE);
74 assert (a <= 0x7FFFFFFE);
75 assert (b <= 0x7FFFFFFE);
76 assert (c <= 0x7FFFFFFE);
77 assert (d <= 0x7FFFFFFE);
78 assert (e <= 0x7FFFFFFE);
79 assert (f <= 0x7FFFFFFE);
80 return mod_mac2(mod_mac(sum, a, b), c, d, e, f);
83 static inline uint_fast32_t mod_mac4(uint_fast32_t sum, uint_fast32_t a, uint_fast32_t b, uint_fast32_t c, uint_fast32_t d, uint_fast32_t e, uint_fast32_t f, uint_fast32_t g, uint_fast32_t h) {
85 assert (sum <= 0x7FFFFFFE);
86 assert (a <= 0x7FFFFFFE);
87 assert (b <= 0x7FFFFFFE);
88 assert (c <= 0x7FFFFFFE);
89 assert (d <= 0x7FFFFFFE);
90 assert (e <= 0x7FFFFFFE);
91 assert (f <= 0x7FFFFFFE);
92 assert (g <= 0x7FFFFFFE);
93 assert (h <= 0x7FFFFFFE);
94 return mod_mac2(mod_mac2(sum, a, b, c, d), e, f, g, h);
102 static inline uint_fast32_t mod_mul_x(uint_fast32_t a) {
103 return mod_mul(a, 107374182);
106 static inline uint_fast32_t mod_mul_y(uint_fast32_t a) {
107 return mod_mul(a, 104480);
110 static inline uint_fast32_t mod_mac_y(uint_fast32_t sum, uint_fast32_t a) {
111 return mod_mac(sum, a, 104480);