From 6f652de71e8aafb062db5a676d728b209c907611 Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Wed, 28 Nov 2018 10:42:11 +0100 Subject: [PATCH 1/7] use literal float --- bn_mp_get_double.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bn_mp_get_double.c b/bn_mp_get_double.c index 542993d..ea78b9f 100644 --- a/bn_mp_get_double.c +++ b/bn_mp_get_double.c @@ -16,9 +16,9 @@ double mp_get_double(const mp_int *a) { int i; - double d = 0, fac = 1; + double d = 0.0, fac = 1.0; for (i = 0; i < DIGIT_BIT; ++i) { - fac *= 2; + fac *= 2.0; } for (i = USED(a); i --> 0;) { d = d * fac + (double)DIGIT(a, i); From 034cd1f444104f92fd19bfcdd5afcbfc2782a5ea Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Wed, 28 Nov 2018 10:44:22 +0100 Subject: [PATCH 2/7] explicit comparison --- bn_mp_get_double.c | 2 +- bn_mp_set_double.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bn_mp_get_double.c b/bn_mp_get_double.c index ea78b9f..a192fb1 100644 --- a/bn_mp_get_double.c +++ b/bn_mp_get_double.c @@ -23,7 +23,7 @@ double mp_get_double(const mp_int *a) for (i = USED(a); i --> 0;) { d = d * fac + (double)DIGIT(a, i); } - return mp_isneg(a) ? -d : d; + return (mp_isneg(a) != MP_NO) ? -d : d; } #endif diff --git a/bn_mp_set_double.c b/bn_mp_set_double.c index 0a5f771..6b847a5 100644 --- a/bn_mp_set_double.c +++ b/bn_mp_set_double.c @@ -38,7 +38,7 @@ int mp_set_double(mp_int *a, double d) } res = exp < 0 ? mp_div_2d(a, -exp, a, 0) : mp_mul_2d(a, exp, a); - if ((cast.bits >> 63) && !mp_iszero(a)) { + if (((cast.bits >> 63) != 0ULL) && (mp_iszero(a) == MP_NO)) { SIGN(a) = MP_NEG; } From e9b85f93724d8f99fa17448d20f83c001e4fe805 Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Wed, 28 Nov 2018 10:46:12 +0100 Subject: [PATCH 3/7] rename parameter as in prototype --- bn_mp_set_double.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bn_mp_set_double.c b/bn_mp_set_double.c index 6b847a5..54b2d09 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) -int mp_set_double(mp_int *a, double d) +int mp_set_double(mp_int *a, double b) { uint64_t frac; int exp, res; @@ -22,7 +22,7 @@ int mp_set_double(mp_int *a, double d) double dbl; uint64_t bits; } cast; - cast.dbl = d; + cast.dbl = b; exp = (int)(cast.bits >> 52) & 0x7FF; frac = (cast.bits & ((1ULL << 52) - 1)) | (1ULL << 52); From 70af503a6e3fffc3c86e7e20c1a3fa06c82a0d73 Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Wed, 28 Nov 2018 10:48:04 +0100 Subject: [PATCH 4/7] literal suffix --- bn_mp_set_double.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bn_mp_set_double.c b/bn_mp_set_double.c index 54b2d09..51ab47d 100644 --- a/bn_mp_set_double.c +++ b/bn_mp_set_double.c @@ -25,7 +25,7 @@ int mp_set_double(mp_int *a, double b) cast.dbl = b; exp = (int)(cast.bits >> 52) & 0x7FF; - frac = (cast.bits & ((1ULL << 52) - 1)) | (1ULL << 52); + frac = (cast.bits & ((1ULL << 52) - 1ULL)) | (1ULL << 52); if (exp == 0x7FF) { /* +-inf, NaN */ return MP_VAL; From 39a15fca7ff04e3b128fe858569ae82c3d5345ac Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Wed, 28 Nov 2018 10:48:59 +0100 Subject: [PATCH 5/7] use NULL instead of 0 --- bn_mp_set_double.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bn_mp_set_double.c b/bn_mp_set_double.c index 51ab47d..e9697fc 100644 --- a/bn_mp_set_double.c +++ b/bn_mp_set_double.c @@ -37,7 +37,7 @@ int mp_set_double(mp_int *a, double b) return res; } - res = exp < 0 ? mp_div_2d(a, -exp, a, 0) : mp_mul_2d(a, exp, a); + res = exp < 0 ? mp_div_2d(a, -exp, a, NULL) : mp_mul_2d(a, exp, a); if (((cast.bits >> 63) != 0ULL) && (mp_iszero(a) == MP_NO)) { SIGN(a) = MP_NEG; } From d1b52524f1188e4827f6499454b2e6990db71e14 Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Wed, 28 Nov 2018 10:50:20 +0100 Subject: [PATCH 6/7] explicit operator precedence --- bn_mp_get_double.c | 2 +- bn_mp_set_double.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bn_mp_get_double.c b/bn_mp_get_double.c index a192fb1..8ce314f 100644 --- a/bn_mp_get_double.c +++ b/bn_mp_get_double.c @@ -21,7 +21,7 @@ double mp_get_double(const mp_int *a) fac *= 2.0; } for (i = USED(a); i --> 0;) { - d = d * fac + (double)DIGIT(a, i); + d = (d * fac) + (double)DIGIT(a, i); } return (mp_isneg(a) != MP_NO) ? -d : d; } diff --git a/bn_mp_set_double.c b/bn_mp_set_double.c index e9697fc..5513af1 100644 --- a/bn_mp_set_double.c +++ b/bn_mp_set_double.c @@ -37,7 +37,7 @@ int mp_set_double(mp_int *a, double b) return res; } - res = exp < 0 ? mp_div_2d(a, -exp, a, NULL) : mp_mul_2d(a, exp, a); + res = (exp < 0) ? mp_div_2d(a, -exp, a, NULL) : mp_mul_2d(a, exp, a); if (((cast.bits >> 63) != 0ULL) && (mp_iszero(a) == MP_NO)) { SIGN(a) = MP_NEG; } From f186e7bd834e8b333c93108141a855dcea7a1aa0 Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Wed, 28 Nov 2018 10:51:30 +0100 Subject: [PATCH 7/7] bitwise operation on unsigned --- bn_mp_set_double.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bn_mp_set_double.c b/bn_mp_set_double.c index 5513af1..0e230c7 100644 --- a/bn_mp_set_double.c +++ b/bn_mp_set_double.c @@ -24,7 +24,7 @@ int mp_set_double(mp_int *a, double b) } cast; cast.dbl = b; - exp = (int)(cast.bits >> 52) & 0x7FF; + exp = (unsigned)(cast.bits >> 52) & 0x7FFU; frac = (cast.bits & ((1ULL << 52) - 1ULL)) | (1ULL << 52); if (exp == 0x7FF) { /* +-inf, NaN */