Home My Page Projects MPFR
Summary Activity Forums Tracker Lists Tasks Docs News SCM Files

[#12964] Use of static assertion

Date:
2011-07-06 13:43
Priority:
3
State:
Closed
Submitted by:
Patrick PELISSIER (pphd)
Assigned to:
Nobody (None)
Category:
None
Target Version:
>3.2
Summary:
Use of static assertion

Detailed description
Static assertions can check conditions at compile time rather than at run time.
See http://en.wikipedia.org/wiki/Assertion_%28computing%29#Static_assertions

A way to implement then in MPFR can be :
#define MPFR_ASSERT_CAT(a,b) MPFR_ASSERT_CAT_(a,b)
#define MPFR_ASSERT_CAT_(a,b) a ## b
#define MPFR_STATIC_ASSERT(c) \
typedef enum { MPFR_ASSERT_CAT(MPFR_STATIC_ASSERT_CONST_,__LINE__) = !(c) } \
MPFR_ASSERT_CAT(MPFR_ASSERT_ ,__LINE__)[(c) ? 1 : -1]

This way should be compliant with the C89 standard and the C99 standard,
provided than no more than one static assertion is defined per line.

In particular, it could be pertinent to add them in init2.c :

/* Check if we can represent the number of limbs
* associated to the maximum of mpfr_prec_t*/
MPFR_STATIC_ASSERT( MP_SIZE_T_MAX >= (MPFR_PREC_MAX/BYTES_PER_MP_LIMB) );

/* Check for correct GMP_NUMB_BITS and BYTES_PER_MP_LIMB */
MPFR_STATIC_ASSERT( GMP_NUMB_BITS == BYTES_PER_MP_LIMB * CHAR_BIT
&& sizeof(mp_limb_t) == BYTES_PER_MP_LIMB );

MPFR_ASSERTN (mp_bits_per_limb == GMP_NUMB_BITS);

/* Check for correct EXP NAN, ZERO & INF in both mpfr.h and mpfr-impl.h */
MPFR_STATIC_ASSERT( __MPFR_EXP_NAN == MPFR_EXP_NAN );
MPFR_STATIC_ASSERT( __MPFR_EXP_ZERO == MPFR_EXP_ZERO );
MPFR_STATIC_ASSERT( __MPFR_EXP_INF == MPFR_EXP_INF );

MPFR_STATIC_ASSERT( MPFR_EMAX_MAX <= (MPFR_EXP_MAX >> 1) );
MPFR_STATIC_ASSERT( MPFR_EMIN_MIN >= -(MPFR_EXP_MAX >> 1) );

Followup

Message
Date: 2012-07-02 10:43
Sender: Patrick PELISSIER

Done in revision 8241

Attached Files:

Changes:

Field Old Value Date By
status_idOpen2012-07-02 10:43pphd
close_dateNone2012-07-02 10:43pphd