Avoid potential negative array index access to cached text.
[LibreOffice.git] / include / o3tl / float_int_conversion.hxx
blob403bed6221c3bc9a5d5bc7f5535b0c4cb6d31c61
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #ifndef INCLUDED_O3TL_FLOAT_INT_CONVERSION_HXX
11 #define INCLUDED_O3TL_FLOAT_INT_CONVERSION_HXX
13 #include <sal/config.h>
15 #include <cmath>
16 #include <limits>
17 #include <type_traits>
19 namespace o3tl
21 // Return true iff `value` of floating-point type `F` converts to a value of integral type `I` no
22 // smaller than `min`:
23 template <typename F, typename I>
24 constexpr std::enable_if_t<std::is_floating_point_v<F> && std::is_integral_v<I>, bool>
25 convertsToAtLeast(F value, I min)
27 // If `F(min)`, `F(min) - F(1)` are too large in magnitude for `F`'s precision, then they either
28 // fall into the same bucket, in which case we should return false if `value` represents that
29 // bucket, or they are on the boundary of two adjacent buckets, in which case we should return
30 // true if `value`represents the higher bucket containing `F(min)`:
31 return value > F(min) - F(1);
34 // Return true iff `value` of floating-point type `F` converts to a value of integral type `I` no
35 // larger than `max`:
36 template <typename F, typename I>
37 constexpr std::enable_if_t<std::is_floating_point_v<F> && std::is_integral_v<I>, bool>
38 convertsToAtMost(F value, I max)
40 // If `F(max)`, `F(max) + F(1)` are too large in magnitude for `F`'s precision, then they either
41 // fall into the same bucket, in which case we should return false if `value` represents that
42 // bucket, or they are on the boundary of two adjacent buckets, in which case we should return
43 // true if `value`represents the lower bucket containing `F(max)`:
44 return value < F(max) + F(1);
47 // Casts a floating-point to an integer, avoiding overflow. Used like:
48 // sal_Int64 n = o3tl::saturating_cast<sal_Int64>(f);
49 template <typename I, typename F>
50 constexpr std::enable_if_t<std::is_floating_point_v<F> && std::is_integral_v<I>, I>
51 saturating_cast(F f)
53 if constexpr (std::is_signed_v<I>)
54 if (!convertsToAtLeast(f, std::numeric_limits<I>::min()))
55 return std::numeric_limits<I>::min();
56 if (!convertsToAtMost(f, std::numeric_limits<I>::max()))
57 return std::numeric_limits<I>::max();
58 return f;
61 // Return `value` of floating-point type `F` rounded to the nearest integer away from zero (which
62 // can be useful in calls to convertsToAtLeast/Most(roundAway(x), n), to reject x that are
63 // smaller/larger than n because they have a fractional part):
64 template <typename F> std::enable_if_t<std::is_floating_point_v<F>, F> roundAway(F value)
66 return value >= 0 ? std::ceil(value) : std::floor(value);
70 #endif
72 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */