WPILibC++ 2024.3.2
ceil.hpp
Go to the documentation of this file.
1/*################################################################################
2 ##
3 ## Copyright (C) 2016-2023 Keith O'Hara
4 ##
5 ## This file is part of the GCE-Math C++ library.
6 ##
7 ## Licensed under the Apache License, Version 2.0 (the "License");
8 ## you may not use this file except in compliance with the License.
9 ## You may obtain a copy of the License at
10 ##
11 ## http://www.apache.org/licenses/LICENSE-2.0
12 ##
13 ## Unless required by applicable law or agreed to in writing, software
14 ## distributed under the License is distributed on an "AS IS" BASIS,
15 ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 ## See the License for the specific language governing permissions and
17 ## limitations under the License.
18 ##
19 ################################################################################*/
20
21#ifndef _gcem_ceil_HPP
22#define _gcem_ceil_HPP
23
24#include <cmath>
25#include <type_traits>
26
27namespace gcem
28{
29
30namespace internal
31{
32
33template<typename T>
34constexpr
35int
36ceil_resid(const T x, const T x_whole)
37noexcept
38{
39 return( (x > T(0)) && (x > x_whole) );
40}
41
42template<typename T>
43constexpr
44T
45ceil_int(const T x, const T x_whole)
46noexcept
47{
48 return( x_whole + static_cast<T>(ceil_resid(x,x_whole)) );
49}
50
51template<typename T>
52constexpr
53T
55noexcept
56{
57 return x;
58}
59
60template<>
61constexpr
62float
63ceil_check_internal<float>(const float x)
64noexcept
65{
66 return( abs(x) >= 8388608.f ? \
67 // if
68 x : \
69 // else
70 ceil_int(x, float(static_cast<int>(x))) );
71}
72
73template<>
74constexpr
75double
76ceil_check_internal<double>(const double x)
77noexcept
78{
79 return( abs(x) >= 4503599627370496. ? \
80 // if
81 x : \
82 // else
83 ceil_int(x, double(static_cast<llint_t>(x))) );
84}
85
86template<>
87constexpr
88long double
89ceil_check_internal<long double>(const long double x)
90noexcept
91{
92 return( abs(x) >= 9223372036854775808.l ? \
93 // if
94 x : \
95 // else
96 ceil_int(x, ((long double)static_cast<ullint_t>(abs(x))) * sgn(x)) );
97}
98
99template<typename T>
100constexpr
101T
102ceil_check(const T x)
103noexcept
104{
105 return( // NaN check
106 is_nan(x) ? \
108 // +/- infinite
109 !is_finite(x) ? \
110 x :
111 // signed-zero cases
112 GCLIM<T>::min() > abs(x) ? \
113 x :
114 // else
116}
117
118}
119
120/**
121 * Compile-time ceil function
122 *
123 * @param x a real-valued input.
124 * @return computes the ceiling-value of the input.
125 */
126
127template<typename T>
128constexpr
129return_t<T>
130ceil(const T x)
131noexcept
132{
134 return internal::ceil_check( static_cast<return_t<T>>(x) );
135 } else {
136 return std::ceil(x);
137 }
138}
139
140}
141
142#endif
constexpr FMT_INLINE auto is_constant_evaluated(bool default_value=false) noexcept -> bool
Definition: core.h:304
constexpr float ceil_check_internal< float >(const float x) noexcept
Definition: ceil.hpp:63
constexpr bool is_nan(const T x) noexcept
Definition: is_nan.hpp:39
constexpr long double ceil_check_internal< long double >(const long double x) noexcept
Definition: ceil.hpp:89
constexpr int ceil_resid(const T x, const T x_whole) noexcept
Definition: ceil.hpp:36
constexpr bool is_finite(const T x) noexcept
Definition: is_finite.hpp:37
constexpr double ceil_check_internal< double >(const double x) noexcept
Definition: ceil.hpp:76
constexpr T ceil_check(const T x) noexcept
Definition: ceil.hpp:102
constexpr T ceil_check_internal(const T x) noexcept
Definition: ceil.hpp:54
constexpr T ceil_int(const T x, const T x_whole) noexcept
Definition: ceil.hpp:45
Definition: is_even.hpp:29
constexpr T abs(const T x) noexcept
Compile-time absolute value function.
Definition: abs.hpp:40
constexpr return_t< T > ceil(const T x) noexcept
Compile-time ceil function.
Definition: ceil.hpp:130
unsigned long long int ullint_t
Definition: gcem_options.hpp:69
long long int llint_t
Definition: gcem_options.hpp:71
constexpr int sgn(const T x) noexcept
Compile-time sign function.
Definition: sgn.hpp:37
std::numeric_limits< T > GCLIM
Definition: gcem_options.hpp:74
typename std::conditional< std::is_integral< T >::value, double, T >::type return_t
Definition: gcem_options.hpp:77