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 | namespace LIBC_NAMESPACE_DECL { |
15 | |
16 | // A type to interact with signed arithmetic types. |
17 | struct Sign { |
18 | LIBC_INLINE constexpr bool is_pos() const { return !is_negative; } |
19 | LIBC_INLINE constexpr bool is_neg() const { return is_negative; } |
20 | |
21 | LIBC_INLINE friend constexpr bool operator==(Sign a, Sign b) { |
22 | return a.is_negative == b.is_negative; |
23 | } |
24 | |
25 | LIBC_INLINE friend constexpr bool operator!=(Sign a, Sign b) { |
26 | return !(a == b); |
27 | } |
28 | |
29 | static const Sign POS; |
30 | static const Sign NEG; |
31 | |
32 | LIBC_INLINE constexpr Sign negate() const { return Sign(!is_negative); } |
33 | |
34 | private: |
35 | LIBC_INLINE constexpr explicit Sign(bool is_negative) |
36 | : is_negative(is_negative) {} |
37 | |
38 | bool is_negative; |
39 | }; |
40 | |
41 | LIBC_INLINE_VAR constexpr Sign Sign::NEG = Sign(true); |
42 | LIBC_INLINE_VAR constexpr Sign Sign::POS = Sign(false); |
43 | |
44 | } // namespace LIBC_NAMESPACE_DECL |
45 | #endif // LLVM_LIBC_SRC___SUPPORT_SIGN_H |
46 |