From 2302cd8615b01bda84e68621ab17c2bfbf4286b9 Mon Sep 17 00:00:00 2001 From: Mark Wooding Date: Wed, 18 Mar 2015 18:59:28 +0000 Subject: [PATCH] math/mpint.[ch]: Consolidate the list of supplied conversions in header. Now there's only one source of ultimate conversion-related truth. --- math/mpint.c | 22 +++------------------- math/mpint.h | 39 +++++++++++++++++---------------------- 2 files changed, 20 insertions(+), 41 deletions(-) diff --git a/math/mpint.c b/math/mpint.c index d89fb078..3511f1ee 100644 --- a/math/mpint.c +++ b/math/mpint.c @@ -33,21 +33,12 @@ /* --- Conversion from C integers --- */ -#define FROM(name, type) \ +#define FROM(name, type, max) \ mp *mp_from##name(mp *d, type i) { \ MP_FROMINT(d, type, i); \ return (d); \ } - -FROM(short, short) -FROM(ushort, unsigned short) -FROM(int, int) -FROM(uint, unsigned) -FROM(uint32, uint32) -FROM(long, long) -FROM(ulong, unsigned long) - -#undef FROM +MPINT_CONVERSIONS(FROM) /* --- Conversion to C integers --- */ @@ -58,14 +49,7 @@ FROM(ulong, unsigned long) MP_TOINT(m, type, max, i); \ return (i); \ } - -TO(short, short, SHRT_MAX) -TO(ushort, unsigned short, USHRT_MAX) -TO(int, int, INT_MAX) -TO(uint, unsigned, UINT_MAX) -TO(uint32, uint32, 0xffffffff) -TO(long, long, LONG_MAX) -TO(ulong, unsigned long, ULONG_MAX) +MPINT_CONVERSIONS(TO) #undef TO diff --git a/math/mpint.h b/math/mpint.h index e351b056..637ec10e 100644 --- a/math/mpint.h +++ b/math/mpint.h @@ -145,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 @@ -155,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@ --- * @@ -181,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 -------------------------------------------------*/ -- 2.11.0