fp-bit.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /* Header file for fp-bit.c. */
  2. /* Copyright (C) 2000-2022 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 * __CHAR_BIT__)
  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 * __CHAR_BIT__)
  110. #endif
  111. #ifdef FLOAT_ONLY
  112. #define NO_DI_MODE
  113. #endif
  114. #if __BYTE_ORDER__ != __FLOAT_WORD_ORDER__
  115. #define FLOAT_WORD_ORDER_MISMATCH
  116. #endif
  117. #ifdef TFLOAT
  118. # ifndef TMODES
  119. # error "TFLOAT requires long double to have 113 bits of mantissa"
  120. # endif
  121. # define PREFIXFPDP tp
  122. # define PREFIXSFDF tf
  123. # define NGARDS 10L /* Is this right? */
  124. # define GARDROUND 0x1ff
  125. # define GARDMASK 0x3ff
  126. # define GARDMSB 0x200
  127. # define FRAC_NBITS 128
  128. # if __LDBL_MANT_DIG__ == 113 /* IEEE quad */
  129. # define EXPBITS 15
  130. # define EXPBIAS 16383
  131. # define EXPMAX (0x7fff)
  132. # define QUIET_NAN ((TItype)0x8 << 108)
  133. # define FRACHIGH ((TItype)0x8 << 124)
  134. # define FRACHIGH2 ((TItype)0xc << 124)
  135. # define FRACBITS 112
  136. # endif
  137. # if __LDBL_MANT_DIG__ == 106 /* IBM extended (double+double) */
  138. # define EXPBITS 11
  139. # define EXPBIAS 1023
  140. # define EXPMAX (0x7ff)
  141. # define QUIET_NAN ((TItype)0x8 << (48 + 64))
  142. # define FRACHIGH ((TItype)0x8 << 124)
  143. # define FRACHIGH2 ((TItype)0xc << 124)
  144. # define FRACBITS 105
  145. # define HALFFRACBITS 52
  146. # define HALFSHIFT 64
  147. # endif
  148. # define pack_d __pack_t
  149. # define unpack_d __unpack_t
  150. # define __fpcmp_parts __fpcmp_parts_t
  151. typedef UTItype fractype;
  152. typedef UDItype halffractype;
  153. typedef USItype qrtrfractype;
  154. #define qrtrfractype qrtrfractype
  155. typedef TFtype FLO_type;
  156. typedef TItype intfrac;
  157. #elif defined FLOAT
  158. # define NGARDS 7L
  159. # define GARDROUND 0x3f
  160. # define GARDMASK 0x7f
  161. # define GARDMSB 0x40
  162. # define EXPBITS 8
  163. # define EXPBIAS 127
  164. # define FRACBITS 23
  165. # define EXPMAX (0xff)
  166. # define QUIET_NAN 0x400000L
  167. # define FRAC_NBITS 32
  168. # define FRACHIGH 0x80000000L
  169. # define FRACHIGH2 0xc0000000L
  170. # define pack_d __pack_f
  171. # define unpack_d __unpack_f
  172. # define __fpcmp_parts __fpcmp_parts_f
  173. typedef USItype fractype;
  174. typedef UHItype halffractype;
  175. typedef SFtype FLO_type;
  176. typedef SItype intfrac;
  177. #else
  178. # define PREFIXFPDP dp
  179. # define PREFIXSFDF df
  180. # define NGARDS 8L
  181. # define GARDROUND 0x7f
  182. # define GARDMASK 0xff
  183. # define GARDMSB 0x80
  184. # define EXPBITS 11
  185. # define EXPBIAS 1023
  186. # define FRACBITS 52
  187. # define EXPMAX (0x7ff)
  188. # define QUIET_NAN 0x8000000000000LL
  189. # define FRAC_NBITS 64
  190. # define FRACHIGH 0x8000000000000000LL
  191. # define FRACHIGH2 0xc000000000000000LL
  192. # define pack_d __pack_d
  193. # define unpack_d __unpack_d
  194. # define __fpcmp_parts __fpcmp_parts_d
  195. typedef UDItype fractype;
  196. typedef USItype halffractype;
  197. typedef DFtype FLO_type;
  198. typedef DItype intfrac;
  199. #endif /* FLOAT */
  200. #ifdef TFLOAT
  201. # define add __addtf3
  202. # define sub __subtf3
  203. # define multiply __multf3
  204. # define divide __divtf3
  205. # define compare __cmptf2
  206. # define _eq_f2 __eqtf2
  207. # define _ne_f2 __netf2
  208. # define _gt_f2 __gttf2
  209. # define _ge_f2 __getf2
  210. # define _lt_f2 __lttf2
  211. # define _le_f2 __letf2
  212. # define _unord_f2 __unordtf2
  213. # define usi_to_float __floatunsitf
  214. # define si_to_float __floatsitf
  215. # define float_to_si __fixtfsi
  216. # define float_to_usi __fixunstfsi
  217. # define negate __negtf2
  218. # define tf_to_sf __trunctfsf2
  219. # define tf_to_df __trunctfdf2
  220. #elif defined FLOAT
  221. # define add __addsf3
  222. # define sub __subsf3
  223. # define multiply __mulsf3
  224. # define divide __divsf3
  225. # define compare __cmpsf2
  226. # define _eq_f2 __eqsf2
  227. # define _ne_f2 __nesf2
  228. # define _gt_f2 __gtsf2
  229. # define _ge_f2 __gesf2
  230. # define _lt_f2 __ltsf2
  231. # define _le_f2 __lesf2
  232. # define _unord_f2 __unordsf2
  233. # define usi_to_float __floatunsisf
  234. # define si_to_float __floatsisf
  235. # define float_to_si __fixsfsi
  236. # define float_to_usi __fixunssfsi
  237. # define negate __negsf2
  238. # define sf_to_df __extendsfdf2
  239. # define sf_to_tf __extendsftf2
  240. #else
  241. # define add __adddf3
  242. # define sub __subdf3
  243. # define multiply __muldf3
  244. # define divide __divdf3
  245. # define compare __cmpdf2
  246. # define _eq_f2 __eqdf2
  247. # define _ne_f2 __nedf2
  248. # define _gt_f2 __gtdf2
  249. # define _ge_f2 __gedf2
  250. # define _lt_f2 __ltdf2
  251. # define _le_f2 __ledf2
  252. # define _unord_f2 __unorddf2
  253. # define usi_to_float __floatunsidf
  254. # define si_to_float __floatsidf
  255. # define float_to_si __fixdfsi
  256. # define float_to_usi __fixunsdfsi
  257. # define negate __negdf2
  258. # define df_to_sf __truncdfsf2
  259. # define df_to_tf __extenddftf2
  260. #endif /* FLOAT */
  261. #ifndef INLINE
  262. #define INLINE __inline__
  263. #endif
  264. /* Preserve the sticky-bit when shifting fractions to the right. */
  265. #define LSHIFT(a, s) { a = (a >> s) | !!(a & (((fractype) 1 << s) - 1)); }
  266. /* numeric parameters */
  267. /* F_D_BITOFF is the number of bits offset between the MSB of the mantissa
  268. of a float and of a double. Assumes there are only two float types.
  269. (double::FRAC_BITS+double::NGARDS-(float::FRAC_BITS+float::NGARDS))
  270. */
  271. #define F_D_BITOFF (52+8-(23+7))
  272. #ifdef TMODES
  273. # define F_T_BITOFF (__LDBL_MANT_DIG__-1+10-(23+7))
  274. # define D_T_BITOFF (__LDBL_MANT_DIG__-1+10-(52+8))
  275. #endif
  276. #define NORMAL_EXPMIN (-(EXPBIAS)+1)
  277. #define IMPLICIT_1 ((fractype)1<<(FRACBITS+NGARDS))
  278. #define IMPLICIT_2 ((fractype)1<<(FRACBITS+1+NGARDS))
  279. /* common types */
  280. typedef enum
  281. {
  282. CLASS_SNAN,
  283. CLASS_QNAN,
  284. CLASS_ZERO,
  285. CLASS_NUMBER,
  286. CLASS_INFINITY
  287. } fp_class_type;
  288. typedef struct
  289. {
  290. #ifdef SMALL_MACHINE
  291. char class;
  292. unsigned char sign;
  293. short normal_exp;
  294. #else
  295. fp_class_type class;
  296. unsigned int sign;
  297. int normal_exp;
  298. #endif
  299. union
  300. {
  301. fractype ll;
  302. halffractype l[2];
  303. } fraction;
  304. } fp_number_type;
  305. typedef union
  306. {
  307. FLO_type value;
  308. fractype value_raw;
  309. #ifndef FLOAT
  310. # ifdef qrtrfractype
  311. qrtrfractype qwords[4];
  312. # else
  313. halffractype words[2];
  314. # endif
  315. #endif
  316. #ifdef _DEBUG_BITFLOAT
  317. struct
  318. {
  319. unsigned int sign:1 __attribute__ ((packed));
  320. unsigned int exp:EXPBITS __attribute__ ((packed));
  321. fractype fraction:FRACBITS __attribute__ ((packed));
  322. }
  323. bits_big_endian;
  324. struct
  325. {
  326. fractype fraction:FRACBITS __attribute__ ((packed));
  327. unsigned int exp:EXPBITS __attribute__ ((packed));
  328. unsigned int sign:1 __attribute__ ((packed));
  329. }
  330. bits_little_endian;
  331. #endif
  332. }
  333. FLO_union_type;
  334. /* Prototypes. */
  335. #if defined(L_pack_df) || defined(L_pack_sf) || defined(L_pack_tf)
  336. extern FLO_type pack_d (const fp_number_type *);
  337. #endif
  338. extern void unpack_d (FLO_union_type *, fp_number_type *);
  339. #if defined(L_addsub_sf) || defined(L_addsub_df) || defined(L_addsub_tf)
  340. extern FLO_type add (FLO_type, FLO_type);
  341. extern FLO_type sub (FLO_type, FLO_type);
  342. #endif
  343. #if defined(L_mul_sf) || defined(L_mul_df) || defined(L_mul_tf)
  344. extern FLO_type multiply (FLO_type, FLO_type);
  345. #endif
  346. #if defined(L_div_sf) || defined(L_div_df) || defined(L_div_tf)
  347. extern FLO_type divide (FLO_type, FLO_type);
  348. #endif
  349. extern int __fpcmp_parts (fp_number_type *, fp_number_type *);
  350. #if defined(L_compare_sf) || defined(L_compare_df) || defined(L_compare_tf)
  351. extern CMPtype compare (FLO_type, FLO_type);
  352. #endif
  353. #if defined(L_eq_sf) || defined(L_eq_df) || defined(L_eq_tf)
  354. extern CMPtype _eq_f2 (FLO_type, FLO_type);
  355. #endif
  356. #if defined(L_ne_sf) || defined(L_ne_df) || defined(L_ne_tf)
  357. extern CMPtype _ne_f2 (FLO_type, FLO_type);
  358. #endif
  359. #if defined(L_gt_sf) || defined(L_gt_df) || defined(L_gt_tf)
  360. extern CMPtype _gt_f2 (FLO_type, FLO_type);
  361. #endif
  362. #if defined(L_ge_sf) || defined(L_ge_df) || defined(L_ge_tf)
  363. extern CMPtype _ge_f2 (FLO_type, FLO_type);
  364. #endif
  365. #if defined(L_lt_sf) || defined(L_lt_df) || defined(L_lt_tf)
  366. extern CMPtype _lt_f2 (FLO_type, FLO_type);
  367. #endif
  368. #if defined(L_le_sf) || defined(L_le_df) || defined(L_le_tf)
  369. extern CMPtype _le_f2 (FLO_type, FLO_type);
  370. #endif
  371. #if defined(L_unord_sf) || defined(L_unord_df) || defined(L_unord_tf)
  372. extern CMPtype _unord_f2 (FLO_type, FLO_type);
  373. #endif
  374. #if defined(L_si_to_sf) || defined(L_si_to_df) || defined(L_si_to_tf)
  375. extern FLO_type si_to_float (SItype);
  376. #endif
  377. #if defined(L_sf_to_si) || defined(L_df_to_si) || defined(L_tf_to_si)
  378. extern SItype float_to_si (FLO_type);
  379. #endif
  380. #if defined(L_tf_to_usi)
  381. extern USItype float_to_usi (FLO_type);
  382. #endif
  383. #if defined(L_usi_to_sf) || defined(L_usi_to_df) || defined(L_usi_to_tf)
  384. extern FLO_type usi_to_float (USItype);
  385. #endif
  386. #if defined(L_negate_sf) || defined(L_negate_df) || defined(L_negate_tf)
  387. extern FLO_type negate (FLO_type);
  388. #endif
  389. #ifdef FLOAT
  390. #if defined(L_make_sf)
  391. extern SFtype __make_fp (fp_class_type, unsigned int, int, USItype);
  392. #endif
  393. #ifndef FLOAT_ONLY
  394. extern DFtype __make_dp (fp_class_type, unsigned int, int, UDItype);
  395. #if defined(L_sf_to_df)
  396. extern DFtype sf_to_df (SFtype);
  397. #endif
  398. #if defined(L_sf_to_tf) && defined(TMODES)
  399. extern TFtype sf_to_tf (SFtype);
  400. #endif
  401. #endif /* ! FLOAT_ONLY */
  402. #endif /* FLOAT */
  403. #ifndef FLOAT
  404. extern SFtype __make_fp (fp_class_type, unsigned int, int, USItype);
  405. #if defined(L_make_df)
  406. extern DFtype __make_dp (fp_class_type, unsigned int, int, UDItype);
  407. #endif
  408. #if defined(L_df_to_sf)
  409. extern SFtype df_to_sf (DFtype);
  410. #endif
  411. #if defined(L_df_to_tf) && defined(TMODES)
  412. extern TFtype df_to_tf (DFtype);
  413. #endif
  414. #endif /* ! FLOAT */
  415. #ifdef TMODES
  416. extern DFtype __make_dp (fp_class_type, unsigned int, int, UDItype);
  417. extern TFtype __make_tp (fp_class_type, unsigned int, int, UTItype);
  418. #ifdef TFLOAT
  419. #if defined(L_tf_to_sf)
  420. extern SFtype tf_to_sf (TFtype);
  421. #endif
  422. #if defined(L_tf_to_df)
  423. extern DFtype tf_to_df (TFtype);
  424. #endif
  425. #if defined(L_di_to_tf)
  426. extern TFtype di_to_df (DItype);
  427. #endif
  428. #endif /* TFLOAT */
  429. #endif /* TMODES */
  430. #endif /* ! GCC_FP_BIT_H */