[PATCH] newlib: strtold: support m68k's 96-bit long double
Bernie Innocenti
bernie@codewiz.org
Thu Oct 29 23:43:54 GMT 2026
It's the compiler that decides, not the CPU: GCC gives long double 12
bytes on every 680x0 from the 68000 to the 68060, with or without an FPU.
Soft-float code uses the same layout. It's the extended format of the
68881/68882, and of the FPUs built into the 68040 and 68060: a 16-bit
sign and exponent, 16 bits of padding, and a 64-bit mantissa.
As for who still uses newlib on the 680x0: we maintain the AmigaOS cross
toolchain (https://github.com/AmigaPorts/m68k-amigaos-gcc), where newlib
is the default C runtime. It's used for new Amiga software and for ports
from Unix.
The fixes I've sent came from running newlib's and musl's libc-test
testsuites on it.
_ // Bernie Innocenti
\X/ https://codewiz.org/
On 10/30/26 07:40, Joel Sherrill wrote:
> Asking because I don't know. Which m68k or Coldfire CPUs have 12 byte
> long double?
>
> RTEMS has historically had great m68k support but Board Support Packages
> have been removed as users give up that hardware. It started with m680x0
> BSPs without network, then those with, and so on. We are now down to a
> few Coldfire BSPs. It would be easy to resurrect removed ones but
> without users, why?
>
> --joel
> RTEMS
>
> On Thu, Oct 29, 2026, 11:11 PM Bernie Innocenti via Newlib
> <newlib@sourceware.org <mailto:newlib@sourceware.org>> wrote:
>
> ULtox() wrote the 10-byte x87 layout, exponent then mantissa, into the
> 12-byte m68k long double, whose mantissa starts after 16 bits of zero
> padding, so every strtold() result was garbage. Select the 96-bit
> layout with IEEE_MC68k and __SIZEOF_LONG_DOUBLE__ == 12; the other
> layouts are unchanged.
>
> Two differences from x87 show up once the layout is right: an exponent
> field of 0 is a real exponent, so the subnormals reach one binade lower
> (0x1p-16445 has mantissa 2, as gcc makes it), and infinity has an
> all-zero mantissa, with no integer bit.
>
> The new newlib.stdlib/strtold.c test compares strtold() with the
> compiler's constants, on the targets with a 64-bit-mantissa long double.
>
> Not fixed here: a few decimal inputs come out one ulp off (1e4931), and
> strtold() of the first value above LDBL_MAX
> ("118973149535723176505351158982948.86679662540046955673e4900") never
> returns, because nothing in strtodg()'s correction loop stops a value
> that rounds up past emax.
>
> Signed-off-by: Bernie Innocenti <bernie@codewiz.org
> <mailto:bernie@codewiz.org>>
> Assisted-By: Claude Opus 5 <noreply@anthropic.com
> <mailto:noreply@anthropic.com>>
> ---
> newlib/libc/stdlib/strtorx.c | 27 +++++++++++++++++++++++-
> newlib/testsuite/newlib.stdlib/strtold.c | 27 ++++++++++++++++++++++++
> 2 files changed, 53 insertions(+), 1 deletion(-)
> create mode 100644 newlib/testsuite/newlib.stdlib/strtold.c
>
> diff --git a/newlib/libc/stdlib/strtorx.c b/newlib/libc/stdlib/strtorx.c
> index 4faed8542..595f89aee 100644
> --- a/newlib/libc/stdlib/strtorx.c
> +++ b/newlib/libc/stdlib/strtorx.c
> @@ -41,12 +41,23 @@ THIS SOFTWARE.
> /* one or the other of IEEE_MC68k or IEEE_8087 should be #defined */
>
> #ifdef IEEE_MC68k
> +#if __SIZEOF_LONG_DOUBLE__ == 12
> +/* m68k's 96-bit format: 16 bits of exponent, 16 zero bits of padding,
> + then the 64-bit mantissa. */
> +#define _0 0
> +#define _PAD 1
> +#define _1 2
> +#define _2 3
> +#define _3 4
> +#define _4 5
> +#else
> #define _0 0
> #define _1 1
> #define _2 2
> #define _3 3
> #define _4 4
> #endif
> +#endif
> #ifdef IEEE_8087
> #define _0 4
> #define _1 3
> @@ -62,10 +73,13 @@ ULtox(L, bits, exp, k) __UShort *L; __ULong
> *bits; Long exp; int k;
> ULtox(__UShort *L, __ULong *bits, Long exp, int k)
> #endif
> {
> +#ifdef _PAD
> + L[_PAD] = 0;
> +#endif
> switch(k & STRTOG_Retmask) {
> case STRTOG_NoNumber:
> case STRTOG_Zero:
> - L[0] = L[1] = L[2] = L[3] = L[4] = 0;
> + L[_0] = L[_1] = L[_2] = L[_3] = L[_4] = 0;
> break;
>
> case STRTOG_Denormal:
> @@ -84,7 +98,12 @@ ULtox(__UShort *L, __ULong *bits, Long exp, int k)
>
> case STRTOG_Infinite:
> L[_0] = 0x7fff;
> +#ifdef _PAD
> + /* no integer bit in m68k's infinity, as the FPU and
> gcc make it */
> + L[_1] = 0;
> +#else
> L[_1] = 0x8000;
> +#endif
> L[_2] = L[_3] = L[_4] = 0;
> break;
>
> @@ -103,7 +122,13 @@ _strtorx_l(struct _reent *p, const char *s,
> char **sp, int rounding, void *L,
> locale_t loc)
> #endif
> {
> +#ifdef _PAD
> + /* An exponent field of 0 is a real exponent on m68k, not an
> alias of 1
> + as on x87, so its subnormals reach one binade lower. */
> + static FPI fpi0 = { 64, 1-16383-64, 32766 - 16383 - 64 + 1,
> 1, SI };
> +#else
> static FPI fpi0 = { 64, 1-16383-64+1, 32766 - 16383 - 64 +
> 1, 1, SI };
> +#endif
> FPI *fpi, fpi1;
> __ULong bits[2];
> Long exp;
> diff --git a/newlib/testsuite/newlib.stdlib/strtold.c b/newlib/
> testsuite/newlib.stdlib/strtold.c
> new file mode 100644
> index 000000000..ceb0e942d
> --- /dev/null
> +++ b/newlib/testsuite/newlib.stdlib/strtold.c
> @@ -0,0 +1,27 @@
> +/* strtold() must return the long double the compiler reads from
> the same
> + text: on m68k, the result was written in the 10-byte x87 layout
> into the
> + 12-byte long double, so every value came back as garbage. */
> +#include <float.h>
> +#include <stdlib.h>
> +
> +static void check(const char *text, long double want)
> +{
> + char *end;
> + long double got = strtold(text, &end);
> +
> + if (*end != '\0' || got != want)
> + abort();
> +}
> +
> +int main(void)
> +{
> +#if LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384
> + check("1.0", 1.0L);
> + check("0x8.111111111111113p0", 0x8.111111111111113p0L);
> +
> check("118973149535723176505351158982948.86679662540046955672e4900",
> LDBL_MAX);
> + check("0x1p-16445", 0x1p-16445L);
> + check("1e5000", __builtin_infl());
> + check("-2.5", -2.5L);
> +#endif
> + return 0;
> +}
> --
> 2.56.0
>
More information about the Newlib
mailing list