From: Szabolcs Nagy Date: Sat, 1 Dec 2018 23:52:34 +0000 (+0000) Subject: math: add eval_as_float and eval_as_double X-Git-Tag: v1.1.23~61 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=fe54544f055959bb7406758881d0fb85920b3f0e;p=oweals%2Fmusl.git math: add eval_as_float and eval_as_double Previously type casts or assignments were used for handling excess precision, which assumed standard C99 semantics, but since it's a rarely needed obscure detail, it's better to use explicit helper functions to document where we rely on this. It also helps if the code is used outside of the libc in non-C99 compilation mode: with the default excess precision handling of gcc, explicit inline asm barriers are needed for narrowing on FLT_EVAL_METHOD!=0 targets. I plan to use this in new code with the existing style that uses double_t and float_t as much as possible. One ugliness is that it is required for almost every return statement since that does not drop excess precision (the standard changed this in C11 annex F, but that does not help in non-standard compilation modes or with old compilers). --- diff --git a/src/internal/libm.h b/src/internal/libm.h index 5669c046..09fcfde3 100644 --- a/src/internal/libm.h +++ b/src/internal/libm.h @@ -59,6 +59,23 @@ union ldshape { #error Unsupported long double representation #endif +/* Evaluate an expression as the specified type. With standard excess + precision handling a type cast or assignment is enough (with + -ffloat-store an assignment is required, in old compilers argument + passing and return statement may not drop excess precision). */ + +static inline float eval_as_float(float x) +{ + float y = x; + return y; +} + +static inline double eval_as_double(double x) +{ + double y = x; + return y; +} + /* fp_barrier returns its input, but limits code transformations as if it had a side-effect (e.g. observable io) and returned an arbitrary value. */