aboutsummaryrefslogtreecommitdiff
path: root/libc/utils/FPUtil/ClassificationFunctions.h
blob: a80e349291cdf2c55502fcf887542374b385f4df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//===-- Floating point classification functions -----------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_UTILS_FPUTIL_CLASSIFICATION_FUNCTIONS_H
#define LLVM_LIBC_UTILS_FPUTIL_CLASSIFICATION_FUNCTIONS_H

#include "BitPatterns.h"
#include "FloatOperations.h"
#include "FloatProperties.h"

#include "utils/CPP/TypeTraits.h"

namespace __llvm_libc {
namespace fputil {

template <typename BitsType> static inline bool bitsAreInf(BitsType bits) {
  using FPType = typename FloatType<BitsType>::Type;
  return ((bits & BitPatterns<FPType>::inf) == BitPatterns<FPType>::inf) &&
         ((bits & FloatProperties<FPType>::mantissaMask) == 0);
}

// Return true if x is infinity (positive or negative.)
template <typename T,
          cpp::EnableIfType<cpp::IsFloatingPointType<T>::Value, int> = 0>
static inline bool isInf(T x) {
  return bitsAreInf(valueAsBits(x));
}

template <typename BitsType> static inline bool bitsAreNaN(BitsType bits) {
  using FPType = typename FloatType<BitsType>::Type;
  return ((bits & BitPatterns<FPType>::inf) == BitPatterns<FPType>::inf) &&
         ((bits & FloatProperties<FPType>::mantissaMask) != 0);
}

// Return true if x is a NAN (quiet or signalling.)
template <typename T,
          cpp::EnableIfType<cpp::IsFloatingPointType<T>::Value, int> = 0>
static inline bool isNaN(T x) {
  return bitsAreNaN(valueAsBits(x));
}

template <typename BitsType> static inline bool bitsAreInfOrNaN(BitsType bits) {
  using FPType = typename FloatType<BitsType>::Type;
  return (bits & BitPatterns<FPType>::inf) == BitPatterns<FPType>::inf;
}

template <typename BitsType> static inline bool bitsAreZero(BitsType bits) {
  using FPType = typename FloatType<BitsType>::Type;
  return (bits == BitPatterns<FPType>::zero) ||
         (bits == BitPatterns<FPType>::negZero);
}

// Return true if x is any kind of NaN or infinity.
template <typename T,
          cpp::EnableIfType<cpp::IsFloatingPointType<T>::Value, int> = 0>
static inline bool isInfOrNaN(T x) {
  return bitsAreInfOrNaN(valueAsBits(x));
}

// Return true if x is a quiet NAN.
template <typename T,
          cpp::EnableIfType<cpp::IsFloatingPointType<T>::Value, int> = 0>
static inline bool isQuietNaN(T x) {
  using Properties = FloatProperties<T>;
  using BitsType = typename FloatProperties<T>::BitsType;
  BitsType bits = valueAsBits(x);
  return ((bits & BitPatterns<T>::inf) == BitPatterns<T>::inf) &&
         ((bits & Properties::quietNaNMask) != 0);
}

// Return true if x is a quiet NAN with sign bit set.
template <typename T,
          cpp::EnableIfType<cpp::IsFloatingPointType<T>::Value, int> = 0>
static inline bool isNegativeQuietNaN(T x) {
  using Properties = FloatProperties<T>;
  using BitsType = typename FloatProperties<T>::BitsType;
  BitsType bits = valueAsBits(x);
  return ((bits & BitPatterns<T>::negInf) == BitPatterns<T>::negInf) &&
         ((bits & Properties::quietNaNMask) != 0);
}

} // namespace fputil
} // namespace __llvm_libc

#endif // LLVM_LIBC_UTILS_FPUTIL_CLASSIFICATION_FUNCTIONS_H