X-Git-Url: https://git.distorted.org.uk/~mdw/catacomb/blobdiff_plain/aa02ed367404c659ab7205ef9662ef92032d7786..2302cd8615b01bda84e68621ab17c2bfbf4286b9:/math/mpint.h diff --git a/math/mpint.h b/math/mpint.h index f551980a..637ec10e 100644 --- a/math/mpint.h +++ b/math/mpint.h @@ -36,6 +36,8 @@ #include +#include + #ifndef CATACOMB_MP_H # include "mp.h" #endif @@ -46,21 +48,9 @@ * * GCC (at least) isn't clever enough to work out that the division in * @MP_FROMINT@ is actually safe (since it will only be executed if @_i > - * MPW_MAX@, which would prove that @(type)MPW_MAX + 1 != 0@). So here's - * some machinery to shut it up. + * MPW_MAX@, which would prove that @(type)MPW_MAX + 1 != 0@). */ -#if __GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) -# define MP_FROMINT_MUFFLE_WARNING(x) do { \ - _Pragma("GCC diagnostic push") \ - _Pragma("GCC diagnostic ignored \"-Wdiv-by-zero\"") \ - x \ - _Pragma("GCC diagnostic pop") \ - } while (0) -#else -# define MP_FROMINT_MUFFLE_WARNING(x) do { x } while (0) -#endif - /* --- @MP_FROMINT@ --- * * * Arguments: @d@ = destination multiprecision integer @@ -90,7 +80,9 @@ if (_i <= MPW_MAX) \ break; \ else \ - MP_FROMINT_MUFFLE_WARNING({ _i /= (type)MPW_MAX + 1; }); \ + MUFFLE_WARNINGS_STMT(GCC_WARNING("-Wdiv-by-zero"), { \ + _i /= (type)MPW_MAX + 1; \ + }); \ } \ } else { \ _d->f |= MP_NEG; \ @@ -103,7 +95,9 @@ if (_i >= -MPW_MAX) \ break; \ else \ - MP_FROMINT_MUFFLE_WARNING({ _i /= (type)MPW_MAX + 1; }); \ + MUFFLE_WARNINGS_STMT(GCC_WARNING("-Wdiv-by-zero"), { \ + _i /= (type)MPW_MAX + 1; \ + }); \ } \ } \ \ @@ -151,6 +145,17 @@ /*----- Functions provided ------------------------------------------------*/ +/* --- Build up the list of conversions to be supplied --- */ + +#define MPINT_CONVERSIONS(_) \ + _(short, short, SHRT_MAX) \ + _(ushort, unsigned short, USHRT_MAX) \ + _(int, int, INT_MAX) \ + _(uint, unsigned, UINT_MAX) \ + _(long, long, LONG_MAX) \ + _(ulong, unsigned long, ULONG_MAX) \ + _(uint32, uint32, MASK32) + /* --- @mp_fromINT@ --- * * * Arguments: @mp *d@ = pointer to destination multiprecision integer @@ -161,17 +166,9 @@ * Use: Converts a standard C integer to a multiprecision integer. */ -#define mp_fromINT(name, type) \ - extern mp *mp_from##name(mp */*d*/, type /*i*/) - -mp_fromINT(short, short); -mp_fromINT(ushort, unsigned short); -mp_fromINT(int, int); -mp_fromINT(uint, unsigned); -mp_fromINT(uint32, uint32); -mp_fromINT(long, long); -mp_fromINT(ulong, unsigned long); - +#define mp_fromINT(name, type, max) \ + extern mp *mp_from##name(mp */*d*/, type /*i*/); +MPINT_CONVERSIONS(mp_fromINT) #undef mp_fromINT /* --- @mp_toINT@ --- * @@ -187,17 +184,9 @@ mp_fromINT(ulong, unsigned long); * type is signed, the behaviour is undefined. */ -#define mp_toINT(name, type) \ - extern type mp_to##name(const mp */*m*/) - -mp_toINT(short, short); -mp_toINT(ushort, unsigned short); -mp_toINT(int, int); -mp_toINT(uint, unsigned); -mp_toINT(uint32, uint32); -mp_toINT(long, long); -mp_toINT(ulong, unsigned long); - +#define mp_toINT(name, type, max) \ + extern type mp_to##name(const mp */*m*/); +MPINT_CONVERSIONS(mp_toINT) #undef mp_toINT /*----- That's all, folks -------------------------------------------------*/