summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/system_wrappers/source/denormal_disabler_unittest.cc
blob: a2849f853fcdaba99da65e7b6a48ba35678a574f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
 *  Copyright (c) 2021 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "system_wrappers/include/denormal_disabler.h"

#include <cmath>
#include <limits>
#include <vector>

#include "rtc_base/checks.h"
#include "test/gtest.h"

namespace webrtc {
namespace {

constexpr float kSmallest = std::numeric_limits<float>::min();

// Float values such that, if used as divisors of `kSmallest`, the division
// produces a denormal or zero depending on whether denormals are enabled.
constexpr float kDenormalDivisors[] = {123.125f, 97.0f, 32.0f, 5.0f, 1.5f};

// Returns true if the result of `dividend` / `divisor` is a denormal.
// `dividend` and `divisor` must not be denormals.
bool DivisionIsDenormal(float dividend, float divisor) {
  RTC_DCHECK_GE(std::fabsf(dividend), kSmallest);
  RTC_DCHECK_GE(std::fabsf(divisor), kSmallest);
  volatile float division = dividend / divisor;
  return division != 0.0f && std::fabsf(division) < kSmallest;
}

}  // namespace

class DenormalDisablerParametrization : public ::testing::TestWithParam<bool> {
};

// Checks that +inf and -inf are not zeroed regardless of whether
// architecture and compiler are supported.
TEST_P(DenormalDisablerParametrization, InfNotZeroedExplicitlySetEnabled) {
  DenormalDisabler denormal_disabler(/*enabled=*/GetParam());
  constexpr float kMax = std::numeric_limits<float>::max();
  for (float x : {-2.0f, 2.0f}) {
    SCOPED_TRACE(x);
    volatile float multiplication = kMax * x;
    EXPECT_TRUE(std::isinf(multiplication));
  }
}

// Checks that a NaN is not zeroed regardless of whether architecture and
// compiler are supported.
TEST_P(DenormalDisablerParametrization, NanNotZeroedExplicitlySetEnabled) {
  DenormalDisabler denormal_disabler(/*enabled=*/GetParam());
  volatile float kNan = std::sqrt(-1.0f);
  EXPECT_TRUE(std::isnan(kNan));
}

INSTANTIATE_TEST_SUITE_P(DenormalDisabler,
                         DenormalDisablerParametrization,
                         ::testing::Values(false, true),
                         [](const ::testing::TestParamInfo<bool>& info) {
                           return info.param ? "enabled" : "disabled";
                         });

// Checks that +inf and -inf are not zeroed regardless of whether
// architecture and compiler are supported.
TEST(DenormalDisabler, InfNotZeroed) {
  DenormalDisabler denormal_disabler;
  constexpr float kMax = std::numeric_limits<float>::max();
  for (float x : {-2.0f, 2.0f}) {
    SCOPED_TRACE(x);
    volatile float multiplication = kMax * x;
    EXPECT_TRUE(std::isinf(multiplication));
  }
}

// Checks that a NaN is not zeroed regardless of whether architecture and
// compiler are supported.
TEST(DenormalDisabler, NanNotZeroed) {
  DenormalDisabler denormal_disabler;
  volatile float kNan = std::sqrt(-1.0f);
  EXPECT_TRUE(std::isnan(kNan));
}

// Checks that denormals are not zeroed if `DenormalDisabler` is disabled and
// architecture and compiler are supported.
TEST(DenormalDisabler, DoNotZeroDenormalsIfDisabled) {
  if (!DenormalDisabler::IsSupported()) {
    GTEST_SKIP() << "Unsupported platform.";
  }
  ASSERT_TRUE(DivisionIsDenormal(kSmallest, kDenormalDivisors[0]))
      << "Precondition not met: denormals must be enabled.";
  DenormalDisabler denormal_disabler(/*enabled=*/false);
  for (float x : kDenormalDivisors) {
    SCOPED_TRACE(x);
    EXPECT_TRUE(DivisionIsDenormal(-kSmallest, x));
    EXPECT_TRUE(DivisionIsDenormal(kSmallest, x));
  }
}

// Checks that denormals are zeroed if `DenormalDisabler` is enabled if
// architecture and compiler are supported.
TEST(DenormalDisabler, ZeroDenormals) {
  if (!DenormalDisabler::IsSupported()) {
    GTEST_SKIP() << "Unsupported platform.";
  }
  DenormalDisabler denormal_disabler;
  for (float x : kDenormalDivisors) {
    SCOPED_TRACE(x);
    EXPECT_FALSE(DivisionIsDenormal(-kSmallest, x));
    EXPECT_FALSE(DivisionIsDenormal(kSmallest, x));
  }
}

// Checks that denormals are zeroed if `DenormalDisabler` is enabled if
// architecture and compiler are supported.
TEST(DenormalDisabler, ZeroDenormalsExplicitlyEnabled) {
  if (!DenormalDisabler::IsSupported()) {
    GTEST_SKIP() << "Unsupported platform.";
  }
  DenormalDisabler denormal_disabler(/*enabled=*/true);
  for (float x : kDenormalDivisors) {
    SCOPED_TRACE(x);
    EXPECT_FALSE(DivisionIsDenormal(-kSmallest, x));
    EXPECT_FALSE(DivisionIsDenormal(kSmallest, x));
  }
}

// Checks that the `DenormalDisabler` dtor re-enables denormals if previously
// enabled and architecture and compiler are supported.
TEST(DenormalDisabler, RestoreDenormalsEnabled) {
  if (!DenormalDisabler::IsSupported()) {
    GTEST_SKIP() << "Unsupported platform.";
  }
  ASSERT_TRUE(DivisionIsDenormal(kSmallest, kDenormalDivisors[0]))
      << "Precondition not met: denormals must be enabled.";
  {
    DenormalDisabler denormal_disabler;
    ASSERT_FALSE(DivisionIsDenormal(kSmallest, kDenormalDivisors[0]));
  }
  EXPECT_TRUE(DivisionIsDenormal(kSmallest, kDenormalDivisors[0]));
}

// Checks that the `DenormalDisabler` dtor re-enables denormals if previously
// enabled and architecture and compiler are supported.
TEST(DenormalDisabler, RestoreDenormalsEnabledExplicitlyEnabled) {
  if (!DenormalDisabler::IsSupported()) {
    GTEST_SKIP() << "Unsupported platform.";
  }
  ASSERT_TRUE(DivisionIsDenormal(kSmallest, kDenormalDivisors[0]))
      << "Precondition not met: denormals must be enabled.";
  {
    DenormalDisabler denormal_disabler(/*enabled=*/true);
    ASSERT_FALSE(DivisionIsDenormal(kSmallest, kDenormalDivisors[0]));
  }
  EXPECT_TRUE(DivisionIsDenormal(kSmallest, kDenormalDivisors[0]));
}

// Checks that the `DenormalDisabler` dtor keeps denormals disabled if
// architecture and compiler are supported and if previously disabled - i.e.,
// nested usage is supported.
TEST(DenormalDisabler, ZeroDenormalsNested) {
  if (!DenormalDisabler::IsSupported()) {
    GTEST_SKIP() << "Unsupported platform.";
  }
  DenormalDisabler d1;
  ASSERT_FALSE(DivisionIsDenormal(kSmallest, kDenormalDivisors[0]));
  {
    DenormalDisabler d2;
    ASSERT_FALSE(DivisionIsDenormal(kSmallest, kDenormalDivisors[0]));
  }
  EXPECT_FALSE(DivisionIsDenormal(kSmallest, kDenormalDivisors[0]));
}

// Checks that the `DenormalDisabler` dtor keeps denormals disabled if
// architecture and compiler are supported and if previously disabled - i.e.,
// nested usage is supported.
TEST(DenormalDisabler, ZeroDenormalsNestedExplicitlyEnabled) {
  if (!DenormalDisabler::IsSupported()) {
    GTEST_SKIP() << "Unsupported platform.";
  }
  DenormalDisabler d1(/*enabled=*/true);
  ASSERT_FALSE(DivisionIsDenormal(kSmallest, kDenormalDivisors[0]));
  {
    DenormalDisabler d2(/*enabled=*/true);
    ASSERT_FALSE(DivisionIsDenormal(kSmallest, kDenormalDivisors[0]));
  }
  EXPECT_FALSE(DivisionIsDenormal(kSmallest, kDenormalDivisors[0]));
}

// Checks that `DenormalDisabler` does not zero denormals if architecture and
// compiler are not supported.
TEST(DenormalDisabler, DoNotZeroDenormalsIfUnsupported) {
  if (DenormalDisabler::IsSupported()) {
    GTEST_SKIP() << "This test should only run on platforms without support "
                    "for DenormalDisabler.";
  }
  DenormalDisabler denormal_disabler;
  for (float x : kDenormalDivisors) {
    SCOPED_TRACE(x);
    EXPECT_TRUE(DivisionIsDenormal(-kSmallest, x));
    EXPECT_TRUE(DivisionIsDenormal(kSmallest, x));
  }
}

// Checks that `DenormalDisabler` does not zero denormals if architecture and
// compiler are not supported.
TEST(DenormalDisabler, DoNotZeroDenormalsIfUnsupportedExplicitlyEnabled) {
  if (DenormalDisabler::IsSupported()) {
    GTEST_SKIP() << "This test should only run on platforms without support "
                    "for DenormalDisabler.";
  }
  DenormalDisabler denormal_disabler(/*enabled=*/true);
  for (float x : kDenormalDivisors) {
    SCOPED_TRACE(x);
    EXPECT_TRUE(DivisionIsDenormal(-kSmallest, x));
    EXPECT_TRUE(DivisionIsDenormal(kSmallest, x));
  }
}

}  // namespace webrtc