floatn-common.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. //
  2. // Copyright 2026 Aarav Ravindra Kharade
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. /* Macros to control TS 18661-3 glibc features where the same
  17. definitions are appropriate for all platforms.
  18. Copyright (C) 2017-2026 Free Software Foundation, Inc.
  19. This file is part of the GNU C Library.
  20. The GNU C Library is free software; you can redistribute it and/or
  21. modify it under the terms of the GNU Lesser General Public
  22. License as published by the Free Software Foundation; either
  23. version 2.1 of the License, or (at your option) any later version.
  24. The GNU C Library is distributed in the hope that it will be useful,
  25. but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  27. Lesser General Public License for more details.
  28. You should have received a copy of the GNU Lesser General Public
  29. License along with the GNU C Library; if not, see
  30. <https://www.gnu.org/licenses/>. */
  31. #ifndef _BITS_FLOATN_COMMON_H
  32. #define _BITS_FLOATN_COMMON_H
  33. #include <features.h>
  34. #include <bits/long-double.h>
  35. /* This header should be included at the bottom of each bits/floatn.h.
  36. It defines the following macros for each _FloatN and _FloatNx type,
  37. where the same definitions, or definitions based only on the macros
  38. in bits/floatn.h, are appropriate for all glibc configurations. */
  39. /* Defined to 1 if the current compiler invocation provides a
  40. floating-point type with the right format for this type, and this
  41. glibc includes corresponding *fN or *fNx interfaces for it. */
  42. #define __HAVE_FLOAT16 0
  43. #define __HAVE_FLOAT32 1
  44. #define __HAVE_FLOAT64 1
  45. #define __HAVE_FLOAT32X 1
  46. #define __HAVE_FLOAT128X 0
  47. /* Defined to 1 if the corresponding __HAVE_<type> macro is 1 and the
  48. type is the first with its format in the sequence of (the default
  49. choices for) float, double, long double, _Float16, _Float32,
  50. _Float64, _Float128, _Float32x, _Float64x, _Float128x for this
  51. glibc; that is, if functions present once per floating-point format
  52. rather than once per type are present for this type.
  53. All configurations supported by glibc have _Float32 the same format
  54. as float, _Float64 and _Float32x the same format as double, the
  55. _Float64x the same format as either long double or _Float128. No
  56. configurations support _Float128x or, as of GCC 7, have compiler
  57. support for a type meeting the requirements for _Float128x. */
  58. #define __HAVE_DISTINCT_FLOAT16 __HAVE_FLOAT16
  59. #define __HAVE_DISTINCT_FLOAT32 0
  60. #define __HAVE_DISTINCT_FLOAT64 0
  61. #define __HAVE_DISTINCT_FLOAT32X 0
  62. #define __HAVE_DISTINCT_FLOAT64X 0
  63. #define __HAVE_DISTINCT_FLOAT128X __HAVE_FLOAT128X
  64. /* Defined to 1 if the corresponding _FloatN type is not binary compatible
  65. with the corresponding ISO C type in the current compilation unit as
  66. opposed to __HAVE_DISTINCT_FLOATN, which indicates the default types built
  67. in glibc. */
  68. #define __HAVE_FLOAT128_UNLIKE_LDBL (__HAVE_DISTINCT_FLOAT128 \
  69. && __LDBL_MANT_DIG__ != 113)
  70. /* Defined to 1 if any _FloatN or _FloatNx types that are not
  71. ABI-distinct are however distinct types at the C language level (so
  72. for the purposes of __builtin_types_compatible_p and _Generic). */
  73. #if __GNUC_PREREQ (7, 0) && !defined __cplusplus
  74. # define __HAVE_FLOATN_NOT_TYPEDEF 1
  75. #else
  76. # define __HAVE_FLOATN_NOT_TYPEDEF 0
  77. #endif
  78. #ifndef __ASSEMBLER__
  79. /* Defined to concatenate the literal suffix to be used with _FloatN
  80. or _FloatNx types, if __HAVE_<type> is 1. The corresponding
  81. literal suffixes exist since GCC 7, for C only. */
  82. # if __HAVE_FLOAT16
  83. # if !__GNUC_PREREQ (7, 0) || (defined __cplusplus && !__GNUC_PREREQ (13, 0))
  84. /* No corresponding suffix available for this type. */
  85. # define __f16(x) ((_Float16) x##f)
  86. # else
  87. # define __f16(x) x##f16
  88. # endif
  89. # endif
  90. # if __HAVE_FLOAT32
  91. # if !__GNUC_PREREQ (7, 0) || (defined __cplusplus && !__GNUC_PREREQ (13, 0))
  92. # define __f32(x) x##f
  93. # else
  94. # define __f32(x) x##f32
  95. # endif
  96. # endif
  97. # if __HAVE_FLOAT64
  98. # if !__GNUC_PREREQ (7, 0) || (defined __cplusplus && !__GNUC_PREREQ (13, 0))
  99. # ifdef __NO_LONG_DOUBLE_MATH
  100. # define __f64(x) x##l
  101. # else
  102. # define __f64(x) x
  103. # endif
  104. # else
  105. # define __f64(x) x##f64
  106. # endif
  107. # endif
  108. # if __HAVE_FLOAT32X
  109. # if !__GNUC_PREREQ (7, 0) || (defined __cplusplus && !__GNUC_PREREQ (13, 0))
  110. # define __f32x(x) x
  111. # else
  112. # define __f32x(x) x##f32x
  113. # endif
  114. # endif
  115. # if __HAVE_FLOAT64X
  116. # if !__GNUC_PREREQ (7, 0) || (defined __cplusplus && !__GNUC_PREREQ (13, 0))
  117. # if __HAVE_FLOAT64X_LONG_DOUBLE
  118. # define __f64x(x) x##l
  119. # else
  120. # define __f64x(x) __f128 (x)
  121. # endif
  122. # else
  123. # define __f64x(x) x##f64x
  124. # endif
  125. # endif
  126. # if __HAVE_FLOAT128X
  127. # if !__GNUC_PREREQ (7, 0) || (defined __cplusplus && !__GNUC_PREREQ (13, 0))
  128. # error "_Float128X supported but no constant suffix"
  129. # else
  130. # define __f128x(x) x##f128x
  131. # endif
  132. # endif
  133. /* Defined to a complex type if __HAVE_<type> is 1. */
  134. # if __HAVE_FLOAT16
  135. # if !__GNUC_PREREQ (7, 0) || (defined __cplusplus && !__GNUC_PREREQ (13, 0))
  136. typedef _Complex float __cfloat16 __attribute__ ((__mode__ (__HC__)));
  137. # define __CFLOAT16 __cfloat16
  138. # else
  139. # define __CFLOAT16 _Complex _Float16
  140. # endif
  141. # endif
  142. # if __HAVE_FLOAT32
  143. # if !__GNUC_PREREQ (7, 0) || (defined __cplusplus && !__GNUC_PREREQ (13, 0))
  144. # define __CFLOAT32 _Complex float
  145. # else
  146. # define __CFLOAT32 _Complex _Float32
  147. # endif
  148. # endif
  149. # if __HAVE_FLOAT64
  150. # if !__GNUC_PREREQ (7, 0) || (defined __cplusplus && !__GNUC_PREREQ (13, 0))
  151. # ifdef __NO_LONG_DOUBLE_MATH
  152. # define __CFLOAT64 _Complex long double
  153. # else
  154. # define __CFLOAT64 _Complex double
  155. # endif
  156. # else
  157. # define __CFLOAT64 _Complex _Float64
  158. # endif
  159. # endif
  160. # if __HAVE_FLOAT32X
  161. # if !__GNUC_PREREQ (7, 0) || (defined __cplusplus && !__GNUC_PREREQ (13, 0))
  162. # define __CFLOAT32X _Complex double
  163. # else
  164. # define __CFLOAT32X _Complex _Float32x
  165. # endif
  166. # endif
  167. # if __HAVE_FLOAT64X
  168. # if !__GNUC_PREREQ (7, 0) || (defined __cplusplus && !__GNUC_PREREQ (13, 0))
  169. # if __HAVE_FLOAT64X_LONG_DOUBLE
  170. # define __CFLOAT64X _Complex long double
  171. # else
  172. # define __CFLOAT64X __CFLOAT128
  173. # endif
  174. # else
  175. # define __CFLOAT64X _Complex _Float64x
  176. # endif
  177. # endif
  178. # if __HAVE_FLOAT128X
  179. # if !__GNUC_PREREQ (7, 0) || (defined __cplusplus && !__GNUC_PREREQ (13, 0))
  180. # error "_Float128X supported but no complex type"
  181. # else
  182. # define __CFLOAT128X _Complex _Float128x
  183. # endif
  184. # endif
  185. /* The remaining of this file provides support for older compilers. */
  186. # if __HAVE_FLOAT16
  187. # if !__GNUC_PREREQ (7, 0) || (defined __cplusplus && !__GNUC_PREREQ (13, 0))
  188. typedef float _Float16 __attribute__ ((__mode__ (__HF__)));
  189. # endif
  190. # if !__GNUC_PREREQ (7, 0)
  191. # define __builtin_huge_valf16() ((_Float16) __builtin_huge_val ())
  192. # define __builtin_inff16() ((_Float16) __builtin_inf ())
  193. # define __builtin_nanf16(x) ((_Float16) __builtin_nan (x))
  194. # define __builtin_nansf16(x) ((_Float16) __builtin_nans (x))
  195. # endif
  196. # endif
  197. # if __HAVE_FLOAT32
  198. # if !__GNUC_PREREQ (7, 0) || (defined __cplusplus && !__GNUC_PREREQ (13, 0))
  199. typedef float _Float32;
  200. # endif
  201. # if !__GNUC_PREREQ (7, 0)
  202. # define __builtin_huge_valf32() (__builtin_huge_valf ())
  203. # define __builtin_inff32() (__builtin_inff ())
  204. # define __builtin_nanf32(x) (__builtin_nanf (x))
  205. # define __builtin_nansf32(x) (__builtin_nansf (x))
  206. # endif
  207. # endif
  208. # if __HAVE_FLOAT64
  209. /* If double, long double and _Float64 all have the same set of
  210. values, TS 18661-3 requires the usual arithmetic conversions on
  211. long double and _Float64 to produce _Float64. For this to be the
  212. case when building with a compiler without a distinct _Float64
  213. type, _Float64 must be a typedef for long double, not for
  214. double. */
  215. # ifdef __NO_LONG_DOUBLE_MATH
  216. # if !__GNUC_PREREQ (7, 0) || (defined __cplusplus && !__GNUC_PREREQ (13, 0))
  217. typedef long double _Float64;
  218. # endif
  219. # if !__GNUC_PREREQ (7, 0)
  220. # define __builtin_huge_valf64() (__builtin_huge_vall ())
  221. # define __builtin_inff64() (__builtin_infl ())
  222. # define __builtin_nanf64(x) (__builtin_nanl (x))
  223. # define __builtin_nansf64(x) (__builtin_nansl (x))
  224. # endif
  225. # else
  226. # if !__GNUC_PREREQ (7, 0) || (defined __cplusplus && !__GNUC_PREREQ (13, 0))
  227. typedef double _Float64;
  228. # endif
  229. # if !__GNUC_PREREQ (7, 0)
  230. # define __builtin_huge_valf64() (__builtin_huge_val ())
  231. # define __builtin_inff64() (__builtin_inf ())
  232. # define __builtin_nanf64(x) (__builtin_nan (x))
  233. # define __builtin_nansf64(x) (__builtin_nans (x))
  234. # endif
  235. # endif
  236. # endif
  237. # if __HAVE_FLOAT32X
  238. # if !__GNUC_PREREQ (7, 0) || (defined __cplusplus && !__GNUC_PREREQ (13, 0))
  239. typedef double _Float32x;
  240. # endif
  241. # if !__GNUC_PREREQ (7, 0)
  242. # define __builtin_huge_valf32x() (__builtin_huge_val ())
  243. # define __builtin_inff32x() (__builtin_inf ())
  244. # define __builtin_nanf32x(x) (__builtin_nan (x))
  245. # define __builtin_nansf32x(x) (__builtin_nans (x))
  246. # endif
  247. # endif
  248. # if __HAVE_FLOAT64X
  249. # if __HAVE_FLOAT64X_LONG_DOUBLE
  250. # if !__GNUC_PREREQ (7, 0) || (defined __cplusplus && !__GNUC_PREREQ (13, 0))
  251. typedef long double _Float64x;
  252. # endif
  253. # if !__GNUC_PREREQ (7, 0)
  254. # define __builtin_huge_valf64x() (__builtin_huge_vall ())
  255. # define __builtin_inff64x() (__builtin_infl ())
  256. # define __builtin_nanf64x(x) (__builtin_nanl (x))
  257. # define __builtin_nansf64x(x) (__builtin_nansl (x))
  258. # endif
  259. # else
  260. # if !__GNUC_PREREQ (7, 0) || (defined __cplusplus && !__GNUC_PREREQ (13, 0))
  261. typedef _Float128 _Float64x;
  262. # endif
  263. # if !__GNUC_PREREQ (7, 0)
  264. # define __builtin_huge_valf64x() (__builtin_huge_valf128 ())
  265. # define __builtin_inff64x() (__builtin_inff128 ())
  266. # define __builtin_nanf64x(x) (__builtin_nanf128 (x))
  267. # define __builtin_nansf64x(x) (__builtin_nansf128 (x))
  268. # endif
  269. # endif
  270. # endif
  271. # if __HAVE_FLOAT128X
  272. # if !__GNUC_PREREQ (7, 0) || (defined __cplusplus && !__GNUC_PREREQ (13, 0))
  273. # error "_Float128x supported but no type"
  274. # endif
  275. # if !__GNUC_PREREQ (7, 0)
  276. # define __builtin_huge_valf128x() ((_Float128x) __builtin_huge_val ())
  277. # define __builtin_inff128x() ((_Float128x) __builtin_inf ())
  278. # define __builtin_nanf128x(x) ((_Float128x) __builtin_nan (x))
  279. # define __builtin_nansf128x(x) ((_Float128x) __builtin_nans (x))
  280. # endif
  281. # endif
  282. #endif /* !__ASSEMBLER__. */
  283. #endif /* _BITS_FLOATN_COMMON_H */