https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108281
Bug ID: 108281 Summary: float value range estimation missing (vs. integer) Product: gcc Version: 12.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: target Assignee: unassigned at gcc dot gnu.org Reporter: aleks at physik dot tu-berlin.de Target Milestone: --- "gcc -O3" and optional: "-funsafe-math-optimizations" (isnan) GCC ignores ranges of float numbers for optimization, tested via https://godbolt.org/ For many frequent used functions (or even all in math.h) gcc could know a range limit and concidering it for comparisons. #include <math.h> int ranges(float x) { if (x!=x) return -1; // optional NAN check if (cos(x) < -1.0f) return -2; if (sin(x) > 1.0f) return -3; if (fabs(x) < 0.0f) return -4; if (atan(x) < -2.0f) return -5; // +-PI/2 if (exp(x) < 0.0f) return -6; if (sqrt(x) < 0.0f) return -7; if (log(x) < 90.0f) return -8; // ln(FLT_MAX)=88.8 // ln(DBL_MAX) = 709.8 return 0; // the only valid return (beside -1) } int sqr2(float x) { // squares give non-negative results return x*x < 0.0f; // == false } int ax2(float x) { return fabs(x) > -1.0f; // == true } int cmp_sqrt(float x, float y) { // similar (!sadly very often seen!) //x = fabs(x); y = fabs(y); // optional sign removal line return sqrtf(x) < sqrtf(y); // == (x < y), hint: sqrt=slow }