From 291fcc1ec51e21e0b34536aec0b9bde2fb4d5660 Mon Sep 17 00:00:00 2001 From: Taylor R Campbell Date: Fri, 12 Jul 2019 16:01:16 +0000 Subject: [PATCH] Honour the floating-point rounding mode in floating-point formatting. C99, Sec. 7.19.6.1 `The fprintf function', paragraph 13, p. 281: (Recommended practice) For e, E, f, F, g, and G conversions, if the number of significant decimal digits is at most DECIMAL_DIG, then the result should be correctly rounded. If the number of significant decimal digits is more than DECIMAL_DIG but the source value is exactly representable with DECIMAL_DIG digits, then the result should be an exact representation with trailing zeros. Otherwise, the source value is bounded by two adjacent decimal strings L < U, both having DECIMAL_DIG significant idgits; the value of the resultant decimal string D should satisfy L <= D <= U, _with the extra stipulation that the error should have a correct sign for the current rounding direction_. [emphasis added] The gdtoa code base already supports respecting the floating-point rounding mode, as long as we compile it with Honor_FLT_ROUNDS defined. However, for this to work, fegetround must be available in libc, which it is not currently -- the fenv logic is in libm. Fortunately, we don't have to move all of fenv from libm to libc -- programs that do not link against libm don't have fesetround, so the rounding mode is always the default (barring asm shenanigans that bypass the API -- tough). So add a stub to libc that just returns FE_TONEAREST with a weak alias named fegetround. --- lib/libc/Makefile | 1 + lib/libc/fenv-stub/Makefile.inc | 7 +++++ lib/libc/fenv-stub/fegetround.c | 49 +++++++++++++++++++++++++++++++++ lib/libc/gdtoa/Makefile.inc | 1 + lib/libc/gdtoa/gdtoa_fltrnds.h | 3 +- 5 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 lib/libc/fenv-stub/Makefile.inc create mode 100644 lib/libc/fenv-stub/fegetround.c diff --git a/lib/libc/Makefile b/lib/libc/Makefile index 907f5acf10e0..9c0a7f106d20 100644 --- a/lib/libc/Makefile +++ b/lib/libc/Makefile @@ -67,6 +67,7 @@ CPPFLAGS+= -D__BUILD_LEGACY .include "${.CURDIR}/compat-43/Makefile.inc" .include "${.CURDIR}/compiler_rt/Makefile.inc" .include "${.CURDIR}/dlfcn/Makefile.inc" +.include "${.CURDIR}/fenv-stub/Makefile.inc" .include "${.CURDIR}/gdtoa/Makefile.inc" .include "${.CURDIR}/gen/Makefile.inc" .include "${.CURDIR}/gmon/Makefile.inc" diff --git a/lib/libc/fenv-stub/Makefile.inc b/lib/libc/fenv-stub/Makefile.inc new file mode 100644 index 000000000000..cda922a219de --- /dev/null +++ b/lib/libc/fenv-stub/Makefile.inc @@ -0,0 +1,7 @@ +# $NetBSD$ + +.include + +.PATH: ${.CURDIR}/fenv-stub + +SRCS+= fegetround.c diff --git a/lib/libc/fenv-stub/fegetround.c b/lib/libc/fenv-stub/fegetround.c new file mode 100644 index 000000000000..61535a9b8205 --- /dev/null +++ b/lib/libc/fenv-stub/fegetround.c @@ -0,0 +1,49 @@ +/* $NetBSD$ */ + +/*- + * Copyright (c) 2019 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Taylor R. Campbell. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#define fegetround __libc_fegetround_stub + +#include + +__weak_alias(fegetround, __libc_fegetround_stub) + +int +__libc_fegetround_stub(void) +{ + + /* + * Programs that have not linked against libm don't have access + * to fesetround, so the rounding mode is limited to be the + * default one. Programs that have linked against libm will + * get the real fegetround, not this weak alias. + */ + return FE_TONEAREST; +} diff --git a/lib/libc/gdtoa/Makefile.inc b/lib/libc/gdtoa/Makefile.inc index 19268daa9148..96b3627e7f70 100644 --- a/lib/libc/gdtoa/Makefile.inc +++ b/lib/libc/gdtoa/Makefile.inc @@ -5,6 +5,7 @@ CPPFLAGS+=-I${.CURDIR}/gdtoa -I${.CURDIR}/locale CPPFLAGS+=-DNO_FENV_H +CPPFLAGS+=-DHonor_FLT_ROUNDS # machine-dependent directory must provide the following: # arith.h gd_qnan.h diff --git a/lib/libc/gdtoa/gdtoa_fltrnds.h b/lib/libc/gdtoa/gdtoa_fltrnds.h index 33e5f9e5342c..f170de5fd158 100644 --- a/lib/libc/gdtoa/gdtoa_fltrnds.h +++ b/lib/libc/gdtoa/gdtoa_fltrnds.h @@ -1,4 +1,5 @@ - FPI *fpi, fpi1; + CONST FPI *fpi; + FPI fpi1; int Rounding; #ifdef Trust_FLT_ROUNDS /*{{ only define this if FLT_ROUNDS really works! */ Rounding = Flt_Rounds;