ieee754.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. /*
  68. * double precision (often aka double)
  69. */
  70. int ieee754dp_class(union ieee754dp x);
  71. union ieee754dp ieee754dp_add(union ieee754dp x, union ieee754dp y);
  72. union ieee754dp ieee754dp_sub(union ieee754dp x, union ieee754dp y);
  73. union ieee754dp ieee754dp_mul(union ieee754dp x, union ieee754dp y);
  74. union ieee754dp ieee754dp_div(union ieee754dp x, union ieee754dp y);
  75. union ieee754dp ieee754dp_abs(union ieee754dp x);
  76. union ieee754dp ieee754dp_neg(union ieee754dp x);
  77. union ieee754dp ieee754dp_fint(int x);
  78. union ieee754dp ieee754dp_flong(s64 x);
  79. union ieee754dp ieee754dp_fsp(union ieee754sp x);
  80. int ieee754dp_tint(union ieee754dp x);
  81. s64 ieee754dp_tlong(union ieee754dp x);
  82. int ieee754dp_cmp(union ieee754dp x, union ieee754dp y, int cop, int sig);
  83. union ieee754dp ieee754dp_sqrt(union ieee754dp x);
  84. /* 5 types of floating point number
  85. */
  86. enum {
  87. IEEE754_CLASS_NORM = 0x00,
  88. IEEE754_CLASS_ZERO = 0x01,
  89. IEEE754_CLASS_DNORM = 0x02,
  90. IEEE754_CLASS_INF = 0x03,
  91. IEEE754_CLASS_SNAN = 0x04,
  92. IEEE754_CLASS_QNAN = 0x05,
  93. };
  94. /* exception numbers */
  95. #define IEEE754_INEXACT 0x01
  96. #define IEEE754_UNDERFLOW 0x02
  97. #define IEEE754_OVERFLOW 0x04
  98. #define IEEE754_ZERO_DIVIDE 0x08
  99. #define IEEE754_INVALID_OPERATION 0x10
  100. /* cmp operators
  101. */
  102. #define IEEE754_CLT 0x01
  103. #define IEEE754_CEQ 0x02
  104. #define IEEE754_CGT 0x04
  105. #define IEEE754_CUN 0x08
  106. /*
  107. * The control status register
  108. */
  109. struct _ieee754_csr {
  110. __BITFIELD_FIELD(unsigned fcc:7, /* condition[7:1] */
  111. __BITFIELD_FIELD(unsigned nod:1, /* set 1 for no denormals */
  112. __BITFIELD_FIELD(unsigned c:1, /* condition[0] */
  113. __BITFIELD_FIELD(unsigned pad0:3,
  114. __BITFIELD_FIELD(unsigned abs2008:1, /* IEEE 754-2008 ABS/NEG.fmt */
  115. __BITFIELD_FIELD(unsigned nan2008:1, /* IEEE 754-2008 NaN mode */
  116. __BITFIELD_FIELD(unsigned cx:6, /* exceptions this operation */
  117. __BITFIELD_FIELD(unsigned mx:5, /* exception enable mask */
  118. __BITFIELD_FIELD(unsigned sx:5, /* exceptions total */
  119. __BITFIELD_FIELD(unsigned rm:2, /* current rounding mode */
  120. ;))))))))))
  121. };
  122. #define ieee754_csr (*(struct _ieee754_csr *)(&current->thread.fpu.fcr31))
  123. static inline unsigned ieee754_getrm(void)
  124. {
  125. return (ieee754_csr.rm);
  126. }
  127. static inline unsigned ieee754_setrm(unsigned rm)
  128. {
  129. return (ieee754_csr.rm = rm);
  130. }
  131. /*
  132. * get current exceptions
  133. */
  134. static inline unsigned ieee754_getcx(void)
  135. {
  136. return (ieee754_csr.cx);
  137. }
  138. /* test for current exception condition
  139. */
  140. static inline int ieee754_cxtest(unsigned n)
  141. {
  142. return (ieee754_csr.cx & n);
  143. }
  144. /*
  145. * get sticky exceptions
  146. */
  147. static inline unsigned ieee754_getsx(void)
  148. {
  149. return (ieee754_csr.sx);
  150. }
  151. /* clear sticky conditions
  152. */
  153. static inline unsigned ieee754_clrsx(void)
  154. {
  155. return (ieee754_csr.sx = 0);
  156. }
  157. /* test for sticky exception condition
  158. */
  159. static inline int ieee754_sxtest(unsigned n)
  160. {
  161. return (ieee754_csr.sx & n);
  162. }
  163. /* debugging */
  164. union ieee754sp ieee754sp_dump(char *s, union ieee754sp x);
  165. union ieee754dp ieee754dp_dump(char *s, union ieee754dp x);
  166. #define IEEE754_SPCVAL_PZERO 0 /* +0.0 */
  167. #define IEEE754_SPCVAL_NZERO 1 /* -0.0 */
  168. #define IEEE754_SPCVAL_PONE 2 /* +1.0 */
  169. #define IEEE754_SPCVAL_NONE 3 /* -1.0 */
  170. #define IEEE754_SPCVAL_PTEN 4 /* +10.0 */
  171. #define IEEE754_SPCVAL_NTEN 5 /* -10.0 */
  172. #define IEEE754_SPCVAL_PINFINITY 6 /* +inf */
  173. #define IEEE754_SPCVAL_NINFINITY 7 /* -inf */
  174. #define IEEE754_SPCVAL_INDEF 8 /* quiet NaN */
  175. #define IEEE754_SPCVAL_PMAX 9 /* +max norm */
  176. #define IEEE754_SPCVAL_NMAX 10 /* -max norm */
  177. #define IEEE754_SPCVAL_PMIN 11 /* +min norm */
  178. #define IEEE754_SPCVAL_NMIN 12 /* -min norm */
  179. #define IEEE754_SPCVAL_PMIND 13 /* +min denorm */
  180. #define IEEE754_SPCVAL_NMIND 14 /* -min denorm */
  181. #define IEEE754_SPCVAL_P1E31 15 /* + 1.0e31 */
  182. #define IEEE754_SPCVAL_P1E63 16 /* + 1.0e63 */
  183. extern const union ieee754dp __ieee754dp_spcvals[];
  184. extern const union ieee754sp __ieee754sp_spcvals[];
  185. #define ieee754dp_spcvals ((const union ieee754dp *)__ieee754dp_spcvals)
  186. #define ieee754sp_spcvals ((const union ieee754sp *)__ieee754sp_spcvals)
  187. /*
  188. * Return infinity with given sign
  189. */
  190. #define ieee754dp_inf(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PINFINITY+(sn)])
  191. #define ieee754dp_zero(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PZERO+(sn)])
  192. #define ieee754dp_one(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PONE+(sn)])
  193. #define ieee754dp_ten(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PTEN+(sn)])
  194. #define ieee754dp_indef() (ieee754dp_spcvals[IEEE754_SPCVAL_INDEF])
  195. #define ieee754dp_max(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PMAX+(sn)])
  196. #define ieee754dp_min(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PMIN+(sn)])
  197. #define ieee754dp_mind(sn) (ieee754dp_spcvals[IEEE754_SPCVAL_PMIND+(sn)])
  198. #define ieee754dp_1e31() (ieee754dp_spcvals[IEEE754_SPCVAL_P1E31])
  199. #define ieee754dp_1e63() (ieee754dp_spcvals[IEEE754_SPCVAL_P1E63])
  200. #define ieee754sp_inf(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PINFINITY+(sn)])
  201. #define ieee754sp_zero(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PZERO+(sn)])
  202. #define ieee754sp_one(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PONE+(sn)])
  203. #define ieee754sp_ten(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PTEN+(sn)])
  204. #define ieee754sp_indef() (ieee754sp_spcvals[IEEE754_SPCVAL_INDEF])
  205. #define ieee754sp_max(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PMAX+(sn)])
  206. #define ieee754sp_min(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PMIN+(sn)])
  207. #define ieee754sp_mind(sn) (ieee754sp_spcvals[IEEE754_SPCVAL_PMIND+(sn)])
  208. #define ieee754sp_1e31() (ieee754sp_spcvals[IEEE754_SPCVAL_P1E31])
  209. #define ieee754sp_1e63() (ieee754sp_spcvals[IEEE754_SPCVAL_P1E63])
  210. /*
  211. * Indefinite integer value
  212. */
  213. static inline int ieee754si_indef(void)
  214. {
  215. return INT_MAX;
  216. }
  217. static inline s64 ieee754di_indef(void)
  218. {
  219. return S64_MAX;
  220. }
  221. /* result types for xctx.rt */
  222. #define IEEE754_RT_SP 0
  223. #define IEEE754_RT_DP 1
  224. #define IEEE754_RT_XP 2
  225. #define IEEE754_RT_SI 3
  226. #define IEEE754_RT_DI 4
  227. /* compat */
  228. #define ieee754dp_fix(x) ieee754dp_tint(x)
  229. #define ieee754sp_fix(x) ieee754sp_tint(x)
  230. #endif /* __ARCH_MIPS_MATH_EMU_IEEE754_H */