Path: blob/main/contrib/llvm-project/libc/src/__support/sign.h
213766 views
//===-- A simple sign type --------------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLVM_LIBC_SRC___SUPPORT_SIGN_H9#define LLVM_LIBC_SRC___SUPPORT_SIGN_H1011#include "src/__support/macros/attributes.h" // LIBC_INLINE, LIBC_INLINE_VAR1213namespace LIBC_NAMESPACE_DECL {1415// A type to interact with signed arithmetic types.16struct Sign {17LIBC_INLINE constexpr bool is_pos() const { return !is_negative; }18LIBC_INLINE constexpr bool is_neg() const { return is_negative; }1920LIBC_INLINE friend constexpr bool operator==(Sign a, Sign b) {21return a.is_negative == b.is_negative;22}2324LIBC_INLINE friend constexpr bool operator!=(Sign a, Sign b) {25return !(a == b);26}2728static const Sign POS;29static const Sign NEG;3031LIBC_INLINE constexpr Sign negate() const { return Sign(!is_negative); }3233private:34LIBC_INLINE constexpr explicit Sign(bool is_negative)35: is_negative(is_negative) {}3637bool is_negative;38};3940LIBC_INLINE_VAR constexpr Sign Sign::NEG = Sign(true);41LIBC_INLINE_VAR constexpr Sign Sign::POS = Sign(false);4243} // namespace LIBC_NAMESPACE_DECL44#endif // LLVM_LIBC_SRC___SUPPORT_SIGN_H454647