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 The 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, see <https://www.gnu.org/licenses/>.
21 */
22
23#include <cmath>
24#include <cstdlib>
25#include <limits>
26#include <math/util.h>
27#include <math/wide_int.h>
28#include <wx/log.h>
29#include <wx/string.h>
30
31#ifdef _MSC_VER
32#include <windows.h>
33#include <intrin.h>
34#endif
35
36// Fix compatibility with wxWidgets version < 3.1.4
37#ifndef wxASCII_STR
38 #define wxASCII_STR(s) wxString::FromAscii(s)
39#endif
40
41void kimathLogDebug( const char* aFormatString, ... )
42{
43 if( wxLog::IsLevelEnabled( wxLOG_Debug, wxString::FromAscii( wxLOG_COMPONENT ) ) )
44 {
45 va_list argList;
46 va_start( argList, aFormatString );
47
48 wxVLogWarning( aFormatString, argList );
49
50 va_end( argList );
51 }
52}
53
54
55void kimathLogOverflow( double v, const char* aTypeName )
56{
57 wxString typeName( aTypeName );
58 wxFAIL_MSG( wxString::Format( wxT( "\n\nOverflow converting value %f to %s." ), v, typeName ) );
59}
60
61
62uint64_t RoundedHypotWide( uint64_t aX, uint64_t aY )
63{
64 const KI_UINT128 n = KI_UINT128( aX ) * KI_UINT128( aX ) + KI_UINT128( aY ) * KI_UINT128( aY );
65 uint64_t f = 0;
66
67 // n <= 2^127 keeps the floor root below 2^64
68 for( int bit = 63; bit >= 0; bit-- )
69 {
70 const uint64_t candidate = f | ( uint64_t( 1 ) << bit );
71
72 if( KI_UINT128( candidate ) * KI_UINT128( candidate ) <= n )
73 f = candidate;
74 }
75
76 // Round up when n - f^2 > f, the same test as the narrow path
77 return f + ( n - KI_UINT128( f ) * KI_UINT128( f ) > KI_UINT128( f ) );
78}
79
80
81template<>
82int rescale( int aNumerator, int aValue, int aDenominator )
83{
84 int64_t numerator = (int64_t) aNumerator * (int64_t) aValue;
85
86 // round to nearest
87 if( ( numerator < 0 ) ^ ( aDenominator < 0 ) )
88 return ( numerator - aDenominator / 2 ) / aDenominator;
89 else
90 return ( numerator + aDenominator / 2 ) / aDenominator;
91
92}
93
94
95template<>
96int64_t rescale( int64_t aNumerator, int64_t aValue, int64_t aDenominator )
97{
98#if defined( _M_X64 ) && ( _MSC_VER >= 1920 )
99 int64_t productHi;
100 uint64_t productLo = static_cast<uint64_t>( _mul128( aNumerator, aValue, &productHi ) );
101
102 int64_t r = ( ( productHi < 0 ) ^ ( aDenominator < 0 ) ) ? -aDenominator / 2 : aDenominator / 2;
103
104 uint64_t rLo = static_cast<uint64_t>( r );
105 int64_t rHi = r < 0 ? -1ll : 0ll;
106
107 productLo += rLo;
108 productHi += rHi + ( productLo < rLo );
109
110 __try
111 {
112 int64_t remainder;
113 int64_t result = _div128( productHi, productLo, aDenominator, &remainder );
114
115 return result;
116 }
117 __except( ( GetExceptionCode() == EXCEPTION_INT_OVERFLOW ) ? EXCEPTION_EXECUTE_HANDLER
118 : EXCEPTION_CONTINUE_SEARCH )
119 {
120 kimathLogDebug( "Overflow in rescale (%lld * %lld + %lld) / %lld", aNumerator, aValue, r,
121 aDenominator );
122 }
123
124 return 0;
125
126#elif defined( __SIZEOF_INT128__ )
127 __int128_t numerator = (__int128_t) aNumerator * (__int128_t) aValue;
128
129 if( ( numerator < 0 ) ^ ( aDenominator < 0 ) )
130 return ( numerator - aDenominator / 2 ) / aDenominator;
131 else
132 return ( numerator + aDenominator / 2 ) / aDenominator;
133
134#else
135 int64_t r = 0;
136 int64_t sign = ( ( aNumerator < 0 ) ? -1 : 1 ) * ( aDenominator < 0 ? -1 : 1 ) *
137 ( aValue < 0 ? -1 : 1 );
138
139 int64_t a = std::abs( aNumerator );
140 int64_t b = std::abs( aValue );
141 int64_t c = std::abs( aDenominator );
142
143 r = c / 2;
144
145 if( b <= std::numeric_limits<int>::max() && c <= std::numeric_limits<int>::max() )
146 {
147 if( a <= std::numeric_limits<int>::max() )
148 return sign * ( ( a * b + r ) / c );
149 else
150 return sign * ( a / c * b + ( a % c * b + r ) / c);
151 }
152 else
153 {
154 uint64_t a0 = a & 0xFFFFFFFF;
155 uint64_t a1 = a >> 32;
156 uint64_t b0 = b & 0xFFFFFFFF;
157 uint64_t b1 = b >> 32;
158 uint64_t t1 = a0 * b1 + a1 * b0;
159 uint64_t t1a = t1 << 32;
160 int i;
161
162 a0 = a0 * b0 + t1a;
163 a1 = a1 * b1 + ( t1 >> 32 ) + ( a0 < t1a );
164 a0 += r;
165 a1 += a0 < (uint64_t)r;
166
167 for( i = 63; i >= 0; i-- )
168 {
169 a1 += a1 + ( ( a0 >> i ) & 1 );
170 t1 += t1;
171
172 if( (uint64_t) c <= a1 )
173 {
174 a1 -= c;
175 t1++;
176 }
177 }
178
179 return t1 * sign;
180 }
181#endif
182}
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition eda_angle.h:437
wxString result
Test unit parsing edge cases and error handling.
int rescale(int aNumerator, int aValue, int aDenominator)
Definition util.cpp:82
void kimathLogOverflow(double v, const char *aTypeName)
Workaround to avoid the empty-string conversion issue in wxWidgets.
Definition util.cpp:55
uint64_t RoundedHypotWide(uint64_t aX, uint64_t aY)
Nearest integer to sqrt( aX^2 + aY^2 ) for magnitudes of at most 2^63, using 128-bit arithmetic.
Definition util.cpp:62
void kimathLogDebug(const char *aFormatString,...)
Helper to avoid directly including wx/log.h for the templated functions in kimath.
Definition util.cpp:41
constexpr int sign(T val)
Definition util.h:166
128-bit integers for exact products of 64-bit coordinate deltas.
unsigned __int128 KI_UINT128
Definition wide_int.h:92