1 /*
2 * This program source code file is part of KICAD, a free EDA CAD application.
3 *
4 * Copyright (c) 2005 Michael Niedermayer <michaelni@gmx.at>
5 * Copyright (C) CERN
6 * Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, you may find one here:
22 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
23 * or you may search the http://www.gnu.org website for the version 2 license,
24 * or you may write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
26 */
27
28 #ifndef UTIL_H
29 #define UTIL_H
30
31 #include <config.h>
32 #include <cstdint>
33 #include <limits>
34 #include <typeinfo>
35
36 /**
37 * Helper to avoid directly including wx/log.h for the templated functions in kimath
38 */
39 void kimathLogDebug( const char* aFormatString, ... );
40
41 /**
42 * Limit @a value within the range @a lower <= @a value <= @a upper.
43 *
44 * It will work on temporary expressions, since they are evaluated only once, and it should
45 * work on most if not all numeric types, string types, or any type for which "operator < ()"
46 * is present. The arguments are accepted in this order so you can remember the expression as
47 * a memory aid:
48 * <p>
49 * result is: lower <= value <= upper
50 *</p>
51 */
Clamp(const T & lower,const T & value,const T & upper)52 template <typename T> inline const T& Clamp( const T& lower, const T& value, const T& upper )
53 {
54 if( value < lower )
55 return lower;
56 else if( upper < value )
57 return upper;
58 return value;
59 }
60
61 // Suppress an annoying warning that the explicit rounding we do is not precise
62 #ifdef HAVE_WIMPLICIT_FLOAT_CONVERSION
63 _Pragma( "GCC diagnostic push" ) \
64 _Pragma( "GCC diagnostic ignored \"-Wimplicit-int-float-conversion\"" )
65 #endif
66
67 /**
68 * Round a floating point number to an integer using "round halfway cases away from zero".
69 *
70 * In Debug build an assert fires if will not fit into the return type.
71 */
72 template <typename fp_type, typename ret_type = int>
KiROUND(fp_type v)73 constexpr ret_type KiROUND( fp_type v )
74 {
75 using max_ret = long long int;
76 fp_type ret = v < 0 ? v - 0.5 : v + 0.5;
77
78 if( std::numeric_limits<ret_type>::max() < ret ||
79 std::numeric_limits<ret_type>::lowest() > ret )
80 {
81 kimathLogDebug( "Overflow KiROUND converting value %f to %s", double( v ),
82 typeid( ret_type ).name() );
83 return 0;
84 }
85
86 return ret_type( max_ret( ret ) );
87 }
88
89 #ifdef HAVE_WIMPLICIT_FLOAT_CONVERSION
90 _Pragma( "GCC diagnostic pop" )
91 #endif
92
93 /**
94 * Scale a number (value) by rational (numerator/denominator). Numerator must be <= denominator.
95 */
96
97 template <typename T>
rescale(T aNumerator,T aValue,T aDenominator)98 T rescale( T aNumerator, T aValue, T aDenominator )
99 {
100 return aNumerator * aValue / aDenominator;
101 }
102
103 template <typename T>
sign(T val)104 int sign( T val )
105 {
106 return ( T( 0 ) < val) - ( val < T( 0 ) );
107 }
108
109 // explicit specializations for integer types, taking care of overflow.
110 template <>
111 int rescale( int aNumerator, int aValue, int aDenominator );
112
113 template <>
114 int64_t rescale( int64_t aNumerator, int64_t aValue, int64_t aDenominator );
115
116 #endif // UTIL_H
117