Skip to content

Commit

Permalink
add safe_float functions
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
chenyang78 committed Sep 21, 2014
1 parent b138a40 commit d69927c
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
73 changes: 73 additions & 0 deletions runtime/safe_math.m4
Expand Up @@ -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
3 changes: 3 additions & 0 deletions src/OutputMgr.cpp
Expand Up @@ -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 <float.h>\n";
}

ExtensionMgr::OutputHeader(out);

Expand Down

0 comments on commit d69927c

Please sign in to comment.