1
0

0003-Provide-a-C-version-of-issignaling-that-does-not-use.patch 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. From 9bcf391a21677c6d5fa1c2be71554ec181e24f39 Mon Sep 17 00:00:00 2001
  2. From: "Gabriel F. T. Gomes" <gftg@linux.vnet.ibm.com>
  3. Date: Mon, 14 Aug 2017 13:46:15 -0300
  4. Subject: [PATCH] Provide a C++ version of issignaling that does not use
  5. __MATH_TG
  6. The macro __MATH_TG contains the logic to select between long double and
  7. _Float128, when these types are ABI-distinct. This logic relies on
  8. __builtin_types_compatible_p, which is not available in C++ mode.
  9. On the other hand, C++ function overloading provides the means to
  10. distinguish between the floating-point types. The overloading
  11. resolution will match the correct parameter regardless of type
  12. qualifiers, i.e.: const and volatile.
  13. Tested for powerpc64le, s390x, and x86_64.
  14. * math/math.h [defined __cplusplus] (issignaling): Provide a C++
  15. definition for issignaling that does not rely on __MATH_TG,
  16. since __MATH_TG uses __builtin_types_compatible_p, which is only
  17. available in C mode.
  18. (CFLAGS-test-math-issignaling.cc): New variable.
  19. * math/Makefile [CXX] (tests): Add test-math-issignaling.
  20. * math/test-math-issignaling.cc: New test for C++ implementation
  21. of type-generic issignaling.
  22. * sysdeps/powerpc/powerpc64le/Makefile [subdir == math]
  23. (CXXFLAGS-test-math-issignaling.cc): Add -mfloat128 to the build
  24. options of test-math-issignaling on powerpc64le.
  25. (cherry picked from commit a16e8bc08edca84d507715c66d6cddbbc7ed3b62)
  26. [Romain rebase on glibc 2.26]
  27. Signed-off-by: Romain Naour <romain.naour@gmail.com>
  28. ---
  29. math/Makefile | 3 +-
  30. math/math.h | 19 +++++-
  31. math/test-math-issignaling.cc | 113 +++++++++++++++++++++++++++++++++++
  32. sysdeps/powerpc/powerpc64le/Makefile | 1 +
  33. 4 files changed, 134 insertions(+), 2 deletions(-)
  34. create mode 100644 math/test-math-issignaling.cc
  35. diff --git a/math/Makefile b/math/Makefile
  36. index e09b0c0..0130fcf 100644
  37. --- a/math/Makefile
  38. +++ b/math/Makefile
  39. @@ -203,7 +203,7 @@ tests-static = test-fpucw-static test-fpucw-ieee-static \
  40. test-signgam-ullong-static test-signgam-ullong-init-static
  41. ifneq (,$(CXX))
  42. -tests += test-math-isinff test-math-iszero
  43. +tests += test-math-isinff test-math-iszero test-math-issignaling
  44. endif
  45. ifneq (no,$(PERL))
  46. @@ -350,6 +350,7 @@ CFLAGS-test-signgam-ullong-init-static.c = -std=c99
  47. CFLAGS-test-math-isinff.cc = -std=gnu++11
  48. CFLAGS-test-math-iszero.cc = -std=gnu++11
  49. +CFLAGS-test-math-issignaling.cc = -std=gnu++11
  50. CFLAGS-test-iszero-excess-precision.c = -fexcess-precision=standard
  51. CFLAGS-test-iseqsig-excess-precision.c = -fexcess-precision=standard
  52. diff --git a/math/math.h b/math/math.h
  53. index dea8dbe..add86af 100644
  54. --- a/math/math.h
  55. +++ b/math/math.h
  56. @@ -474,7 +474,24 @@ enum
  57. # include <bits/iscanonical.h>
  58. /* Return nonzero value if X is a signaling NaN. */
  59. -# define issignaling(x) __MATH_TG ((x), __issignaling, (x))
  60. +# ifndef __cplusplus
  61. +# define issignaling(x) __MATH_TG ((x), __issignaling, (x))
  62. +# else
  63. + /* In C++ mode, __MATH_TG cannot be used, because it relies on
  64. + __builtin_types_compatible_p, which is a C-only builtin. On the
  65. + other hand, overloading provides the means to distinguish between
  66. + the floating-point types. The overloading resolution will match
  67. + the correct parameter (regardless of type qualifiers (i.e.: const
  68. + and volatile). */
  69. +extern "C++" {
  70. +inline int issignaling (float __val) { return __issignalingf (__val); }
  71. +inline int issignaling (double __val) { return __issignaling (__val); }
  72. +inline int issignaling (long double __val) { return __issignalingl (__val); }
  73. +# if __HAVE_DISTINCT_FLOAT128
  74. +inline int issignaling (_Float128 __val) { return __issignalingf128 (__val); }
  75. +# endif
  76. +} /* extern C++ */
  77. +# endif
  78. /* Return nonzero value if X is subnormal. */
  79. # define issubnormal(x) (fpclassify (x) == FP_SUBNORMAL)
  80. diff --git a/math/test-math-issignaling.cc b/math/test-math-issignaling.cc
  81. new file mode 100644
  82. index 0000000..22ae9e1
  83. --- /dev/null
  84. +++ b/math/test-math-issignaling.cc
  85. @@ -0,0 +1,113 @@
  86. +/* Test for the C++ implementation of issignaling.
  87. + Copyright (C) 2017 Free Software Foundation, Inc.
  88. + This file is part of the GNU C Library.
  89. +
  90. + The GNU C Library is free software; you can redistribute it and/or
  91. + modify it under the terms of the GNU Lesser General Public
  92. + License as published by the Free Software Foundation; either
  93. + version 2.1 of the License, or (at your option) any later version.
  94. +
  95. + The GNU C Library is distributed in the hope that it will be useful,
  96. + but WITHOUT ANY WARRANTY; without even the implied warranty of
  97. + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  98. + Lesser General Public License for more details.
  99. +
  100. + You should have received a copy of the GNU Lesser General Public
  101. + License along with the GNU C Library; if not, see
  102. + <http://www.gnu.org/licenses/>. */
  103. +
  104. +#define _GNU_SOURCE 1
  105. +#include <math.h>
  106. +#include <stdio.h>
  107. +
  108. +#include <limits>
  109. +
  110. +/* There is no signaling_NaN for _Float128 in std::numeric_limits.
  111. + Include ieee754_float128.h and use the bitfields in the union
  112. + ieee854_float128.ieee_nan to build a signaling NaN. */
  113. +#if __HAVE_DISTINCT_FLOAT128
  114. +# include <ieee754_float128.h>
  115. +#endif
  116. +
  117. +static bool errors;
  118. +
  119. +static void
  120. +check (int actual, int expected, const char *actual_expr, int line)
  121. +{
  122. + if (actual != expected)
  123. + {
  124. + errors = true;
  125. + printf ("%s:%d: error: %s\n", __FILE__, line, actual_expr);
  126. + printf ("%s:%d: expected: %d\n", __FILE__, line, expected);
  127. + printf ("%s:%d: actual: %d\n", __FILE__, line, actual);
  128. + }
  129. +}
  130. +
  131. +#define CHECK(actual, expected) \
  132. + check ((actual), (expected), #actual, __LINE__)
  133. +
  134. +template <class T>
  135. +static void
  136. +check_type ()
  137. +{
  138. + typedef std::numeric_limits<T> limits;
  139. + CHECK (issignaling (T{0}), 0);
  140. + if (limits::has_infinity)
  141. + {
  142. + CHECK (issignaling (limits::infinity ()), 0);
  143. + CHECK (issignaling (-limits::infinity ()), 0);
  144. + }
  145. + if (limits::has_quiet_NaN)
  146. + CHECK (issignaling (limits::quiet_NaN ()), 0);
  147. + if (limits::has_signaling_NaN)
  148. + CHECK (issignaling (limits::signaling_NaN ()), 1);
  149. +}
  150. +
  151. +#if __HAVE_DISTINCT_FLOAT128
  152. +static void
  153. +check_float128 ()
  154. +{
  155. + ieee854_float128 q;
  156. +
  157. + q.d = 0;
  158. + CHECK (issignaling (q.d), 0);
  159. +
  160. + /* Infinity. */
  161. + q.ieee.negative = 0;
  162. + q.ieee.exponent = 0x7FFF;
  163. + q.ieee.mantissa0 = 0x0000;
  164. + q.ieee.mantissa1 = 0x00000000;
  165. + q.ieee.mantissa2 = 0x00000000;
  166. + q.ieee.mantissa3 = 0x00000000;
  167. + CHECK (issignaling (q.d), 0);
  168. +
  169. + /* Quiet NaN. */
  170. + q.ieee_nan.quiet_nan = 1;
  171. + q.ieee_nan.mantissa0 = 0x0000;
  172. + CHECK (issignaling (q.d), 0);
  173. +
  174. + /* Still a quiet NaN. */
  175. + q.ieee_nan.quiet_nan = 1;
  176. + q.ieee_nan.mantissa0 = 0x4000;
  177. + CHECK (issignaling (q.d), 0);
  178. +
  179. + /* Signaling NaN. */
  180. + q.ieee_nan.quiet_nan = 0;
  181. + q.ieee_nan.mantissa0 = 0x4000;
  182. + CHECK (issignaling (q.d), 1);
  183. +}
  184. +#endif
  185. +
  186. +static int
  187. +do_test (void)
  188. +{
  189. + check_type<float> ();
  190. + check_type<double> ();
  191. + check_type<long double> ();
  192. +#if __HAVE_DISTINCT_FLOAT128
  193. + check_float128 ();
  194. +#endif
  195. + return errors;
  196. +}
  197. +
  198. +#include <support/test-driver.c>
  199. diff --git a/sysdeps/powerpc/powerpc64le/Makefile b/sysdeps/powerpc/powerpc64le/Makefile
  200. index 77617b6..19adbfa 100644
  201. --- a/sysdeps/powerpc/powerpc64le/Makefile
  202. +++ b/sysdeps/powerpc/powerpc64le/Makefile
  203. @@ -16,6 +16,7 @@ $(foreach suf,$(all-object-suffixes),%f128_r$(suf)): CFLAGS += -mfloat128
  204. $(foreach suf,$(all-object-suffixes),$(objpfx)test-float128%$(suf)): CFLAGS += -mfloat128
  205. $(foreach suf,$(all-object-suffixes),$(objpfx)test-ifloat128%$(suf)): CFLAGS += -mfloat128
  206. CFLAGS-libm-test-support-float128.c += -mfloat128
  207. +CFLAGS-test-math-issignaling.cc += -mfloat128
  208. $(objpfx)test-float128% $(objpfx)test-ifloat128%: \
  209. gnulib-tests += $(f128-loader-link)
  210. endif
  211. --
  212. 2.9.5