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

Lines changed: 8 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 26 deletions
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 40 additions & 0 deletions
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

Lines changed: 1 addition & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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();

0 commit comments

Comments
 (0)