ieee754.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * MIPS floating point support
  3. * Copyright (C) 1994-2000 Algorithmics Ltd.
  4. *
  5. * This program is free software; you can distribute it and/or modify it
  6. * under the terms of the GNU General Public License (Version 2) as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17. *
  18. * Nov 7, 2000
  19. * Modification to allow integration with Linux kernel
  20. *
  21. * Kevin D. Kissell, kevink@mips.com and Carsten Langgard, carstenl@mips.com
  22. * Copyright (C) 2000 MIPS Technologies, Inc. All rights reserved.
  23. */
  24. #ifndef __ARCH_MIPS_MATH_EMU_IEEE754_H
  25. #define __ARCH_MIPS_MATH_EMU_IEEE754_H
  26. #include <linux/compiler.h>
  27. #include <asm/byteorder.h>
  28. #include <linux/kernel.h>
  29. #include <linux/types.h>
  30. #include <linux/sched.h>
  31. #include <asm/bitfield.h>
  32. union ieee754dp {
  33. struct {
  34. __BITFIELD_FIELD(unsigned int sign:1,
  35. __BITFIELD_FIELD(unsigned int bexp:11,
  36. __BITFIELD_FIELD(u64 mant:52,
  37. ;)))
  38. };
  39. u64 bits;
  40. };
  41. union ieee754sp {
  42. struct {
  43. __BITFIELD_FIELD(unsigned sign:1,
  44. __BITFIELD_FIELD(unsigned bexp:8,
  45. __BITFIELD_FIELD(unsigned mant:23,
  46. ;)))
  47. };
  48. u32 bits;
  49. };
  50. /*
  51. * single precision (often aka float)
  52. */
  53. int ieee754sp_class(union ieee754sp x);
  54. union ieee754sp ieee754sp_abs(union ieee754sp x);
  55. union ieee754sp ieee754sp_neg(union ieee754sp x);
  56. union ieee754sp ieee754sp_add(union ieee754sp x, union ieee754sp y);
  57. union ieee754sp ieee754sp_sub(union ieee754sp x, union ieee754sp y);
  58. union ieee754sp ieee754sp_mul(union ieee754sp x, union ieee754sp y);
  59. union ieee754sp ieee754sp_div(union ieee754sp x, union ieee754sp y);
  60. union ieee754sp ieee754sp_fint(int x);
  61. union ieee754sp ieee754sp_flong(s64 x);
  62. union ieee754sp ieee754sp_fdp(union ieee754dp x);
  63. int ieee754sp_tint(union ieee754sp x);
  64. s64 ieee754sp_tlong(union ieee754sp x);
  65. int ieee754sp_cmp(union ieee754sp x, union ieee754sp y, int cop, int sig);
  66. union ieee754sp ieee754sp_sqrt(union ieee754sp x);
  67. union ieee754sp ieee754sp_maddf(union ieee754sp z, union ieee754sp x,
  68. union ieee754sp y);
  69. union ieee754sp ieee754sp_msubf(union ieee754sp z, union ieee754sp x,
  70. union ieee754sp y);
  71. int ieee754sp_2008class(union ieee754sp x);
  72. union ieee754sp ieee754sp_fmin(union ieee754sp x, union ieee754sp y);
  73. union ieee754sp ieee754sp_fmina(union ieee754sp x, union ieee754sp y);
  74. union ieee754sp ieee754sp_fmax(union ieee754sp x, union ieee754sp y);
  75. union ieee754sp ieee754sp_fmaxa(union ieee754sp x, union ieee754sp y);
  76. /*
  77. * double precision (often aka double)
  78. */
  79. int ieee754dp_class(union ieee754dp x);
  80. union ieee754dp ieee754dp_add(union ieee754dp x, union ieee754dp y);
  81. union ieee754dp ieee754dp_sub(union ieee754dp x, union ieee754dp y);
  82. union ieee754dp ieee754dp_mul(union ieee754dp x, union ieee754dp y);
  83. union ieee754dp ieee754dp_div(union ieee754dp x, union ieee754dp y);
  84. union ieee754dp ieee754dp_abs(union ieee754dp x);
  85. union ieee754dp ieee754dp_neg(union ieee754dp x);
  86. union ieee754dp ieee754dp_fint(int x);
  87. union ieee754dp ieee754dp_flong(s64 x);
  88. union ieee754dp ieee754dp_fsp(union ieee754sp x);
  89. int ieee754dp_tint(union ieee754dp x);
  90. s64 ieee754dp_tlong(union ieee754dp x);
  91. int ieee754dp_cmp(union ieee754dp x, union ieee754dp y, int cop, int sig);
  92. union ieee754dp ieee754dp_sqrt(union ieee754dp x);
  93. union ieee754dp ieee754dp_maddf(union ieee754dp z, union ieee754dp x,
  94. union ieee754dp y);
  95. union ieee754dp ieee754dp_msubf(union ieee754dp z, union ieee754dp x,
  96. union ieee754dp y);
  97. int ieee754dp_2008class(union ieee754dp x);
  98. union ieee754dp ieee754dp_fmin(union ieee754dp x, union ieee754dp y);
  99. union ieee754dp ieee754dp_fmina(union ieee754dp x, union ieee754dp y);
  100. union ieee754dp ieee754dp_fmax(union ieee754dp x, union ieee754dp y);
  101. union ieee754dp ieee754dp_fmaxa(union ieee754dp x, union ieee754dp y);
  102. /* 5 types of floating point number
  103. */
  104. enum {
  105. IEEE754_CLASS_NORM = 0x00,
  106. IEEE754_CLASS_ZERO = 0x01,
  107. IEEE754_CLASS_DNORM = 0x02,
  108. IEEE754_CLASS_INF = 0x03,
  109. IEEE754_CLASS_SNAN = 0x04,
  110. IEEE754_CLASS_QNAN = 0x05,
  111. };
  112. /* exception numbers */
  113. #define IEEE754_INEXACT 0x01
  114. #define IEEE754_UNDERFLOW 0x02
  115. #define IEEE754_OVERFLOW 0x04
  116. #define IEEE754_ZERO_DIVIDE 0x08
  117. #define IEEE754_INVALID_OPERATION 0x10
  118. /* cmp operators
  119. */
  120. #define IEEE754_CLT 0x01
  121. #define IEEE754_CEQ 0x02
  122. #define IEEE754_CGT 0x04
  123. #define IEEE754_CUN 0x08
  124. /*
  125. * The control status register
  126. */
  127. struct _ieee754_csr {
  128. __BITFIELD_FIELD(unsigned fcc:7, /* condition[7:1] */
  129. __BITFIELD_FIELD(unsigned nod:1, /* set 1 for no denormals */
  130. __BITFIELD_FIELD(unsigned c:1, /* condition[0] */
  131. __BITFIELD_FIELD(unsigned pad0:3,
  132. __BITFIELD_FIELD(unsigned abs2008:1, /* IEEE 754-2008 ABS/NEG.fmt */
  133. __BITFIELD_FIELD(unsigned nan2008:1, /* IEEE 754-2008 NaN mode */
  134. __BITFIELD_FIELD(unsigned cx:6, /* exceptions this operation */
  135. __BITFIELD_FIELD(unsigned mx:5, /* exception enable mask */
  136. __BITFIELD_FIELD(unsigned sx:5, /* exceptions total */
  137. __BITFIELD_FIELD(unsigned rm:2, /* current rounding mode */
  138. ;))))))))))
  139. };
  140. #define ieee754_csr (*(struct _ieee754_csr *)(&current->thread.fpu.fcr31))
  141. static inline unsigned ieee754_getrm(void)
  142. {
  143. return (ieee754_csr.rm);
  144. }
  145. static inline unsigned ieee754_setrm(unsigned rm)
  146. {
  147. return (ieee754_csr.rm = rm);
  148. }
  149. /*
  150. * get current exceptions
  151. */
  152. static inline unsigned ieee754_getcx(void)
  153. {
  154. return (ieee754_csr.cx);
  155. }
  156. /* test for current exception condition
  157. */
  158. static inline int ieee754_cxtest(unsigned n)
  159. {
  160. return (ieee754_csr.cx & n);
  161. }
  162. /*
  163. * get sticky exceptions
  164. */
  165. static inline unsigned ieee754_getsx(void)
  166. {
  167. return (ieee754_csr.sx);
  168. }
  169. /* clear sticky conditions
  170. */
  171. static inline unsigned ieee754_clrsx(void)
  172. {
  173. return (ieee754_csr.sx = 0);
  174. }
  175. /* test for sticky exception condition
  176. */
  177. static inline int ieee754_sxtest(unsigned n)
  178. {
  179. return (ieee754_csr.sx & n);
  180. }
  181. /* debugging */
  182. union ieee754sp ieee754sp_dump(char *s, union ieee754sp x);
  183. union ieee754dp ieee754dp_dump(char *s, union ieee754dp x);
  184. #define IEEE754_SPCVAL_PZERO 0 /* +0.0 */
  185. #define IEEE754_SPCVAL_NZERO 1 /* -0.0 */
  186. #define IEEE754_SPCVAL_PONE 2 /* +1.0 */
  187. #define IEEE754_SPCVAL_NONE 3 /* -1.0 */
  188. #define IEEE754_SPCVAL_PTEN 4 /* +10.0 */
  189. #define IEEE754_SPCVAL_NTEN 5 /* -10.0 */
  190. #define IEEE754_SPCVAL_PINFINITY 6 /* +inf */
  191. #define IEEE754_SPCVAL_NINFINITY 7 /* -inf */
  192. #define IEEE754_SPCVAL_INDEF_LEG 8 /* legacy quiet NaN */
  193. #define IEEE754_SPCVAL_INDEF_2008 9 /* IEEE 754-2008 quiet NaN */
  194. #define IEEE754_SPCVAL_PMAX 10 /* +max norm */
  195. #define IEEE754_SPCVAL_NMAX 11 /* -max norm */
  196. #define IEEE754_SPCVAL_PMIN 12 /* +min norm */
  197. #define IEEE754_SPCVAL_NMIN 13 /* -min norm */
  198. #define IEEE754_SPCVAL_PMIND 14 /* +min denorm */
  199. #define IEEE754_SPCVAL_NMIND 15 /* -min denorm */
  200. #define IEEE754_SPCVAL_P1E31 16 /* + 1.0e31 */
  201. #define IEEE754_SPCVAL_P1E63 17 /* + 1.0e63 */
  202. extern const union ieee754dp __ieee754dp_spcvals[];
  203. extern const union ieee754sp __ieee754sp_spcvals[];
  204. #define ieee754dp_spcvals ((const union ieee754dp *)__ieee754dp_spcvals)
  205. #define ieee754sp_spcvals ((const union ieee754sp *)__ieee754sp_spcvals)
  206. /*
  207. * Return infinity with given sign
  208. */
  209. #define ieee754dp_inf(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PINFINITY+(sn)])
  210. #define ieee754dp_zero(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PZERO+(sn)])
  211. #define ieee754dp_one(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PONE+(sn)])
  212. #define ieee754dp_ten(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PTEN+(sn)])
  213. #define ieee754dp_indef() (ieee754dp_spcvals[IEEE754_SPCVAL_INDEF_LEG + \
  214. ieee754_csr.nan2008])
  215. #define ieee754dp_max(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PMAX+(sn)])
  216. #define ieee754dp_min(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PMIN+(sn)])
  217. #define ieee754dp_mind(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PMIND+(sn)])
  218. #define ieee754dp_1e31() (ieee754dp_spcvals[IEEE754_SPCVAL_P1E31])
  219. #define ieee754dp_1e63() (ieee754dp_spcvals[IEEE754_SPCVAL_P1E63])
  220. #define ieee754sp_inf(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PINFINITY+(sn)])
  221. #define ieee754sp_zero(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PZERO+(sn)])
  222. #define ieee754sp_one(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PONE+(sn)])
  223. #define ieee754sp_ten(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PTEN+(sn)])
  224. #define ieee754sp_indef() (ieee754sp_spcvals[IEEE754_SPCVAL_INDEF_LEG + \
  225. ieee754_csr.nan2008])
  226. #define ieee754sp_max(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PMAX+(sn)])
  227. #define ieee754sp_min(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PMIN+(sn)])
  228. #define ieee754sp_mind(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PMIND+(sn)])
  229. #define ieee754sp_1e31() (ieee754sp_spcvals[IEEE754_SPCVAL_P1E31])
  230. #define ieee754sp_1e63() (ieee754sp_spcvals[IEEE754_SPCVAL_P1E63])
  231. /*
  232. * Indefinite integer value
  233. */
  234. static inline int ieee754si_indef(void)
  235. {
  236. return ieee754_csr.nan2008 ? 0 : INT_MAX;
  237. }
  238. static inline s64 ieee754di_indef(void)
  239. {
  240. return ieee754_csr.nan2008 ? 0 : S64_MAX;
  241. }
  242. /*
  243. * Overflow integer value
  244. */
  245. static inline int ieee754si_overflow(int xs)
  246. {
  247. return ieee754_csr.nan2008 && xs ? INT_MIN : INT_MAX;
  248. }
  249. static inline s64 ieee754di_overflow(int xs)
  250. {
  251. return ieee754_csr.nan2008 && xs ? S64_MIN : S64_MAX;
  252. }
  253. /* result types for xctx.rt */
  254. #define IEEE754_RT_SP 0
  255. #define IEEE754_RT_DP 1
  256. #define IEEE754_RT_XP 2
  257. #define IEEE754_RT_SI 3
  258. #define IEEE754_RT_DI 4
  259. /* compat */
  260. #define ieee754dp_fix(x) ieee754dp_tint(x)
  261. #define ieee754sp_fix(x) ieee754sp_tint(x)
  262. #endif /* __ARCH_MIPS_MATH_EMU_IEEE754_H */