Skip to content

Commit

Permalink
[libc][NFC] Move Sign type to separate header (llvm#85930)
Browse files Browse the repository at this point in the history
  • Loading branch information
gchatelet committed Mar 20, 2024
1 parent 5f5a641 commit 2137894
Show file tree
Hide file tree
Showing 54 changed files with 94 additions and 82 deletions.
8 changes: 8 additions & 0 deletions libc/src/__support/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ add_header_library(
libc.src.__support.macros.config
)

add_header_library(
sign
HDRS
sign.h
DEPENDS
libc.src.__support.macros.attributes
)

add_header_library(
error_or
HDRS
Expand Down
1 change: 1 addition & 0 deletions libc/src/__support/FPUtil/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ add_header_library(
libc.src.__support.macros.attributes
libc.src.__support.macros.properties.types
libc.src.__support.math_extras
libc.src.__support.sign
libc.src.__support.uint128
)

Expand Down
27 changes: 1 addition & 26 deletions libc/src/__support/FPUtil/FPBits.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "src/__support/macros/attributes.h" // LIBC_INLINE, LIBC_INLINE_VAR
#include "src/__support/macros/properties/types.h" // LIBC_TYPES_HAS_FLOAT128
#include "src/__support/math_extras.h" // mask_trailing_ones
#include "src/__support/sign.h" // Sign

#include <stdint.h>

Expand All @@ -32,32 +33,6 @@ enum class FPType {
X86_Binary80,
};

// A type to interact with floating point type signs.
// This may be moved outside of 'fputil' if useful.
struct Sign {
LIBC_INLINE constexpr bool is_pos() const { return !is_negative; }
LIBC_INLINE constexpr bool is_neg() const { return is_negative; }

LIBC_INLINE friend constexpr bool operator==(Sign a, Sign b) {
return a.is_negative == b.is_negative;
}
LIBC_INLINE friend constexpr bool operator!=(Sign a, Sign b) {
return !(a == b);
}

static const Sign POS;
static const Sign NEG;

private:
LIBC_INLINE constexpr explicit Sign(bool is_negative)
: is_negative(is_negative) {}

bool is_negative;
};

LIBC_INLINE_VAR constexpr Sign Sign::NEG = Sign(true);
LIBC_INLINE_VAR constexpr Sign Sign::POS = Sign(false);

// The classes hierarchy is as follows:
//
// ┌───────────────────┐
Expand Down
1 change: 0 additions & 1 deletion libc/src/__support/FPUtil/fpbits_str.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ using ZeroPaddedHexFmt = IntegerToString<
// floating encoding.
template <typename T> LIBC_INLINE cpp::string str(fputil::FPBits<T> x) {
using StorageType = typename fputil::FPBits<T>::StorageType;
using Sign = fputil::Sign;

if (x.is_nan())
return "(NaN)";
Expand Down
40 changes: 40 additions & 0 deletions libc/src/__support/sign.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//===-- A simple sign type --------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC___SUPPORT_SIGN_H
#define LLVM_LIBC_SRC___SUPPORT_SIGN_H

#include "src/__support/macros/attributes.h" // LIBC_INLINE, LIBC_INLINE_VAR

// A type to interact with signed arithmetic types.
struct Sign {
LIBC_INLINE constexpr bool is_pos() const { return !is_negative; }
LIBC_INLINE constexpr bool is_neg() const { return is_negative; }

LIBC_INLINE friend constexpr bool operator==(Sign a, Sign b) {
return a.is_negative == b.is_negative;
}

LIBC_INLINE friend constexpr bool operator!=(Sign a, Sign b) {
return !(a == b);
}

static const Sign POS;
static const Sign NEG;

private:
LIBC_INLINE constexpr explicit Sign(bool is_negative)
: is_negative(is_negative) {}

bool is_negative;
};

LIBC_INLINE_VAR constexpr Sign Sign::NEG = Sign(true);
LIBC_INLINE_VAR constexpr Sign Sign::POS = Sign(false);

#endif // LLVM_LIBC_SRC___SUPPORT_SIGN_H
4 changes: 1 addition & 3 deletions libc/src/__support/str_to_float.h
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,6 @@ clinger_fast_path(ExpandedFloat<T> init_num,
RoundDirection round = RoundDirection::Nearest) {
using FPBits = typename fputil::FPBits<T>;
using StorageType = typename FPBits::StorageType;
using Sign = fputil::Sign;

StorageType mantissa = init_num.mantissa;
int32_t exp10 = init_num.exponent;
Expand Down Expand Up @@ -1085,7 +1084,6 @@ template <class T>
LIBC_INLINE StrToNumResult<T> strtofloatingpoint(const char *__restrict src) {
using FPBits = typename fputil::FPBits<T>;
using StorageType = typename FPBits::StorageType;
using Sign = fputil::Sign;

FPBits result = FPBits();
bool seen_digit = false;
Expand Down Expand Up @@ -1223,7 +1221,7 @@ template <class T> LIBC_INLINE StrToNumResult<T> strtonan(const char *arg) {
if (arg[index] == '\0')
nan_mantissa = nan_mantissa_from_ncharseq<T>(cpp::string_view(arg, index));

result = FPBits::quiet_nan(fputil::Sign::POS, nan_mantissa);
result = FPBits::quiet_nan(Sign::POS, nan_mantissa);
return {result.get_val(), 0, error};
}

Expand Down
2 changes: 1 addition & 1 deletion libc/src/math/generic/acosf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ static constexpr fputil::ExceptValues<float, N_EXCEPTS> ACOSF_EXCEPTS = {{

LLVM_LIBC_FUNCTION(float, acosf, (float x)) {
using FPBits = typename fputil::FPBits<float>;
using Sign = fputil::Sign;

FPBits xbits(x);
uint32_t x_uint = xbits.uintval();
uint32_t x_abs = xbits.uintval() & 0x7fff'ffffU;
Expand Down
2 changes: 1 addition & 1 deletion libc/src/math/generic/asinf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ static constexpr fputil::ExceptValues<float, N_EXCEPTS> ASINF_EXCEPTS_HI = {{

LLVM_LIBC_FUNCTION(float, asinf, (float x)) {
using FPBits = typename fputil::FPBits<float>;
using Sign = fputil::Sign;

FPBits xbits(x);
uint32_t x_uint = xbits.uintval();
uint32_t x_abs = xbits.uintval() & 0x7fff'ffffU;
Expand Down
1 change: 0 additions & 1 deletion libc/src/math/generic/atanf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(float, atanf, (float x)) {
using FPBits = typename fputil::FPBits<float>;
using Sign = fputil::Sign;

constexpr double FINAL_SIGN[2] = {1.0, -1.0};
constexpr double SIGNED_PI_OVER_2[2] = {0x1.921fb54442d18p0,
Expand Down
2 changes: 1 addition & 1 deletion libc/src/math/generic/atanhf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(float, atanhf, (float x)) {
using FPBits = typename fputil::FPBits<float>;
using Sign = fputil::Sign;

FPBits xbits(x);
Sign sign = xbits.sign();
uint32_t x_abs = xbits.abs().uintval();
Expand Down
2 changes: 1 addition & 1 deletion libc/src/math/generic/cosf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ static constexpr fputil::ExceptValues<float, N_EXCEPTS> COSF_EXCEPTS{{

LLVM_LIBC_FUNCTION(float, cosf, (float x)) {
using FPBits = typename fputil::FPBits<float>;
using Sign = fputil::Sign;

FPBits xbits(x);
xbits.set_sign(Sign::POS);

Expand Down
2 changes: 1 addition & 1 deletion libc/src/math/generic/coshf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(float, coshf, (float x)) {
using FPBits = typename fputil::FPBits<float>;
using Sign = fputil::Sign;

FPBits xbits(x);
xbits.set_sign(Sign::POS);
x = xbits.get_val();
Expand Down
2 changes: 1 addition & 1 deletion libc/src/math/generic/exp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace LIBC_NAMESPACE {
using fputil::DoubleDouble;
using fputil::TripleDouble;
using Float128 = typename fputil::DyadicFloat<128>;
using Sign = fputil::Sign;

using LIBC_NAMESPACE::operator""_u128;

// log2(e)
Expand Down
2 changes: 1 addition & 1 deletion libc/src/math/generic/exp10.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace LIBC_NAMESPACE {
using fputil::DoubleDouble;
using fputil::TripleDouble;
using Float128 = typename fputil::DyadicFloat<128>;
using Sign = fputil::Sign;

using LIBC_NAMESPACE::operator""_u128;

// log2(10)
Expand Down
2 changes: 1 addition & 1 deletion libc/src/math/generic/exp2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace LIBC_NAMESPACE {
using fputil::DoubleDouble;
using fputil::TripleDouble;
using Float128 = typename fputil::DyadicFloat<128>;
using Sign = fputil::Sign;

using LIBC_NAMESPACE::operator""_u128;

// Error bounds:
Expand Down
4 changes: 2 additions & 2 deletions libc/src/math/generic/expm1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace LIBC_NAMESPACE {
using fputil::DoubleDouble;
using fputil::TripleDouble;
using Float128 = typename fputil::DyadicFloat<128>;
using Sign = fputil::Sign;

using LIBC_NAMESPACE::operator""_u128;

// log2(e)
Expand Down Expand Up @@ -276,7 +276,7 @@ double set_exceptional(double x) {

LLVM_LIBC_FUNCTION(double, expm1, (double x)) {
using FPBits = typename fputil::FPBits<double>;
using Sign = fputil::Sign;

FPBits xbits(x);

bool x_is_neg = xbits.is_neg();
Expand Down
4 changes: 2 additions & 2 deletions libc/src/math/generic/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace LIBC_NAMESPACE {

// 128-bit precision dyadic floating point numbers.
using Float128 = typename fputil::DyadicFloat<128>;
using Sign = fputil::Sign;

using LIBC_NAMESPACE::operator""_u128;

namespace {
Expand Down Expand Up @@ -735,7 +735,7 @@ double log_accurate(int e_x, int index, double m_x) {

LLVM_LIBC_FUNCTION(double, log, (double x)) {
using FPBits_t = typename fputil::FPBits<double>;
using Sign = fputil::Sign;

FPBits_t xbits(x);
uint64_t x_u = xbits.uintval();

Expand Down
4 changes: 2 additions & 2 deletions libc/src/math/generic/log10.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace LIBC_NAMESPACE {

// 128-bit precision dyadic floating point numbers.
using Float128 = typename fputil::DyadicFloat<128>;
using Sign = fputil::Sign;

using LIBC_NAMESPACE::operator""_u128;

namespace {
Expand Down Expand Up @@ -737,7 +737,7 @@ double log10_accurate(int e_x, int index, double m_x) {

LLVM_LIBC_FUNCTION(double, log10, (double x)) {
using FPBits_t = typename fputil::FPBits<double>;
using Sign = fputil::Sign;

FPBits_t xbits(x);
uint64_t x_u = xbits.uintval();

Expand Down
2 changes: 1 addition & 1 deletion libc/src/math/generic/log10f.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ LLVM_LIBC_FUNCTION(float, log10f, (float x)) {
constexpr double LOG10_2 = 0x1.34413509f79ffp-2;

using FPBits = typename fputil::FPBits<float>;
using Sign = fputil::Sign;

FPBits xbits(x);
uint32_t x_u = xbits.uintval();

Expand Down
4 changes: 2 additions & 2 deletions libc/src/math/generic/log1p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace LIBC_NAMESPACE {

// 128-bit precision dyadic floating point numbers.
using Float128 = typename fputil::DyadicFloat<128>;
using Sign = fputil::Sign;

using LIBC_NAMESPACE::operator""_u128;

namespace {
Expand Down Expand Up @@ -877,7 +877,7 @@ LIBC_INLINE double log1p_accurate(int e_x, int index,

LLVM_LIBC_FUNCTION(double, log1p, (double x)) {
using FPBits_t = typename fputil::FPBits<double>;
using Sign = fputil::Sign;

constexpr int EXP_BIAS = FPBits_t::EXP_BIAS;
constexpr int FRACTION_LEN = FPBits_t::FRACTION_LEN;
constexpr uint64_t FRACTION_MASK = FPBits_t::FRACTION_MASK;
Expand Down
2 changes: 1 addition & 1 deletion libc/src/math/generic/log1pf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ LLVM_LIBC_FUNCTION(float, log1pf, (float x)) {
case 0xbf800000U: // x = -1.0
fputil::set_errno_if_required(ERANGE);
fputil::raise_except_if_required(FE_DIVBYZERO);
return FPBits::inf(fputil::Sign::NEG).get_val();
return FPBits::inf(Sign::NEG).get_val();
#ifndef LIBC_TARGET_CPU_HAS_FMA
case 0x4cc1c80bU: // x = 0x1.839016p+26f
return fputil::round_result_slightly_down(0x1.26fc04p+4f);
Expand Down
4 changes: 2 additions & 2 deletions libc/src/math/generic/log2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace LIBC_NAMESPACE {

// 128-bit precision dyadic floating point numbers.
using Float128 = typename fputil::DyadicFloat<128>;
using Sign = fputil::Sign;

using LIBC_NAMESPACE::operator""_u128;

namespace {
Expand Down Expand Up @@ -857,7 +857,7 @@ double log2_accurate(int e_x, int index, double m_x) {

LLVM_LIBC_FUNCTION(double, log2, (double x)) {
using FPBits_t = typename fputil::FPBits<double>;
using Sign = fputil::Sign;

FPBits_t xbits(x);
uint64_t x_u = xbits.uintval();

Expand Down
2 changes: 1 addition & 1 deletion libc/src/math/generic/log2f.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(float, log2f, (float x)) {
using FPBits = typename fputil::FPBits<float>;
using Sign = fputil::Sign;

FPBits xbits(x);
uint32_t x_u = xbits.uintval();

Expand Down
1 change: 0 additions & 1 deletion libc/src/math/generic/log_range_reduction.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ log_range_reduction(double m_x, const LogRR &log_table,
fputil::DyadicFloat<128> &sum) {
using Float128 = typename fputil::DyadicFloat<128>;
using MType = typename Float128::MantissaType;
using Sign = fputil::Sign;

int64_t v = static_cast<int64_t>(m_x * 0x1.0p60); // ulp = 2^-60

Expand Down
2 changes: 1 addition & 1 deletion libc/src/math/generic/logf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ namespace LIBC_NAMESPACE {
LLVM_LIBC_FUNCTION(float, logf, (float x)) {
constexpr double LOG_2 = 0x1.62e42fefa39efp-1;
using FPBits = typename fputil::FPBits<float>;
using Sign = fputil::Sign;

FPBits xbits(x);
uint32_t x_u = xbits.uintval();

Expand Down
4 changes: 2 additions & 2 deletions libc/src/math/generic/powf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ LIBC_INLINE bool larger_exponent(double a, double b) {
double powf_double_double(int idx_x, double dx, double y6, double lo6_hi,
const DoubleDouble &exp2_hi_mid) {
using DoubleBits = typename fputil::FPBits<double>;
using Sign = fputil::Sign;

// Perform a second range reduction step:
// idx2 = round(2^14 * (dx + 2^-8)) = round ( dx * 2^14 + 2^6)
// dx2 = (1 + dx) * r2 - 1
Expand Down Expand Up @@ -513,7 +513,7 @@ double powf_double_double(int idx_x, double dx, double y6, double lo6_hi,
LLVM_LIBC_FUNCTION(float, powf, (float x, float y)) {
using FloatBits = typename fputil::FPBits<float>;
using DoubleBits = typename fputil::FPBits<double>;
using Sign = fputil::Sign;

FloatBits xbits(x), ybits(y);

uint32_t x_u = xbits.uintval();
Expand Down
2 changes: 1 addition & 1 deletion libc/src/stdio/printf_core/float_dec_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ constexpr uint32_t MAX_BLOCK = 999999999;
constexpr char DECIMAL_POINT = '.';

LIBC_INLINE RoundDirection get_round_direction(int last_digit, bool truncated,
fputil::Sign sign) {
Sign sign) {
switch (fputil::quick_get_round()) {
case FE_TONEAREST:
// Round to nearest, if it's exactly halfway then round to even.
Expand Down
3 changes: 1 addition & 2 deletions libc/test/UnitTest/FPMatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ template <TestCond C, typename T> FPMatcher<T, C> getMatcher(T expectedValue) {
template <typename T> struct FPTest : public Test {
using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>;
using StorageType = typename FPBits::StorageType;
using Sign = LIBC_NAMESPACE::fputil::Sign;
static constexpr StorageType STORAGE_MAX =
LIBC_NAMESPACE::cpp::numeric_limits<StorageType>::max();
static constexpr T zero = FPBits::zero(Sign::POS).get_val();
Expand Down Expand Up @@ -92,7 +91,7 @@ template <typename T> struct FPTest : public Test {
#define DECLARE_SPECIAL_CONSTANTS(T) \
using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>; \
using StorageType = typename FPBits::StorageType; \
using Sign = LIBC_NAMESPACE::fputil::Sign; \
\
static constexpr StorageType STORAGE_MAX = \
LIBC_NAMESPACE::cpp::numeric_limits<StorageType>::max(); \
const T zero = FPBits::zero(Sign::POS).get_val(); \
Expand Down
Loading

0 comments on commit 2137894

Please sign in to comment.