KiCad PCB EDA Suite
Loading...
Searching...
No Matches
util.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KICAD, a free EDA CAD application.
3 *
4 * Copyright (c) 2005 Michael Niedermayer <[email protected]>
5 * Copyright (C) CERN
6 * Copyright (C) 2021-2024 KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * @author Tomasz Wlostowski <[email protected]>
9 *
10 * The equals() method to compare two floating point values adapted from
11 * AlmostEqualRelativeAndAbs() on
12 * https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
13 * (C) Bruce Dawson subject to the Apache 2.0 license.
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, you may find one here:
27 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
28 * or you may search the http://www.gnu.org website for the version 2 license,
29 * or you may write to the Free Software Foundation, Inc.,
30 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
31 */
32
33#ifndef UTIL_H
34#define UTIL_H
35
36#include <config.h>
37#include <cassert>
38#include <cmath>
39#include <cstdint>
40#include <limits>
41#include <typeinfo>
42#include <type_traits>
43
47void kimathLogDebug( const char* aFormatString, ... );
48
52void kimathLogOverflow( double v, const char* aTypeName );
53
54
55// Suppress an annoying warning that the explicit rounding we do is not precise
56#ifdef HAVE_WIMPLICIT_FLOAT_CONVERSION
57 _Pragma( "GCC diagnostic push" ) \
58 _Pragma( "GCC diagnostic ignored \"-Wimplicit-int-float-conversion\"" )
59#endif
60
61
67template <typename in_type = long long int, typename ret_type = int>
68inline constexpr ret_type KiCheckedCast( in_type v )
69{
70 if constexpr( std::is_same_v<in_type, long long int> && std::is_same_v<ret_type, int> )
71 {
72 if( v > std::numeric_limits<int>::max() )
73 {
74 kimathLogOverflow( double( v ), typeid( int ).name() );
75
76 return std::numeric_limits<int>::max();
77 }
78 else if( v < std::numeric_limits<int>::lowest() )
79 {
80 kimathLogOverflow( double( v ), typeid( int ).name() );
81
82 return std::numeric_limits<int>::lowest();
83 }
84
85 return int( v );
86 }
87 else
88 {
89 return v;
90 }
91}
92
93
99template <typename fp_type, typename ret_type = int>
100constexpr ret_type KiROUND( fp_type v, bool aQuiet = false )
101{
102 using max_ret = long long int;
103 fp_type ret = v < 0 ? v - 0.5 : v + 0.5;
104
105 if( ret > std::numeric_limits<ret_type>::max() )
106 {
107 if( !aQuiet )
108 {
109 kimathLogOverflow( double( v ), typeid( ret_type ).name() );
110 }
111
112 return std::numeric_limits<ret_type>::max() - 1;
113 }
114 else if( ret < std::numeric_limits<ret_type>::lowest() )
115 {
116 if( !aQuiet )
117 {
118 kimathLogOverflow( double( v ), typeid( ret_type ).name() );
119 }
120
121 if( std::numeric_limits<ret_type>::is_signed )
122 return std::numeric_limits<ret_type>::lowest() + 1;
123 else
124 return 0;
125 }
126#if __cplusplus >= 202302L // isnan is not constexpr until C++23
127 else if constexpr( std::is_floating_point_v<fp_type> )
128 {
129 if( std::isnan( v ) )
130 {
131 if( !aQuiet )
132 {
133 kimathLogOverflow( double( v ), typeid( ret_type ).name() );
134 }
135
136 return 0;
137 }
138 }
139#endif
140
141 return ret_type( max_ret( ret ) );
142}
143
144#ifdef HAVE_WIMPLICIT_FLOAT_CONVERSION
145 _Pragma( "GCC diagnostic pop" )
146#endif
147
152template <typename T>
153T rescale( T aNumerator, T aValue, T aDenominator )
154{
155 return aNumerator * aValue / aDenominator;
156}
157
158template <typename T>
159int sign( T val )
160{
161 return ( T( 0 ) < val) - ( val < T( 0 ) );
162}
163
164// explicit specializations for integer types, taking care of overflow.
165template <>
166int rescale( int aNumerator, int aValue, int aDenominator );
167
168template <>
169int64_t rescale( int64_t aNumerator, int64_t aValue, int64_t aDenominator );
170
171
180template <class T>
181typename std::enable_if<std::is_floating_point<T>::value, bool>::type
182equals( T aFirst, T aSecond, T aEpsilon = std::numeric_limits<T>::epsilon() )
183{
184 T diff = std::abs( aFirst - aSecond );
185
186 if( diff < aEpsilon )
187 {
188 return true;
189 }
190
191 aFirst = std::abs( aFirst );
192 aSecond = std::abs( aSecond );
193 T largest = aFirst > aSecond ? aFirst : aSecond;
194
195 if( diff <= largest * aEpsilon )
196 {
197 return true;
198 }
199
200 return false;
201}
202
203
204#endif // UTIL_H
const char * name
Definition: DXF_plotter.cpp:57
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition: eda_angle.h:390
std::enable_if< std::is_floating_point< T >::value, bool >::type equals(T aFirst, T aSecond, T aEpsilon=std::numeric_limits< T >::epsilon())
Template to compare two floating point values for equality within a required epsilon.
Definition: util.h:182
int sign(T val)
Definition: util.h:159
void kimathLogOverflow(double v, const char *aTypeName)
Workaround to avoid the empty-string conversion issue in wxWidgets.
Definition: util.cpp:58
constexpr ret_type KiROUND(fp_type v, bool aQuiet=false)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:100
T rescale(T aNumerator, T aValue, T aDenominator)
Scale a number (value) by rational (numerator/denominator).
Definition: util.h:153
void kimathLogDebug(const char *aFormatString,...)
Helper to avoid directly including wx/log.h for the templated functions in kimath.
Definition: util.cpp:44
constexpr ret_type KiCheckedCast(in_type v)
Perform a cast between numerical types.
Definition: util.h:68