multi_arith.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /* multi_arith.h: multi-precision integer arithmetic functions, needed
  3. to do extended-precision floating point.
  4. (c) 1998 David Huggins-Daines.
  5. Somewhat based on arch/alpha/math-emu/ieee-math.c, which is (c)
  6. David Mosberger-Tang.
  7. */
  8. /* Note:
  9. These are not general multi-precision math routines. Rather, they
  10. implement the subset of integer arithmetic that we need in order to
  11. multiply, divide, and normalize 128-bit unsigned mantissae. */
  12. #ifndef MULTI_ARITH_H
  13. #define MULTI_ARITH_H
  14. static inline void fp_denormalize(struct fp_ext *reg, unsigned int cnt)
  15. {
  16. reg->exp += cnt;
  17. switch (cnt) {
  18. case 0 ... 8:
  19. reg->lowmant = reg->mant.m32[1] << (8 - cnt);
  20. reg->mant.m32[1] = (reg->mant.m32[1] >> cnt) |
  21. (reg->mant.m32[0] << (32 - cnt));
  22. reg->mant.m32[0] = reg->mant.m32[0] >> cnt;
  23. break;
  24. case 9 ... 32:
  25. reg->lowmant = reg->mant.m32[1] >> (cnt - 8);
  26. if (reg->mant.m32[1] << (40 - cnt))
  27. reg->lowmant |= 1;
  28. reg->mant.m32[1] = (reg->mant.m32[1] >> cnt) |
  29. (reg->mant.m32[0] << (32 - cnt));
  30. reg->mant.m32[0] = reg->mant.m32[0] >> cnt;
  31. break;
  32. case 33 ... 39:
  33. asm volatile ("bfextu %1{%2,#8},%0" : "=d" (reg->lowmant)
  34. : "m" (reg->mant.m32[0]), "d" (64 - cnt));
  35. if (reg->mant.m32[1] << (40 - cnt))
  36. reg->lowmant |= 1;
  37. reg->mant.m32[1] = reg->mant.m32[0] >> (cnt - 32);
  38. reg->mant.m32[0] = 0;
  39. break;
  40. case 40 ... 71:
  41. reg->lowmant = reg->mant.m32[0] >> (cnt - 40);
  42. if ((reg->mant.m32[0] << (72 - cnt)) || reg->mant.m32[1])
  43. reg->lowmant |= 1;
  44. reg->mant.m32[1] = reg->mant.m32[0] >> (cnt - 32);
  45. reg->mant.m32[0] = 0;
  46. break;
  47. default:
  48. reg->lowmant = reg->mant.m32[0] || reg->mant.m32[1];
  49. reg->mant.m32[0] = 0;
  50. reg->mant.m32[1] = 0;
  51. break;
  52. }
  53. }
  54. static inline int fp_overnormalize(struct fp_ext *reg)
  55. {
  56. int shift;
  57. if (reg->mant.m32[0]) {
  58. asm ("bfffo %1{#0,#32},%0" : "=d" (shift) : "dm" (reg->mant.m32[0]));
  59. reg->mant.m32[0] = (reg->mant.m32[0] << shift) | (reg->mant.m32[1] >> (32 - shift));
  60. reg->mant.m32[1] = (reg->mant.m32[1] << shift);
  61. } else {
  62. asm ("bfffo %1{#0,#32},%0" : "=d" (shift) : "dm" (reg->mant.m32[1]));
  63. reg->mant.m32[0] = (reg->mant.m32[1] << shift);
  64. reg->mant.m32[1] = 0;
  65. shift += 32;
  66. }
  67. return shift;
  68. }
  69. static inline int fp_addmant(struct fp_ext *dest, struct fp_ext *src)
  70. {
  71. int carry;
  72. /* we assume here, gcc only insert move and a clr instr */
  73. asm volatile ("add.b %1,%0" : "=d,g" (dest->lowmant)
  74. : "g,d" (src->lowmant), "0,0" (dest->lowmant));
  75. asm volatile ("addx.l %1,%0" : "=d" (dest->mant.m32[1])
  76. : "d" (src->mant.m32[1]), "0" (dest->mant.m32[1]));
  77. asm volatile ("addx.l %1,%0" : "=d" (dest->mant.m32[0])
  78. : "d" (src->mant.m32[0]), "0" (dest->mant.m32[0]));
  79. asm volatile ("addx.l %0,%0" : "=d" (carry) : "0" (0));
  80. return carry;
  81. }
  82. static inline int fp_addcarry(struct fp_ext *reg)
  83. {
  84. if (++reg->exp == 0x7fff) {
  85. if (reg->mant.m64)
  86. fp_set_sr(FPSR_EXC_INEX2);
  87. reg->mant.m64 = 0;
  88. fp_set_sr(FPSR_EXC_OVFL);
  89. return 0;
  90. }
  91. reg->lowmant = (reg->mant.m32[1] << 7) | (reg->lowmant ? 1 : 0);
  92. reg->mant.m32[1] = (reg->mant.m32[1] >> 1) |
  93. (reg->mant.m32[0] << 31);
  94. reg->mant.m32[0] = (reg->mant.m32[0] >> 1) | 0x80000000;
  95. return 1;
  96. }
  97. static inline void fp_submant(struct fp_ext *dest, struct fp_ext *src1,
  98. struct fp_ext *src2)
  99. {
  100. /* we assume here, gcc only insert move and a clr instr */
  101. asm volatile ("sub.b %1,%0" : "=d,g" (dest->lowmant)
  102. : "g,d" (src2->lowmant), "0,0" (src1->lowmant));
  103. asm volatile ("subx.l %1,%0" : "=d" (dest->mant.m32[1])
  104. : "d" (src2->mant.m32[1]), "0" (src1->mant.m32[1]));
  105. asm volatile ("subx.l %1,%0" : "=d" (dest->mant.m32[0])
  106. : "d" (src2->mant.m32[0]), "0" (src1->mant.m32[0]));
  107. }
  108. #define fp_mul64(desth, destl, src1, src2) ({ \
  109. asm ("mulu.l %2,%1:%0" : "=d" (destl), "=d" (desth) \
  110. : "dm" (src1), "0" (src2)); \
  111. })
  112. #define fp_div64(quot, rem, srch, srcl, div) \
  113. asm ("divu.l %2,%1:%0" : "=d" (quot), "=d" (rem) \
  114. : "dm" (div), "1" (srch), "0" (srcl))
  115. #define fp_add64(dest1, dest2, src1, src2) ({ \
  116. asm ("add.l %1,%0" : "=d,dm" (dest2) \
  117. : "dm,d" (src2), "0,0" (dest2)); \
  118. asm ("addx.l %1,%0" : "=d" (dest1) \
  119. : "d" (src1), "0" (dest1)); \
  120. })
  121. #define fp_addx96(dest, src) ({ \
  122. /* we assume here, gcc only insert move and a clr instr */ \
  123. asm volatile ("add.l %1,%0" : "=d,g" (dest->m32[2]) \
  124. : "g,d" (temp.m32[1]), "0,0" (dest->m32[2])); \
  125. asm volatile ("addx.l %1,%0" : "=d" (dest->m32[1]) \
  126. : "d" (temp.m32[0]), "0" (dest->m32[1])); \
  127. asm volatile ("addx.l %1,%0" : "=d" (dest->m32[0]) \
  128. : "d" (0), "0" (dest->m32[0])); \
  129. })
  130. #define fp_sub64(dest, src) ({ \
  131. asm ("sub.l %1,%0" : "=d,dm" (dest.m32[1]) \
  132. : "dm,d" (src.m32[1]), "0,0" (dest.m32[1])); \
  133. asm ("subx.l %1,%0" : "=d" (dest.m32[0]) \
  134. : "d" (src.m32[0]), "0" (dest.m32[0])); \
  135. })
  136. #define fp_sub96c(dest, srch, srcm, srcl) ({ \
  137. char carry; \
  138. asm ("sub.l %1,%0" : "=d,dm" (dest.m32[2]) \
  139. : "dm,d" (srcl), "0,0" (dest.m32[2])); \
  140. asm ("subx.l %1,%0" : "=d" (dest.m32[1]) \
  141. : "d" (srcm), "0" (dest.m32[1])); \
  142. asm ("subx.l %2,%1; scs %0" : "=d" (carry), "=d" (dest.m32[0]) \
  143. : "d" (srch), "1" (dest.m32[0])); \
  144. carry; \
  145. })
  146. static inline void fp_multiplymant(union fp_mant128 *dest, struct fp_ext *src1,
  147. struct fp_ext *src2)
  148. {
  149. union fp_mant64 temp;
  150. fp_mul64(dest->m32[0], dest->m32[1], src1->mant.m32[0], src2->mant.m32[0]);
  151. fp_mul64(dest->m32[2], dest->m32[3], src1->mant.m32[1], src2->mant.m32[1]);
  152. fp_mul64(temp.m32[0], temp.m32[1], src1->mant.m32[0], src2->mant.m32[1]);
  153. fp_addx96(dest, temp);
  154. fp_mul64(temp.m32[0], temp.m32[1], src1->mant.m32[1], src2->mant.m32[0]);
  155. fp_addx96(dest, temp);
  156. }
  157. static inline void fp_dividemant(union fp_mant128 *dest, struct fp_ext *src,
  158. struct fp_ext *div)
  159. {
  160. union fp_mant128 tmp;
  161. union fp_mant64 tmp64;
  162. unsigned long *mantp = dest->m32;
  163. unsigned long fix, rem, first, dummy;
  164. int i;
  165. /* the algorithm below requires dest to be smaller than div,
  166. but both have the high bit set */
  167. if (src->mant.m64 >= div->mant.m64) {
  168. fp_sub64(src->mant, div->mant);
  169. *mantp = 1;
  170. } else
  171. *mantp = 0;
  172. mantp++;
  173. /* basic idea behind this algorithm: we can't divide two 64bit numbers
  174. (AB/CD) directly, but we can calculate AB/C0, but this means this
  175. quotient is off by C0/CD, so we have to multiply the first result
  176. to fix the result, after that we have nearly the correct result
  177. and only a few corrections are needed. */
  178. /* C0/CD can be precalculated, but it's an 64bit division again, but
  179. we can make it a bit easier, by dividing first through C so we get
  180. 10/1D and now only a single shift and the value fits into 32bit. */
  181. fix = 0x80000000;
  182. dummy = div->mant.m32[1] / div->mant.m32[0] + 1;
  183. dummy = (dummy >> 1) | fix;
  184. fp_div64(fix, dummy, fix, 0, dummy);
  185. fix--;
  186. for (i = 0; i < 3; i++, mantp++) {
  187. if (src->mant.m32[0] == div->mant.m32[0]) {
  188. fp_div64(first, rem, 0, src->mant.m32[1], div->mant.m32[0]);
  189. fp_mul64(*mantp, dummy, first, fix);
  190. *mantp += fix;
  191. } else {
  192. fp_div64(first, rem, src->mant.m32[0], src->mant.m32[1], div->mant.m32[0]);
  193. fp_mul64(*mantp, dummy, first, fix);
  194. }
  195. fp_mul64(tmp.m32[0], tmp.m32[1], div->mant.m32[0], first - *mantp);
  196. fp_add64(tmp.m32[0], tmp.m32[1], 0, rem);
  197. tmp.m32[2] = 0;
  198. fp_mul64(tmp64.m32[0], tmp64.m32[1], *mantp, div->mant.m32[1]);
  199. fp_sub96c(tmp, 0, tmp64.m32[0], tmp64.m32[1]);
  200. src->mant.m32[0] = tmp.m32[1];
  201. src->mant.m32[1] = tmp.m32[2];
  202. while (!fp_sub96c(tmp, 0, div->mant.m32[0], div->mant.m32[1])) {
  203. src->mant.m32[0] = tmp.m32[1];
  204. src->mant.m32[1] = tmp.m32[2];
  205. *mantp += 1;
  206. }
  207. }
  208. }
  209. static inline void fp_putmant128(struct fp_ext *dest, union fp_mant128 *src,
  210. int shift)
  211. {
  212. unsigned long tmp;
  213. switch (shift) {
  214. case 0:
  215. dest->mant.m64 = src->m64[0];
  216. dest->lowmant = src->m32[2] >> 24;
  217. if (src->m32[3] || (src->m32[2] << 8))
  218. dest->lowmant |= 1;
  219. break;
  220. case 1:
  221. asm volatile ("lsl.l #1,%0"
  222. : "=d" (tmp) : "0" (src->m32[2]));
  223. asm volatile ("roxl.l #1,%0"
  224. : "=d" (dest->mant.m32[1]) : "0" (src->m32[1]));
  225. asm volatile ("roxl.l #1,%0"
  226. : "=d" (dest->mant.m32[0]) : "0" (src->m32[0]));
  227. dest->lowmant = tmp >> 24;
  228. if (src->m32[3] || (tmp << 8))
  229. dest->lowmant |= 1;
  230. break;
  231. case 31:
  232. asm volatile ("lsr.l #1,%1; roxr.l #1,%0"
  233. : "=d" (dest->mant.m32[0])
  234. : "d" (src->m32[0]), "0" (src->m32[1]));
  235. asm volatile ("roxr.l #1,%0"
  236. : "=d" (dest->mant.m32[1]) : "0" (src->m32[2]));
  237. asm volatile ("roxr.l #1,%0"
  238. : "=d" (tmp) : "0" (src->m32[3]));
  239. dest->lowmant = tmp >> 24;
  240. if (src->m32[3] << 7)
  241. dest->lowmant |= 1;
  242. break;
  243. case 32:
  244. dest->mant.m32[0] = src->m32[1];
  245. dest->mant.m32[1] = src->m32[2];
  246. dest->lowmant = src->m32[3] >> 24;
  247. if (src->m32[3] << 8)
  248. dest->lowmant |= 1;
  249. break;
  250. }
  251. }
  252. #endif /* MULTI_ARITH_H */