Skip to content

Commit

Permalink
Optimize floatutils (NeuralNetworkVerification#191)
Browse files Browse the repository at this point in the history
Move most of FloatUtils implementation to .h (to enable compiler inline optimization)
AVG CPU results (on acas xu properties) 
p1:
original: 140421.1
inline: 139222.3
p2:
original: 63249.9
inline: 61641.4
p3:
original: 23690.4
inline: 22135.7
p4:
original: 10303.6
inline: 9252.6
  • Loading branch information
yuvaljacoby authored and AleksandarZeljic committed Oct 9, 2020
1 parent 288ea30 commit d50793a
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 99 deletions.
81 changes: 0 additions & 81 deletions src/common/FloatUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

#include "FloatUtils.h"
#include <iomanip>
#include <math.h>
#include <sstream>

// https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
Expand All @@ -39,81 +38,6 @@ bool FloatUtils::areEqual( double x, double y, double epsilon )
return false;
}

double FloatUtils::abs( double x )
{
return fabs( x );
}

bool FloatUtils::areDisequal( double x, double y, double epsilon )
{
return !areEqual( x, y, epsilon );
}

bool FloatUtils::isZero( double x, double epsilon )
{
return ( -epsilon <= x ) && ( x <= epsilon );
}

double FloatUtils::roundToZero( double x, double epsilon )
{
return isZero( x, epsilon ) ? 0.0 : x;
}

bool FloatUtils::isPositive( double x, double epsilon )
{
return ( !isZero( x, epsilon ) ) && ( x > 0.0 );
}

bool FloatUtils::isNegative( double x, double epsilon )
{
return ( !isZero( x, epsilon ) ) && ( x < 0.0 );
}

bool FloatUtils::isFinite( double x )
{
return ( x != infinity() ) && ( x != negativeInfinity() );
}

bool FloatUtils::gt( double x, double y, double epsilon )
{
return isPositive( x - y, epsilon );
}

bool FloatUtils::gte( double x, double y, double epsilon )
{
return !isNegative( x - y, epsilon );
}

bool FloatUtils::lt( double x, double y, double epsilon )
{
return gt( y, x, epsilon );
}

bool FloatUtils::lte( double x, double y, double epsilon )
{
return gte( y, x, epsilon );
}

double FloatUtils::min( double x, double y, double epsilon )
{
return lt( x, y, epsilon ) ? x : y;
}

double FloatUtils::max( double x, double y, double epsilon )
{
return gt( x, y, epsilon ) ? x : y;
}

double FloatUtils::infinity()
{
return DBL_MAX;
}

double FloatUtils::negativeInfinity()
{
return -DBL_MAX;
}

String FloatUtils::doubleToString( double x, unsigned precision )
{
std::ostringstream strout;
Expand All @@ -128,11 +52,6 @@ String FloatUtils::doubleToString( double x, unsigned precision )
return str;
}

bool FloatUtils::wellFormed( double x )
{
return !isNan( x ) && !isInf( x );
}

bool FloatUtils::isNan( double x )
{
return isnan( x );
Expand Down
107 changes: 89 additions & 18 deletions src/common/FloatUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
#ifndef __FloatUtils_h__
#define __FloatUtils_h__

#include "Debug.h"
#include "GlobalConfiguration.h"
#include "MString.h"

#include <cfloat>
#include <math.h>

#ifdef _WIN32
#undef max
Expand All @@ -30,28 +32,97 @@ class FloatUtils
{
public:
static bool areEqual( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS );
static double abs( double x );
static bool areDisequal( double x,
double y,
double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS );
static bool isZero( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS );
static double roundToZero( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS );
static bool isPositive( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS );
static bool isNegative( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS );
static bool gt( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS );
static bool gte( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS );
static bool lt( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS );
static bool lte( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS );
static double min( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS );
static double max( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS );
static double infinity();
static double negativeInfinity();
static bool isFinite( double x );
static String doubleToString( double x,
unsigned precision = GlobalConfiguration::DEFAULT_DOUBLE_TO_STRING_PRECISION );
static bool wellFormed( double x );

static bool isNan( double x );

static bool isInf( double x );

static bool isZero( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS )
{
return ( -epsilon <= x ) && ( x <= epsilon );
}

static bool isPositive( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS )
{
ASSERT( epsilon > 0 );
return x > epsilon;
}

static bool isNegative( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS )
{
ASSERT( epsilon > 0 );
return x < -epsilon;
}

static double abs( double x )
{
return fabs( x );
}

static bool areDisequal( double x,
double y,
double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS )
{
return !areEqual( x, y, epsilon );
}

static double roundToZero( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS )
{
return isZero( x, epsilon ) ? 0.0 : x;
}

static bool gt( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS )
{
return isPositive( x - y, epsilon );
}

static bool gte( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS )
{
return !isNegative( x - y, epsilon );
}

static bool lt( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS )
{
return gt( y, x, epsilon );
}

static bool lte( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS )
{
return gte( y, x, epsilon );
}

static double min( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS )
{
return lt( x, y, epsilon ) ? x : y;
}

static double max( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS )
{
return gt( x, y, epsilon ) ? x : y;
}

static double infinity()
{
return DBL_MAX;
}

static double negativeInfinity()
{
return -DBL_MAX;
}

static bool isFinite( double x )
{
return ( x != infinity() ) && ( x != negativeInfinity() );
}

static bool wellFormed( double x )
{
return !isNan( x ) && !isInf( x );
}

};

#endif // __FloatUtils_h__
Expand Down

0 comments on commit d50793a

Please sign in to comment.