KiCad PCB EDA Suite
util.cpp File Reference
#include <cmath>
#include <cstdlib>
#include <climits>
#include <math/util.h>
#include <wx/log.h>
#include <wx/string.h>

Go to the source code of this file.

Macros

#define wxASCII_STR(s)   wxString::FromAscii(s)
 

Functions

void kimathLogDebug (const char *aFormatString,...)
 Helper to avoid directly including wx/log.h for the templated functions in kimath. More...
 
void kimathLogOverflow (double v, const char *aTypeName)
 Workaround to avoid the empty-string conversion issue in wxWidgets. More...
 
template<>
int rescale (int aNumerator, int aValue, int aDenominator)
 
template<>
int64_t rescale (int64_t aNumerator, int64_t aValue, int64_t aDenominator)
 

Macro Definition Documentation

◆ wxASCII_STR

#define wxASCII_STR (   s)    wxString::FromAscii(s)

Definition at line 41 of file util.cpp.

Function Documentation

◆ kimathLogDebug()

void kimathLogDebug ( const char *  aFormatString,
  ... 
)

Helper to avoid directly including wx/log.h for the templated functions in kimath.

Definition at line 44 of file util.cpp.

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}

Referenced by rescale().

◆ kimathLogOverflow()

void kimathLogOverflow ( double  v,
const char *  aTypeName 
)

Workaround to avoid the empty-string conversion issue in wxWidgets.

Definition at line 58 of file util.cpp.

59{
60 wxString typeName( aTypeName );
61 wxLogWarning( wxString::Format( wxT( "Overflow converting value %f to %s." ), v, typeName ) );
62}
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200

References Format().

Referenced by KiROUND().

◆ rescale() [1/2]

template<>
int rescale ( int  aNumerator,
int  aValue,
int  aDenominator 
)

Definition at line 66 of file util.cpp.

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}

◆ rescale() [2/2]

template<>
int64_t rescale ( int64_t  aNumerator,
int64_t  aValue,
int64_t  aDenominator 
)

Definition at line 80 of file util.cpp.

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 <= INT_MAX && c <= INT_MAX )
130 {
131 if( a <= 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:418
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:124

References std::abs(), kimathLogDebug(), and sign().