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 |
41 |