Debian Codegraph @

3rd_party/router/kimath/src/math/util.cpp

raw
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 * @author Tomasz Wlostowski <[email protected]>
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
33
34 void kimathLogDebug( const char* aFormatString, ... )
35 {
36
37 }
38
39
40 template<>
41 int rescale( int aNumerator, int aValue, int aDenominator )
42 {
43 int64_t numerator = (int64_t) aNumerator * (int64_t) aValue;
44
45 // round to nearest
46 if( ( numerator < 0 ) ^ ( aDenominator < 0 ) )
47 return ( numerator - aDenominator / 2 ) / aDenominator;
48 else
49 return ( numerator + aDenominator / 2 ) / aDenominator;
50
51 }
52
53
54 template<>
55 int64_t rescale( int64_t aNumerator, int64_t aValue, int64_t aDenominator )
56 {
57 #ifdef __SIZEOF_INT128__
58 __int128_t numerator = (__int128_t) aNumerator * (__int128_t) aValue;
59
60 if( ( numerator < 0 ) ^ ( aDenominator < 0 ) )
61 return ( numerator - aDenominator / 2 ) / aDenominator;
62 else
63 return ( numerator + aDenominator / 2 ) / aDenominator;
64 #else
65 int64_t r = 0;
66 int64_t sign = ( ( aNumerator < 0 ) ? -1 : 1 ) * ( aDenominator < 0 ? -1 : 1 ) *
67 ( aValue < 0 ? -1 : 1 );
68
69 int64_t a = std::abs( aNumerator );
70 int64_t b = std::abs( aValue );
71 int64_t c = std::abs( aDenominator );
72
73 r = c / 2;
74
75 if( b <= INT_MAX && c <= INT_MAX )
76 {
77 if( a <= INT_MAX )
78 return sign * ( ( a * b + r ) / c );
79 else
80 return sign * ( a / c * b + ( a % c * b + r ) / c);
81 }
82 else
83 {
84 uint64_t a0 = a & 0xFFFFFFFF;
85 uint64_t a1 = a >> 32;
86 uint64_t b0 = b & 0xFFFFFFFF;
87 uint64_t b1 = b >> 32;
88 uint64_t t1 = a0 * b1 + a1 * b0;
89 uint64_t t1a = t1 << 32;
90 int i;
91
92 a0 = a0 * b0 + t1a;
93 a1 = a1 * b1 + ( t1 >> 32 ) + ( a0 < t1a );
94 a0 += r;
95 a1 += a0 < (uint64_t)r;
96
97 for( i = 63; i >= 0; i-- )
98 {
99 a1 += a1 + ( ( a0 >> i ) & 1 );
100 t1 += t1;
101
102 if( (uint64_t) c <= a1 )
103 {
104 a1 -= c;
105 t1++;
106 }
107 }
108
109 return t1 * sign;
110 }
111 #endif
112 }