24 changes: 15 additions & 9 deletions libc/src/string/memory_utils/elements_x86.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#ifndef LLVM_LIBC_SRC_STRING_MEMORY_UTILS_ELEMENTS_X86_H
#define LLVM_LIBC_SRC_STRING_MEMORY_UTILS_ELEMENTS_X86_H

#include "src/__support/CPP/Bit.h"
#include "src/__support/architectures.h"

#if defined(LLVM_LIBC_ARCH_X86)
Expand Down Expand Up @@ -66,16 +67,18 @@ struct M128 {
using T = char __attribute__((__vector_size__(SIZE)));
static uint16_t mask(T value) {
// NOLINTNEXTLINE(llvmlibc-callee-namespace)
return _mm_movemask_epi8(value);
return _mm_movemask_epi8(__llvm_libc::bit_cast<__m128i>(value));
}
static uint16_t not_equal_mask(T a, T b) { return mask(a != b); }
static T load(const char *ptr) {
// NOLINTNEXTLINE(llvmlibc-callee-namespace)
return _mm_loadu_si128(reinterpret_cast<__m128i_u const *>(ptr));
return __llvm_libc::bit_cast<T>(
_mm_loadu_si128(reinterpret_cast<__m128i_u const *>(ptr)));
}
static void store(char *ptr, T value) {
// NOLINTNEXTLINE(llvmlibc-callee-namespace)
return _mm_storeu_si128(reinterpret_cast<__m128i_u *>(ptr), value);
return _mm_storeu_si128(reinterpret_cast<__m128i_u *>(ptr),
__llvm_libc::bit_cast<__m128i>(value));
}
static T get_splatted_value(const char v) {
const T splatted = {v, v, v, v, v, v, v, v, v, v, v, v, v, v, v, v};
Expand All @@ -91,16 +94,18 @@ struct M256 {
using T = char __attribute__((__vector_size__(SIZE)));
static uint32_t mask(T value) {
// NOLINTNEXTLINE(llvmlibc-callee-namespace)
return _mm256_movemask_epi8(value);
return _mm256_movemask_epi8(__llvm_libc::bit_cast<__m256i>(value));
}
static uint32_t not_equal_mask(T a, T b) { return mask(a != b); }
static T load(const char *ptr) {
// NOLINTNEXTLINE(llvmlibc-callee-namespace)
return _mm256_loadu_si256(reinterpret_cast<__m256i const *>(ptr));
return __llvm_libc::bit_cast<T>(
_mm256_loadu_si256(reinterpret_cast<__m256i const *>(ptr)));
}
static void store(char *ptr, T value) {
// NOLINTNEXTLINE(llvmlibc-callee-namespace)
return _mm256_storeu_si256(reinterpret_cast<__m256i *>(ptr), value);
return _mm256_storeu_si256(reinterpret_cast<__m256i *>(ptr),
__llvm_libc::bit_cast<__m256i>(value));
}
static T get_splatted_value(const char v) {
const T splatted = {v, v, v, v, v, v, v, v, v, v, v, v, v, v, v, v,
Expand All @@ -117,15 +122,16 @@ struct M512 {
using T = char __attribute__((__vector_size__(SIZE)));
static uint64_t not_equal_mask(T a, T b) {
// NOLINTNEXTLINE(llvmlibc-callee-namespace)
return _mm512_cmpneq_epi8_mask(a, b);
return _mm512_cmpneq_epi8_mask(__llvm_libc::bit_cast<__m512i>(a),
__llvm_libc::bit_cast<__m512i>(b));
}
static T load(const char *ptr) {
// NOLINTNEXTLINE(llvmlibc-callee-namespace)
return _mm512_loadu_epi8(ptr);
return __llvm_libc::bit_cast<T>(_mm512_loadu_epi8(ptr));
}
static void store(char *ptr, T value) {
// NOLINTNEXTLINE(llvmlibc-callee-namespace)
return _mm512_storeu_epi8(ptr, value);
return _mm512_storeu_epi8(ptr, __llvm_libc::bit_cast<__m512i>(value));
}
static T get_splatted_value(const char v) {
const T splatted = {v, v, v, v, v, v, v, v, v, v, v, v, v, v, v, v,
Expand Down
41 changes: 21 additions & 20 deletions libc/test/src/math/NextAfterTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#ifndef LLVM_LIBC_TEST_SRC_MATH_NEXTAFTERTEST_H
#define LLVM_LIBC_TEST_SRC_MATH_NEXTAFTERTEST_H

#include "src/__support/CPP/Bit.h"
#include "src/__support/CPP/TypeTraits.h"
#include "src/__support/FPUtil/BasicOperations.h"
#include "src/__support/FPUtil/FPBits.h"
Expand Down Expand Up @@ -51,90 +52,90 @@ class NextAfterTestTemplate : public __llvm_libc::testing::Test {
T x = zero;
T result = func(x, T(1));
UIntType expected_bits = 1;
T expected = *reinterpret_cast<T *>(&expected_bits);
T expected = __llvm_libc::bit_cast<T>(expected_bits);
ASSERT_FP_EQ(result, expected);

result = func(x, T(-1));
expected_bits = (UIntType(1) << (BIT_WIDTH_OF_TYPE - 1)) + 1;
expected = *reinterpret_cast<T *>(&expected_bits);
expected = __llvm_libc::bit_cast<T>(expected_bits);
ASSERT_FP_EQ(result, expected);

x = neg_zero;
result = func(x, 1);
expected_bits = 1;
expected = *reinterpret_cast<T *>(&expected_bits);
expected = __llvm_libc::bit_cast<T>(expected_bits);
ASSERT_FP_EQ(result, expected);

result = func(x, -1);
expected_bits = (UIntType(1) << (BIT_WIDTH_OF_TYPE - 1)) + 1;
expected = *reinterpret_cast<T *>(&expected_bits);
expected = __llvm_libc::bit_cast<T>(expected_bits);
ASSERT_FP_EQ(result, expected);

// 'from' is max subnormal value.
x = *reinterpret_cast<const T *>(&max_subnormal);
x = __llvm_libc::bit_cast<T>(max_subnormal);
result = func(x, 1);
expected = *reinterpret_cast<const T *>(&min_normal);
expected = __llvm_libc::bit_cast<T>(min_normal);
ASSERT_FP_EQ(result, expected);

result = func(x, 0);
expected_bits = max_subnormal - 1;
expected = *reinterpret_cast<T *>(&expected_bits);
expected = __llvm_libc::bit_cast<T>(expected_bits);
ASSERT_FP_EQ(result, expected);

x = -x;

result = func(x, -1);
expected_bits = (UIntType(1) << (BIT_WIDTH_OF_TYPE - 1)) + min_normal;
expected = *reinterpret_cast<T *>(&expected_bits);
expected = __llvm_libc::bit_cast<T>(expected_bits);
ASSERT_FP_EQ(result, expected);

result = func(x, 0);
expected_bits =
(UIntType(1) << (BIT_WIDTH_OF_TYPE - 1)) + max_subnormal - 1;
expected = *reinterpret_cast<T *>(&expected_bits);
expected = __llvm_libc::bit_cast<T>(expected_bits);
ASSERT_FP_EQ(result, expected);

// 'from' is min subnormal value.
x = *reinterpret_cast<const T *>(&min_subnormal);
x = __llvm_libc::bit_cast<T>(min_subnormal);
result = func(x, 1);
expected_bits = min_subnormal + 1;
expected = *reinterpret_cast<T *>(&expected_bits);
expected = __llvm_libc::bit_cast<T>(expected_bits);
ASSERT_FP_EQ(result, expected);
ASSERT_FP_EQ(func(x, 0), 0);

x = -x;
result = func(x, -1);
expected_bits =
(UIntType(1) << (BIT_WIDTH_OF_TYPE - 1)) + min_subnormal + 1;
expected = *reinterpret_cast<T *>(&expected_bits);
expected = __llvm_libc::bit_cast<T>(expected_bits);
ASSERT_FP_EQ(result, expected);
ASSERT_FP_EQ(func(x, 0), T(-0.0));

// 'from' is min normal.
x = *reinterpret_cast<const T *>(&min_normal);
x = __llvm_libc::bit_cast<T>(min_normal);
result = func(x, 0);
expected_bits = max_subnormal;
expected = *reinterpret_cast<T *>(&expected_bits);
expected = __llvm_libc::bit_cast<T>(expected_bits);
ASSERT_FP_EQ(result, expected);

result = func(x, inf);
expected_bits = min_normal + 1;
expected = *reinterpret_cast<T *>(&expected_bits);
expected = __llvm_libc::bit_cast<T>(expected_bits);
ASSERT_FP_EQ(result, expected);

x = -x;
result = func(x, 0);
expected_bits = (UIntType(1) << (BIT_WIDTH_OF_TYPE - 1)) + max_subnormal;
expected = *reinterpret_cast<T *>(&expected_bits);
expected = __llvm_libc::bit_cast<T>(expected_bits);
ASSERT_FP_EQ(result, expected);

result = func(x, -inf);
expected_bits = (UIntType(1) << (BIT_WIDTH_OF_TYPE - 1)) + min_normal + 1;
expected = *reinterpret_cast<T *>(&expected_bits);
expected = __llvm_libc::bit_cast<T>(expected_bits);
ASSERT_FP_EQ(result, expected);

// 'from' is max normal and 'to' is infinity.
x = *reinterpret_cast<const T *>(&max_normal);
x = __llvm_libc::bit_cast<T>(max_normal);
result = func(x, inf);
ASSERT_FP_EQ(result, inf);

Expand All @@ -145,14 +146,14 @@ class NextAfterTestTemplate : public __llvm_libc::testing::Test {
x = inf;
result = func(x, 0);
expected_bits = max_normal;
expected = *reinterpret_cast<T *>(&expected_bits);
expected = __llvm_libc::bit_cast<T>(expected_bits);
ASSERT_FP_EQ(result, expected);
ASSERT_FP_EQ(func(x, inf), inf);

x = neg_inf;
result = func(x, 0);
expected_bits = (UIntType(1) << (BIT_WIDTH_OF_TYPE - 1)) + max_normal;
expected = *reinterpret_cast<T *>(&expected_bits);
expected = __llvm_libc::bit_cast<T>(expected_bits);
ASSERT_FP_EQ(result, expected);
ASSERT_FP_EQ(func(x, neg_inf), neg_inf);

Expand Down
5 changes: 3 additions & 2 deletions libc/test/src/math/SqrtTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "src/__support/CPP/Bit.h"
#include "utils/MPFRWrapper/MPFRUtils.h"
#include "utils/UnitTest/FPMatcher.h"
#include "utils/UnitTest/Test.h"
Expand Down Expand Up @@ -47,7 +48,7 @@ template <typename T> class SqrtTest : public __llvm_libc::testing::Test {
constexpr UIntType COUNT = 1'000'001;
constexpr UIntType STEP = HIDDEN_BIT / COUNT;
for (UIntType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
T x = *reinterpret_cast<T *>(&v);
T x = __llvm_libc::bit_cast<T>(v);
test_all_rounding_modes(func, x);
}
}
Expand All @@ -56,7 +57,7 @@ template <typename T> class SqrtTest : public __llvm_libc::testing::Test {
constexpr UIntType COUNT = 10'000'001;
constexpr UIntType STEP = UIntType(-1) / COUNT;
for (UIntType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
T x = *reinterpret_cast<T *>(&v);
T x = __llvm_libc::bit_cast<T>(v);
if (isnan(x) || (x < 0)) {
continue;
}
Expand Down
2 changes: 2 additions & 0 deletions utils/bazel/llvm-project-overlay/libc/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ cc_library(
hdrs = [
"src/__support/CPP/Array.h",
"src/__support/CPP/ArrayRef.h",
"src/__support/CPP/Bit.h",
"src/__support/CPP/Bitset.h",
"src/__support/CPP/Functional.h",
"src/__support/CPP/Limits.h",
Expand Down Expand Up @@ -661,6 +662,7 @@ cc_library(
],
deps = [
":__support_common",
":__support_standalone_cpp",
":libc_root",
],
)
Expand Down