Skip to content

Commit

Permalink
Fix undefined behaviour of std::numeric_limits<double>::infinity()) i…
Browse files Browse the repository at this point in the history
…n -ffastmath (-ffinite-math-only) code.
  • Loading branch information
daschuer committed Nov 14, 2024
1 parent 2f93444 commit 654323d
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 16 deletions.
10 changes: 5 additions & 5 deletions src/test/durationutiltest.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include <limits>

#include <gtest/gtest.h>

#include "util/duration.h"

#include <QtDebug>
#include <limits>

#include "util/duration.h"
#include "util/fpclassify.h"

namespace {

Expand Down Expand Up @@ -115,7 +115,7 @@ TEST_F(DurationUtilTest, formatTime) {
formatTime("24:00:00.000", 24 * 3600);
formatTime("24:00:01.000", 24 * 3600 + 1);
formatTime("25:00:01.000", 25 * 3600 + 1);
formatTime("?", std::numeric_limits<double>::infinity());
formatTime("?", util_double_infinity());
formatTime("?", -1);
}

Expand Down
13 changes: 5 additions & 8 deletions src/test/frametest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <QDebug>

#include "audio/frame.h"
#include "util/fpclassify.h"

class FrameTest : public testing::Test {
};
Expand All @@ -18,7 +19,7 @@ TEST_F(FrameTest, TestFramePosValid) {
// Denormals
EXPECT_TRUE(mixxx::audio::FramePos(0.0).isValid());
EXPECT_TRUE(mixxx::audio::FramePos(std::numeric_limits<double>::min() / 2.0).isValid());
EXPECT_FALSE(mixxx::audio::FramePos(std::numeric_limits<double>::infinity()).isValid());
EXPECT_FALSE(mixxx::audio::FramePos(util_double_infinity()).isValid());
// NaN
EXPECT_FALSE(mixxx::audio::FramePos().isValid());
EXPECT_FALSE(mixxx::audio::FramePos(std::numeric_limits<double>::quiet_NaN()).isValid());
Expand Down Expand Up @@ -54,16 +55,12 @@ TEST_F(FrameTest, TestFramePosEquality) {
mixxx::audio::FramePos(std::numeric_limits<
mixxx::audio::FramePos::value_t>::quiet_NaN()));
EXPECT_EQ(mixxx::audio::FramePos(),
mixxx::audio::FramePos(std::numeric_limits<
mixxx::audio::FramePos::value_t>::infinity()));
mixxx::audio::FramePos(util_double_infinity()));
EXPECT_EQ(mixxx::audio::FramePos(),
mixxx::audio::FramePos(
-std::numeric_limits<
mixxx::audio::FramePos::value_t>::infinity()));
mixxx::audio::FramePos(-util_double_infinity()));
EXPECT_EQ(mixxx::audio::FramePos(std::numeric_limits<
mixxx::audio::FramePos::value_t>::quiet_NaN()),
mixxx::audio::FramePos(std::numeric_limits<
mixxx::audio::FramePos::value_t>::infinity()));
mixxx::audio::FramePos(util_double_infinity()));
}

TEST_F(FrameTest, LowerFrameBoundary) {
Expand Down
6 changes: 3 additions & 3 deletions src/test/mathutiltest.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include <gtest/gtest.h>

#include <QtDebug>
#include <limits>

#include "util/denormalsarezero.h"
#include "util/fpclassify.h"
#include "util/math.h"

namespace {
Expand Down Expand Up @@ -51,11 +51,11 @@ TEST_F(MathUtilTest, IsNaN) {
TEST_F(MathUtilTest, IsInf) {
// Test floats can be recognized as infinity.
EXPECT_FALSE(util_isinf(0.0f));
EXPECT_TRUE(util_isinf(std::numeric_limits<float>::infinity()));
EXPECT_TRUE(util_isinf(util_float_infinity()));

// Test doubles can be recognized as infinity.
EXPECT_FALSE(util_isinf(0.0f));
EXPECT_TRUE(util_isinf(std::numeric_limits<double>::infinity()));
EXPECT_TRUE(util_isinf(util_double_infinity()));
}

TEST_F(MathUtilTest, Denormal) {
Expand Down
9 changes: 9 additions & 0 deletions src/util/fpclassify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#endif

#include <cmath>
#include <limits>

int util_fpclassify(float x) {
return std::fpclassify(x);
Expand Down Expand Up @@ -48,3 +49,11 @@ int util_isnan(double x) {
int util_isinf(double x) {
return std::isinf(x);
}

float util_float_infinity() {
return std::numeric_limits<double>::infinity();
}

double util_double_infinity() {
return std::numeric_limits<double>::infinity();
}
3 changes: 3 additions & 0 deletions src/util/fpclassify.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ int util_isnan(double x);

int util_isinf(float x);
int util_isinf(double x);

float util_float_infinity();
double util_double_infinity();

0 comments on commit 654323d

Please sign in to comment.