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