KiCad PCB EDA Suite
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 <michaelni@gmx.at>
5  * Copyright (C) CERN
6  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 #ifndef UTIL_H
27 #define UTIL_H
28 
29 #include <config.h>
30 #include <cstdint>
31 #include <limits>
32 #include <typeinfo>
33 #include <wx/debug.h>
34 #include <wx/log.h>
35 
46 template <typename T> inline const T& Clamp( const T& lower, const T& value, const T& upper )
47 {
48  wxASSERT( lower <= upper );
49  if( value < lower )
50  return lower;
51  else if( upper < value )
52  return upper;
53  return value;
54 }
55 
56 // Surpress an annoying warning that the explicit rounding we do is not precise
57 #ifdef HAVE_WIMPLICIT_FLOAT_CONVERSION
58  _Pragma( "GCC diagnostic push" ) \
59  _Pragma( "GCC diagnostic ignored \"-Wimplicit-int-float-conversion\"" )
60 #endif
61 
67 template <typename fp_type, typename ret_type = int>
68 constexpr ret_type KiROUND( fp_type v )
69 {
70  using max_ret = long long int;
71  fp_type ret = v < 0 ? v - 0.5 : v + 0.5;
72 
73  if( std::numeric_limits<ret_type>::max() < ret ||
74  std::numeric_limits<ret_type>::lowest() > ret )
75  {
76  wxLogDebug
77  ( "Overflow KiROUND converting value %f to %s", double( v ), typeid(ret_type).name() );
78  return 0;
79  }
80 
81  return ret_type( max_ret( ret ) );
82 }
83 
84 #ifdef HAVE_WIMPLICIT_FLOAT_CONVERSION
85  _Pragma( "GCC diagnostic pop" )
86 #endif
87 
94 template <typename T>
95 T rescale( T aNumerator, T aValue, T aDenominator )
96 {
97  return aNumerator * aValue / aDenominator;
98 }
99 
100 template <typename T>
101 int sign( T val )
102 {
103  return ( T( 0 ) < val) - ( val < T( 0 ) );
104 }
105 
106 // explicit specializations for integer types, taking care of overflow.
107 template <>
108 int rescale( int aNumerator, int aValue, int aDenominator );
109 
110 template <>
111 int64_t rescale( int64_t aNumerator, int64_t aValue, int64_t aDenominator );
112 
113 #endif // UTIL_H
int sign(T val)
Definition: util.h:101
T
enum T contains all this lexer's tokens.
const char * name
Definition: DXF_plotter.cpp:60
T rescale(T aNumerator, T aValue, T aDenominator)
Function rescale()
Definition: util.h:95
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:68
const T & Clamp(const T &lower, const T &value, const T &upper)
Function Clamp limits value within the range lower <= value <= upper.
Definition: util.h:46