From d69927c63879ea925de558c62c73fc296a98967d Mon Sep 17 00:00:00 2001 From: Yang Chen Date: Sat, 20 Sep 2014 22:22:27 -0700 Subject: [PATCH] add safe_float functions * modified versions of safe_float functions suggested by Pascal * include float.h when --float is passed * should have mentioned in previous commit: floating point support is disabled by default, passing --float to enable it --- runtime/safe_math.m4 | 73 ++++++++++++++++++++++++++++++++++++++++++++ src/OutputMgr.cpp | 3 ++ 2 files changed, 76 insertions(+) diff --git a/runtime/safe_math.m4 b/runtime/safe_math.m4 index 4a286c303..314a1cdcc 100644 --- a/runtime/safe_math.m4 +++ b/runtime/safe_math.m4 @@ -287,4 +287,77 @@ safe_unsigned_math(uint32_t,UINT32_MAX) safe_unsigned_math(uint64_t,UINT64_MAX) #endif +dnl safe floating point computation, based on Pascal's suggestion + +define(`safe_float_math',` + +STATIC $1 +FUNC_NAME(add_func_$1_f_f)($1 sf1, $1 sf2 LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE_FLOAT + (fabs((0.5 * sf1) + (0.5 * sf2)) > (0.5 * DBL_MAX)) ? + UNDEFINED(sf1) : +#endif + (sf1 + sf2); +} + +STATIC $1 +FUNC_NAME(sub_func_$1_f_f)($1 sf1, $1 sf2 LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE_FLOAT + (fabs((0.5 * sf1) - (0.5 * sf2)) > (0.5 * DBL_MAX)) ? + UNDEFINED(sf1) : +#endif + (sf1 - sf2); +} + +STATIC $1 +FUNC_NAME(mul_func_$1_f_f)($1 sf1, $1 sf2 LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE_FLOAT + (fabs((0x1.0p-512 * sf1) * (0x1.0p-512 * sf2)) > (0x1.0p-1024 * DBL_MAX)) ? + UNDEFINED(sf1) : +#endif + (sf1 * sf2); +} + +STATIC $1 +FUNC_NAME(div_func_$1_f_f)($1 sf1, $1 sf2 LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE_FLOAT + (((sf2 == 0.0) || (fabs((0x1.0p-600 * sf1) / (0x1.0p600 * sf2))) > (0x1.0p-1000 * DBL_MAX))) ? + UNDEFINED(sf1) : +#endif + (sf1 / sf2); +} + +') + +safe_float_math(float) + +define(`safe_float_conversion',` +STATIC $2 +FUNC_NAME(convert_func_$1_to_$2)($1 sf1 LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE_FLOAT + ((sf1 <= $3) || (sf1 >= $4)) ? + UNDEFINED($4) : +#endif + (($2)(sf1)); +} +') + +safe_float_conversion(float, int32_t, INT32_MIN, INT32_MAX) + + #endif diff --git a/src/OutputMgr.cpp b/src/OutputMgr.cpp index a03c5e8c7..d5d300782 100644 --- a/src/OutputMgr.cpp +++ b/src/OutputMgr.cpp @@ -286,6 +286,9 @@ OutputMgr::OutputHeader(int argc, char *argv[], unsigned long seed) out << "#define NO_LONGLONG" << std::endl; out << endl; } + if (!CGOptions::enable_float()) { + out << "#include \n"; + } ExtensionMgr::OutputHeader(out);