KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 <[email protected]>
5 * Copyright (C) CERN
6 * Copyright (C) 2022-2023 KiCad Developers, see AUTHORS.txt for contributors.
7 * @author Tomasz Wlostowski <[email protected]>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, you may find one here:
21 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22 * or you may search the http://www.gnu.org website for the version 2 license,
23 * or you may write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 */
26
27#include <cmath>
28#include <cstdlib>
29#include <limits>
30#include <math/util.h>
31#include <wx/log.h>
32#include <wx/string.h>
33
34#ifdef _MSC_VER
35#include <windows.h>
36#include <intrin.h>
37#endif
38
39// Fix compatibility with wxWidgets version < 3.1.4
40#ifndef wxASCII_STR
41 #define wxASCII_STR(s) wxString::FromAscii(s)
42#endif
43
44void kimathLogDebug( const char* aFormatString, ... )
45{
46 if( wxLog::IsLevelEnabled( wxLOG_Debug, wxString::FromAscii( wxLOG_COMPONENT ) ) )
47 {
48 va_list argList;
49 va_start( argList, aFormatString );
50
51 wxVLogWarning( aFormatString, argList );
52
53 va_end( argList );
54 }
55}
56
57
58void kimathLogOverflow( double v, const char* aTypeName )
59{
60 wxString typeName( aTypeName );
61 wxFAIL_MSG( wxString::Format( wxT( "\n\nOverflow converting value %f to %s." ), v, typeName ) );
62}
63
64
65template<>
66int rescale( int aNumerator, int aValue, int aDenominator )
67{
68 int64_t numerator = (int64_t) aNumerator * (int64_t) aValue;
69
70 // round to nearest
71 if( ( numerator < 0 ) ^ ( aDenominator < 0 ) )
72 return ( numerator - aDenominator / 2 ) / aDenominator;
73 else
74 return ( numerator + aDenominator / 2 ) / aDenominator;
75
76}
77
78
79template<>
80int64_t rescale( int64_t aNumerator, int64_t aValue, int64_t aDenominator )
81{
82#if defined( _M_X64 ) && ( _MSC_VER >= 1920 )
83 int64_t productHi;
84 uint64_t productLo = static_cast<uint64_t>( _mul128( aNumerator, aValue, &productHi ) );
85
86 int64_t r = ( ( productHi < 0 ) ^ ( aDenominator < 0 ) ) ? -aDenominator / 2 : aDenominator / 2;
87
88 uint64_t rLo = static_cast<uint64_t>( r );
89 int64_t rHi = r < 0 ? -1ll : 0ll;
90
91 productLo += rLo;
92 productHi += rHi + ( productLo < rLo );
93
94 __try
95 {
96 int64_t remainder;
97 int64_t result = _div128( productHi, productLo, aDenominator, &remainder );
98
99 return result;
100 }
101 __except( ( GetExceptionCode() == EXCEPTION_INT_OVERFLOW ) ? EXCEPTION_EXECUTE_HANDLER
102 : EXCEPTION_CONTINUE_SEARCH )
103 {
104 kimathLogDebug( "Overflow in rescale (%lld * %lld + %lld) / %lld", aNumerator, aValue, r,
105 aDenominator );
106 }
107
108 return 0;
109
110#elif defined( __SIZEOF_INT128__ )
111 __int128_t numerator = (__int128_t) aNumerator * (__int128_t) aValue;
112
113 if( ( numerator < 0 ) ^ ( aDenominator < 0 ) )
114 return ( numerator - aDenominator / 2 ) / aDenominator;
115 else
116 return ( numerator + aDenominator / 2 ) / aDenominator;
117
118#else
119 int64_t r = 0;
120 int64_t sign = ( ( aNumerator < 0 ) ? -1 : 1 ) * ( aDenominator < 0 ? -1 : 1 ) *
121 ( aValue < 0 ? -1 : 1 );
122
123 int64_t a = std::abs( aNumerator );
124 int64_t b = std::abs( aValue );
125 int64_t c = std::abs( aDenominator );
126
127 r = c / 2;
128
129 if( b <= std::numeric_limits<int>::max() && c <= std::numeric_limits<int>::max() )
130 {
131 if( a <= std::numeric_limits<int>::max() )
132 return sign * ( ( a * b + r ) / c );
133 else
134 return sign * ( a / c * b + ( a % c * b + r ) / c);
135 }
136 else
137 {
138 uint64_t a0 = a & 0xFFFFFFFF;
139 uint64_t a1 = a >> 32;
140 uint64_t b0 = b & 0xFFFFFFFF;
141 uint64_t b1 = b >> 32;
142 uint64_t t1 = a0 * b1 + a1 * b0;
143 uint64_t t1a = t1 << 32;
144 int i;
145
146 a0 = a0 * b0 + t1a;
147 a1 = a1 * b1 + ( t1 >> 32 ) + ( a0 < t1a );
148 a0 += r;
149 a1 += a0 < (uint64_t)r;
150
151 for( i = 63; i >= 0; i-- )
152 {
153 a1 += a1 + ( ( a0 >> i ) & 1 );
154 t1 += t1;
155
156 if( (uint64_t) c <= a1 )
157 {
158 a1 -= c;
159 t1++;
160 }
161 }
162
163 return t1 * sign;
164 }
165#endif
166}
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition: eda_angle.h:424
int rescale(int aNumerator, int aValue, int aDenominator)
Definition: util.cpp:66
void kimathLogOverflow(double v, const char *aTypeName)
Workaround to avoid the empty-string conversion issue in wxWidgets.
Definition: util.cpp:58
void kimathLogDebug(const char *aFormatString,...)
Helper to avoid directly including wx/log.h for the templated functions in kimath.
Definition: util.cpp:44
int sign(T val)
Definition: util.h:135