From df2d43ca8737875c641cc06ca519c7da55463d51 Mon Sep 17 00:00:00 2001 From: czurnieden Date: Thu, 7 Feb 2019 22:09:06 +0100 Subject: [PATCH 1/8] patch for m86k floats --- bn_mp_set_double.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++ demo/demo.c | 25 ++++++++++++- 2 files changed, 114 insertions(+), 2 deletions(-) diff --git a/bn_mp_set_double.c b/bn_mp_set_double.c index 76f6293f7..ca33af7b6 100644 --- a/bn_mp_set_double.c +++ b/bn_mp_set_double.c @@ -13,6 +13,96 @@ */ #if defined(__STDC_IEC_559__) || defined(__GCC_IEC_559) +/* defined by GNU C, SAS/C, and Stratus VOS C -- in that order */ +#if ((defined __m68k__) || (defined __MC68K__) || (defined M68000)) +static double s_math_h_less_frexp(double x, int *exp) +{ + int exponent = 0; + while (x >= 1.0) { + exponent++; + /* exp > DBL_MAX_EXP, it is inf */ + if (exponent == 1025) { + *exp = exponent; + return x; + } + x /= 2.0; + } + + *exp = exponent; + return x; +} + +static double s_pow(double d, int e){ + double t; + + if (e == 0) { + return d; + } + t = 1.0; + while (e > 1) { + if (e % 2 == 0) { + d *= d; + e /= 2; + } else { + t *= d; + d *= d; + e = (e - 1)/2; + } + } + return d * t; + +} + +int mp_set_double(mp_int *a, double b) +{ + uint64_t frac; + int exp = 0, res, sign = 1; + + /* Check for NaN */ + if (b != b) { + return MP_VAL; + } + + if (b < 0) { + b = b * (-1.0); + sign = -1; + } + /* Numbers smaller than 1 truncate to zero */ + if (b < 1.0) { + mp_zero(a); + return MP_OKAY; + } + + b = s_math_h_less_frexp(b, &exp); + /* +/-inf if exp > DBL_MAX_EXP */ + if (exp == 1025) { + return MP_VAL; + } + + /* 52 bit mantissa plus the one implicit bit */ + b = b * s_pow(2.0,53); + /* TODO: use proper rounding instead of truncating? */ + frac = (uint64_t) b; + exp -= 53; + + res = mp_set_long_long(a, frac); + if (res != MP_OKAY) { + return res; + } + + res = (exp < 0) ? mp_div_2d(a, -exp, a, NULL) : mp_mul_2d(a, exp, a); + if (res != MP_OKAY) { + return res; + } + + if ( (sign < 0) && (mp_iszero(a) == MP_NO)) { + SIGN(a) = MP_NEG; + } + + return MP_OKAY; +} +#else + int mp_set_double(mp_int *a, double b) { uint64_t frac; @@ -47,6 +137,7 @@ int mp_set_double(mp_int *a, double b) return MP_OKAY; } +#endif #else /* pragma message() not supported by several compilers (in mostly older but still used versions) */ # ifdef _MSC_VER diff --git a/demo/demo.c b/demo/demo.c index 642eab73f..741c65bc4 100644 --- a/demo/demo.c +++ b/demo/demo.c @@ -483,6 +483,9 @@ int main(void) /* test mp_get_double/mp_set_double */ #if defined(__STDC_IEC_559__) || defined(__GCC_IEC_559) printf("\n\nTesting: mp_get_double"); +#if ((defined __m68k__) || (defined __MC68K__) || (defined M68000)) + printf(" with a m86k cpu "); +#endif if (mp_set_double(&a, +1.0/0.0) != MP_VAL) { printf("\nmp_set_double should return MP_VAL for +inf"); return EXIT_FAILURE; @@ -500,6 +503,24 @@ int main(void) return EXIT_FAILURE; } +#include + if (mp_set_double(&a, DBL_MAX) != MP_OKAY) { + printf("\nmp_set_double(DBL_MAX) failed"); + return EXIT_FAILURE; + } + if (DBL_MAX != mp_get_double(&a)) { + printf("\nmp_get_double(DBL_MAX) bad result! %20.2f != %20.2f \n", DBL_MAX, mp_get_double(&a)); + return EXIT_FAILURE; + } + if (mp_set_double(&a, DBL_MIN) != MP_OKAY) { + printf("\nmp_set_double(DBL_MIN) failed"); + return EXIT_FAILURE; + } + if (0.0 != mp_get_double(&a)) { + printf("\nmp_get_double(DBL_MIN) bad result! %20.2f != %20.2f \n", DBL_MAX, mp_get_double(&a)); + return EXIT_FAILURE; + } + for (i = 0; i < 1000; ++i) { int tmp = rand(); double dbl = (double)tmp * rand() + 1; @@ -508,7 +529,7 @@ int main(void) return EXIT_FAILURE; } if (dbl != mp_get_double(&a)) { - printf("\nmp_get_double() bad result!"); + printf("\nmp_get_double() bad result! %20.2f != %20.2f \n", dbl, mp_get_double(&a)); return EXIT_FAILURE; } if (mp_set_double(&a, -dbl) != MP_OKAY) { @@ -516,7 +537,7 @@ int main(void) return EXIT_FAILURE; } if (-dbl != mp_get_double(&a)) { - printf("\nmp_get_double() bad result!"); + printf("\nmp_get_double() bad result! %20.2f != %20.2f \n", dbl, mp_get_double(&a)); return EXIT_FAILURE; } } From 60875d61bb3d8d59426be6d2048b2dbe26b6c11b Mon Sep 17 00:00:00 2001 From: czurnieden Date: Sat, 9 Feb 2019 00:18:12 +0100 Subject: [PATCH 2/8] changed computed value to literal and added some verbosity to demo.c --- bn_mp_set_double.c | 26 +++----------------------- demo/demo.c | 2 +- 2 files changed, 4 insertions(+), 24 deletions(-) diff --git a/bn_mp_set_double.c b/bn_mp_set_double.c index ca33af7b6..80c5a094a 100644 --- a/bn_mp_set_double.c +++ b/bn_mp_set_double.c @@ -14,7 +14,7 @@ #if defined(__STDC_IEC_559__) || defined(__GCC_IEC_559) /* defined by GNU C, SAS/C, and Stratus VOS C -- in that order */ -#if ((defined __m68k__) || (defined __MC68K__) || (defined M68000)) +#if ((defined __m68k__) || (defined __MC68K__) || (defined M68000) ) static double s_math_h_less_frexp(double x, int *exp) { int exponent = 0; @@ -32,26 +32,6 @@ static double s_math_h_less_frexp(double x, int *exp) return x; } -static double s_pow(double d, int e){ - double t; - - if (e == 0) { - return d; - } - t = 1.0; - while (e > 1) { - if (e % 2 == 0) { - d *= d; - e /= 2; - } else { - t *= d; - d *= d; - e = (e - 1)/2; - } - } - return d * t; - -} int mp_set_double(mp_int *a, double b) { @@ -79,8 +59,8 @@ int mp_set_double(mp_int *a, double b) return MP_VAL; } - /* 52 bit mantissa plus the one implicit bit */ - b = b * s_pow(2.0,53); + /* 0x1p+53 = 52 bit mantissa plus the one implicit bit */ + b = b * 9007199254740992.0; /* TODO: use proper rounding instead of truncating? */ frac = (uint64_t) b; exp -= 53; diff --git a/demo/demo.c b/demo/demo.c index 741c65bc4..f9dfc892c 100644 --- a/demo/demo.c +++ b/demo/demo.c @@ -483,7 +483,7 @@ int main(void) /* test mp_get_double/mp_set_double */ #if defined(__STDC_IEC_559__) || defined(__GCC_IEC_559) printf("\n\nTesting: mp_get_double"); -#if ((defined __m68k__) || (defined __MC68K__) || (defined M68000)) +#if ((defined __m68k__) || (defined __MC68K__) || (defined M68000) || (defined TEST_M68K) ) printf(" with a m86k cpu "); #endif if (mp_set_double(&a, +1.0/0.0) != MP_VAL) { From 565758679568665caab4da8161ff425558caf374 Mon Sep 17 00:00:00 2001 From: czurnieden Date: Sat, 9 Feb 2019 21:15:03 +0100 Subject: [PATCH 3/8] switched off M68K support for get/set double --- bn_mp_get_double.c | 3 +- bn_mp_set_double.c | 70 +--------------------------------------------- demo/demo.c | 43 ++++++++++++++++++++++++---- tommath.h | 7 +++-- 4 files changed, 45 insertions(+), 78 deletions(-) diff --git a/bn_mp_get_double.c b/bn_mp_get_double.c index 3ed5a717c..51dc2b9c1 100644 --- a/bn_mp_get_double.c +++ b/bn_mp_get_double.c @@ -11,7 +11,7 @@ * * SPDX-License-Identifier: Unlicense */ - +#if !((defined __m68k__) || (defined __MC68K__) || (defined M68000) ) double mp_get_double(const mp_int *a) { int i; @@ -25,6 +25,7 @@ double mp_get_double(const mp_int *a) return (mp_isneg(a) != MP_NO) ? -d : d; } #endif +#endif /* ref: $Format:%D$ */ /* git commit: $Format:%H$ */ diff --git a/bn_mp_set_double.c b/bn_mp_set_double.c index 80c5a094a..a5f1197b5 100644 --- a/bn_mp_set_double.c +++ b/bn_mp_set_double.c @@ -14,75 +14,7 @@ #if defined(__STDC_IEC_559__) || defined(__GCC_IEC_559) /* defined by GNU C, SAS/C, and Stratus VOS C -- in that order */ -#if ((defined __m68k__) || (defined __MC68K__) || (defined M68000) ) -static double s_math_h_less_frexp(double x, int *exp) -{ - int exponent = 0; - while (x >= 1.0) { - exponent++; - /* exp > DBL_MAX_EXP, it is inf */ - if (exponent == 1025) { - *exp = exponent; - return x; - } - x /= 2.0; - } - - *exp = exponent; - return x; -} - - -int mp_set_double(mp_int *a, double b) -{ - uint64_t frac; - int exp = 0, res, sign = 1; - - /* Check for NaN */ - if (b != b) { - return MP_VAL; - } - - if (b < 0) { - b = b * (-1.0); - sign = -1; - } - /* Numbers smaller than 1 truncate to zero */ - if (b < 1.0) { - mp_zero(a); - return MP_OKAY; - } - - b = s_math_h_less_frexp(b, &exp); - /* +/-inf if exp > DBL_MAX_EXP */ - if (exp == 1025) { - return MP_VAL; - } - - /* 0x1p+53 = 52 bit mantissa plus the one implicit bit */ - b = b * 9007199254740992.0; - /* TODO: use proper rounding instead of truncating? */ - frac = (uint64_t) b; - exp -= 53; - - res = mp_set_long_long(a, frac); - if (res != MP_OKAY) { - return res; - } - - res = (exp < 0) ? mp_div_2d(a, -exp, a, NULL) : mp_mul_2d(a, exp, a); - if (res != MP_OKAY) { - return res; - } - - if ( (sign < 0) && (mp_iszero(a) == MP_NO)) { - SIGN(a) = MP_NEG; - } - - return MP_OKAY; -} -#else - +#if !((defined __m68k__) || (defined __MC68K__) || (defined M68000) ) int mp_set_double(mp_int *a, double b) { uint64_t frac; diff --git a/demo/demo.c b/demo/demo.c index f9dfc892c..41db07ca1 100644 --- a/demo/demo.c +++ b/demo/demo.c @@ -166,6 +166,9 @@ int main(void) unsigned long long q, r; mp_digit mp; int i, n, err, should, cnt; +#if !((defined __m68k__) || (defined __MC68K__) || (defined M68000) ) + double dbl_count; +#endif #endif if (mp_init_multi(&a, &b, &c, &d, &e, &f, NULL)!= MP_OKAY) @@ -482,10 +485,12 @@ int main(void) /* test mp_get_double/mp_set_double */ #if defined(__STDC_IEC_559__) || defined(__GCC_IEC_559) +/* Does not work in a QEMU (patched) emulated environment, temporarily switched off*/ +#if !((defined __m68k__) || (defined __MC68K__) || (defined M68000) ) printf("\n\nTesting: mp_get_double"); -#if ((defined __m68k__) || (defined __MC68K__) || (defined M68000) || (defined TEST_M68K) ) - printf(" with a m86k cpu "); -#endif + + /*printf(" with a m86k cpu ");*/ + if (mp_set_double(&a, +1.0/0.0) != MP_VAL) { printf("\nmp_set_double should return MP_VAL for +inf"); return EXIT_FAILURE; @@ -521,9 +526,37 @@ int main(void) return EXIT_FAILURE; } + dbl_count = 2.0; + for (i = 0; i < 301; ++i) { + if (mp_set_double(&a, -dbl_count) != MP_OKAY) { + printf("\nmp_set_double(dbl_count) failed"); + return EXIT_FAILURE; + } + if (-dbl_count != mp_get_double(&a)) { + printf("\nmp_get_double(+dbl_count) at i = %d bad result! %20.20f != %20.20f\n", + i, -dbl_count, mp_get_double(&a) ); + return EXIT_FAILURE; + } + dbl_count = (dbl_count * 2.0); + } + /* Fails for M68K in QEMU (the patched version) for i in {53..63}*/ + dbl_count = 2.0; + for (i = 0; i < 301; ++i) { + if (mp_set_double(&a, -dbl_count) != MP_OKAY) { + printf("\nmp_set_double(dbl_count) failed"); + return EXIT_FAILURE; + } + if (-dbl_count != mp_get_double(&a)) { + printf("\nmp_get_double(+dbl_count) at i = %d bad result! %20.20f != %20.20f\n", + i, -dbl_count, mp_get_double(&a) ); + return EXIT_FAILURE; + } + dbl_count = (dbl_count * 2.0) -1; + } + for (i = 0; i < 1000; ++i) { int tmp = rand(); - double dbl = (double)tmp * rand() + 1; + double dbl = (double) tmp * rand() + 1.0; if (mp_set_double(&a, dbl) != MP_OKAY) { printf("\nmp_set_double() failed"); return EXIT_FAILURE; @@ -542,7 +575,7 @@ int main(void) } } #endif - +#endif /* test mp_get_int */ printf("\n\nTesting: mp_get_int"); for (i = 0; i < 1000; ++i) { diff --git a/tommath.h b/tommath.h index ee5da86e7..6d1a5e565 100644 --- a/tommath.h +++ b/tommath.h @@ -202,8 +202,9 @@ void mp_zero(mp_int *a); void mp_set(mp_int *a, mp_digit b); /* set a double */ +#if !((defined __m68k__) || (defined __MC68K__) || (defined M68000) ) int mp_set_double(mp_int *a, double b); - +#endif /* set a 32-bit const */ int mp_set_int(mp_int *a, unsigned long b); @@ -212,10 +213,10 @@ int mp_set_long(mp_int *a, unsigned long b); /* set a platform dependent unsigned long long value */ int mp_set_long_long(mp_int *a, unsigned long long b); - +#if !((defined __m68k__) || (defined __MC68K__) || (defined M68000) ) /* get a double */ double mp_get_double(const mp_int *a); - +#endif /* get a 32-bit value */ unsigned long mp_get_int(const mp_int *a); From 31bab263cb4d54ee71584ff3d9e0ae59f4f7d69d Mon Sep 17 00:00:00 2001 From: czurnieden Date: Sun, 10 Feb 2019 04:04:31 +0100 Subject: [PATCH 4/8] reinstate m68k support --- bn_mp_get_double.c | 4 ++-- bn_mp_set_double.c | 2 -- demo/demo.c | 7 +------ makefile_include.mk | 8 ++++++++ tommath.h | 7 +++---- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/bn_mp_get_double.c b/bn_mp_get_double.c index 51dc2b9c1..74a2c7768 100644 --- a/bn_mp_get_double.c +++ b/bn_mp_get_double.c @@ -11,7 +11,7 @@ * * SPDX-License-Identifier: Unlicense */ -#if !((defined __m68k__) || (defined __MC68K__) || (defined M68000) ) + double mp_get_double(const mp_int *a) { int i; @@ -24,7 +24,7 @@ double mp_get_double(const mp_int *a) } return (mp_isneg(a) != MP_NO) ? -d : d; } -#endif + #endif /* ref: $Format:%D$ */ diff --git a/bn_mp_set_double.c b/bn_mp_set_double.c index a5f1197b5..6da8473f8 100644 --- a/bn_mp_set_double.c +++ b/bn_mp_set_double.c @@ -14,7 +14,6 @@ #if defined(__STDC_IEC_559__) || defined(__GCC_IEC_559) /* defined by GNU C, SAS/C, and Stratus VOS C -- in that order */ -#if !((defined __m68k__) || (defined __MC68K__) || (defined M68000) ) int mp_set_double(mp_int *a, double b) { uint64_t frac; @@ -49,7 +48,6 @@ int mp_set_double(mp_int *a, double b) return MP_OKAY; } -#endif #else /* pragma message() not supported by several compilers (in mostly older but still used versions) */ # ifdef _MSC_VER diff --git a/demo/demo.c b/demo/demo.c index 41db07ca1..0f2dce764 100644 --- a/demo/demo.c +++ b/demo/demo.c @@ -166,9 +166,8 @@ int main(void) unsigned long long q, r; mp_digit mp; int i, n, err, should, cnt; -#if !((defined __m68k__) || (defined __MC68K__) || (defined M68000) ) double dbl_count; -#endif + #endif if (mp_init_multi(&a, &b, &c, &d, &e, &f, NULL)!= MP_OKAY) @@ -485,8 +484,6 @@ int main(void) /* test mp_get_double/mp_set_double */ #if defined(__STDC_IEC_559__) || defined(__GCC_IEC_559) -/* Does not work in a QEMU (patched) emulated environment, temporarily switched off*/ -#if !((defined __m68k__) || (defined __MC68K__) || (defined M68000) ) printf("\n\nTesting: mp_get_double"); /*printf(" with a m86k cpu ");*/ @@ -539,7 +536,6 @@ int main(void) } dbl_count = (dbl_count * 2.0); } - /* Fails for M68K in QEMU (the patched version) for i in {53..63}*/ dbl_count = 2.0; for (i = 0; i < 301; ++i) { if (mp_set_double(&a, -dbl_count) != MP_OKAY) { @@ -574,7 +570,6 @@ int main(void) return EXIT_FAILURE; } } -#endif #endif /* test mp_get_int */ printf("\n\nTesting: mp_get_int"); diff --git a/makefile_include.mk b/makefile_include.mk index 5ebcd5d6a..4bbb1aa65 100644 --- a/makefile_include.mk +++ b/makefile_include.mk @@ -95,6 +95,14 @@ else _ARCH := $(shell arch) endif +#TODO: check for more compilers that support both m68k and something +# like "-ffloat-store" +ifeq ($(_ARCH),m68k) + ifneq ($(findstring gcc,$(CC)),) +CFLAGS += -ffloat-store + endif +endif + # adjust coverage set ifneq ($(filter $(_ARCH), i386 i686 x86_64 amd64 ia64),) COVERAGE = test_standalone timing diff --git a/tommath.h b/tommath.h index 6d1a5e565..ee5da86e7 100644 --- a/tommath.h +++ b/tommath.h @@ -202,9 +202,8 @@ void mp_zero(mp_int *a); void mp_set(mp_int *a, mp_digit b); /* set a double */ -#if !((defined __m68k__) || (defined __MC68K__) || (defined M68000) ) int mp_set_double(mp_int *a, double b); -#endif + /* set a 32-bit const */ int mp_set_int(mp_int *a, unsigned long b); @@ -213,10 +212,10 @@ int mp_set_long(mp_int *a, unsigned long b); /* set a platform dependent unsigned long long value */ int mp_set_long_long(mp_int *a, unsigned long long b); -#if !((defined __m68k__) || (defined __MC68K__) || (defined M68000) ) + /* get a double */ double mp_get_double(const mp_int *a); -#endif + /* get a 32-bit value */ unsigned long mp_get_int(const mp_int *a); From 55d9fa41b024c8855ea68dc374195154745fc63b Mon Sep 17 00:00:00 2001 From: czurnieden Date: Mon, 11 Feb 2019 13:41:21 +0100 Subject: [PATCH 5/8] removed arch dependency --- demo/demo.c | 50 ++++++++++++++++++++++++++++++++++++++++----- makefile_include.mk | 8 -------- 2 files changed, 45 insertions(+), 13 deletions(-) diff --git a/demo/demo.c b/demo/demo.c index 0f2dce764..33a504f9b 100644 --- a/demo/demo.c +++ b/demo/demo.c @@ -25,6 +25,32 @@ #include "tommath.h" +/* VERY simpel comparing function, for use in this case and this case only! */ + +/* MIN() macro is in tommath_private.h */ +#ifndef MIN +#define MIN(x, y) (((x) < (y)) ? (x) : (y)) +#endif +/* avoid libmath */ +static double s_abs(double d){ + return (d < 0.0)?-d:d; +} +#include +/* check relative error against DBL_EPSILON. Input numbers are big enough to do so */ +static int s_compare_doubles(double a, double b){ + double abs_a, abs_b, delta; + + /* NaN, inf's, small numbers, and subnormals ignored, not needed in this case */ + if (a == b) { + return 1; + } + abs_a = s_abs(a); + abs_b = s_abs(b); + delta = s_abs(a - b); + + return ( (delta/MIN(abs_a, abs_b)) < DBL_EPSILON ); +} + static void ndraw(mp_int *a, const char *name) { char buf[16000]; @@ -536,14 +562,28 @@ int main(void) } dbl_count = (dbl_count * 2.0); } + + dbl_count = 2.0; + for (i = 0; i < 301; ++i) { + if (mp_set_double(&a, dbl_count) != MP_OKAY) { + printf("\nmp_set_double(+dbl_count - 1) failed"); + return EXIT_FAILURE; + } + if ( !s_compare_doubles(dbl_count, mp_get_double(&a)) ) { + printf("\nmp_get_double(+dbl_count - 1) at i = %d bad result! %20.20f != %20.20f\n", + i, dbl_count, mp_get_double(&a) ); + return EXIT_FAILURE; + } + dbl_count = (dbl_count * 2.0) -1; + } dbl_count = 2.0; for (i = 0; i < 301; ++i) { if (mp_set_double(&a, -dbl_count) != MP_OKAY) { - printf("\nmp_set_double(dbl_count) failed"); + printf("\nmp_set_double((-dbl_count) - 1) failed"); return EXIT_FAILURE; } - if (-dbl_count != mp_get_double(&a)) { - printf("\nmp_get_double(+dbl_count) at i = %d bad result! %20.20f != %20.20f\n", + if ( !s_compare_doubles(-dbl_count, mp_get_double(&a)) ) { + printf("\nmp_get_double((-dbl_count) - 1) at i = %d bad result! %20.20f != %20.20f\n", i, -dbl_count, mp_get_double(&a) ); return EXIT_FAILURE; } @@ -557,7 +597,7 @@ int main(void) printf("\nmp_set_double() failed"); return EXIT_FAILURE; } - if (dbl != mp_get_double(&a)) { + if ( !s_compare_doubles(dbl, mp_get_double(&a))) { printf("\nmp_get_double() bad result! %20.2f != %20.2f \n", dbl, mp_get_double(&a)); return EXIT_FAILURE; } @@ -565,7 +605,7 @@ int main(void) printf("\nmp_set_double() failed"); return EXIT_FAILURE; } - if (-dbl != mp_get_double(&a)) { + if ( !s_compare_doubles(-dbl, mp_get_double(&a))) { printf("\nmp_get_double() bad result! %20.2f != %20.2f \n", dbl, mp_get_double(&a)); return EXIT_FAILURE; } diff --git a/makefile_include.mk b/makefile_include.mk index 4bbb1aa65..5ebcd5d6a 100644 --- a/makefile_include.mk +++ b/makefile_include.mk @@ -95,14 +95,6 @@ else _ARCH := $(shell arch) endif -#TODO: check for more compilers that support both m68k and something -# like "-ffloat-store" -ifeq ($(_ARCH),m68k) - ifneq ($(findstring gcc,$(CC)),) -CFLAGS += -ffloat-store - endif -endif - # adjust coverage set ifneq ($(filter $(_ARCH), i386 i686 x86_64 amd64 ia64),) COVERAGE = test_standalone timing From bd2bc4d97ef6815581ca8f1a5ad43ca15bdfb3f7 Mon Sep 17 00:00:00 2001 From: czurnieden Date: Mon, 11 Feb 2019 13:57:16 +0100 Subject: [PATCH 6/8] put s_compare_doubles in the correct place --- demo/demo.c | 53 +++++++++++++++++++++++++++-------------------------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/demo/demo.c b/demo/demo.c index 33a504f9b..622e57a5f 100644 --- a/demo/demo.c +++ b/demo/demo.c @@ -25,32 +25,6 @@ #include "tommath.h" -/* VERY simpel comparing function, for use in this case and this case only! */ - -/* MIN() macro is in tommath_private.h */ -#ifndef MIN -#define MIN(x, y) (((x) < (y)) ? (x) : (y)) -#endif -/* avoid libmath */ -static double s_abs(double d){ - return (d < 0.0)?-d:d; -} -#include -/* check relative error against DBL_EPSILON. Input numbers are big enough to do so */ -static int s_compare_doubles(double a, double b){ - double abs_a, abs_b, delta; - - /* NaN, inf's, small numbers, and subnormals ignored, not needed in this case */ - if (a == b) { - return 1; - } - abs_a = s_abs(a); - abs_b = s_abs(b); - delta = s_abs(a - b); - - return ( (delta/MIN(abs_a, abs_b)) < DBL_EPSILON ); -} - static void ndraw(mp_int *a, const char *name) { char buf[16000]; @@ -124,6 +98,33 @@ static void _cleanup(void) #endif } #if LTM_DEMO_TEST_VS_MTEST == 0 + +/* VERY simpel comparing function, for use in this case and this case only! */ + +/* MIN() macro is in tommath_private.h */ +#ifndef MIN +#define MIN(x, y) (((x) < (y)) ? (x) : (y)) +#endif +/* avoid libmath */ +static double s_abs(double s_d){ + return (s_d < 0.0)?-s_d:s_d; +} +#include +/* check relative error against DBL_EPSILON. Input numbers are big enough to do so */ +static int s_compare_doubles(double s_a, double s_b){ + double abs_a, abs_b, delta; + + /* NaN, inf's, small numbers, and subnormals ignored, not needed in this case */ + if (s_a == s_b) { + return 1; + } + abs_a = s_abs(s_a); + abs_b = s_abs(s_b); + delta = s_abs(s_a - s_b); + + return ( (delta/MIN(abs_a, abs_b)) < DBL_EPSILON ); +} + struct mp_sqrtmod_prime_st { unsigned long p; unsigned long n; From 74d040b80e4653313889c64fefed92a5d1f5284e Mon Sep 17 00:00:00 2001 From: czurnieden Date: Mon, 11 Feb 2019 15:24:09 +0100 Subject: [PATCH 7/8] restrict m68k patch to m68k --- demo/demo.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/demo/demo.c b/demo/demo.c index 622e57a5f..23ed99c37 100644 --- a/demo/demo.c +++ b/demo/demo.c @@ -99,8 +99,8 @@ static void _cleanup(void) } #if LTM_DEMO_TEST_VS_MTEST == 0 +#if ((defined __m68k__) || (defined __MC68K__) || (defined M68000)) /* VERY simpel comparing function, for use in this case and this case only! */ - /* MIN() macro is in tommath_private.h */ #ifndef MIN #define MIN(x, y) (((x) < (y)) ? (x) : (y)) @@ -124,7 +124,10 @@ static int s_compare_doubles(double s_a, double s_b){ return ( (delta/MIN(abs_a, abs_b)) < DBL_EPSILON ); } - +#define S_COMPARE_DOUBLES(x,y) s_compare_doubles( (x), (y)) +#else +#define S_COMPARE_DOUBLES(x,y) ( (x) == (y)) +#endif struct mp_sqrtmod_prime_st { unsigned long p; unsigned long n; @@ -570,7 +573,7 @@ int main(void) printf("\nmp_set_double(+dbl_count - 1) failed"); return EXIT_FAILURE; } - if ( !s_compare_doubles(dbl_count, mp_get_double(&a)) ) { + if ( !S_COMPARE_DOUBLES(dbl_count, mp_get_double(&a)) ) { printf("\nmp_get_double(+dbl_count - 1) at i = %d bad result! %20.20f != %20.20f\n", i, dbl_count, mp_get_double(&a) ); return EXIT_FAILURE; @@ -583,7 +586,7 @@ int main(void) printf("\nmp_set_double((-dbl_count) - 1) failed"); return EXIT_FAILURE; } - if ( !s_compare_doubles(-dbl_count, mp_get_double(&a)) ) { + if ( !S_COMPARE_DOUBLES(-dbl_count, mp_get_double(&a)) ) { printf("\nmp_get_double((-dbl_count) - 1) at i = %d bad result! %20.20f != %20.20f\n", i, -dbl_count, mp_get_double(&a) ); return EXIT_FAILURE; @@ -598,7 +601,7 @@ int main(void) printf("\nmp_set_double() failed"); return EXIT_FAILURE; } - if ( !s_compare_doubles(dbl, mp_get_double(&a))) { + if ( !S_COMPARE_DOUBLES(dbl, mp_get_double(&a))) { printf("\nmp_get_double() bad result! %20.2f != %20.2f \n", dbl, mp_get_double(&a)); return EXIT_FAILURE; } @@ -606,7 +609,7 @@ int main(void) printf("\nmp_set_double() failed"); return EXIT_FAILURE; } - if ( !s_compare_doubles(-dbl, mp_get_double(&a))) { + if ( !S_COMPARE_DOUBLES(-dbl, mp_get_double(&a))) { printf("\nmp_get_double() bad result! %20.2f != %20.2f \n", dbl, mp_get_double(&a)); return EXIT_FAILURE; } From 52e99434cdafb99d99fd25269b3fdc77345a1fec Mon Sep 17 00:00:00 2001 From: czurnieden Date: Wed, 13 Feb 2019 14:25:50 +0100 Subject: [PATCH 8/8] removed PIE from build of shared library --- makefile.shared | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/makefile.shared b/makefile.shared index 78b383a70..6ed2c89a9 100644 --- a/makefile.shared +++ b/makefile.shared @@ -17,6 +17,9 @@ ifndef LIBTOOL LIBTOOL:=libtool endif endif + +# PIE and PIC do not play well together in a shared library +ADDITIONAL_LDFLAGS= -Wl,--warn-shared-textrel -Wl,--fatal-warnings -fno-PIE -no-pie LTCOMPILE = $(LIBTOOL) --mode=compile --tag=CC $(CC) LTLINK = $(LIBTOOL) --mode=link --tag=CC $(CC) @@ -56,12 +59,12 @@ bn_s_mp_mul_high_digs.o bn_s_mp_sqr.o bn_s_mp_sub.o bncore.o objs: $(OBJECTS) .c.o: - $(LTCOMPILE) $(CFLAGS) $(LDFLAGS) -o $@ -c $< + $(LTCOMPILE) $(CFLAGS) $(LDFLAGS) $(ADDITIONAL_LDFLAGS) -o $@ -c $< LOBJECTS = $(OBJECTS:.o=.lo) $(LIBNAME): $(OBJECTS) - $(LTLINK) $(LDFLAGS) $(LOBJECTS) -o $(LIBNAME) -rpath $(LIBPATH) -version-info $(VERSION_SO) $(LIBTOOLFLAGS) + $(LTLINK) $(LDFLAGS) $(ADDITIONAL_LDFLAGS) $(LOBJECTS) -o $(LIBNAME) -rpath $(LIBPATH) -version-info $(VERSION_SO) $(LIBTOOLFLAGS) install: $(LIBNAME) install -d $(DESTDIR)$(LIBPATH)