sfp-machine.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /* Machine-dependent software floating-point definitions. PPC version.
  2. Copyright (C) 1997 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with the GNU C Library; see the file COPYING.LIB. If
  14. not, write to the Free Software Foundation, Inc.,
  15. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. Actually, this is a PPC (32bit) version, written based on the
  17. i386, sparc, and sparc64 versions, by me,
  18. Peter Maydell (pmaydell@chiark.greenend.org.uk).
  19. Comments are by and large also mine, although they may be inaccurate.
  20. In picking out asm fragments I've gone with the lowest common
  21. denominator, which also happens to be the hardware I have :->
  22. That is, a SPARC without hardware multiply and divide.
  23. */
  24. /* basic word size definitions */
  25. #define _FP_W_TYPE_SIZE 32
  26. #define _FP_W_TYPE unsigned int
  27. #define _FP_WS_TYPE signed int
  28. #define _FP_I_TYPE int
  29. #define __ll_B ((UWtype) 1 << (W_TYPE_SIZE / 2))
  30. #define __ll_lowpart(t) ((UWtype) (t) & (__ll_B - 1))
  31. #define __ll_highpart(t) ((UWtype) (t) >> (W_TYPE_SIZE / 2))
  32. /* You can optionally code some things like addition in asm. For
  33. * example, i386 defines __FP_FRAC_ADD_2 as asm. If you don't
  34. * then you get a fragment of C code [if you change an #ifdef 0
  35. * in op-2.h] or a call to add_ssaaaa (see below).
  36. * Good places to look for asm fragments to use are gcc and glibc.
  37. * gcc's longlong.h is useful.
  38. */
  39. /* We need to know how to multiply and divide. If the host word size
  40. * is >= 2*fracbits you can use FP_MUL_MEAT_n_imm(t,R,X,Y) which
  41. * codes the multiply with whatever gcc does to 'a * b'.
  42. * _FP_MUL_MEAT_n_wide(t,R,X,Y,f) is used when you have an asm
  43. * function that can multiply two 1W values and get a 2W result.
  44. * Otherwise you're stuck with _FP_MUL_MEAT_n_hard(t,R,X,Y) which
  45. * does bitshifting to avoid overflow.
  46. * For division there is FP_DIV_MEAT_n_imm(t,R,X,Y,f) for word size
  47. * >= 2*fracbits, where f is either _FP_DIV_HELP_imm or
  48. * _FP_DIV_HELP_ldiv (see op-1.h).
  49. * _FP_DIV_MEAT_udiv() is if you have asm to do 2W/1W => (1W, 1W).
  50. * [GCC and glibc have longlong.h which has the asm macro udiv_qrnnd
  51. * to do this.]
  52. * In general, 'n' is the number of words required to hold the type,
  53. * and 't' is either S, D or Q for single/double/quad.
  54. * -- PMM
  55. */
  56. /* Example: SPARC64:
  57. * #define _FP_MUL_MEAT_S(R,X,Y) _FP_MUL_MEAT_1_imm(S,R,X,Y)
  58. * #define _FP_MUL_MEAT_D(R,X,Y) _FP_MUL_MEAT_1_wide(D,R,X,Y,umul_ppmm)
  59. * #define _FP_MUL_MEAT_Q(R,X,Y) _FP_MUL_MEAT_2_wide(Q,R,X,Y,umul_ppmm)
  60. *
  61. * #define _FP_DIV_MEAT_S(R,X,Y) _FP_DIV_MEAT_1_imm(S,R,X,Y,_FP_DIV_HELP_imm)
  62. * #define _FP_DIV_MEAT_D(R,X,Y) _FP_DIV_MEAT_1_udiv(D,R,X,Y)
  63. * #define _FP_DIV_MEAT_Q(R,X,Y) _FP_DIV_MEAT_2_udiv_64(Q,R,X,Y)
  64. *
  65. * Example: i386:
  66. * #define _FP_MUL_MEAT_S(R,X,Y) _FP_MUL_MEAT_1_wide(S,R,X,Y,_i386_mul_32_64)
  67. * #define _FP_MUL_MEAT_D(R,X,Y) _FP_MUL_MEAT_2_wide(D,R,X,Y,_i386_mul_32_64)
  68. *
  69. * #define _FP_DIV_MEAT_S(R,X,Y) _FP_DIV_MEAT_1_udiv(S,R,X,Y,_i386_div_64_32)
  70. * #define _FP_DIV_MEAT_D(R,X,Y) _FP_DIV_MEAT_2_udiv_64(D,R,X,Y)
  71. */
  72. #define _FP_MUL_MEAT_S(R,X,Y) _FP_MUL_MEAT_1_wide(_FP_WFRACBITS_S,R,X,Y,umul_ppmm)
  73. #define _FP_MUL_MEAT_D(R,X,Y) _FP_MUL_MEAT_2_wide(_FP_WFRACBITS_D,R,X,Y,umul_ppmm)
  74. #define _FP_DIV_MEAT_S(R,X,Y) _FP_DIV_MEAT_1_udiv_norm(S,R,X,Y)
  75. #define _FP_DIV_MEAT_D(R,X,Y) _FP_DIV_MEAT_2_udiv(D,R,X,Y)
  76. /* These macros define what NaN looks like. They're supposed to expand to
  77. * a comma-separated set of 32bit unsigned ints that encode NaN.
  78. */
  79. #define _FP_NANFRAC_S ((_FP_QNANBIT_S << 1) - 1)
  80. #define _FP_NANFRAC_D ((_FP_QNANBIT_D << 1) - 1), -1
  81. #define _FP_NANFRAC_Q ((_FP_QNANBIT_Q << 1) - 1), -1, -1, -1
  82. #define _FP_NANSIGN_S 0
  83. #define _FP_NANSIGN_D 0
  84. #define _FP_NANSIGN_Q 0
  85. #define _FP_KEEPNANFRACP 1
  86. #ifdef FP_EX_BOOKE_E500_SPE
  87. #define FP_EX_INEXACT (1 << 21)
  88. #define FP_EX_INVALID (1 << 20)
  89. #define FP_EX_DIVZERO (1 << 19)
  90. #define FP_EX_UNDERFLOW (1 << 18)
  91. #define FP_EX_OVERFLOW (1 << 17)
  92. #define FP_INHIBIT_RESULTS 0
  93. #define __FPU_FPSCR (current->thread.spefscr)
  94. #define __FPU_ENABLED_EXC \
  95. ({ \
  96. (__FPU_FPSCR >> 2) & 0x1f; \
  97. })
  98. #else
  99. /* Exception flags. We use the bit positions of the appropriate bits
  100. in the FPSCR, which also correspond to the FE_* bits. This makes
  101. everything easier ;-). */
  102. #define FP_EX_INVALID (1 << (31 - 2))
  103. #define FP_EX_INVALID_SNAN EFLAG_VXSNAN
  104. #define FP_EX_INVALID_ISI EFLAG_VXISI
  105. #define FP_EX_INVALID_IDI EFLAG_VXIDI
  106. #define FP_EX_INVALID_ZDZ EFLAG_VXZDZ
  107. #define FP_EX_INVALID_IMZ EFLAG_VXIMZ
  108. #define FP_EX_OVERFLOW (1 << (31 - 3))
  109. #define FP_EX_UNDERFLOW (1 << (31 - 4))
  110. #define FP_EX_DIVZERO (1 << (31 - 5))
  111. #define FP_EX_INEXACT (1 << (31 - 6))
  112. #define __FPU_FPSCR (current->thread.fp_state.fpscr)
  113. /* We only actually write to the destination register
  114. * if exceptions signalled (if any) will not trap.
  115. */
  116. #define __FPU_ENABLED_EXC \
  117. ({ \
  118. (__FPU_FPSCR >> 3) & 0x1f; \
  119. })
  120. #endif
  121. /*
  122. * If one NaN is signaling and the other is not,
  123. * we choose that one, otherwise we choose X.
  124. */
  125. #define _FP_CHOOSENAN(fs, wc, R, X, Y, OP) \
  126. do { \
  127. if ((_FP_FRAC_HIGH_RAW_##fs(Y) & _FP_QNANBIT_##fs) \
  128. && !(_FP_FRAC_HIGH_RAW_##fs(X) & _FP_QNANBIT_##fs)) \
  129. { \
  130. R##_s = X##_s; \
  131. _FP_FRAC_COPY_##wc(R,X); \
  132. } \
  133. else \
  134. { \
  135. R##_s = Y##_s; \
  136. _FP_FRAC_COPY_##wc(R,Y); \
  137. } \
  138. R##_c = FP_CLS_NAN; \
  139. } while (0)
  140. #include <linux/kernel.h>
  141. #include <linux/sched.h>
  142. #define __FPU_TRAP_P(bits) \
  143. ((__FPU_ENABLED_EXC & (bits)) != 0)
  144. #define __FP_PACK_S(val,X) \
  145. ({ int __exc = _FP_PACK_CANONICAL(S,1,X); \
  146. if(!__exc || !__FPU_TRAP_P(__exc)) \
  147. _FP_PACK_RAW_1_P(S,val,X); \
  148. __exc; \
  149. })
  150. #define __FP_PACK_D(val,X) \
  151. do { \
  152. _FP_PACK_CANONICAL(D, 2, X); \
  153. if (!FP_CUR_EXCEPTIONS || !__FPU_TRAP_P(FP_CUR_EXCEPTIONS)) \
  154. _FP_PACK_RAW_2_P(D, val, X); \
  155. } while (0)
  156. #define __FP_PACK_DS(val,X) \
  157. do { \
  158. FP_DECL_S(__X); \
  159. FP_CONV(S, D, 1, 2, __X, X); \
  160. _FP_PACK_CANONICAL(S, 1, __X); \
  161. if (!FP_CUR_EXCEPTIONS || !__FPU_TRAP_P(FP_CUR_EXCEPTIONS)) { \
  162. _FP_UNPACK_CANONICAL(S, 1, __X); \
  163. FP_CONV(D, S, 2, 1, X, __X); \
  164. _FP_PACK_CANONICAL(D, 2, X); \
  165. if (!FP_CUR_EXCEPTIONS || !__FPU_TRAP_P(FP_CUR_EXCEPTIONS)) \
  166. _FP_PACK_RAW_2_P(D, val, X); \
  167. } \
  168. } while (0)
  169. /* Obtain the current rounding mode. */
  170. #define FP_ROUNDMODE \
  171. ({ \
  172. __FPU_FPSCR & 0x3; \
  173. })
  174. /* the asm fragments go here: all these are taken from glibc-2.0.5's
  175. * stdlib/longlong.h
  176. */
  177. #include <linux/types.h>
  178. #include <asm/byteorder.h>
  179. /* add_ssaaaa is used in op-2.h and should be equivalent to
  180. * #define add_ssaaaa(sh,sl,ah,al,bh,bl) (sh = ah+bh+ (( sl = al+bl) < al))
  181. * add_ssaaaa(high_sum, low_sum, high_addend_1, low_addend_1,
  182. * high_addend_2, low_addend_2) adds two UWtype integers, composed by
  183. * HIGH_ADDEND_1 and LOW_ADDEND_1, and HIGH_ADDEND_2 and LOW_ADDEND_2
  184. * respectively. The result is placed in HIGH_SUM and LOW_SUM. Overflow
  185. * (i.e. carry out) is not stored anywhere, and is lost.
  186. */
  187. #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
  188. do { \
  189. if (__builtin_constant_p (bh) && (bh) == 0) \
  190. __asm__ ("{a%I4|add%I4c} %1,%3,%4\n\t{aze|addze} %0,%2" \
  191. : "=r" ((USItype)(sh)), \
  192. "=&r" ((USItype)(sl)) \
  193. : "%r" ((USItype)(ah)), \
  194. "%r" ((USItype)(al)), \
  195. "rI" ((USItype)(bl))); \
  196. else if (__builtin_constant_p (bh) && (bh) ==~(USItype) 0) \
  197. __asm__ ("{a%I4|add%I4c} %1,%3,%4\n\t{ame|addme} %0,%2" \
  198. : "=r" ((USItype)(sh)), \
  199. "=&r" ((USItype)(sl)) \
  200. : "%r" ((USItype)(ah)), \
  201. "%r" ((USItype)(al)), \
  202. "rI" ((USItype)(bl))); \
  203. else \
  204. __asm__ ("{a%I5|add%I5c} %1,%4,%5\n\t{ae|adde} %0,%2,%3" \
  205. : "=r" ((USItype)(sh)), \
  206. "=&r" ((USItype)(sl)) \
  207. : "%r" ((USItype)(ah)), \
  208. "r" ((USItype)(bh)), \
  209. "%r" ((USItype)(al)), \
  210. "rI" ((USItype)(bl))); \
  211. } while (0)
  212. /* sub_ddmmss is used in op-2.h and udivmodti4.c and should be equivalent to
  213. * #define sub_ddmmss(sh, sl, ah, al, bh, bl) (sh = ah-bh - ((sl = al-bl) > al))
  214. * sub_ddmmss(high_difference, low_difference, high_minuend, low_minuend,
  215. * high_subtrahend, low_subtrahend) subtracts two two-word UWtype integers,
  216. * composed by HIGH_MINUEND_1 and LOW_MINUEND_1, and HIGH_SUBTRAHEND_2 and
  217. * LOW_SUBTRAHEND_2 respectively. The result is placed in HIGH_DIFFERENCE
  218. * and LOW_DIFFERENCE. Overflow (i.e. carry out) is not stored anywhere,
  219. * and is lost.
  220. */
  221. #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
  222. do { \
  223. if (__builtin_constant_p (ah) && (ah) == 0) \
  224. __asm__ ("{sf%I3|subf%I3c} %1,%4,%3\n\t{sfze|subfze} %0,%2" \
  225. : "=r" ((USItype)(sh)), \
  226. "=&r" ((USItype)(sl)) \
  227. : "r" ((USItype)(bh)), \
  228. "rI" ((USItype)(al)), \
  229. "r" ((USItype)(bl))); \
  230. else if (__builtin_constant_p (ah) && (ah) ==~(USItype) 0) \
  231. __asm__ ("{sf%I3|subf%I3c} %1,%4,%3\n\t{sfme|subfme} %0,%2" \
  232. : "=r" ((USItype)(sh)), \
  233. "=&r" ((USItype)(sl)) \
  234. : "r" ((USItype)(bh)), \
  235. "rI" ((USItype)(al)), \
  236. "r" ((USItype)(bl))); \
  237. else if (__builtin_constant_p (bh) && (bh) == 0) \
  238. __asm__ ("{sf%I3|subf%I3c} %1,%4,%3\n\t{ame|addme} %0,%2" \
  239. : "=r" ((USItype)(sh)), \
  240. "=&r" ((USItype)(sl)) \
  241. : "r" ((USItype)(ah)), \
  242. "rI" ((USItype)(al)), \
  243. "r" ((USItype)(bl))); \
  244. else if (__builtin_constant_p (bh) && (bh) ==~(USItype) 0) \
  245. __asm__ ("{sf%I3|subf%I3c} %1,%4,%3\n\t{aze|addze} %0,%2" \
  246. : "=r" ((USItype)(sh)), \
  247. "=&r" ((USItype)(sl)) \
  248. : "r" ((USItype)(ah)), \
  249. "rI" ((USItype)(al)), \
  250. "r" ((USItype)(bl))); \
  251. else \
  252. __asm__ ("{sf%I4|subf%I4c} %1,%5,%4\n\t{sfe|subfe} %0,%3,%2" \
  253. : "=r" ((USItype)(sh)), \
  254. "=&r" ((USItype)(sl)) \
  255. : "r" ((USItype)(ah)), \
  256. "r" ((USItype)(bh)), \
  257. "rI" ((USItype)(al)), \
  258. "r" ((USItype)(bl))); \
  259. } while (0)
  260. /* asm fragments for mul and div */
  261. /* umul_ppmm(high_prod, low_prod, multipler, multiplicand) multiplies two
  262. * UWtype integers MULTIPLER and MULTIPLICAND, and generates a two UWtype
  263. * word product in HIGH_PROD and LOW_PROD.
  264. */
  265. #define umul_ppmm(ph, pl, m0, m1) \
  266. do { \
  267. USItype __m0 = (m0), __m1 = (m1); \
  268. __asm__ ("mulhwu %0,%1,%2" \
  269. : "=r" ((USItype)(ph)) \
  270. : "%r" (__m0), \
  271. "r" (__m1)); \
  272. (pl) = __m0 * __m1; \
  273. } while (0)
  274. /* udiv_qrnnd(quotient, remainder, high_numerator, low_numerator,
  275. * denominator) divides a UDWtype, composed by the UWtype integers
  276. * HIGH_NUMERATOR and LOW_NUMERATOR, by DENOMINATOR and places the quotient
  277. * in QUOTIENT and the remainder in REMAINDER. HIGH_NUMERATOR must be less
  278. * than DENOMINATOR for correct operation. If, in addition, the most
  279. * significant bit of DENOMINATOR must be 1, then the pre-processor symbol
  280. * UDIV_NEEDS_NORMALIZATION is defined to 1.
  281. */
  282. #define udiv_qrnnd(q, r, n1, n0, d) \
  283. do { \
  284. UWtype __d1, __d0, __q1, __q0, __r1, __r0, __m; \
  285. __d1 = __ll_highpart (d); \
  286. __d0 = __ll_lowpart (d); \
  287. \
  288. __r1 = (n1) % __d1; \
  289. __q1 = (n1) / __d1; \
  290. __m = (UWtype) __q1 * __d0; \
  291. __r1 = __r1 * __ll_B | __ll_highpart (n0); \
  292. if (__r1 < __m) \
  293. { \
  294. __q1--, __r1 += (d); \
  295. if (__r1 >= (d)) /* we didn't get carry when adding to __r1 */ \
  296. if (__r1 < __m) \
  297. __q1--, __r1 += (d); \
  298. } \
  299. __r1 -= __m; \
  300. \
  301. __r0 = __r1 % __d1; \
  302. __q0 = __r1 / __d1; \
  303. __m = (UWtype) __q0 * __d0; \
  304. __r0 = __r0 * __ll_B | __ll_lowpart (n0); \
  305. if (__r0 < __m) \
  306. { \
  307. __q0--, __r0 += (d); \
  308. if (__r0 >= (d)) \
  309. if (__r0 < __m) \
  310. __q0--, __r0 += (d); \
  311. } \
  312. __r0 -= __m; \
  313. \
  314. (q) = (UWtype) __q1 * __ll_B | __q0; \
  315. (r) = __r0; \
  316. } while (0)
  317. #define UDIV_NEEDS_NORMALIZATION 1
  318. #define abort() \
  319. return 0
  320. #ifdef __BIG_ENDIAN
  321. #define __BYTE_ORDER __BIG_ENDIAN
  322. #else
  323. #define __BYTE_ORDER __LITTLE_ENDIAN
  324. #endif
  325. /* Exception flags. */
  326. #define EFLAG_INVALID (1 << (31 - 2))
  327. #define EFLAG_OVERFLOW (1 << (31 - 3))
  328. #define EFLAG_UNDERFLOW (1 << (31 - 4))
  329. #define EFLAG_DIVZERO (1 << (31 - 5))
  330. #define EFLAG_INEXACT (1 << (31 - 6))
  331. #define EFLAG_VXSNAN (1 << (31 - 7))
  332. #define EFLAG_VXISI (1 << (31 - 8))
  333. #define EFLAG_VXIDI (1 << (31 - 9))
  334. #define EFLAG_VXZDZ (1 << (31 - 10))
  335. #define EFLAG_VXIMZ (1 << (31 - 11))
  336. #define EFLAG_VXVC (1 << (31 - 12))
  337. #define EFLAG_VXSOFT (1 << (31 - 21))
  338. #define EFLAG_VXSQRT (1 << (31 - 22))
  339. #define EFLAG_VXCVI (1 << (31 - 23))