xref: /llvm-project/libc/src/__support/sign.h (revision f6b2a222beed734df1d91bd5c165a5233c19fddb)
12137894aSGuillaume Chatelet //===-- A simple sign type --------------------------------------*- C++ -*-===//
22137894aSGuillaume Chatelet //
32137894aSGuillaume Chatelet // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42137894aSGuillaume Chatelet // See https://llvm.org/LICENSE.txt for license information.
52137894aSGuillaume Chatelet // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
62137894aSGuillaume Chatelet //
72137894aSGuillaume Chatelet //===----------------------------------------------------------------------===//
82137894aSGuillaume Chatelet 
92137894aSGuillaume Chatelet #ifndef LLVM_LIBC_SRC___SUPPORT_SIGN_H
102137894aSGuillaume Chatelet #define LLVM_LIBC_SRC___SUPPORT_SIGN_H
112137894aSGuillaume Chatelet 
122137894aSGuillaume Chatelet #include "src/__support/macros/attributes.h" // LIBC_INLINE, LIBC_INLINE_VAR
132137894aSGuillaume Chatelet 
14*f6b2a222SMichael Jones namespace LIBC_NAMESPACE_DECL {
15*f6b2a222SMichael Jones 
162137894aSGuillaume Chatelet // A type to interact with signed arithmetic types.
172137894aSGuillaume Chatelet struct Sign {
182137894aSGuillaume Chatelet   LIBC_INLINE constexpr bool is_pos() const { return !is_negative; }
192137894aSGuillaume Chatelet   LIBC_INLINE constexpr bool is_neg() const { return is_negative; }
202137894aSGuillaume Chatelet 
212137894aSGuillaume Chatelet   LIBC_INLINE friend constexpr bool operator==(Sign a, Sign b) {
222137894aSGuillaume Chatelet     return a.is_negative == b.is_negative;
232137894aSGuillaume Chatelet   }
242137894aSGuillaume Chatelet 
252137894aSGuillaume Chatelet   LIBC_INLINE friend constexpr bool operator!=(Sign a, Sign b) {
262137894aSGuillaume Chatelet     return !(a == b);
272137894aSGuillaume Chatelet   }
282137894aSGuillaume Chatelet 
292137894aSGuillaume Chatelet   static const Sign POS;
302137894aSGuillaume Chatelet   static const Sign NEG;
312137894aSGuillaume Chatelet 
322137894aSGuillaume Chatelet private:
332137894aSGuillaume Chatelet   LIBC_INLINE constexpr explicit Sign(bool is_negative)
342137894aSGuillaume Chatelet       : is_negative(is_negative) {}
352137894aSGuillaume Chatelet 
362137894aSGuillaume Chatelet   bool is_negative;
372137894aSGuillaume Chatelet };
382137894aSGuillaume Chatelet 
392137894aSGuillaume Chatelet LIBC_INLINE_VAR constexpr Sign Sign::NEG = Sign(true);
402137894aSGuillaume Chatelet LIBC_INLINE_VAR constexpr Sign Sign::POS = Sign(false);
412137894aSGuillaume Chatelet 
42*f6b2a222SMichael Jones } // namespace LIBC_NAMESPACE_DECL
432137894aSGuillaume Chatelet #endif // LLVM_LIBC_SRC___SUPPORT_SIGN_H
44