cnv_float.h 13 KB

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