KiCad PCB EDA Suite
util.cpp
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 #include <cmath>
27 #include <cstdlib>
28 #include <climits>
29 #include <math/util.h>
30 #include <wx/log.h>
31 
32 // Fix compatibility with wxWidgets version < 3.1.4
33 #ifndef wxASCII_STR
34  #define wxASCII_STR(s) wxString::FromAscii(s)
35 #endif
36 
37 void kimathLogDebug( const char* aFormatString, ... )
38 {
39  if( wxLog::IsLevelEnabled( wxLOG_Debug, wxString::FromAscii( wxLOG_COMPONENT ) ) )
40  {
41  va_list argList;
42  va_start( argList, aFormatString );
43 
44  wxVLogWarning( aFormatString, argList );
45 
46  va_end( argList );
47  }
48 }
49 
50 
51 template<>
52 int rescale( int aNumerator, int aValue, int aDenominator )
53 {
54  int64_t numerator = (int64_t) aNumerator * (int64_t) aValue;
55 
56  // round to nearest
57  if( ( numerator < 0 ) ^ ( aDenominator < 0 ) )
58  return ( numerator - aDenominator / 2 ) / aDenominator;
59  else
60  return ( numerator + aDenominator / 2 ) / aDenominator;
61 
62 }
63 
64 
65 template<>
66 int64_t rescale( int64_t aNumerator, int64_t aValue, int64_t aDenominator )
67 {
68 #ifdef __SIZEOF_INT128__
69  __int128_t numerator = (__int128_t) aNumerator * (__int128_t) aValue;
70 
71  if( ( numerator < 0 ) ^ ( aDenominator < 0 ) )
72  return ( numerator - aDenominator / 2 ) / aDenominator;
73  else
74  return ( numerator + aDenominator / 2 ) / aDenominator;
75 #else
76  int64_t r = 0;
77  int64_t sign = ( ( aNumerator < 0 ) ? -1 : 1 ) * ( aDenominator < 0 ? -1 : 1 ) *
78  ( aValue < 0 ? -1 : 1 );
79 
80  int64_t a = std::abs( aNumerator );
81  int64_t b = std::abs( aValue );
82  int64_t c = std::abs( aDenominator );
83 
84  r = c / 2;
85 
86  if( b <= INT_MAX && c <= INT_MAX )
87  {
88  if( a <= INT_MAX )
89  return sign * ( ( a * b + r ) / c );
90  else
91  return sign * ( a / c * b + ( a % c * b + r ) / c);
92  }
93  else
94  {
95  uint64_t a0 = a & 0xFFFFFFFF;
96  uint64_t a1 = a >> 32;
97  uint64_t b0 = b & 0xFFFFFFFF;
98  uint64_t b1 = b >> 32;
99  uint64_t t1 = a0 * b1 + a1 * b0;
100  uint64_t t1a = t1 << 32;
101  int i;
102 
103  a0 = a0 * b0 + t1a;
104  a1 = a1 * b1 + ( t1 >> 32 ) + ( a0 < t1a );
105  a0 += r;
106  a1 += a0 < (uint64_t)r;
107 
108  for( i = 63; i >= 0; i-- )
109  {
110  a1 += a1 + ( ( a0 >> i ) & 1 );
111  t1 += t1;
112 
113  if( (uint64_t) c <= a1 )
114  {
115  a1 -= c;
116  t1++;
117  }
118  }
119 
120  return t1 * sign;
121  }
122 #endif
123 }
int sign(T val)
Definition: util.h:104
void kimathLogDebug(const char *aFormatString,...)
Helper to avoid directly including wx/log.h for the templated functions in kimath.
Definition: util.cpp:37
int rescale(int aNumerator, int aValue, int aDenominator)
Definition: util.cpp:52