LibreOffice Module o3tl (master) 1
float_int_conversion.hxx
Go to the documentation of this file.
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.
4 *
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 */
9
10#ifndef INCLUDED_O3TL_FLOAT_INT_CONVERSION_HXX
11#define INCLUDED_O3TL_FLOAT_INT_CONVERSION_HXX
12
13#include <sal/config.h>
14
15#include <cmath>
16#include <limits>
17#include <type_traits>
18
19namespace o3tl
20{
21// Return true iff `value` of floating-point type `F` converts to a value of integral type `I` no
22// smaller than `min`:
23template <typename F, typename I>
24constexpr std::enable_if_t<std::is_floating_point_v<F> && std::is_integral_v<I>, bool>
25convertsToAtLeast(F value, I min)
26{
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);
32}
33
34// Return true iff `value` of floating-point type `F` converts to a value of integral type `I` no
35// larger than `max`:
36template <typename F, typename I>
37constexpr std::enable_if_t<std::is_floating_point_v<F> && std::is_integral_v<I>, bool>
38convertsToAtMost(F value, I max)
39{
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);
45}
46
47// Casts a floating-point to an integer, avoiding overflow. Used like:
48// sal_Int64 n = o3tl::saturating_cast<sal_Int64>(f);
49template <typename I, typename F>
50constexpr std::enable_if_t<std::is_floating_point_v<F> && std::is_integral_v<I>, I>
52{
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;
59}
60
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):
64template <typename F> std::enable_if_t<std::is_floating_point_v<F>, F> roundAway(F value)
65{
66 return value >= 0 ? std::ceil(value) : std::floor(value);
67}
68}
69
70#endif
71
72/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
Any value
#define max(a, b)
constexpr std::enable_if_t< std::is_floating_point_v< F > &&std::is_integral_v< I >, bool > convertsToAtLeast(F value, I min)
std::enable_if_t< std::is_floating_point_v< F >, F > roundAway(F value)
constexpr std::enable_if_t< std::is_floating_point_v< F > &&std::is_integral_v< I >, I > saturating_cast(F f)
constexpr std::enable_if_t< std::is_floating_point_v< F > &&std::is_integral_v< I >, bool > convertsToAtMost(F value, I max)
SwNodeOffset min(const SwNodeOffset &a, const SwNodeOffset &b)