Skip to content

Commit 2137894

Browse files
authored
[libc][NFC] Move Sign type to separate header (llvm#85930)
1 parent 5f5a641 commit 2137894

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+94
-82
lines changed

libc/src/__support/CMakeLists.txt

+8
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ add_header_library(
4141
libc.src.__support.macros.config
4242
)
4343

44+
add_header_library(
45+
sign
46+
HDRS
47+
sign.h
48+
DEPENDS
49+
libc.src.__support.macros.attributes
50+
)
51+
4452
add_header_library(
4553
error_or
4654
HDRS

libc/src/__support/FPUtil/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ add_header_library(
3535
libc.src.__support.macros.attributes
3636
libc.src.__support.macros.properties.types
3737
libc.src.__support.math_extras
38+
libc.src.__support.sign
3839
libc.src.__support.uint128
3940
)
4041

libc/src/__support/FPUtil/FPBits.h

+1-26
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "src/__support/macros/attributes.h" // LIBC_INLINE, LIBC_INLINE_VAR
1818
#include "src/__support/macros/properties/types.h" // LIBC_TYPES_HAS_FLOAT128
1919
#include "src/__support/math_extras.h" // mask_trailing_ones
20+
#include "src/__support/sign.h" // Sign
2021

2122
#include <stdint.h>
2223

@@ -32,32 +33,6 @@ enum class FPType {
3233
X86_Binary80,
3334
};
3435

35-
// A type to interact with floating point type signs.
36-
// This may be moved outside of 'fputil' if useful.
37-
struct Sign {
38-
LIBC_INLINE constexpr bool is_pos() const { return !is_negative; }
39-
LIBC_INLINE constexpr bool is_neg() const { return is_negative; }
40-
41-
LIBC_INLINE friend constexpr bool operator==(Sign a, Sign b) {
42-
return a.is_negative == b.is_negative;
43-
}
44-
LIBC_INLINE friend constexpr bool operator!=(Sign a, Sign b) {
45-
return !(a == b);
46-
}
47-
48-
static const Sign POS;
49-
static const Sign NEG;
50-
51-
private:
52-
LIBC_INLINE constexpr explicit Sign(bool is_negative)
53-
: is_negative(is_negative) {}
54-
55-
bool is_negative;
56-
};
57-
58-
LIBC_INLINE_VAR constexpr Sign Sign::NEG = Sign(true);
59-
LIBC_INLINE_VAR constexpr Sign Sign::POS = Sign(false);
60-
6136
// The classes hierarchy is as follows:
6237
//
6338
// ┌───────────────────┐

libc/src/__support/FPUtil/fpbits_str.h

-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ using ZeroPaddedHexFmt = IntegerToString<
3535
// floating encoding.
3636
template <typename T> LIBC_INLINE cpp::string str(fputil::FPBits<T> x) {
3737
using StorageType = typename fputil::FPBits<T>::StorageType;
38-
using Sign = fputil::Sign;
3938

4039
if (x.is_nan())
4140
return "(NaN)";

libc/src/__support/sign.h

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===-- A simple sign type --------------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC___SUPPORT_SIGN_H
10+
#define LLVM_LIBC_SRC___SUPPORT_SIGN_H
11+
12+
#include "src/__support/macros/attributes.h" // LIBC_INLINE, LIBC_INLINE_VAR
13+
14+
// A type to interact with signed arithmetic types.
15+
struct Sign {
16+
LIBC_INLINE constexpr bool is_pos() const { return !is_negative; }
17+
LIBC_INLINE constexpr bool is_neg() const { return is_negative; }
18+
19+
LIBC_INLINE friend constexpr bool operator==(Sign a, Sign b) {
20+
return a.is_negative == b.is_negative;
21+
}
22+
23+
LIBC_INLINE friend constexpr bool operator!=(Sign a, Sign b) {
24+
return !(a == b);
25+
}
26+
27+
static const Sign POS;
28+
static const Sign NEG;
29+
30+
private:
31+
LIBC_INLINE constexpr explicit Sign(bool is_negative)
32+
: is_negative(is_negative) {}
33+
34+
bool is_negative;
35+
};
36+
37+
LIBC_INLINE_VAR constexpr Sign Sign::NEG = Sign(true);
38+
LIBC_INLINE_VAR constexpr Sign Sign::POS = Sign(false);
39+
40+
#endif // LLVM_LIBC_SRC___SUPPORT_SIGN_H

libc/src/__support/str_to_float.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,6 @@ clinger_fast_path(ExpandedFloat<T> init_num,
513513
RoundDirection round = RoundDirection::Nearest) {
514514
using FPBits = typename fputil::FPBits<T>;
515515
using StorageType = typename FPBits::StorageType;
516-
using Sign = fputil::Sign;
517516

518517
StorageType mantissa = init_num.mantissa;
519518
int32_t exp10 = init_num.exponent;
@@ -1085,7 +1084,6 @@ template <class T>
10851084
LIBC_INLINE StrToNumResult<T> strtofloatingpoint(const char *__restrict src) {
10861085
using FPBits = typename fputil::FPBits<T>;
10871086
using StorageType = typename FPBits::StorageType;
1088-
using Sign = fputil::Sign;
10891087

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

1226-
result = FPBits::quiet_nan(fputil::Sign::POS, nan_mantissa);
1224+
result = FPBits::quiet_nan(Sign::POS, nan_mantissa);
12271225
return {result.get_val(), 0, error};
12281226
}
12291227

libc/src/math/generic/acosf.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ static constexpr fputil::ExceptValues<float, N_EXCEPTS> ACOSF_EXCEPTS = {{
3838

3939
LLVM_LIBC_FUNCTION(float, acosf, (float x)) {
4040
using FPBits = typename fputil::FPBits<float>;
41-
using Sign = fputil::Sign;
41+
4242
FPBits xbits(x);
4343
uint32_t x_uint = xbits.uintval();
4444
uint32_t x_abs = xbits.uintval() & 0x7fff'ffffU;

libc/src/math/generic/asinf.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ static constexpr fputil::ExceptValues<float, N_EXCEPTS> ASINF_EXCEPTS_HI = {{
4444

4545
LLVM_LIBC_FUNCTION(float, asinf, (float x)) {
4646
using FPBits = typename fputil::FPBits<float>;
47-
using Sign = fputil::Sign;
47+
4848
FPBits xbits(x);
4949
uint32_t x_uint = xbits.uintval();
5050
uint32_t x_abs = xbits.uintval() & 0x7fff'ffffU;

libc/src/math/generic/atanf.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ namespace LIBC_NAMESPACE {
2020

2121
LLVM_LIBC_FUNCTION(float, atanf, (float x)) {
2222
using FPBits = typename fputil::FPBits<float>;
23-
using Sign = fputil::Sign;
2423

2524
constexpr double FINAL_SIGN[2] = {1.0, -1.0};
2625
constexpr double SIGNED_PI_OVER_2[2] = {0x1.921fb54442d18p0,

libc/src/math/generic/atanhf.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace LIBC_NAMESPACE {
1515

1616
LLVM_LIBC_FUNCTION(float, atanhf, (float x)) {
1717
using FPBits = typename fputil::FPBits<float>;
18-
using Sign = fputil::Sign;
18+
1919
FPBits xbits(x);
2020
Sign sign = xbits.sign();
2121
uint32_t x_abs = xbits.abs().uintval();

libc/src/math/generic/cosf.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ static constexpr fputil::ExceptValues<float, N_EXCEPTS> COSF_EXCEPTS{{
4242

4343
LLVM_LIBC_FUNCTION(float, cosf, (float x)) {
4444
using FPBits = typename fputil::FPBits<float>;
45-
using Sign = fputil::Sign;
45+
4646
FPBits xbits(x);
4747
xbits.set_sign(Sign::POS);
4848

libc/src/math/generic/coshf.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace LIBC_NAMESPACE {
1717

1818
LLVM_LIBC_FUNCTION(float, coshf, (float x)) {
1919
using FPBits = typename fputil::FPBits<float>;
20-
using Sign = fputil::Sign;
20+
2121
FPBits xbits(x);
2222
xbits.set_sign(Sign::POS);
2323
x = xbits.get_val();

libc/src/math/generic/exp.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace LIBC_NAMESPACE {
3131
using fputil::DoubleDouble;
3232
using fputil::TripleDouble;
3333
using Float128 = typename fputil::DyadicFloat<128>;
34-
using Sign = fputil::Sign;
34+
3535
using LIBC_NAMESPACE::operator""_u128;
3636

3737
// log2(e)

libc/src/math/generic/exp10.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace LIBC_NAMESPACE {
3131
using fputil::DoubleDouble;
3232
using fputil::TripleDouble;
3333
using Float128 = typename fputil::DyadicFloat<128>;
34-
using Sign = fputil::Sign;
34+
3535
using LIBC_NAMESPACE::operator""_u128;
3636

3737
// log2(10)

libc/src/math/generic/exp2.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace LIBC_NAMESPACE {
3131
using fputil::DoubleDouble;
3232
using fputil::TripleDouble;
3333
using Float128 = typename fputil::DyadicFloat<128>;
34-
using Sign = fputil::Sign;
34+
3535
using LIBC_NAMESPACE::operator""_u128;
3636

3737
// Error bounds:

libc/src/math/generic/expm1.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ namespace LIBC_NAMESPACE {
3939
using fputil::DoubleDouble;
4040
using fputil::TripleDouble;
4141
using Float128 = typename fputil::DyadicFloat<128>;
42-
using Sign = fputil::Sign;
42+
4343
using LIBC_NAMESPACE::operator""_u128;
4444

4545
// log2(e)
@@ -276,7 +276,7 @@ double set_exceptional(double x) {
276276

277277
LLVM_LIBC_FUNCTION(double, expm1, (double x)) {
278278
using FPBits = typename fputil::FPBits<double>;
279-
using Sign = fputil::Sign;
279+
280280
FPBits xbits(x);
281281

282282
bool x_is_neg = xbits.is_neg();

libc/src/math/generic/log.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace LIBC_NAMESPACE {
2424

2525
// 128-bit precision dyadic floating point numbers.
2626
using Float128 = typename fputil::DyadicFloat<128>;
27-
using Sign = fputil::Sign;
27+
2828
using LIBC_NAMESPACE::operator""_u128;
2929

3030
namespace {
@@ -735,7 +735,7 @@ double log_accurate(int e_x, int index, double m_x) {
735735

736736
LLVM_LIBC_FUNCTION(double, log, (double x)) {
737737
using FPBits_t = typename fputil::FPBits<double>;
738-
using Sign = fputil::Sign;
738+
739739
FPBits_t xbits(x);
740740
uint64_t x_u = xbits.uintval();
741741

libc/src/math/generic/log10.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace LIBC_NAMESPACE {
2424

2525
// 128-bit precision dyadic floating point numbers.
2626
using Float128 = typename fputil::DyadicFloat<128>;
27-
using Sign = fputil::Sign;
27+
2828
using LIBC_NAMESPACE::operator""_u128;
2929

3030
namespace {
@@ -737,7 +737,7 @@ double log10_accurate(int e_x, int index, double m_x) {
737737

738738
LLVM_LIBC_FUNCTION(double, log10, (double x)) {
739739
using FPBits_t = typename fputil::FPBits<double>;
740-
using Sign = fputil::Sign;
740+
741741
FPBits_t xbits(x);
742742
uint64_t x_u = xbits.uintval();
743743

libc/src/math/generic/log10f.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ LLVM_LIBC_FUNCTION(float, log10f, (float x)) {
106106
constexpr double LOG10_2 = 0x1.34413509f79ffp-2;
107107

108108
using FPBits = typename fputil::FPBits<float>;
109-
using Sign = fputil::Sign;
109+
110110
FPBits xbits(x);
111111
uint32_t x_u = xbits.uintval();
112112

libc/src/math/generic/log1p.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace LIBC_NAMESPACE {
2323

2424
// 128-bit precision dyadic floating point numbers.
2525
using Float128 = typename fputil::DyadicFloat<128>;
26-
using Sign = fputil::Sign;
26+
2727
using LIBC_NAMESPACE::operator""_u128;
2828

2929
namespace {
@@ -877,7 +877,7 @@ LIBC_INLINE double log1p_accurate(int e_x, int index,
877877

878878
LLVM_LIBC_FUNCTION(double, log1p, (double x)) {
879879
using FPBits_t = typename fputil::FPBits<double>;
880-
using Sign = fputil::Sign;
880+
881881
constexpr int EXP_BIAS = FPBits_t::EXP_BIAS;
882882
constexpr int FRACTION_LEN = FPBits_t::FRACTION_LEN;
883883
constexpr uint64_t FRACTION_MASK = FPBits_t::FRACTION_MASK;

libc/src/math/generic/log1pf.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ LLVM_LIBC_FUNCTION(float, log1pf, (float x)) {
106106
case 0xbf800000U: // x = -1.0
107107
fputil::set_errno_if_required(ERANGE);
108108
fputil::raise_except_if_required(FE_DIVBYZERO);
109-
return FPBits::inf(fputil::Sign::NEG).get_val();
109+
return FPBits::inf(Sign::NEG).get_val();
110110
#ifndef LIBC_TARGET_CPU_HAS_FMA
111111
case 0x4cc1c80bU: // x = 0x1.839016p+26f
112112
return fputil::round_result_slightly_down(0x1.26fc04p+4f);

libc/src/math/generic/log2.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace LIBC_NAMESPACE {
2424

2525
// 128-bit precision dyadic floating point numbers.
2626
using Float128 = typename fputil::DyadicFloat<128>;
27-
using Sign = fputil::Sign;
27+
2828
using LIBC_NAMESPACE::operator""_u128;
2929

3030
namespace {
@@ -857,7 +857,7 @@ double log2_accurate(int e_x, int index, double m_x) {
857857

858858
LLVM_LIBC_FUNCTION(double, log2, (double x)) {
859859
using FPBits_t = typename fputil::FPBits<double>;
860-
using Sign = fputil::Sign;
860+
861861
FPBits_t xbits(x);
862862
uint64_t x_u = xbits.uintval();
863863

libc/src/math/generic/log2f.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ namespace LIBC_NAMESPACE {
5555

5656
LLVM_LIBC_FUNCTION(float, log2f, (float x)) {
5757
using FPBits = typename fputil::FPBits<float>;
58-
using Sign = fputil::Sign;
58+
5959
FPBits xbits(x);
6060
uint32_t x_u = xbits.uintval();
6161

libc/src/math/generic/log_range_reduction.h

-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ log_range_reduction(double m_x, const LogRR &log_table,
3737
fputil::DyadicFloat<128> &sum) {
3838
using Float128 = typename fputil::DyadicFloat<128>;
3939
using MType = typename Float128::MantissaType;
40-
using Sign = fputil::Sign;
4140

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

libc/src/math/generic/logf.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ namespace LIBC_NAMESPACE {
5454
LLVM_LIBC_FUNCTION(float, logf, (float x)) {
5555
constexpr double LOG_2 = 0x1.62e42fefa39efp-1;
5656
using FPBits = typename fputil::FPBits<float>;
57-
using Sign = fputil::Sign;
57+
5858
FPBits xbits(x);
5959
uint32_t x_u = xbits.uintval();
6060

libc/src/math/generic/powf.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ LIBC_INLINE bool larger_exponent(double a, double b) {
424424
double powf_double_double(int idx_x, double dx, double y6, double lo6_hi,
425425
const DoubleDouble &exp2_hi_mid) {
426426
using DoubleBits = typename fputil::FPBits<double>;
427-
using Sign = fputil::Sign;
427+
428428
// Perform a second range reduction step:
429429
// idx2 = round(2^14 * (dx + 2^-8)) = round ( dx * 2^14 + 2^6)
430430
// dx2 = (1 + dx) * r2 - 1
@@ -513,7 +513,7 @@ double powf_double_double(int idx_x, double dx, double y6, double lo6_hi,
513513
LLVM_LIBC_FUNCTION(float, powf, (float x, float y)) {
514514
using FloatBits = typename fputil::FPBits<float>;
515515
using DoubleBits = typename fputil::FPBits<double>;
516-
using Sign = fputil::Sign;
516+
517517
FloatBits xbits(x), ybits(y);
518518

519519
uint32_t x_u = xbits.uintval();

libc/src/stdio/printf_core/float_dec_converter.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ constexpr uint32_t MAX_BLOCK = 999999999;
4848
constexpr char DECIMAL_POINT = '.';
4949

5050
LIBC_INLINE RoundDirection get_round_direction(int last_digit, bool truncated,
51-
fputil::Sign sign) {
51+
Sign sign) {
5252
switch (fputil::quick_get_round()) {
5353
case FE_TONEAREST:
5454
// Round to nearest, if it's exactly halfway then round to even.

libc/test/UnitTest/FPMatcher.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ template <TestCond C, typename T> FPMatcher<T, C> getMatcher(T expectedValue) {
6363
template <typename T> struct FPTest : public Test {
6464
using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>;
6565
using StorageType = typename FPBits::StorageType;
66-
using Sign = LIBC_NAMESPACE::fputil::Sign;
6766
static constexpr StorageType STORAGE_MAX =
6867
LIBC_NAMESPACE::cpp::numeric_limits<StorageType>::max();
6968
static constexpr T zero = FPBits::zero(Sign::POS).get_val();
@@ -92,7 +91,7 @@ template <typename T> struct FPTest : public Test {
9291
#define DECLARE_SPECIAL_CONSTANTS(T) \
9392
using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>; \
9493
using StorageType = typename FPBits::StorageType; \
95-
using Sign = LIBC_NAMESPACE::fputil::Sign; \
94+
\
9695
static constexpr StorageType STORAGE_MAX = \
9796
LIBC_NAMESPACE::cpp::numeric_limits<StorageType>::max(); \
9897
const T zero = FPBits::zero(Sign::POS).get_val(); \

0 commit comments

Comments
 (0)