Skip to content

Commit 091a9f4

Browse files
[libc++][test] Avoid truncation warnings from double to float (#74184)
Found while running libc++'s test suite with MSVC's STL, where we use both MSVC's compiler and Clang/LLVM. MSVC really enjoys emitting "warning C4305: 'initializing': truncation from 'double' to 'float'". It might look repetitive, but `T(value)` avoids this warning. (According to my understanding, the compiler looks at the immediate context, and if it's a functional-style cast, it's satisfied that the truncation was intentional. Not even the direct-initialization context of `T unexpected(-9999.99)` is enough to activate that "ok, the programmer knows what they're getting" codepath.) I usually prefer to use `static_cast` instead of functional-style casts, but I chose to remain consistent with the surrounding code. By the way, you might notice that `1.5` doesn't need these changes. This is because it's exactly representable as both a `double` and a `float`. Values like `3.125` instead of `3.1` would similarly avoid truncation warnings without the need for casts, but I didn't want to intrusively change the test code.
1 parent 5fcf3bb commit 091a9f4

15 files changed

+31
-31
lines changed

libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/assign.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void test_impl() {
3131

3232
// assignment
3333
{
34-
MaybeVolatile<std::atomic<T>> a(3.1);
34+
MaybeVolatile<std::atomic<T>> a(T(3.1));
3535
std::same_as<T> decltype(auto) r = (a = T(1.2));
3636
assert(a.load() == T(1.2));
3737
assert(r == T(1.2));

libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/compare_exchange_strong.pass.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ void testBasic(MemoryOrder... memory_order) {
4444
// compare pass
4545
{
4646
MaybeVolatile<std::atomic<T>> a(T(1.2));
47-
T expected(1.2);
48-
const T desired(2.3);
47+
T expected(T(1.2));
48+
const T desired(T(2.3));
4949
std::same_as<bool> decltype(auto) r = a.compare_exchange_strong(expected, desired, memory_order...);
5050

5151
assert(r);
@@ -57,7 +57,7 @@ void testBasic(MemoryOrder... memory_order) {
5757
{
5858
MaybeVolatile<std::atomic<T>> a(T(1.2));
5959
T expected(1.5);
60-
const T desired(2.3);
60+
const T desired(T(2.3));
6161
std::same_as<bool> decltype(auto) r = a.compare_exchange_strong(expected, desired, memory_order...);
6262

6363
assert(!r);
@@ -168,7 +168,7 @@ void test_impl() {
168168
auto store = [](MaybeVolatile<std::atomic<T>>& x, T, T new_val) { x.store(new_val, std::memory_order::release); };
169169
auto load = [](MaybeVolatile<std::atomic<T>>& x) {
170170
auto result = x.load(std::memory_order::relaxed);
171-
T unexpected(-9999.99);
171+
T unexpected(T(-9999.99));
172172
bool r = x.compare_exchange_strong(unexpected, unexpected, std::memory_order_relaxed, std::memory_order_acquire);
173173
assert(!r);
174174
return result;
@@ -177,7 +177,7 @@ void test_impl() {
177177

178178
auto load_one_arg = [](MaybeVolatile<std::atomic<T>>& x) {
179179
auto result = x.load(std::memory_order::relaxed);
180-
T unexpected(-9999.99);
180+
T unexpected(T(-9999.99));
181181
bool r = x.compare_exchange_strong(unexpected, unexpected, std::memory_order_acquire);
182182
assert(!r);
183183
return result;
@@ -187,7 +187,7 @@ void test_impl() {
187187
// acq_rel replaced by acquire
188188
auto load_one_arg_acq_rel = [](MaybeVolatile<std::atomic<T>>& x) {
189189
auto result = x.load(std::memory_order::relaxed);
190-
T unexpected(-9999.99);
190+
T unexpected(T(-9999.99));
191191
bool r = x.compare_exchange_strong(unexpected, unexpected, std::memory_order_acq_rel);
192192
assert(!r);
193193
return result;
@@ -200,7 +200,7 @@ void test_impl() {
200200
auto store = [](MaybeVolatile<std::atomic<T>>& x, T, T new_val) { x.store(new_val, std::memory_order::seq_cst); };
201201
auto load = [](MaybeVolatile<std::atomic<T>>& x) {
202202
auto result = x.load(std::memory_order::relaxed);
203-
T unexpected(-9999.99);
203+
T unexpected(T(-9999.99));
204204
bool r = x.compare_exchange_strong(unexpected, unexpected, std::memory_order_relaxed, std::memory_order::seq_cst);
205205
assert(!r);
206206
return result;

libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/compare_exchange_weak.pass.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ void testBasic(MemoryOrder... memory_order) {
4444
// compare pass
4545
{
4646
MaybeVolatile<std::atomic<T>> a(T(1.2));
47-
T expected(1.2);
48-
const T desired(2.3);
47+
T expected(T(1.2));
48+
const T desired(T(2.3));
4949
std::same_as<bool> decltype(auto) r = a.compare_exchange_weak(expected, desired, memory_order...);
5050

5151
// could be false spuriously
@@ -61,7 +61,7 @@ void testBasic(MemoryOrder... memory_order) {
6161
{
6262
MaybeVolatile<std::atomic<T>> a(T(1.2));
6363
T expected(1.5);
64-
const T desired(2.3);
64+
const T desired(T(2.3));
6565
std::same_as<bool> decltype(auto) r = a.compare_exchange_weak(expected, desired, memory_order...);
6666

6767
assert(!r);
@@ -183,7 +183,7 @@ void test_impl() {
183183
auto store = [](MaybeVolatile<std::atomic<T>>& x, T, T new_val) { x.store(new_val, std::memory_order::release); };
184184
auto load = [](MaybeVolatile<std::atomic<T>>& x) {
185185
auto result = x.load(std::memory_order::relaxed);
186-
T unexpected(-9999.99);
186+
T unexpected(T(-9999.99));
187187
bool r = x.compare_exchange_weak(unexpected, unexpected, std::memory_order_relaxed, std::memory_order_acquire);
188188
assert(!r);
189189
return result;
@@ -192,7 +192,7 @@ void test_impl() {
192192

193193
auto load_one_arg = [](MaybeVolatile<std::atomic<T>>& x) {
194194
auto result = x.load(std::memory_order::relaxed);
195-
T unexpected(-9999.99);
195+
T unexpected(T(-9999.99));
196196
bool r = x.compare_exchange_weak(unexpected, unexpected, std::memory_order_acquire);
197197
assert(!r);
198198
return result;
@@ -202,7 +202,7 @@ void test_impl() {
202202
// acq_rel replaced by acquire
203203
auto load_one_arg_acq_rel = [](MaybeVolatile<std::atomic<T>>& x) {
204204
auto result = x.load(std::memory_order::relaxed);
205-
T unexpected(-9999.99);
205+
T unexpected(T(-9999.99));
206206
bool r = x.compare_exchange_weak(unexpected, unexpected, std::memory_order_acq_rel);
207207
assert(!r);
208208
return result;
@@ -215,7 +215,7 @@ void test_impl() {
215215
auto store = [](MaybeVolatile<std::atomic<T>>& x, T, T new_val) { x.store(new_val, std::memory_order::seq_cst); };
216216
auto load = [](MaybeVolatile<std::atomic<T>>& x) {
217217
auto result = x.load(std::memory_order::relaxed);
218-
T unexpected(-9999.99);
218+
T unexpected(T(-9999.99));
219219
bool r = x.compare_exchange_weak(unexpected, unexpected, std::memory_order_relaxed, std::memory_order::seq_cst);
220220
assert(!r);
221221
return result;

libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/exchange.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ void test_impl() {
3434

3535
// exchange
3636
{
37-
MaybeVolatile<std::atomic<T>> a(3.1);
37+
MaybeVolatile<std::atomic<T>> a(T(3.1));
3838
std::same_as<T> decltype(auto) r = a.exchange(T(1.2), std::memory_order::relaxed);
3939
assert(a.load() == T(1.2));
4040
assert(r == T(3.1));

libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/fetch_add.pass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ void test_impl() {
4242

4343
// fetch_add
4444
{
45-
MaybeVolatile<std::atomic<T>> a(3.1);
45+
MaybeVolatile<std::atomic<T>> a(T(3.1));
4646
std::same_as<T> decltype(auto) r = a.fetch_add(T(1.2), std::memory_order::relaxed);
4747
assert(r == T(3.1));
4848
assert(a.load() == T(3.1) + T(1.2));
@@ -78,7 +78,7 @@ void test_impl() {
7878
return res;
7979
};
8080

81-
assert(at.load() == times(1.234, number_of_threads * loop));
81+
assert(at.load() == times(T(1.234), number_of_threads * loop));
8282
}
8383
#endif
8484

libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/fetch_sub.pass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ void test_impl() {
4343

4444
// fetch_sub
4545
{
46-
MaybeVolatile<std::atomic<T>> a(3.1);
46+
MaybeVolatile<std::atomic<T>> a(T(3.1));
4747
std::same_as<T> decltype(auto) r = a.fetch_sub(T(1.2), std::memory_order::relaxed);
4848
assert(r == T(3.1));
4949
assert(a.load() == T(3.1) - T(1.2));
@@ -79,7 +79,7 @@ void test_impl() {
7979
return res;
8080
};
8181

82-
assert(at.load() == accu_neg(1.234, number_of_threads * loop));
82+
assert(at.load() == accu_neg(T(1.234), number_of_threads * loop));
8383
}
8484
#endif
8585

libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/load.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ void test_impl() {
4141

4242
// load
4343
{
44-
MaybeVolatile<std::atomic<T>> a(3.1);
44+
MaybeVolatile<std::atomic<T>> a(T(3.1));
4545
a.store(T(1.2));
4646
std::same_as<T> decltype(auto) r = a.load(std::memory_order::relaxed);
4747
assert(r == T(1.2));

libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/notify_all.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void test_impl() {
3737
// should x87 80bit long double work at all?
3838
if constexpr (!std::same_as<T, long double>) {
3939
for (auto i = 0; i < 100; ++i) {
40-
const T old = 3.1;
40+
const T old = T(3.1);
4141
MaybeVolatile<std::atomic<T>> a(old);
4242

4343
bool done = false;

libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/notify_one.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void test_impl() {
3737
// should x87 80bit long double work at all?
3838
if constexpr (!std::same_as<T, long double>) {
3939
for (auto i = 0; i < 100; ++i) {
40-
const T old = 3.1;
40+
const T old = T(3.1);
4141
MaybeVolatile<std::atomic<T>> a(old);
4242

4343
std::atomic_bool started = false;

libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/operator.float.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ void test_impl() {
2828

2929
// operator float
3030
{
31-
MaybeVolatile<std::atomic<T>> a(3.1);
31+
MaybeVolatile<std::atomic<T>> a(T(3.1));
3232
T r = a;
3333
assert(r == T(3.1));
3434
}

libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/operator.minus_equals.pass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void test_impl() {
3939

4040
// -=
4141
{
42-
MaybeVolatile<std::atomic<T>> a(3.1);
42+
MaybeVolatile<std::atomic<T>> a(T(3.1));
4343
std::same_as<T> decltype(auto) r = a -= T(1.2);
4444
assert(r == T(3.1) - T(1.2));
4545
assert(a.load() == T(3.1) - T(1.2));
@@ -75,7 +75,7 @@ void test_impl() {
7575
return res;
7676
};
7777

78-
assert(at.load() == accu_neg(1.234, number_of_threads * loop));
78+
assert(at.load() == accu_neg(T(1.234), number_of_threads * loop));
7979
}
8080
#endif
8181

libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/operator.plus_equals.pass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void test_impl() {
3939

4040
// +=
4141
{
42-
MaybeVolatile<std::atomic<T>> a(3.1);
42+
MaybeVolatile<std::atomic<T>> a(T(3.1));
4343
std::same_as<T> decltype(auto) r = a += T(1.2);
4444
assert(r == T(3.1) + T(1.2));
4545
assert(a.load() == T(3.1) + T(1.2));
@@ -75,7 +75,7 @@ void test_impl() {
7575
return res;
7676
};
7777

78-
assert(at.load() == times(1.234, number_of_threads * loop));
78+
assert(at.load() == times(T(1.234), number_of_threads * loop));
7979
}
8080

8181
// memory_order::seq_cst

libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/store.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ void test_impl() {
4141

4242
// store
4343
{
44-
MaybeVolatile<std::atomic<T>> a(3.1);
44+
MaybeVolatile<std::atomic<T>> a(T(3.1));
4545
a.store(T(1.2), std::memory_order::relaxed);
4646
assert(a.load() == T(1.2));
4747
}

libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/test_helper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
template <class T>
2525
bool approximately_equals(T x, T y) {
26-
T epsilon = 0.001;
26+
T epsilon = T(0.001);
2727
return std::abs(x - y) < epsilon;
2828
}
2929

libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/wait.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void test_impl() {
5151
// should x87 80bit long double work at all?
5252
if constexpr (!std::same_as<T, long double>) {
5353
for (auto i = 0; i < 100; ++i) {
54-
const T old = 3.1;
54+
const T old = T(3.1);
5555
MaybeVolatile<std::atomic<T>> a(old);
5656

5757
std::atomic_bool started = false;

0 commit comments

Comments
 (0)