Disabling floating point code in mini-gmp?
Niels Möller
nisse at lysator.liu.se
Wed Sep 24 09:17:03 CEST 2025
Hi,
in mini-gmp.c there's check #if !defined(MINI_GMP_DONT_USE_FLOAT_H), to
not include float.h, in which case we'll assume 53 mantissa bits. This
is set where mini-gmp.c is included in bootstrap.c. As far as I'm aware,
it's not used anywhere else.
I think I'd like to replace this with a knob to exclude all float
functions. I think we've discussed earlier if mini-gmp should be made
more modular; it might be nice to include mini-gmp.c and enable only the
functions that you need. Which may be a rabbit hole if we try to do it
thoroughly.
My motivation for disabling floats in particular is for supporting
embedded systems without floating point, where it's desirable to not get
a link dependency on a software float implementation. The hardware I'm
interested in at the moment is the tillitis tkey (an open hardware
device, which uses a 32-bit riscv with a multipler, but no divider and
no floats).
Proposed patch below. What do you think?
Regards,
/Niels
diff -r 1dc7d5dabc8d bootstrap.c
--- a/bootstrap.c Fri Sep 19 17:18:39 2025 +0200
+++ b/bootstrap.c Wed Sep 24 08:59:37 2025 +0200
@@ -29,7 +29,7 @@
see https://www.gnu.org/licenses/. */
-#define MINI_GMP_DONT_USE_FLOAT_H 1
+#define MINI_GMP_ENABLE_FLOAT 0
#include "mini-gmp/mini-gmp.c"
#define MIN(l,o) ((l) < (o) ? (l) : (o))
diff -r 1dc7d5dabc8d mini-gmp/mini-gmp.c
--- a/mini-gmp/mini-gmp.c Fri Sep 19 17:18:39 2025 +0200
+++ b/mini-gmp/mini-gmp.c Wed Sep 24 08:59:37 2025 +0200
@@ -51,7 +51,11 @@
#include "mini-gmp.h"
-#if !defined(MINI_GMP_DONT_USE_FLOAT_H)
+#ifndef MINI_GMP_ENABLE_FLOAT
+#define MINI_GMP_ENABLE_FLOAT 1
+#endif
+
+#if MINI_GMP_ENABLE_FLOAT
#include <float.h>
#endif
@@ -1705,6 +1709,7 @@
}
+#if MINI_GMP_ENABLE_FLOAT
/* Conversions and comparison to double. */
void
mpz_set_d (mpz_t r, double x)
@@ -1861,6 +1866,7 @@
return mpz_cmpabs_d (x, d);
}
}
+#endif /* MINI_GMP_ENABLE_FLOAT */
/* MPZ comparisons and the like. */
diff -r 1dc7d5dabc8d mini-gmp/mini-mpq.c
--- a/mini-gmp/mini-mpq.c Fri Sep 19 17:18:39 2025 +0200
+++ b/mini-gmp/mini-mpq.c Wed Sep 24 08:59:37 2025 +0200
@@ -41,6 +41,10 @@
#include "mini-mpq.h"
+#ifndef MINI_GMP_ENABLE_FLOAT
+#define MINI_GMP_ENABLE_FLOAT 1
+#endif
+
#ifndef GMP_LIMB_HIGHBIT
/* Define macros and static functions already defined by mini-gmp.c */
#define GMP_LIMB_BITS (sizeof(mp_limb_t) * CHAR_BIT)
@@ -420,6 +424,7 @@
}
+#if MINI_GMP_ENABLE_FLOAT
/* MPQ to/from double. */
void
mpq_set_d (mpq_t r, double x)
@@ -472,7 +477,7 @@
return ret;
}
-
+#endif /* MINI_GMP_ENABLE_FLOAT */
/* MPQ and strings/streams. */
char *
--
Niels Möller. PGP key CB4962D070D77D7FCB8BA36271D8F1FF368C6677.
Internet email is subject to wholesale government surveillance.
More information about the gmp-devel
mailing list