xref: /aosp_15_r20/external/llvm-libc/src/__support/sign.h (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
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 {
is_posSign18   LIBC_INLINE constexpr bool is_pos() const { return !is_negative; }
is_negSign19   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 private:
SignSign33   LIBC_INLINE constexpr explicit Sign(bool is_negative)
34       : is_negative(is_negative) {}
35 
36   bool is_negative;
37 };
38 
39 LIBC_INLINE_VAR constexpr Sign Sign::NEG = Sign(true);
40 LIBC_INLINE_VAR constexpr Sign Sign::POS = Sign(false);
41 
42 } // namespace LIBC_NAMESPACE_DECL
43 #endif // LLVM_LIBC_SRC___SUPPORT_SIGN_H
44