summaryrefslogtreecommitdiff
path: root/src/mpfr-impl.h
diff options
context:
space:
mode:
authorvlefevre <vlefevre@280ebfd0-de03-0410-8827-d642c229c3f4>2020-03-27 13:59:05 +0000
committervlefevre <vlefevre@280ebfd0-de03-0410-8827-d642c229c3f4>2020-03-27 13:59:05 +0000
commitc0cde50aadc3787cdf3f3a8c088a92cd54ec3976 (patch)
treeaf2b34c5e2589433a7b94dd035fb0f044682b06c /src/mpfr-impl.h
parent0c7823f97958454bf2b6c6aeaf0a13b128f683c9 (diff)
downloadmpfr-c0cde50aadc3787cdf3f3a8c088a92cd54ec3976.tar.gz
[src] Moved the definition of the numberof() macro from mpfr-gmp.h to
mpfr-impl.h in order to always use our own. Also make sure that the type is signed, so that the value can be used in arbitrary expressions without the risk of silently switching to unsigned arithmetic. git-svn-id: svn://scm.gforge.inria.fr/svn/mpfr/trunk@13830 280ebfd0-de03-0410-8827-d642c229c3f4
Diffstat (limited to 'src/mpfr-impl.h')
-rw-r--r--src/mpfr-impl.h27
1 files changed, 26 insertions, 1 deletions
diff --git a/src/mpfr-impl.h b/src/mpfr-impl.h
index 6e9658f6f..b190ec171 100644
--- a/src/mpfr-impl.h
+++ b/src/mpfr-impl.h
@@ -1620,10 +1620,35 @@ do { \
#define MPFR_THOUSANDS_SEPARATOR ((char) '\0')
#endif
-
/* Size of an array, as a constant expression. */
#define numberof_const(x) (sizeof (x) / sizeof ((x)[0]))
+/* Size of an array, safe version but not a constant expression:
+ Since an array can silently be converted to a pointer, we check
+ that this macro is applied on an array, not a pointer.
+ Also make sure that the type is signed ("long" is sufficient
+ in practice since the sizes come from the MPFR source), so that
+ the value can be used in arbitrary expressions without the risk
+ of silently switching to unsigned arithmetic. */
+#undef numberof
+#if 0
+/* The following should work with GCC as documented in its manual,
+ but fails: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=38377#c10
+ Thus disabled for now. */
+# define numberof(x) \
+ ( __extension__ ({ \
+ int is_array = (void *) &(x) == (void *) &(x)[0]; \
+ MPFR_STAT_STATIC_ASSERT (__builtin_constant_p (is_array) ? \
+ is_array : 1); \
+ MPFR_ASSERTN (is_array); \
+ (long) numberof_const (x); \
+ }) )
+#else
+# define numberof(x) \
+ (MPFR_ASSERTN ((void *) &(x) == (void *) &(x)[0]), \
+ (long) numberof_const (x))
+#endif
+
/* Addition with carry (detected by GCC and other good compilers). */
#define ADD_LIMB(u,v,c) ((u) += (v), (c) = (u) < (v))