cnv_float.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Linux/PA-RISC Project (http://www.parisc-linux.org/)
  4. *
  5. * Floating-point emulation code
  6. * Copyright (C) 2001 Hewlett-Packard (Paul Bame) <[email protected]>
  7. */
  8. #ifdef __NO_PA_HDRS
  9. PA header file -- do not include this header file for non-PA builds.
  10. #endif
  11. /*
  12. * Some more constants
  13. */
  14. #define SGL_FX_MAX_EXP 30
  15. #define DBL_FX_MAX_EXP 62
  16. #define QUAD_FX_MAX_EXP 126
  17. #define Dintp1(object) (object)
  18. #define Dintp2(object) (object)
  19. #define Duintp1(object) (object)
  20. #define Duintp2(object) (object)
  21. #define Qintp0(object) (object)
  22. #define Qintp1(object) (object)
  23. #define Qintp2(object) (object)
  24. #define Qintp3(object) (object)
  25. /*
  26. * These macros will be used specifically by the convert instructions.
  27. *
  28. *
  29. * Single format macros
  30. */
  31. #define Sgl_to_dbl_exponent(src_exponent,dest) \
  32. Deposit_dexponent(dest,src_exponent+(DBL_BIAS-SGL_BIAS))
  33. #define Sgl_to_dbl_mantissa(src_mantissa,destA,destB) \
  34. Deposit_dmantissap1(destA,src_mantissa>>3); \
  35. Dmantissap2(destB) = src_mantissa << 29
  36. #define Sgl_isinexact_to_fix(sgl_value,exponent) \
  37. ((exponent < (SGL_P - 1)) ? \
  38. (Sall(sgl_value) << (SGL_EXP_LENGTH + 1 + exponent)) : FALSE)
  39. #define Int_isinexact_to_sgl(int_value) ((int_value << 33 - SGL_EXP_LENGTH) != 0)
  40. #define Sgl_roundnearest_from_int(int_value,sgl_value) \
  41. if (int_value & 1<<(SGL_EXP_LENGTH - 2)) /* round bit */ \
  42. if (((int_value << 34 - SGL_EXP_LENGTH) != 0) || Slow(sgl_value)) \
  43. Sall(sgl_value)++
  44. #define Dint_isinexact_to_sgl(dint_valueA,dint_valueB) \
  45. (((Dintp1(dint_valueA) << 33 - SGL_EXP_LENGTH) != 0) || Dintp2(dint_valueB))
  46. #define Sgl_roundnearest_from_dint(dint_valueA,dint_valueB,sgl_value) \
  47. if (Dintp1(dint_valueA) & 1<<(SGL_EXP_LENGTH - 2)) \
  48. if (((Dintp1(dint_valueA) << 34 - SGL_EXP_LENGTH) != 0) || \
  49. Dintp2(dint_valueB) || Slow(sgl_value)) Sall(sgl_value)++
  50. #define Dint_isinexact_to_dbl(dint_value) \
  51. (Dintp2(dint_value) << 33 - DBL_EXP_LENGTH)
  52. #define Dbl_roundnearest_from_dint(dint_opndB,dbl_opndA,dbl_opndB) \
  53. if (Dintp2(dint_opndB) & 1<<(DBL_EXP_LENGTH - 2)) \
  54. if ((Dintp2(dint_opndB) << 34 - DBL_EXP_LENGTH) || Dlowp2(dbl_opndB)) \
  55. if ((++Dallp2(dbl_opndB))==0) Dallp1(dbl_opndA)++
  56. #define Sgl_isone_roundbit(sgl_value,exponent) \
  57. ((Sall(sgl_value) << (SGL_EXP_LENGTH + 1 + exponent)) >> 31)
  58. #define Sgl_isone_stickybit(sgl_value,exponent) \
  59. (exponent < (SGL_P - 2) ? \
  60. Sall(sgl_value) << (SGL_EXP_LENGTH + 2 + exponent) : FALSE)
  61. /*
  62. * Double format macros
  63. */
  64. #define Dbl_to_sgl_exponent(src_exponent,dest) \
  65. dest = src_exponent + (SGL_BIAS - DBL_BIAS)
  66. #define Dbl_to_sgl_mantissa(srcA,srcB,dest,inexact,guard,sticky,odd) \
  67. Shiftdouble(Dmantissap1(srcA),Dmantissap2(srcB),29,dest); \
  68. guard = Dbit3p2(srcB); \
  69. sticky = Dallp2(srcB)<<4; \
  70. inexact = guard | sticky; \
  71. odd = Dbit2p2(srcB)
  72. #define Dbl_to_sgl_denormalized(srcA,srcB,exp,dest,inexact,guard,sticky,odd,tiny) \
  73. Deposit_dexponent(srcA,1); \
  74. tiny = TRUE; \
  75. if (exp >= -2) { \
  76. if (exp == 0) { \
  77. inexact = Dallp2(srcB) << 3; \
  78. guard = inexact >> 31; \
  79. sticky = inexact << 1; \
  80. Shiftdouble(Dmantissap1(srcA),Dmantissap2(srcB),29,dest); \
  81. odd = dest << 31; \
  82. if (inexact) { \
  83. switch(Rounding_mode()) { \
  84. case ROUNDPLUS: \
  85. if (Dbl_iszero_sign(srcA)) { \
  86. dest++; \
  87. if (Sgl_isone_hidden(dest)) \
  88. tiny = FALSE; \
  89. dest--; \
  90. } \
  91. break; \
  92. case ROUNDMINUS: \
  93. if (Dbl_isone_sign(srcA)) { \
  94. dest++; \
  95. if (Sgl_isone_hidden(dest)) \
  96. tiny = FALSE; \
  97. dest--; \
  98. } \
  99. break; \
  100. case ROUNDNEAREST: \
  101. if (guard && (sticky || odd)) { \
  102. dest++; \
  103. if (Sgl_isone_hidden(dest)) \
  104. tiny = FALSE; \
  105. dest--; \
  106. } \
  107. break; \
  108. } \
  109. } \
  110. /* shift right by one to get correct result */ \
  111. guard = odd; \
  112. sticky = inexact; \
  113. inexact |= guard; \
  114. dest >>= 1; \
  115. Deposit_dsign(srcA,0); \
  116. Shiftdouble(Dallp1(srcA),Dallp2(srcB),30,dest); \
  117. odd = dest << 31; \
  118. } \
  119. else { \
  120. inexact = Dallp2(srcB) << (2 + exp); \
  121. guard = inexact >> 31; \
  122. sticky = inexact << 1; \
  123. Deposit_dsign(srcA,0); \
  124. if (exp == -2) dest = Dallp1(srcA); \
  125. else Variable_shift_double(Dallp1(srcA),Dallp2(srcB),30-exp,dest); \
  126. odd = dest << 31; \
  127. } \
  128. } \
  129. else { \
  130. Deposit_dsign(srcA,0); \
  131. if (exp > (1 - SGL_P)) { \
  132. dest = Dallp1(srcA) >> (- 2 - exp); \
  133. inexact = Dallp1(srcA) << (34 + exp); \
  134. guard = inexact >> 31; \
  135. sticky = (inexact << 1) | Dallp2(srcB); \
  136. inexact |= Dallp2(srcB); \
  137. odd = dest << 31; \
  138. } \
  139. else { \
  140. dest = 0; \
  141. inexact = Dallp1(srcA) | Dallp2(srcB); \
  142. if (exp == (1 - SGL_P)) { \
  143. guard = Dhidden(srcA); \
  144. sticky = Dmantissap1(srcA) | Dallp2(srcB); \
  145. } \
  146. else { \
  147. guard = 0; \
  148. sticky = inexact; \
  149. } \
  150. odd = 0; \
  151. } \
  152. } \
  153. exp = 0
  154. #define Dbl_isinexact_to_fix(dbl_valueA,dbl_valueB,exponent) \
  155. (exponent < (DBL_P-33) ? \
  156. Dallp2(dbl_valueB) || Dallp1(dbl_valueA) << (DBL_EXP_LENGTH+1+exponent) : \
  157. (exponent < (DBL_P-1) ? Dallp2(dbl_valueB) << (exponent + (33-DBL_P)) : \
  158. FALSE))
  159. #define Dbl_isoverflow_to_int(exponent,dbl_valueA,dbl_valueB) \
  160. ((exponent > SGL_FX_MAX_EXP + 1) || Dsign(dbl_valueA)==0 || \
  161. Dmantissap1(dbl_valueA)!=0 || (Dallp2(dbl_valueB)>>21)!=0 )
  162. #define Dbl_isone_roundbit(dbl_valueA,dbl_valueB,exponent) \
  163. ((exponent < (DBL_P - 33) ? \
  164. Dallp1(dbl_valueA) >> ((30 - DBL_EXP_LENGTH) - exponent) : \
  165. Dallp2(dbl_valueB) >> ((DBL_P - 2) - exponent)) & 1)
  166. #define Dbl_isone_stickybit(dbl_valueA,dbl_valueB,exponent) \
  167. (exponent < (DBL_P-34) ? \
  168. (Dallp2(dbl_valueB) || Dallp1(dbl_valueA)<<(DBL_EXP_LENGTH+2+exponent)) : \
  169. (exponent<(DBL_P-2) ? (Dallp2(dbl_valueB) << (exponent + (34-DBL_P))) : \
  170. FALSE))
  171. /* Int macros */
  172. #define Int_from_sgl_mantissa(sgl_value,exponent) \
  173. Sall(sgl_value) = \
  174. (unsigned)(Sall(sgl_value) << SGL_EXP_LENGTH)>>(31 - exponent)
  175. #define Int_from_dbl_mantissa(dbl_valueA,dbl_valueB,exponent) \
  176. Shiftdouble(Dallp1(dbl_valueA),Dallp2(dbl_valueB),22,Dallp1(dbl_valueA)); \
  177. if (exponent < 31) Dallp1(dbl_valueA) >>= 30 - exponent; \
  178. else Dallp1(dbl_valueA) <<= 1
  179. #define Int_negate(int_value) int_value = -int_value
  180. /* Dint macros */
  181. #define Dint_from_sgl_mantissa(sgl_value,exponent,dresultA,dresultB) \
  182. {Sall(sgl_value) <<= SGL_EXP_LENGTH; /* left-justify */ \
  183. if (exponent <= 31) { \
  184. Dintp1(dresultA) = 0; \
  185. Dintp2(dresultB) = (unsigned)Sall(sgl_value) >> (31 - exponent); \
  186. } \
  187. else { \
  188. Dintp1(dresultA) = Sall(sgl_value) >> (63 - exponent); \
  189. Dintp2(dresultB) = Sall(sgl_value) << (exponent - 31); \
  190. }}
  191. #define Dint_from_dbl_mantissa(dbl_valueA,dbl_valueB,exponent,destA,destB) \
  192. {if (exponent < 32) { \
  193. Dintp1(destA) = 0; \
  194. if (exponent <= 20) \
  195. Dintp2(destB) = Dallp1(dbl_valueA) >> 20-exponent; \
  196. else Variable_shift_double(Dallp1(dbl_valueA),Dallp2(dbl_valueB), \
  197. 52-exponent,Dintp2(destB)); \
  198. } \
  199. else { \
  200. if (exponent <= 52) { \
  201. Dintp1(destA) = Dallp1(dbl_valueA) >> 52-exponent; \
  202. if (exponent == 52) Dintp2(destB) = Dallp2(dbl_valueB); \
  203. else Variable_shift_double(Dallp1(dbl_valueA),Dallp2(dbl_valueB), \
  204. 52-exponent,Dintp2(destB)); \
  205. } \
  206. else { \
  207. Variable_shift_double(Dallp1(dbl_valueA),Dallp2(dbl_valueB), \
  208. 84-exponent,Dintp1(destA)); \
  209. Dintp2(destB) = Dallp2(dbl_valueB) << exponent-52; \
  210. } \
  211. }}
  212. #define Dint_setzero(dresultA,dresultB) \
  213. Dintp1(dresultA) = 0; \
  214. Dintp2(dresultB) = 0
  215. #define Dint_setone_sign(dresultA,dresultB) \
  216. Dintp1(dresultA) = ~Dintp1(dresultA); \
  217. if ((Dintp2(dresultB) = -Dintp2(dresultB)) == 0) Dintp1(dresultA)++
  218. #define Dint_set_minint(dresultA,dresultB) \
  219. Dintp1(dresultA) = (unsigned int)1<<31; \
  220. Dintp2(dresultB) = 0
  221. #define Dint_isone_lowp2(dresultB) (Dintp2(dresultB) & 01)
  222. #define Dint_increment(dresultA,dresultB) \
  223. if ((++Dintp2(dresultB))==0) Dintp1(dresultA)++
  224. #define Dint_decrement(dresultA,dresultB) \
  225. if ((Dintp2(dresultB)--)==0) Dintp1(dresultA)--
  226. #define Dint_negate(dresultA,dresultB) \
  227. Dintp1(dresultA) = ~Dintp1(dresultA); \
  228. if ((Dintp2(dresultB) = -Dintp2(dresultB))==0) Dintp1(dresultA)++
  229. #define Dint_copyfromptr(src,destA,destB) \
  230. Dintp1(destA) = src->wd0; \
  231. Dintp2(destB) = src->wd1
  232. #define Dint_copytoptr(srcA,srcB,dest) \
  233. dest->wd0 = Dintp1(srcA); \
  234. dest->wd1 = Dintp2(srcB)
  235. /* other macros */
  236. #define Find_ms_one_bit(value, position) \
  237. { \
  238. int var; \
  239. for (var=8; var >=1; var >>= 1) { \
  240. if (value >> 32 - position) \
  241. position -= var; \
  242. else position += var; \
  243. } \
  244. if ((value >> 32 - position) == 0) \
  245. position--; \
  246. else position -= 2; \
  247. }
  248. /*
  249. * Unsigned int macros
  250. */
  251. #define Duint_copyfromptr(src,destA,destB) \
  252. Dint_copyfromptr(src,destA,destB)
  253. #define Duint_copytoptr(srcA,srcB,dest) \
  254. Dint_copytoptr(srcA,srcB,dest)
  255. #define Suint_isinexact_to_sgl(int_value) \
  256. (int_value << 32 - SGL_EXP_LENGTH)
  257. #define Sgl_roundnearest_from_suint(suint_value,sgl_value) \
  258. if (suint_value & 1<<(SGL_EXP_LENGTH - 1)) /* round bit */ \
  259. if ((suint_value << 33 - SGL_EXP_LENGTH) || Slow(sgl_value)) \
  260. Sall(sgl_value)++
  261. #define Duint_isinexact_to_sgl(duint_valueA,duint_valueB) \
  262. ((Duintp1(duint_valueA) << 32 - SGL_EXP_LENGTH) || Duintp2(duint_valueB))
  263. #define Sgl_roundnearest_from_duint(duint_valueA,duint_valueB,sgl_value) \
  264. if (Duintp1(duint_valueA) & 1<<(SGL_EXP_LENGTH - 1)) \
  265. if ((Duintp1(duint_valueA) << 33 - SGL_EXP_LENGTH) || \
  266. Duintp2(duint_valueB) || Slow(sgl_value)) Sall(sgl_value)++
  267. #define Duint_isinexact_to_dbl(duint_value) \
  268. (Duintp2(duint_value) << 32 - DBL_EXP_LENGTH)
  269. #define Dbl_roundnearest_from_duint(duint_opndB,dbl_opndA,dbl_opndB) \
  270. if (Duintp2(duint_opndB) & 1<<(DBL_EXP_LENGTH - 1)) \
  271. if ((Duintp2(duint_opndB) << 33 - DBL_EXP_LENGTH) || Dlowp2(dbl_opndB)) \
  272. if ((++Dallp2(dbl_opndB))==0) Dallp1(dbl_opndA)++
  273. #define Suint_from_sgl_mantissa(src,exponent,result) \
  274. Sall(result) = (unsigned)(Sall(src) << SGL_EXP_LENGTH)>>(31 - exponent)
  275. #define Sgl_isinexact_to_unsigned(sgl_value,exponent) \
  276. Sgl_isinexact_to_fix(sgl_value,exponent)
  277. #define Duint_from_sgl_mantissa(sgl_value,exponent,dresultA,dresultB) \
  278. {unsigned int val = Sall(sgl_value) << SGL_EXP_LENGTH; \
  279. if (exponent <= 31) { \
  280. Dintp1(dresultA) = 0; \
  281. Dintp2(dresultB) = val >> (31 - exponent); \
  282. } \
  283. else { \
  284. Dintp1(dresultA) = val >> (63 - exponent); \
  285. Dintp2(dresultB) = exponent <= 62 ? val << (exponent - 31) : 0; \
  286. } \
  287. }
  288. #define Duint_setzero(dresultA,dresultB) \
  289. Dint_setzero(dresultA,dresultB)
  290. #define Duint_increment(dresultA,dresultB) Dint_increment(dresultA,dresultB)
  291. #define Duint_isone_lowp2(dresultB) Dint_isone_lowp2(dresultB)
  292. #define Suint_from_dbl_mantissa(srcA,srcB,exponent,dest) \
  293. Shiftdouble(Dallp1(srcA),Dallp2(srcB),21,dest); \
  294. dest = (unsigned)dest >> 31 - exponent
  295. #define Dbl_isinexact_to_unsigned(dbl_valueA,dbl_valueB,exponent) \
  296. Dbl_isinexact_to_fix(dbl_valueA,dbl_valueB,exponent)
  297. #define Duint_from_dbl_mantissa(dbl_valueA,dbl_valueB,exponent,destA,destB) \
  298. Dint_from_dbl_mantissa(dbl_valueA,dbl_valueB,exponent,destA,destB)