fp-bit.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /* Header file for fp-bit.c. */
  2. /* Copyright (C) 2000-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. #ifndef GCC_FP_BIT_H
  20. #define GCC_FP_BIT_H
  21. /* Defining FINE_GRAINED_LIBRARIES allows one to select which routines
  22. from this file are compiled via additional -D options.
  23. This avoids the need to pull in the entire fp emulation library
  24. when only a small number of functions are needed.
  25. If FINE_GRAINED_LIBRARIES is not defined, then compile every
  26. suitable routine. */
  27. #ifndef FINE_GRAINED_LIBRARIES
  28. #define L_pack_df
  29. #define L_unpack_df
  30. #define L_pack_sf
  31. #define L_unpack_sf
  32. #define L_addsub_sf
  33. #define L_addsub_df
  34. #define L_mul_sf
  35. #define L_mul_df
  36. #define L_div_sf
  37. #define L_div_df
  38. #define L_fpcmp_parts_sf
  39. #define L_fpcmp_parts_df
  40. #define L_compare_sf
  41. #define L_compare_df
  42. #define L_eq_sf
  43. #define L_eq_df
  44. #define L_ne_sf
  45. #define L_ne_df
  46. #define L_gt_sf
  47. #define L_gt_df
  48. #define L_ge_sf
  49. #define L_ge_df
  50. #define L_lt_sf
  51. #define L_lt_df
  52. #define L_le_sf
  53. #define L_le_df
  54. #define L_unord_sf
  55. #define L_unord_df
  56. #define L_usi_to_sf
  57. #define L_usi_to_df
  58. #define L_si_to_sf
  59. #define L_si_to_df
  60. #define L_sf_to_si
  61. #define L_df_to_si
  62. #define L_f_to_usi
  63. #define L_df_to_usi
  64. #define L_negate_sf
  65. #define L_negate_df
  66. #define L_make_sf
  67. #define L_make_df
  68. #define L_sf_to_df
  69. #define L_df_to_sf
  70. #ifdef FLOAT
  71. #define L_thenan_sf
  72. #else
  73. #define L_thenan_df
  74. #endif
  75. #endif /* ! FINE_GRAINED_LIBRARIES */
  76. #if __LDBL_MANT_DIG__ == 113 || __LDBL_MANT_DIG__ == 106
  77. # if defined(TFLOAT) || defined(L_sf_to_tf) || defined(L_df_to_tf)
  78. # define TMODES
  79. # endif
  80. #endif
  81. typedef float SFtype __attribute__ ((mode (SF)));
  82. typedef float DFtype __attribute__ ((mode (DF)));
  83. #ifdef TMODES
  84. typedef float TFtype __attribute__ ((mode (TF)));
  85. #endif
  86. typedef int HItype __attribute__ ((mode (HI)));
  87. typedef int SItype __attribute__ ((mode (SI)));
  88. typedef int DItype __attribute__ ((mode (DI)));
  89. #ifdef TMODES
  90. typedef int TItype __attribute__ ((mode (TI)));
  91. #endif
  92. /* The type of the result of a floating point comparison. This must
  93. match `__libgcc_cmp_return__' in GCC for the target. */
  94. #ifndef CMPtype
  95. typedef int CMPtype __attribute__ ((mode (__libgcc_cmp_return__)));
  96. #endif
  97. typedef unsigned int UHItype __attribute__ ((mode (HI)));
  98. typedef unsigned int USItype __attribute__ ((mode (SI)));
  99. typedef unsigned int UDItype __attribute__ ((mode (DI)));
  100. #ifdef TMODES
  101. typedef unsigned int UTItype __attribute__ ((mode (TI)));
  102. #endif
  103. #define MAX_USI_INT (~(USItype)0)
  104. #define MAX_SI_INT ((SItype) (MAX_USI_INT >> 1))
  105. #define BITS_PER_SI (4 * BITS_PER_UNIT)
  106. #ifdef TMODES
  107. #define MAX_UDI_INT (~(UDItype)0)
  108. #define MAX_DI_INT ((DItype) (MAX_UDI_INT >> 1))
  109. #define BITS_PER_DI (8 * BITS_PER_UNIT)
  110. #endif
  111. #ifdef FLOAT_ONLY
  112. #define NO_DI_MODE
  113. #endif
  114. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  115. #define FLOAT_BIT_ORDER_MISMATCH
  116. #endif
  117. #if __BYTE_ORDER__ != __FLOAT_WORD_ORDER__
  118. #define FLOAT_WORD_ORDER_MISMATCH
  119. #endif
  120. #ifdef TFLOAT
  121. # ifndef TMODES
  122. # error "TFLOAT requires long double to have 113 bits of mantissa"
  123. # endif
  124. # define PREFIXFPDP tp
  125. # define PREFIXSFDF tf
  126. # define NGARDS 10L /* Is this right? */
  127. # define GARDROUND 0x1ff
  128. # define GARDMASK 0x3ff
  129. # define GARDMSB 0x200
  130. # define FRAC_NBITS 128
  131. # if __LDBL_MANT_DIG__ == 113 /* IEEE quad */
  132. # define EXPBITS 15
  133. # define EXPBIAS 16383
  134. # define EXPMAX (0x7fff)
  135. # define QUIET_NAN ((TItype)0x8 << 108)
  136. # define FRACHIGH ((TItype)0x8 << 124)
  137. # define FRACHIGH2 ((TItype)0xc << 124)
  138. # define FRACBITS 112
  139. # endif
  140. # if __LDBL_MANT_DIG__ == 106 /* IBM extended (double+double) */
  141. # define EXPBITS 11
  142. # define EXPBIAS 1023
  143. # define EXPMAX (0x7ff)
  144. # define QUIET_NAN ((TItype)0x8 << (48 + 64))
  145. # define FRACHIGH ((TItype)0x8 << 124)
  146. # define FRACHIGH2 ((TItype)0xc << 124)
  147. # define FRACBITS 105
  148. # define HALFFRACBITS 52
  149. # define HALFSHIFT 64
  150. # endif
  151. # define pack_d __pack_t
  152. # define unpack_d __unpack_t
  153. # define __fpcmp_parts __fpcmp_parts_t
  154. typedef UTItype fractype;
  155. typedef UDItype halffractype;
  156. typedef USItype qrtrfractype;
  157. #define qrtrfractype qrtrfractype
  158. typedef TFtype FLO_type;
  159. typedef TItype intfrac;
  160. #elif defined FLOAT
  161. # define NGARDS 7L
  162. # define GARDROUND 0x3f
  163. # define GARDMASK 0x7f
  164. # define GARDMSB 0x40
  165. # define EXPBITS 8
  166. # define EXPBIAS 127
  167. # define FRACBITS 23
  168. # define EXPMAX (0xff)
  169. # define QUIET_NAN 0x400000L
  170. # define FRAC_NBITS 32
  171. # define FRACHIGH 0x80000000L
  172. # define FRACHIGH2 0xc0000000L
  173. # define pack_d __pack_f
  174. # define unpack_d __unpack_f
  175. # define __fpcmp_parts __fpcmp_parts_f
  176. typedef USItype fractype;
  177. typedef UHItype halffractype;
  178. typedef SFtype FLO_type;
  179. typedef SItype intfrac;
  180. #else
  181. # define PREFIXFPDP dp
  182. # define PREFIXSFDF df
  183. # define NGARDS 8L
  184. # define GARDROUND 0x7f
  185. # define GARDMASK 0xff
  186. # define GARDMSB 0x80
  187. # define EXPBITS 11
  188. # define EXPBIAS 1023
  189. # define FRACBITS 52
  190. # define EXPMAX (0x7ff)
  191. # define QUIET_NAN 0x8000000000000LL
  192. # define FRAC_NBITS 64
  193. # define FRACHIGH 0x8000000000000000LL
  194. # define FRACHIGH2 0xc000000000000000LL
  195. # define pack_d __pack_d
  196. # define unpack_d __unpack_d
  197. # define __fpcmp_parts __fpcmp_parts_d
  198. typedef UDItype fractype;
  199. typedef USItype halffractype;
  200. typedef DFtype FLO_type;
  201. typedef DItype intfrac;
  202. #endif /* FLOAT */
  203. #ifdef TFLOAT
  204. # define add __addtf3
  205. # define sub __subtf3
  206. # define multiply __multf3
  207. # define divide __divtf3
  208. # define compare __cmptf2
  209. # define _eq_f2 __eqtf2
  210. # define _ne_f2 __netf2
  211. # define _gt_f2 __gttf2
  212. # define _ge_f2 __getf2
  213. # define _lt_f2 __lttf2
  214. # define _le_f2 __letf2
  215. # define _unord_f2 __unordtf2
  216. # define usi_to_float __floatunsitf
  217. # define si_to_float __floatsitf
  218. # define float_to_si __fixtfsi
  219. # define float_to_usi __fixunstfsi
  220. # define negate __negtf2
  221. # define tf_to_sf __trunctfsf2
  222. # define tf_to_df __trunctfdf2
  223. #elif defined FLOAT
  224. # define add __addsf3
  225. # define sub __subsf3
  226. # define multiply __mulsf3
  227. # define divide __divsf3
  228. # define compare __cmpsf2
  229. # define _eq_f2 __eqsf2
  230. # define _ne_f2 __nesf2
  231. # define _gt_f2 __gtsf2
  232. # define _ge_f2 __gesf2
  233. # define _lt_f2 __ltsf2
  234. # define _le_f2 __lesf2
  235. # define _unord_f2 __unordsf2
  236. # define usi_to_float __floatunsisf
  237. # define si_to_float __floatsisf
  238. # define float_to_si __fixsfsi
  239. # define float_to_usi __fixunssfsi
  240. # define negate __negsf2
  241. # define sf_to_df __extendsfdf2
  242. # define sf_to_tf __extendsftf2
  243. #else
  244. # define add __adddf3
  245. # define sub __subdf3
  246. # define multiply __muldf3
  247. # define divide __divdf3
  248. # define compare __cmpdf2
  249. # define _eq_f2 __eqdf2
  250. # define _ne_f2 __nedf2
  251. # define _gt_f2 __gtdf2
  252. # define _ge_f2 __gedf2
  253. # define _lt_f2 __ltdf2
  254. # define _le_f2 __ledf2
  255. # define _unord_f2 __unorddf2
  256. # define usi_to_float __floatunsidf
  257. # define si_to_float __floatsidf
  258. # define float_to_si __fixdfsi
  259. # define float_to_usi __fixunsdfsi
  260. # define negate __negdf2
  261. # define df_to_sf __truncdfsf2
  262. # define df_to_tf __extenddftf2
  263. #endif /* FLOAT */
  264. #ifndef INLINE
  265. #define INLINE __inline__
  266. #endif
  267. /* Preserve the sticky-bit when shifting fractions to the right. */
  268. #define LSHIFT(a, s) { a = (a >> s) | !!(a & (((fractype) 1 << s) - 1)); }
  269. /* numeric parameters */
  270. /* F_D_BITOFF is the number of bits offset between the MSB of the mantissa
  271. of a float and of a double. Assumes there are only two float types.
  272. (double::FRAC_BITS+double::NGARDS-(float::FRAC_BITS+float::NGARDS))
  273. */
  274. #define F_D_BITOFF (52+8-(23+7))
  275. #ifdef TMODES
  276. # define F_T_BITOFF (__LDBL_MANT_DIG__-1+10-(23+7))
  277. # define D_T_BITOFF (__LDBL_MANT_DIG__-1+10-(52+8))
  278. #endif
  279. #define NORMAL_EXPMIN (-(EXPBIAS)+1)
  280. #define IMPLICIT_1 ((fractype)1<<(FRACBITS+NGARDS))
  281. #define IMPLICIT_2 ((fractype)1<<(FRACBITS+1+NGARDS))
  282. /* common types */
  283. typedef enum
  284. {
  285. CLASS_SNAN,
  286. CLASS_QNAN,
  287. CLASS_ZERO,
  288. CLASS_NUMBER,
  289. CLASS_INFINITY
  290. } fp_class_type;
  291. typedef struct
  292. {
  293. #ifdef SMALL_MACHINE
  294. char class;
  295. unsigned char sign;
  296. short normal_exp;
  297. #else
  298. fp_class_type class;
  299. unsigned int sign;
  300. int normal_exp;
  301. #endif
  302. union
  303. {
  304. fractype ll;
  305. halffractype l[2];
  306. } fraction;
  307. } fp_number_type;
  308. typedef union
  309. {
  310. FLO_type value;
  311. fractype value_raw;
  312. #ifndef FLOAT
  313. # ifdef qrtrfractype
  314. qrtrfractype qwords[4];
  315. # else
  316. halffractype words[2];
  317. # endif
  318. #endif
  319. #ifdef FLOAT_BIT_ORDER_MISMATCH
  320. struct
  321. {
  322. fractype fraction:FRACBITS __attribute__ ((packed));
  323. unsigned int exp:EXPBITS __attribute__ ((packed));
  324. unsigned int sign:1 __attribute__ ((packed));
  325. }
  326. bits;
  327. #endif
  328. #ifdef _DEBUG_BITFLOAT
  329. struct
  330. {
  331. unsigned int sign:1 __attribute__ ((packed));
  332. unsigned int exp:EXPBITS __attribute__ ((packed));
  333. fractype fraction:FRACBITS __attribute__ ((packed));
  334. }
  335. bits_big_endian;
  336. struct
  337. {
  338. fractype fraction:FRACBITS __attribute__ ((packed));
  339. unsigned int exp:EXPBITS __attribute__ ((packed));
  340. unsigned int sign:1 __attribute__ ((packed));
  341. }
  342. bits_little_endian;
  343. #endif
  344. }
  345. FLO_union_type;
  346. /* Prototypes. */
  347. #if defined(L_pack_df) || defined(L_pack_sf) || defined(L_pack_tf)
  348. extern FLO_type pack_d (const fp_number_type *);
  349. #endif
  350. extern void unpack_d (FLO_union_type *, fp_number_type *);
  351. #if defined(L_addsub_sf) || defined(L_addsub_df) || defined(L_addsub_tf)
  352. extern FLO_type add (FLO_type, FLO_type);
  353. extern FLO_type sub (FLO_type, FLO_type);
  354. #endif
  355. #if defined(L_mul_sf) || defined(L_mul_df) || defined(L_mul_tf)
  356. extern FLO_type multiply (FLO_type, FLO_type);
  357. #endif
  358. #if defined(L_div_sf) || defined(L_div_df) || defined(L_div_tf)
  359. extern FLO_type divide (FLO_type, FLO_type);
  360. #endif
  361. extern int __fpcmp_parts (fp_number_type *, fp_number_type *);
  362. #if defined(L_compare_sf) || defined(L_compare_df) || defined(L_compare_tf)
  363. extern CMPtype compare (FLO_type, FLO_type);
  364. #endif
  365. #if defined(L_eq_sf) || defined(L_eq_df) || defined(L_eq_tf)
  366. extern CMPtype _eq_f2 (FLO_type, FLO_type);
  367. #endif
  368. #if defined(L_ne_sf) || defined(L_ne_df) || defined(L_ne_tf)
  369. extern CMPtype _ne_f2 (FLO_type, FLO_type);
  370. #endif
  371. #if defined(L_gt_sf) || defined(L_gt_df) || defined(L_gt_tf)
  372. extern CMPtype _gt_f2 (FLO_type, FLO_type);
  373. #endif
  374. #if defined(L_ge_sf) || defined(L_ge_df) || defined(L_ge_tf)
  375. extern CMPtype _ge_f2 (FLO_type, FLO_type);
  376. #endif
  377. #if defined(L_lt_sf) || defined(L_lt_df) || defined(L_lt_tf)
  378. extern CMPtype _lt_f2 (FLO_type, FLO_type);
  379. #endif
  380. #if defined(L_le_sf) || defined(L_le_df) || defined(L_le_tf)
  381. extern CMPtype _le_f2 (FLO_type, FLO_type);
  382. #endif
  383. #if defined(L_unord_sf) || defined(L_unord_df) || defined(L_unord_tf)
  384. extern CMPtype _unord_f2 (FLO_type, FLO_type);
  385. #endif
  386. #if defined(L_si_to_sf) || defined(L_si_to_df) || defined(L_si_to_tf)
  387. extern FLO_type si_to_float (SItype);
  388. #endif
  389. #if defined(L_sf_to_si) || defined(L_df_to_si) || defined(L_tf_to_si)
  390. extern SItype float_to_si (FLO_type);
  391. #endif
  392. #if defined(L_tf_to_usi)
  393. extern USItype float_to_usi (FLO_type);
  394. #endif
  395. #if defined(L_usi_to_sf) || defined(L_usi_to_df) || defined(L_usi_to_tf)
  396. extern FLO_type usi_to_float (USItype);
  397. #endif
  398. #if defined(L_negate_sf) || defined(L_negate_df) || defined(L_negate_tf)
  399. extern FLO_type negate (FLO_type);
  400. #endif
  401. #ifdef FLOAT
  402. #if defined(L_make_sf)
  403. extern SFtype __make_fp (fp_class_type, unsigned int, int, USItype);
  404. #endif
  405. #ifndef FLOAT_ONLY
  406. extern DFtype __make_dp (fp_class_type, unsigned int, int, UDItype);
  407. #if defined(L_sf_to_df)
  408. extern DFtype sf_to_df (SFtype);
  409. #endif
  410. #if defined(L_sf_to_tf) && defined(TMODES)
  411. extern TFtype sf_to_tf (SFtype);
  412. #endif
  413. #endif /* ! FLOAT_ONLY */
  414. #endif /* FLOAT */
  415. #ifndef FLOAT
  416. extern SFtype __make_fp (fp_class_type, unsigned int, int, USItype);
  417. #if defined(L_make_df)
  418. extern DFtype __make_dp (fp_class_type, unsigned int, int, UDItype);
  419. #endif
  420. #if defined(L_df_to_sf)
  421. extern SFtype df_to_sf (DFtype);
  422. #endif
  423. #if defined(L_df_to_tf) && defined(TMODES)
  424. extern TFtype df_to_tf (DFtype);
  425. #endif
  426. #endif /* ! FLOAT */
  427. #ifdef TMODES
  428. extern DFtype __make_dp (fp_class_type, unsigned int, int, UDItype);
  429. extern TFtype __make_tp (fp_class_type, unsigned int, int, UTItype);
  430. #ifdef TFLOAT
  431. #if defined(L_tf_to_sf)
  432. extern SFtype tf_to_sf (TFtype);
  433. #endif
  434. #if defined(L_tf_to_df)
  435. extern DFtype tf_to_df (TFtype);
  436. #endif
  437. #if defined(L_di_to_tf)
  438. extern TFtype di_to_df (DItype);
  439. #endif
  440. #endif /* TFLOAT */
  441. #endif /* TMODES */
  442. #endif /* ! GCC_FP_BIT_H */