math-narrow.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /* Helper macros for functions returning a narrower type.
  2. Copyright (C) 2018-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifndef _MATH_NARROW_H
  16. #define _MATH_NARROW_H 1
  17. #include <bits/floatn.h>
  18. #include <bits/long-double.h>
  19. #include <errno.h>
  20. #include <fenv.h>
  21. #include <ieee754.h>
  22. #include <math-barriers.h>
  23. #include <math_private.h>
  24. #include <fenv_private.h>
  25. /* Carry out a computation using round-to-odd. The computation is
  26. EXPR; the union type in which to store the result is UNION and the
  27. subfield of the "ieee" field of that union with the low part of the
  28. mantissa is MANTISSA; SUFFIX is the suffix for the libc_fe* macros
  29. to ensure that the correct rounding mode is used, for platforms
  30. with multiple rounding modes where those macros set only the
  31. relevant mode. This macro does not work correctly if the sign of
  32. an exact zero result depends on the rounding mode, so that case
  33. must be checked for separately. */
  34. #define ROUND_TO_ODD(EXPR, UNION, SUFFIX, MANTISSA) \
  35. ({ \
  36. fenv_t env; \
  37. UNION u; \
  38. \
  39. libc_feholdexcept_setround ## SUFFIX (&env, FE_TOWARDZERO); \
  40. u.d = (EXPR); \
  41. math_force_eval (u.d); \
  42. u.ieee.MANTISSA \
  43. |= libc_feupdateenv_test ## SUFFIX (&env, FE_INEXACT) != 0; \
  44. \
  45. u.d; \
  46. })
  47. /* Check for error conditions from a narrowing add function returning
  48. RET with arguments X and Y and set errno as needed. Overflow and
  49. underflow can occur for finite arguments and a domain error for
  50. infinite ones. */
  51. #define CHECK_NARROW_ADD(RET, X, Y) \
  52. do \
  53. { \
  54. if (!isfinite (RET)) \
  55. { \
  56. if (isnan (RET)) \
  57. { \
  58. if (!isnan (X) && !isnan (Y)) \
  59. __set_errno (EDOM); \
  60. } \
  61. else if (isfinite (X) && isfinite (Y)) \
  62. __set_errno (ERANGE); \
  63. } \
  64. else if ((RET) == 0 && (X) != -(Y)) \
  65. __set_errno (ERANGE); \
  66. } \
  67. while (0)
  68. /* Implement narrowing add using round-to-odd. The arguments are X
  69. and Y, the return type is TYPE and UNION, MANTISSA and SUFFIX are
  70. as for ROUND_TO_ODD. */
  71. #define NARROW_ADD_ROUND_TO_ODD(X, Y, TYPE, UNION, SUFFIX, MANTISSA) \
  72. do \
  73. { \
  74. TYPE ret; \
  75. \
  76. /* Ensure a zero result is computed in the original rounding \
  77. mode. */ \
  78. if ((X) == -(Y)) \
  79. ret = (TYPE) ((X) + (Y)); \
  80. else \
  81. ret = (TYPE) ROUND_TO_ODD (math_opt_barrier (X) + (Y), \
  82. UNION, SUFFIX, MANTISSA); \
  83. \
  84. CHECK_NARROW_ADD (ret, (X), (Y)); \
  85. return ret; \
  86. } \
  87. while (0)
  88. /* Implement a narrowing add function that is not actually narrowing
  89. or where no attempt is made to be correctly rounding (the latter
  90. only applies to IBM long double). The arguments are X and Y and
  91. the return type is TYPE. */
  92. #define NARROW_ADD_TRIVIAL(X, Y, TYPE) \
  93. do \
  94. { \
  95. TYPE ret; \
  96. \
  97. ret = (TYPE) ((X) + (Y)); \
  98. CHECK_NARROW_ADD (ret, (X), (Y)); \
  99. return ret; \
  100. } \
  101. while (0)
  102. /* Check for error conditions from a narrowing subtract function
  103. returning RET with arguments X and Y and set errno as needed.
  104. Overflow and underflow can occur for finite arguments and a domain
  105. error for infinite ones. */
  106. #define CHECK_NARROW_SUB(RET, X, Y) \
  107. do \
  108. { \
  109. if (!isfinite (RET)) \
  110. { \
  111. if (isnan (RET)) \
  112. { \
  113. if (!isnan (X) && !isnan (Y)) \
  114. __set_errno (EDOM); \
  115. } \
  116. else if (isfinite (X) && isfinite (Y)) \
  117. __set_errno (ERANGE); \
  118. } \
  119. else if ((RET) == 0 && (X) != (Y)) \
  120. __set_errno (ERANGE); \
  121. } \
  122. while (0)
  123. /* Implement narrowing subtract using round-to-odd. The arguments are
  124. X and Y, the return type is TYPE and UNION, MANTISSA and SUFFIX are
  125. as for ROUND_TO_ODD. */
  126. #define NARROW_SUB_ROUND_TO_ODD(X, Y, TYPE, UNION, SUFFIX, MANTISSA) \
  127. do \
  128. { \
  129. TYPE ret; \
  130. \
  131. /* Ensure a zero result is computed in the original rounding \
  132. mode. */ \
  133. if ((X) == (Y)) \
  134. ret = (TYPE) ((X) - (Y)); \
  135. else \
  136. ret = (TYPE) ROUND_TO_ODD (math_opt_barrier (X) - (Y), \
  137. UNION, SUFFIX, MANTISSA); \
  138. \
  139. CHECK_NARROW_SUB (ret, (X), (Y)); \
  140. return ret; \
  141. } \
  142. while (0)
  143. /* Implement a narrowing subtract function that is not actually
  144. narrowing or where no attempt is made to be correctly rounding (the
  145. latter only applies to IBM long double). The arguments are X and Y
  146. and the return type is TYPE. */
  147. #define NARROW_SUB_TRIVIAL(X, Y, TYPE) \
  148. do \
  149. { \
  150. TYPE ret; \
  151. \
  152. ret = (TYPE) ((X) - (Y)); \
  153. CHECK_NARROW_SUB (ret, (X), (Y)); \
  154. return ret; \
  155. } \
  156. while (0)
  157. /* Check for error conditions from a narrowing multiply function
  158. returning RET with arguments X and Y and set errno as needed.
  159. Overflow and underflow can occur for finite arguments and a domain
  160. error for Inf * 0. */
  161. #define CHECK_NARROW_MUL(RET, X, Y) \
  162. do \
  163. { \
  164. if (!isfinite (RET)) \
  165. { \
  166. if (isnan (RET)) \
  167. { \
  168. if (!isnan (X) && !isnan (Y)) \
  169. __set_errno (EDOM); \
  170. } \
  171. else if (isfinite (X) && isfinite (Y)) \
  172. __set_errno (ERANGE); \
  173. } \
  174. else if ((RET) == 0 && (X) != 0 && (Y) != 0) \
  175. __set_errno (ERANGE); \
  176. } \
  177. while (0)
  178. /* Implement narrowing multiply using round-to-odd. The arguments are
  179. X and Y, the return type is TYPE and UNION, MANTISSA and SUFFIX are
  180. as for ROUND_TO_ODD. */
  181. #define NARROW_MUL_ROUND_TO_ODD(X, Y, TYPE, UNION, SUFFIX, MANTISSA) \
  182. do \
  183. { \
  184. TYPE ret; \
  185. \
  186. ret = (TYPE) ROUND_TO_ODD (math_opt_barrier (X) * (Y), \
  187. UNION, SUFFIX, MANTISSA); \
  188. \
  189. CHECK_NARROW_MUL (ret, (X), (Y)); \
  190. return ret; \
  191. } \
  192. while (0)
  193. /* Implement a narrowing multiply function that is not actually
  194. narrowing or where no attempt is made to be correctly rounding (the
  195. latter only applies to IBM long double). The arguments are X and Y
  196. and the return type is TYPE. */
  197. #define NARROW_MUL_TRIVIAL(X, Y, TYPE) \
  198. do \
  199. { \
  200. TYPE ret; \
  201. \
  202. ret = (TYPE) ((X) * (Y)); \
  203. CHECK_NARROW_MUL (ret, (X), (Y)); \
  204. return ret; \
  205. } \
  206. while (0)
  207. /* Check for error conditions from a narrowing divide function
  208. returning RET with arguments X and Y and set errno as needed.
  209. Overflow, underflow and divide-by-zero can occur for finite
  210. arguments and a domain error for Inf / Inf and 0 / 0. */
  211. #define CHECK_NARROW_DIV(RET, X, Y) \
  212. do \
  213. { \
  214. if (!isfinite (RET)) \
  215. { \
  216. if (isnan (RET)) \
  217. { \
  218. if (!isnan (X) && !isnan (Y)) \
  219. __set_errno (EDOM); \
  220. } \
  221. else if (isfinite (X)) \
  222. __set_errno (ERANGE); \
  223. } \
  224. else if ((RET) == 0 && (X) != 0 && !isinf (Y)) \
  225. __set_errno (ERANGE); \
  226. } \
  227. while (0)
  228. /* Implement narrowing divide using round-to-odd. The arguments are
  229. X and Y, the return type is TYPE and UNION, MANTISSA and SUFFIX are
  230. as for ROUND_TO_ODD. */
  231. #define NARROW_DIV_ROUND_TO_ODD(X, Y, TYPE, UNION, SUFFIX, MANTISSA) \
  232. do \
  233. { \
  234. TYPE ret; \
  235. \
  236. ret = (TYPE) ROUND_TO_ODD (math_opt_barrier (X) / (Y), \
  237. UNION, SUFFIX, MANTISSA); \
  238. \
  239. CHECK_NARROW_DIV (ret, (X), (Y)); \
  240. return ret; \
  241. } \
  242. while (0)
  243. /* Implement a narrowing divide function that is not actually
  244. narrowing or where no attempt is made to be correctly rounding (the
  245. latter only applies to IBM long double). The arguments are X and Y
  246. and the return type is TYPE. */
  247. #define NARROW_DIV_TRIVIAL(X, Y, TYPE) \
  248. do \
  249. { \
  250. TYPE ret; \
  251. \
  252. ret = (TYPE) ((X) / (Y)); \
  253. CHECK_NARROW_DIV (ret, (X), (Y)); \
  254. return ret; \
  255. } \
  256. while (0)
  257. /* The following macros declare aliases for a narrowing function. The
  258. sole argument is the base name of a family of functions, such as
  259. "add". If any platform changes long double format after the
  260. introduction of narrowing functions, in a way requiring symbol
  261. versioning compatibility, additional variants of these macros will
  262. be needed. */
  263. #define libm_alias_float_double_main(func) \
  264. weak_alias (__f ## func, f ## func) \
  265. weak_alias (__f ## func, f32 ## func ## f64) \
  266. weak_alias (__f ## func, f32 ## func ## f32x)
  267. #ifdef NO_LONG_DOUBLE
  268. # define libm_alias_float_double(func) \
  269. libm_alias_float_double_main (func) \
  270. weak_alias (__f ## func, f ## func ## l)
  271. #else
  272. # define libm_alias_float_double(func) \
  273. libm_alias_float_double_main (func)
  274. #endif
  275. #define libm_alias_float32x_float64_main(func) \
  276. weak_alias (__f32x ## func ## f64, f32x ## func ## f64)
  277. #ifdef NO_LONG_DOUBLE
  278. # define libm_alias_float32x_float64(func) \
  279. libm_alias_float32x_float64_main (func) \
  280. weak_alias (__f32x ## func ## f64, d ## func ## l)
  281. #elif defined __LONG_DOUBLE_MATH_OPTIONAL
  282. # define libm_alias_float32x_float64(func) \
  283. libm_alias_float32x_float64_main (func) \
  284. weak_alias (__f32x ## func ## f64, __nldbl_d ## func ## l)
  285. #else
  286. # define libm_alias_float32x_float64(func) \
  287. libm_alias_float32x_float64_main (func)
  288. #endif
  289. #if __HAVE_FLOAT128 && !__HAVE_DISTINCT_FLOAT128
  290. # define libm_alias_float_ldouble_f128(func) \
  291. weak_alias (__f ## func ## l, f32 ## func ## f128)
  292. # define libm_alias_double_ldouble_f128(func) \
  293. weak_alias (__d ## func ## l, f32x ## func ## f128) \
  294. weak_alias (__d ## func ## l, f64 ## func ## f128)
  295. #else
  296. # define libm_alias_float_ldouble_f128(func)
  297. # define libm_alias_double_ldouble_f128(func)
  298. #endif
  299. #if __HAVE_FLOAT64X_LONG_DOUBLE
  300. # define libm_alias_float_ldouble_f64x(func) \
  301. weak_alias (__f ## func ## l, f32 ## func ## f64x)
  302. # define libm_alias_double_ldouble_f64x(func) \
  303. weak_alias (__d ## func ## l, f32x ## func ## f64x) \
  304. weak_alias (__d ## func ## l, f64 ## func ## f64x)
  305. #else
  306. # define libm_alias_float_ldouble_f64x(func)
  307. # define libm_alias_double_ldouble_f64x(func)
  308. #endif
  309. #define libm_alias_float_ldouble(func) \
  310. weak_alias (__f ## func ## l, f ## func ## l) \
  311. libm_alias_float_ldouble_f128 (func) \
  312. libm_alias_float_ldouble_f64x (func)
  313. #define libm_alias_double_ldouble(func) \
  314. weak_alias (__d ## func ## l, d ## func ## l) \
  315. libm_alias_double_ldouble_f128 (func) \
  316. libm_alias_double_ldouble_f64x (func)
  317. #define libm_alias_float64x_float128(func) \
  318. weak_alias (__f64x ## func ## f128, f64x ## func ## f128)
  319. #define libm_alias_float32_float128_main(func) \
  320. weak_alias (__f32 ## func ## f128, f32 ## func ## f128)
  321. #define libm_alias_float64_float128_main(func) \
  322. weak_alias (__f64 ## func ## f128, f64 ## func ## f128) \
  323. weak_alias (__f64 ## func ## f128, f32x ## func ## f128)
  324. #if __HAVE_FLOAT64X_LONG_DOUBLE
  325. # define libm_alias_float32_float128(func) \
  326. libm_alias_float32_float128_main (func)
  327. # define libm_alias_float64_float128(func) \
  328. libm_alias_float64_float128_main (func)
  329. #else
  330. # define libm_alias_float32_float128(func) \
  331. libm_alias_float32_float128_main (func) \
  332. weak_alias (__f32 ## func ## f128, f32 ## func ## f64x)
  333. # define libm_alias_float64_float128(func) \
  334. libm_alias_float64_float128_main (func) \
  335. weak_alias (__f64 ## func ## f128, f64 ## func ## f64x) \
  336. weak_alias (__f64 ## func ## f128, f32x ## func ## f64x)
  337. #endif
  338. #endif /* math-narrow.h. */