| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
#ifndef BASEARITH_H |
| 30 |
#define BASEARITH_H |
| 31 |
|
| 32 |
|
| 33 |
#include "mpdecimal.h" |
| 34 |
#include <stdio.h> |
| 35 |
#include "typearith.h" |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
MPD_PRAGMA(MPD_HIDE_SYMBOLS_START) |
| 40 |
|
| 41 |
|
| 42 |
mpd_uint_t _mpd_baseadd(mpd_uint_t *w, const mpd_uint_t *u, const mpd_uint_t *v, |
| 43 |
mpd_size_t m, mpd_size_t n); |
| 44 |
void _mpd_baseaddto(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n); |
| 45 |
mpd_uint_t _mpd_shortadd(mpd_uint_t *w, mpd_size_t m, mpd_uint_t v); |
| 46 |
mpd_uint_t _mpd_shortadd_b(mpd_uint_t *w, mpd_size_t m, mpd_uint_t v, |
| 47 |
mpd_uint_t b); |
| 48 |
mpd_uint_t _mpd_baseincr(mpd_uint_t *u, mpd_size_t n); |
| 49 |
void _mpd_basesub(mpd_uint_t *w, const mpd_uint_t *u, const mpd_uint_t *v, |
| 50 |
mpd_size_t m, mpd_size_t n); |
| 51 |
void _mpd_basesubfrom(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n); |
| 52 |
void _mpd_basemul(mpd_uint_t *w, const mpd_uint_t *u, const mpd_uint_t *v, |
| 53 |
mpd_size_t m, mpd_size_t n); |
| 54 |
void _mpd_shortmul(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n, |
| 55 |
mpd_uint_t v); |
| 56 |
mpd_uint_t _mpd_shortmul_c(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n, |
| 57 |
mpd_uint_t v); |
| 58 |
mpd_uint_t _mpd_shortmul_b(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n, |
| 59 |
mpd_uint_t v, mpd_uint_t b); |
| 60 |
mpd_uint_t _mpd_shortdiv(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n, |
| 61 |
mpd_uint_t v); |
| 62 |
mpd_uint_t _mpd_shortdiv_b(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n, |
| 63 |
mpd_uint_t v, mpd_uint_t b); |
| 64 |
int _mpd_basedivmod(mpd_uint_t *q, mpd_uint_t *r, const mpd_uint_t *uconst, |
| 65 |
const mpd_uint_t *vconst, mpd_size_t nplusm, mpd_size_t n); |
| 66 |
void _mpd_baseshiftl(mpd_uint_t *dest, mpd_uint_t *src, mpd_size_t n, |
| 67 |
mpd_size_t m, mpd_size_t shift); |
| 68 |
mpd_uint_t _mpd_baseshiftr(mpd_uint_t *dest, mpd_uint_t *src, mpd_size_t slen, |
| 69 |
mpd_size_t shift); |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
#ifdef CONFIG_64 |
| 74 |
extern const mpd_uint_t mprime_rdx; |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
static inline void |
| 99 |
_mpd_div_words_r(mpd_uint_t *q, mpd_uint_t *r, mpd_uint_t hi, mpd_uint_t lo) |
| 100 |
{ |
| 101 |
mpd_uint_t n_adj, h, l, t; |
| 102 |
mpd_uint_t n1_neg; |
| 103 |
|
| 104 |
|
| 105 |
n1_neg = (lo & (1ULL<<63)) ? MPD_UINT_MAX : 0; |
| 106 |
|
| 107 |
n_adj = lo + (n1_neg & MPD_RADIX); |
| 108 |
|
| 109 |
|
| 110 |
_mpd_mul_words(&h, &l, mprime_rdx, hi-n1_neg); |
| 111 |
l = l + n_adj; |
| 112 |
if (l < n_adj) h++; |
| 113 |
t = h + hi; |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
t = MPD_UINT_MAX - t; |
| 120 |
|
| 121 |
|
| 122 |
_mpd_mul_words(&h, &l, t, MPD_RADIX); |
| 123 |
l = l + lo; |
| 124 |
if (l < lo) h++; |
| 125 |
h += hi; |
| 126 |
h -= MPD_RADIX; |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
*q = (h - t); |
| 139 |
*r = l + (MPD_RADIX & h); |
| 140 |
} |
| 141 |
#else |
| 142 |
static inline void |
| 143 |
_mpd_div_words_r(mpd_uint_t *q, mpd_uint_t *r, mpd_uint_t hi, mpd_uint_t lo) |
| 144 |
{ |
| 145 |
_mpd_div_words(q, r, hi, lo, MPD_RADIX); |
| 146 |
} |
| 147 |
#endif |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
static inline void |
| 152 |
_mpd_singlemul(mpd_uint_t w[2], mpd_uint_t u, mpd_uint_t v) |
| 153 |
{ |
| 154 |
mpd_uint_t hi, lo; |
| 155 |
|
| 156 |
_mpd_mul_words(&hi, &lo, u, v); |
| 157 |
_mpd_div_words_r(&w[1], &w[0], hi, lo); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
static inline void |
| 162 |
_mpd_mul_2_le2(mpd_uint_t w[4], mpd_uint_t u[2], mpd_uint_t v[2], mpd_ssize_t m) |
| 163 |
{ |
| 164 |
mpd_uint_t hi, lo; |
| 165 |
|
| 166 |
_mpd_mul_words(&hi, &lo, u[0], v[0]); |
| 167 |
_mpd_div_words_r(&w[1], &w[0], hi, lo); |
| 168 |
|
| 169 |
_mpd_mul_words(&hi, &lo, u[1], v[0]); |
| 170 |
lo = w[1] + lo; |
| 171 |
if (lo < w[1]) hi++; |
| 172 |
_mpd_div_words_r(&w[2], &w[1], hi, lo); |
| 173 |
if (m == 1) return; |
| 174 |
|
| 175 |
_mpd_mul_words(&hi, &lo, u[0], v[1]); |
| 176 |
lo = w[1] + lo; |
| 177 |
if (lo < w[1]) hi++; |
| 178 |
_mpd_div_words_r(&w[3], &w[1], hi, lo); |
| 179 |
|
| 180 |
_mpd_mul_words(&hi, &lo, u[1], v[1]); |
| 181 |
lo = w[2] + lo; |
| 182 |
if (lo < w[2]) hi++; |
| 183 |
lo = w[3] + lo; |
| 184 |
if (lo < w[3]) hi++; |
| 185 |
_mpd_div_words_r(&w[3], &w[2], hi, lo); |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
static inline int |
| 194 |
_mpd_isallzero(const mpd_uint_t *data, mpd_ssize_t len) |
| 195 |
{ |
| 196 |
while (--len >= 0) { |
| 197 |
if (data[len] != 0) return 0; |
| 198 |
} |
| 199 |
return 1; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
static inline int |
| 207 |
_mpd_isallnine(const mpd_uint_t *data, mpd_ssize_t len) |
| 208 |
{ |
| 209 |
while (--len >= 0) { |
| 210 |
if (data[len] != MPD_RADIX-1) return 0; |
| 211 |
} |
| 212 |
return 1; |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
MPD_PRAGMA(MPD_HIDE_SYMBOLS_END) |
| 217 |
|
| 218 |
|
| 219 |
#endif |
| 220 |
|
| 221 |
|
| 222 |
|