typearith.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. /*
  2. * Copyright (c) 2008-2010 Stefan Krah. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. * SUCH DAMAGE.
  26. */
  27. #ifndef TYPEARITH_H
  28. #define TYPEARITH_H
  29. #include "mpdecimal.h"
  30. /*****************************************************************************/
  31. /* Native arithmetic on basic types */
  32. /*****************************************************************************/
  33. /** ------------------------------------------------------------
  34. ** Double width multiplication and division
  35. ** ------------------------------------------------------------
  36. */
  37. #if defined(CONFIG_64)
  38. #if defined(ANSI)
  39. #if defined(HAVE_UINT128_T)
  40. static inline void
  41. _mpd_mul_words(mpd_uint_t *hi, mpd_uint_t *lo, mpd_uint_t a, mpd_uint_t b)
  42. {
  43. __uint128_t hl;
  44. hl = (__uint128_t)a * b;
  45. *hi = hl >> 64;
  46. *lo = (mpd_uint_t)hl;
  47. }
  48. static inline void
  49. _mpd_div_words(mpd_uint_t *q, mpd_uint_t *r, mpd_uint_t hi, mpd_uint_t lo,
  50. mpd_uint_t d)
  51. {
  52. __uint128_t hl;
  53. hl = ((__uint128_t)hi<<64) + lo;
  54. *q = (mpd_uint_t)(hl / d); /* quotient is known to fit */
  55. *r = (mpd_uint_t)(hl - (__uint128_t)(*q) * d);
  56. }
  57. #else
  58. static inline void
  59. _mpd_mul_words(mpd_uint_t *hi, mpd_uint_t *lo, mpd_uint_t a, mpd_uint_t b)
  60. {
  61. uint32_t w[4], carry;
  62. uint32_t ah, al, bh, bl;
  63. uint64_t hl;
  64. ah = (uint32_t)(a>>32); al = (uint32_t)a;
  65. bh = (uint32_t)(b>>32); bl = (uint32_t)b;
  66. hl = (uint64_t)al * bl;
  67. w[0] = (uint32_t)hl;
  68. carry = (uint32_t)(hl>>32);
  69. hl = (uint64_t)ah * bl + carry;
  70. w[1] = (uint32_t)hl;
  71. w[2] = (uint32_t)(hl>>32);
  72. hl = (uint64_t)al * bh + w[1];
  73. w[1] = (uint32_t)hl;
  74. carry = (uint32_t)(hl>>32);
  75. hl = ((uint64_t)ah * bh + w[2]) + carry;
  76. w[2] = (uint32_t)hl;
  77. w[3] = (uint32_t)(hl>>32);
  78. *hi = ((uint64_t)w[3]<<32) + w[2];
  79. *lo = ((uint64_t)w[1]<<32) + w[0];
  80. }
  81. /*
  82. * By Henry S. Warren: http://www.hackersdelight.org/HDcode/divlu.c.txt
  83. * http://www.hackersdelight.org/permissions.htm:
  84. * "You are free to use, copy, and distribute any of the code on this web
  85. * site, whether modified by you or not. You need not give attribution."
  86. *
  87. * Slightly modified, comments are mine.
  88. */
  89. static inline int
  90. nlz(uint64_t x)
  91. {
  92. int n;
  93. if (x == 0) return(64);
  94. n = 0;
  95. if (x <= 0x00000000FFFFFFFF) {n = n +32; x = x <<32;}
  96. if (x <= 0x0000FFFFFFFFFFFF) {n = n +16; x = x <<16;}
  97. if (x <= 0x00FFFFFFFFFFFFFF) {n = n + 8; x = x << 8;}
  98. if (x <= 0x0FFFFFFFFFFFFFFF) {n = n + 4; x = x << 4;}
  99. if (x <= 0x3FFFFFFFFFFFFFFF) {n = n + 2; x = x << 2;}
  100. if (x <= 0x7FFFFFFFFFFFFFFF) {n = n + 1;}
  101. return n;
  102. }
  103. static inline void
  104. _mpd_div_words(mpd_uint_t *q, mpd_uint_t *r, mpd_uint_t u1, mpd_uint_t u0,
  105. mpd_uint_t v)
  106. {
  107. const mpd_uint_t b = 4294967296;
  108. mpd_uint_t un1, un0,
  109. vn1, vn0,
  110. q1, q0,
  111. un32, un21, un10,
  112. rhat, t;
  113. int s;
  114. assert(u1 < v);
  115. s = nlz(v);
  116. v = v << s;
  117. vn1 = v >> 32;
  118. vn0 = v & 0xFFFFFFFF;
  119. t = (s == 0) ? 0 : u0 >> (64 - s);
  120. un32 = (u1 << s) | t;
  121. un10 = u0 << s;
  122. un1 = un10 >> 32;
  123. un0 = un10 & 0xFFFFFFFF;
  124. q1 = un32 / vn1;
  125. rhat = un32 - q1*vn1;
  126. again1:
  127. if (q1 >= b || q1*vn0 > b*rhat + un1) {
  128. q1 = q1 - 1;
  129. rhat = rhat + vn1;
  130. if (rhat < b) goto again1;
  131. }
  132. /*
  133. * Before again1 we had:
  134. * (1) q1*vn1 + rhat = un32
  135. * (2) q1*vn1*b + rhat*b + un1 = un32*b + un1
  136. *
  137. * The statements inside the if-clause do not change the value
  138. * of the left-hand side of (2), and the loop is only exited
  139. * if q1*vn0 <= rhat*b + un1, so:
  140. *
  141. * (3) q1*vn1*b + q1*vn0 <= un32*b + un1
  142. * (4) q1*v <= un32*b + un1
  143. * (5) 0 <= un32*b + un1 - q1*v
  144. *
  145. * By (5) we are certain that the possible add-back step from
  146. * Knuth's algorithm D is never required.
  147. *
  148. * Since the final quotient is less than 2**64, the following
  149. * must be true:
  150. *
  151. * (6) un32*b + un1 - q1*v <= UINT64_MAX
  152. *
  153. * This means that in the following line, the high words
  154. * of un32*b and q1*v can be discarded without any effect
  155. * on the result.
  156. */
  157. un21 = un32*b + un1 - q1*v;
  158. q0 = un21 / vn1;
  159. rhat = un21 - q0*vn1;
  160. again2:
  161. if (q0 >= b || q0*vn0 > b*rhat + un0) {
  162. q0 = q0 - 1;
  163. rhat = rhat + vn1;
  164. if (rhat < b) goto again2;
  165. }
  166. *q = q1*b + q0;
  167. *r = (un21*b + un0 - q0*v) >> s;
  168. }
  169. #endif
  170. /* END ANSI */
  171. #elif defined(ASM)
  172. static inline void
  173. _mpd_mul_words(mpd_uint_t *hi, mpd_uint_t *lo, mpd_uint_t a, mpd_uint_t b)
  174. {
  175. mpd_uint_t h, l;
  176. asm ( "mulq %3\n\t"
  177. : "=d" (h), "=a" (l)
  178. : "%a" (a), "rm" (b)
  179. : "cc"
  180. );
  181. *hi = h;
  182. *lo = l;
  183. }
  184. static inline void
  185. _mpd_div_words(mpd_uint_t *q, mpd_uint_t *r, mpd_uint_t hi, mpd_uint_t lo,
  186. mpd_uint_t d)
  187. {
  188. mpd_uint_t qq, rr;
  189. asm ( "divq %4\n\t"
  190. : "=a" (qq), "=d" (rr)
  191. : "a" (lo), "d" (hi), "rm" (d)
  192. : "cc"
  193. );
  194. *q = qq;
  195. *r = rr;
  196. }
  197. /* END GCC ASM */
  198. #elif defined(MASM)
  199. #include <intrin.h>
  200. #pragma intrinsic(_umul128)
  201. static inline void
  202. _mpd_mul_words(mpd_uint_t *hi, mpd_uint_t *lo, mpd_uint_t a, mpd_uint_t b)
  203. {
  204. *lo = _umul128(a, b, hi);
  205. }
  206. void _mpd_div_words(mpd_uint_t *q, mpd_uint_t *r, mpd_uint_t hi, mpd_uint_t lo,
  207. mpd_uint_t d);
  208. /* END MASM (_MSC_VER) */
  209. #else
  210. #error "need platform specific 128 bit multiplication and division"
  211. #endif
  212. static inline void
  213. _mpd_divmod_pow10(mpd_uint_t *q, mpd_uint_t *r, mpd_uint_t v, mpd_uint_t exp)
  214. {
  215. assert(exp <= 19);
  216. if (exp <= 9) {
  217. if (exp <= 4) {
  218. switch (exp) {
  219. case 0: *q = v; *r = 0; break;
  220. case 1: *q = v / 10ULL; *r = v - *q * 10ULL; break;
  221. case 2: *q = v / 100ULL; *r = v - *q * 100ULL; break;
  222. case 3: *q = v / 1000ULL; *r = v - *q * 1000ULL; break;
  223. case 4: *q = v / 10000ULL; *r = v - *q * 10000ULL; break;
  224. }
  225. }
  226. else {
  227. switch (exp) {
  228. case 5: *q = v / 100000ULL; *r = v - *q * 100000ULL; break;
  229. case 6: *q = v / 1000000ULL; *r = v - *q * 1000000ULL; break;
  230. case 7: *q = v / 10000000ULL; *r = v - *q * 10000000ULL; break;
  231. case 8: *q = v / 100000000ULL; *r = v - *q * 100000000ULL; break;
  232. case 9: *q = v / 1000000000ULL; *r = v - *q * 1000000000ULL; break;
  233. }
  234. }
  235. }
  236. else {
  237. if (exp <= 14) {
  238. switch (exp) {
  239. case 10: *q = v / 10000000000ULL; *r = v - *q * 10000000000ULL; break;
  240. case 11: *q = v / 100000000000ULL; *r = v - *q * 100000000000ULL; break;
  241. case 12: *q = v / 1000000000000ULL; *r = v - *q * 1000000000000ULL; break;
  242. case 13: *q = v / 10000000000000ULL; *r = v - *q * 10000000000000ULL; break;
  243. case 14: *q = v / 100000000000000ULL; *r = v - *q * 100000000000000ULL; break;
  244. }
  245. }
  246. else {
  247. switch (exp) {
  248. case 15: *q = v / 1000000000000000ULL; *r = v - *q * 1000000000000000ULL; break;
  249. case 16: *q = v / 10000000000000000ULL; *r = v - *q * 10000000000000000ULL; break;
  250. case 17: *q = v / 100000000000000000ULL; *r = v - *q * 100000000000000000ULL; break;
  251. case 18: *q = v / 1000000000000000000ULL; *r = v - *q * 1000000000000000000ULL; break;
  252. case 19: *q = v / 10000000000000000000ULL; *r = v - *q * 10000000000000000000ULL; break; /* GCOV_NOT_REACHED */
  253. }
  254. }
  255. }
  256. }
  257. /* END CONFIG_64 */
  258. #elif defined(CONFIG_32)
  259. #if defined(ANSI)
  260. #if !defined(LEGACY_COMPILER)
  261. static inline void
  262. _mpd_mul_words(mpd_uint_t *hi, mpd_uint_t *lo, mpd_uint_t a, mpd_uint_t b)
  263. {
  264. mpd_uuint_t hl;
  265. hl = (mpd_uuint_t)a * b;
  266. *hi = hl >> 32;
  267. *lo = (mpd_uint_t)hl;
  268. }
  269. static inline void
  270. _mpd_div_words(mpd_uint_t *q, mpd_uint_t *r, mpd_uint_t hi, mpd_uint_t lo,
  271. mpd_uint_t d)
  272. {
  273. mpd_uuint_t hl;
  274. hl = ((mpd_uuint_t)hi<<32) + lo;
  275. *q = (mpd_uint_t)(hl / d); /* quotient is known to fit */
  276. *r = (mpd_uint_t)(hl - (mpd_uuint_t)(*q) * d);
  277. }
  278. /* END ANSI + uint64_t */
  279. #else
  280. static inline void
  281. _mpd_mul_words(mpd_uint_t *hi, mpd_uint_t *lo, mpd_uint_t a, mpd_uint_t b)
  282. {
  283. uint16_t w[4], carry;
  284. uint16_t ah, al, bh, bl;
  285. uint32_t hl;
  286. ah = (uint16_t)(a>>16); al = (uint16_t)a;
  287. bh = (uint16_t)(b>>16); bl = (uint16_t)b;
  288. hl = (uint32_t)al * bl;
  289. w[0] = (uint16_t)hl;
  290. carry = (uint16_t)(hl>>16);
  291. hl = (uint32_t)ah * bl + carry;
  292. w[1] = (uint16_t)hl;
  293. w[2] = (uint16_t)(hl>>16);
  294. hl = (uint32_t)al * bh + w[1];
  295. w[1] = (uint16_t)hl;
  296. carry = (uint16_t)(hl>>16);
  297. hl = ((uint32_t)ah * bh + w[2]) + carry;
  298. w[2] = (uint16_t)hl;
  299. w[3] = (uint16_t)(hl>>16);
  300. *hi = ((uint32_t)w[3]<<16) + w[2];
  301. *lo = ((uint32_t)w[1]<<16) + w[0];
  302. }
  303. /*
  304. * By Henry S. Warren: http://www.hackersdelight.org/HDcode/divlu.c.txt
  305. * http://www.hackersdelight.org/permissions.htm:
  306. * "You are free to use, copy, and distribute any of the code on this web
  307. * site, whether modified by you or not. You need not give attribution."
  308. *
  309. * Slightly modified, comments are mine.
  310. */
  311. static inline int
  312. nlz(uint32_t x)
  313. {
  314. int n;
  315. if (x == 0) return(32);
  316. n = 0;
  317. if (x <= 0x0000FFFF) {n = n +16; x = x <<16;}
  318. if (x <= 0x00FFFFFF) {n = n + 8; x = x << 8;}
  319. if (x <= 0x0FFFFFFF) {n = n + 4; x = x << 4;}
  320. if (x <= 0x3FFFFFFF) {n = n + 2; x = x << 2;}
  321. if (x <= 0x7FFFFFFF) {n = n + 1;}
  322. return n;
  323. }
  324. static inline void
  325. _mpd_div_words(mpd_uint_t *q, mpd_uint_t *r, mpd_uint_t u1, mpd_uint_t u0,
  326. mpd_uint_t v)
  327. {
  328. const mpd_uint_t b = 65536;
  329. mpd_uint_t un1, un0,
  330. vn1, vn0,
  331. q1, q0,
  332. un32, un21, un10,
  333. rhat, t;
  334. int s;
  335. assert(u1 < v);
  336. s = nlz(v);
  337. v = v << s;
  338. vn1 = v >> 16;
  339. vn0 = v & 0xFFFF;
  340. t = (s == 0) ? 0 : u0 >> (32 - s);
  341. un32 = (u1 << s) | t;
  342. un10 = u0 << s;
  343. un1 = un10 >> 16;
  344. un0 = un10 & 0xFFFF;
  345. q1 = un32 / vn1;
  346. rhat = un32 - q1*vn1;
  347. again1:
  348. if (q1 >= b || q1*vn0 > b*rhat + un1) {
  349. q1 = q1 - 1;
  350. rhat = rhat + vn1;
  351. if (rhat < b) goto again1;
  352. }
  353. /*
  354. * Before again1 we had:
  355. * (1) q1*vn1 + rhat = un32
  356. * (2) q1*vn1*b + rhat*b + un1 = un32*b + un1
  357. *
  358. * The statements inside the if-clause do not change the value
  359. * of the left-hand side of (2), and the loop is only exited
  360. * if q1*vn0 <= rhat*b + un1, so:
  361. *
  362. * (3) q1*vn1*b + q1*vn0 <= un32*b + un1
  363. * (4) q1*v <= un32*b + un1
  364. * (5) 0 <= un32*b + un1 - q1*v
  365. *
  366. * By (5) we are certain that the possible add-back step from
  367. * Knuth's algorithm D is never required.
  368. *
  369. * Since the final quotient is less than 2**32, the following
  370. * must be true:
  371. *
  372. * (6) un32*b + un1 - q1*v <= UINT32_MAX
  373. *
  374. * This means that in the following line, the high words
  375. * of un32*b and q1*v can be discarded without any effect
  376. * on the result.
  377. */
  378. un21 = un32*b + un1 - q1*v;
  379. q0 = un21 / vn1;
  380. rhat = un21 - q0*vn1;
  381. again2:
  382. if (q0 >= b || q0*vn0 > b*rhat + un0) {
  383. q0 = q0 - 1;
  384. rhat = rhat + vn1;
  385. if (rhat < b) goto again2;
  386. }
  387. *q = q1*b + q0;
  388. *r = (un21*b + un0 - q0*v) >> s;
  389. }
  390. #endif /* END ANSI + LEGACY_COMPILER */
  391. /* END ANSI */
  392. #elif defined(ASM)
  393. static inline void
  394. _mpd_mul_words(mpd_uint_t *hi, mpd_uint_t *lo, mpd_uint_t a, mpd_uint_t b)
  395. {
  396. mpd_uint_t h, l;
  397. asm ( "mull %3\n\t"
  398. : "=d" (h), "=a" (l)
  399. : "%a" (a), "rm" (b)
  400. : "cc"
  401. );
  402. *hi = h;
  403. *lo = l;
  404. }
  405. static inline void
  406. _mpd_div_words(mpd_uint_t *q, mpd_uint_t *r, mpd_uint_t hi, mpd_uint_t lo,
  407. mpd_uint_t d)
  408. {
  409. mpd_uint_t qq, rr;
  410. asm ( "divl %4\n\t"
  411. : "=a" (qq), "=d" (rr)
  412. : "a" (lo), "d" (hi), "rm" (d)
  413. : "cc"
  414. );
  415. *q = qq;
  416. *r = rr;
  417. }
  418. /* END GCC ASM */
  419. #elif defined(MASM)
  420. static inline void __cdecl
  421. _mpd_mul_words(mpd_uint_t *hi, mpd_uint_t *lo, mpd_uint_t a, mpd_uint_t b)
  422. {
  423. mpd_uint_t h, l;
  424. __asm {
  425. mov eax, a
  426. mul b
  427. mov h, edx
  428. mov l, eax
  429. }
  430. *hi = h;
  431. *lo = l;
  432. }
  433. static inline void __cdecl
  434. _mpd_div_words(mpd_uint_t *q, mpd_uint_t *r, mpd_uint_t hi, mpd_uint_t lo,
  435. mpd_uint_t d)
  436. {
  437. mpd_uint_t qq, rr;
  438. __asm {
  439. mov eax, lo
  440. mov edx, hi
  441. div d
  442. mov qq, eax
  443. mov rr, edx
  444. }
  445. *q = qq;
  446. *r = rr;
  447. }
  448. /* END MASM (_MSC_VER) */
  449. #else
  450. #error "need platform specific 64 bit multiplication and division"
  451. #endif
  452. static inline void
  453. _mpd_divmod_pow10(mpd_uint_t *q, mpd_uint_t *r, mpd_uint_t v, mpd_uint_t exp)
  454. {
  455. assert(exp <= 9);
  456. if (exp <= 4) {
  457. switch (exp) {
  458. case 0: *q = v; *r = 0; break;
  459. case 1: *q = v / 10UL; *r = v - *q * 10UL; break;
  460. case 2: *q = v / 100UL; *r = v - *q * 100UL; break;
  461. case 3: *q = v / 1000UL; *r = v - *q * 1000UL; break;
  462. case 4: *q = v / 10000UL; *r = v - *q * 10000UL; break;
  463. }
  464. }
  465. else {
  466. switch (exp) {
  467. case 5: *q = v / 100000UL; *r = v - *q * 100000UL; break;
  468. case 6: *q = v / 1000000UL; *r = v - *q * 1000000UL; break;
  469. case 7: *q = v / 10000000UL; *r = v - *q * 10000000UL; break;
  470. case 8: *q = v / 100000000UL; *r = v - *q * 100000000UL; break;
  471. case 9: *q = v / 1000000000UL; *r = v - *q * 1000000000UL; break; /* GCOV_NOT_REACHED */
  472. }
  473. }
  474. }
  475. /* END CONFIG_32 */
  476. /* NO CONFIG */
  477. #else
  478. #error "define CONFIG_64 or CONFIG_32"
  479. #endif /* CONFIG */
  480. static inline void
  481. _mpd_div_word(mpd_uint_t *q, mpd_uint_t *r, mpd_uint_t v, mpd_uint_t d)
  482. {
  483. *q = v / d;
  484. *r = v - *q * d;
  485. }
  486. static inline void
  487. _mpd_idiv_word(mpd_ssize_t *q, mpd_ssize_t *r, mpd_ssize_t v, mpd_ssize_t d)
  488. {
  489. *q = v / d;
  490. *r = v - *q * d;
  491. }
  492. /** ------------------------------------------------------------
  493. ** Arithmetic with overflow checking
  494. ** ------------------------------------------------------------
  495. */
  496. static inline mpd_size_t
  497. add_size_t(mpd_size_t a, mpd_size_t b)
  498. {
  499. if (a > MPD_SIZE_MAX - b) {
  500. mpd_err_fatal("add_size_t(): overflow: check the context"); /* GCOV_NOT_REACHED */
  501. }
  502. return a + b;
  503. }
  504. static inline mpd_size_t
  505. sub_size_t(mpd_size_t a, mpd_size_t b)
  506. {
  507. if (b > a) {
  508. mpd_err_fatal("sub_size_t(): overflow: check the context"); /* GCOV_NOT_REACHED */
  509. }
  510. return a - b;
  511. }
  512. #if MPD_SIZE_MAX != MPD_UINT_MAX
  513. #error "adapt mul_size_t() and mulmod_size_t()"
  514. #endif
  515. static inline mpd_size_t
  516. mul_size_t(mpd_size_t a, mpd_size_t b)
  517. {
  518. mpd_uint_t hi, lo;
  519. _mpd_mul_words(&hi, &lo, (mpd_uint_t)a, (mpd_uint_t)b);
  520. if (hi) {
  521. mpd_err_fatal("mul_size_t(): overflow: check the context"); /* GCOV_NOT_REACHED */
  522. }
  523. return lo;
  524. }
  525. static inline mpd_ssize_t
  526. mod_mpd_ssize_t(mpd_ssize_t a, mpd_ssize_t m)
  527. {
  528. mpd_ssize_t r = a % m;
  529. return (r < 0) ? r + m : r;
  530. }
  531. static inline mpd_size_t
  532. mulmod_size_t(mpd_size_t a, mpd_size_t b, mpd_size_t m)
  533. {
  534. mpd_uint_t hi, lo;
  535. mpd_uint_t q, r;
  536. _mpd_mul_words(&hi, &lo, (mpd_uint_t)a, (mpd_uint_t)b);
  537. _mpd_div_words(&q, &r, hi, lo, (mpd_uint_t)m);
  538. return r;
  539. }
  540. #endif /* TYPEARITH_H */