Horizon
Loading...
Searching...
No Matches
util.h
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
39void kimathLogDebug( const char* aFormatString, ... );
40
52template <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
72template <typename fp_type, typename ret_type = int>
73constexpr 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
97template <typename T>
98T rescale( T aNumerator, T aValue, T aDenominator )
99{
100 return aNumerator * aValue / aDenominator;
101}
102
103template <typename T>
104int sign( T val )
105{
106 return ( T( 0 ) < val) - ( val < T( 0 ) );
107}
108
109// explicit specializations for integer types, taking care of overflow.
110template <>
111int rescale( int aNumerator, int aValue, int aDenominator );
112
113template <>
114int64_t rescale( int64_t aNumerator, int64_t aValue, int64_t aDenominator );
115
116#endif // UTIL_H