aboutsummaryrefslogtreecommitdiff
path: root/libc/utils/FPUtil/NearestIntegerOperations.h
blob: 7f93de6701126e7abc1ba8e5d01f2ced9485905f (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//===-- Nearest integer floating-point operations ---------------*- 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_NEAREST_INTEGER_OPERATIONS_H
#define LLVM_LIBC_UTILS_FPUTIL_NEAREST_INTEGER_OPERATIONS_H

#include "ClassificationFunctions.h"
#include "FloatOperations.h"
#include "FloatProperties.h"

#include "utils/CPP/TypeTraits.h"

namespace __llvm_libc {
namespace fputil {

template <typename T,
          cpp::EnableIfType<cpp::IsFloatingPointType<T>::Value, int> = 0>
static inline T trunc(T x) {
  using Properties = FloatProperties<T>;
  using BitsType = typename FloatProperties<T>::BitsType;

  BitsType bits = valueAsBits(x);

  // If x is infinity, NaN or zero, return it.
  if (bitsAreInfOrNaN(bits) || bitsAreZero(bits))
    return x;

  int exponent = getExponentFromBits(bits);

  // If the exponent is greater than the most negative mantissa
  // exponent, then x is already an integer.
  if (exponent >= static_cast<int>(Properties::mantissaWidth))
    return x;

  // If the exponent is such that abs(x) is less than 1, then return 0.
  if (exponent <= -1) {
    if (Properties::signMask & bits)
      return T(-0.0);
    else
      return T(0.0);
  }

  uint32_t trimSize = Properties::mantissaWidth - exponent;
  return valueFromBits((bits >> trimSize) << trimSize);
}

template <typename T,
          cpp::EnableIfType<cpp::IsFloatingPointType<T>::Value, int> = 0>
static inline T ceil(T x) {
  using Properties = FloatProperties<T>;
  using BitsType = typename FloatProperties<T>::BitsType;

  BitsType bits = valueAsBits(x);

  // If x is infinity NaN or zero, return it.
  if (bitsAreInfOrNaN(bits) || bitsAreZero(bits))
    return x;

  bool isNeg = bits & Properties::signMask;
  int exponent = getExponentFromBits(bits);

  // If the exponent is greater than the most negative mantissa
  // exponent, then x is already an integer.
  if (exponent >= static_cast<int>(Properties::mantissaWidth))
    return x;

  if (exponent <= -1) {
    if (isNeg)
      return T(-0.0);
    else
      return T(1.0);
  }

  uint32_t trimSize = Properties::mantissaWidth - exponent;
  // If x is already an integer, return it.
  if ((bits << (Properties::bitWidth - trimSize)) == 0)
    return x;

  BitsType truncBits = (bits >> trimSize) << trimSize;
  T truncValue = valueFromBits(truncBits);

  // If x is negative, the ceil operation is equivalent to the trunc operation.
  if (isNeg)
    return truncValue;

  return truncValue + T(1.0);
}

template <typename T,
          cpp::EnableIfType<cpp::IsFloatingPointType<T>::Value, int> = 0>
static inline T floor(T x) {
  auto bits = valueAsBits(x);
  if (FloatProperties<T>::signMask & bits) {
    return -ceil(-x);
  } else {
    return trunc(x);
  }
}

template <typename T,
          cpp::EnableIfType<cpp::IsFloatingPointType<T>::Value, int> = 0>
static inline T round(T x) {
  using Properties = FloatProperties<T>;
  using BitsType = typename FloatProperties<T>::BitsType;

  BitsType bits = valueAsBits(x);

  // If x is infinity, NaN or zero, return it.
  if (bitsAreInfOrNaN(bits) || bitsAreZero(bits))
    return x;

  bool isNeg = bits & Properties::signMask;
  int exponent = getExponentFromBits(bits);

  // If the exponent is greater than the most negative mantissa
  // exponent, then x is already an integer.
  if (exponent >= static_cast<int>(Properties::mantissaWidth))
    return x;

  if (exponent == -1) {
    // Absolute value of x is greater than equal to 0.5 but less than 1.
    if (isNeg)
      return T(-1.0);
    else
      return T(1.0);
  }

  if (exponent <= -2) {
    // Absolute value of x is less than 0.5.
    if (isNeg)
      return T(-0.0);
    else
      return T(0.0);
  }

  uint32_t trimSize = Properties::mantissaWidth - exponent;
  // If x is already an integer, return it.
  if ((bits << (Properties::bitWidth - trimSize)) == 0)
    return x;

  BitsType truncBits = (bits >> trimSize) << trimSize;
  T truncValue = valueFromBits(truncBits);

  if ((bits & (BitsType(1) << (trimSize - 1))) == 0) {
    // Franctional part is less than 0.5 so round value is the
    // same as the trunc value.
    return truncValue;
  }

  if (isNeg)
    return truncValue - T(1.0);
  else
    return truncValue + T(1.0);
}

} // namespace fputil
} // namespace __llvm_libc

#endif // LLVM_LIBC_UTILS_FPUTIL_NEAREST_INTEGER_OPERATIONS_H