KiCad PCB EDA Suite
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 <michaelni@gmx.at>
5  * Copyright (C) CERN
6  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
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 
31 template<>
32 int rescale( int aNumerator, int aValue, int aDenominator )
33 {
34  int64_t numerator = (int64_t) aNumerator * (int64_t) aValue;
35 
36  // round to nearest
37  if( ( numerator < 0 ) ^ ( aDenominator < 0 ) )
38  return ( numerator - aDenominator / 2 ) / aDenominator;
39  else
40  return ( numerator + aDenominator / 2 ) / aDenominator;
41 
42 }
43 
44 
45 template<>
46 int64_t rescale( int64_t aNumerator, int64_t aValue, int64_t aDenominator )
47 {
48 #ifdef __SIZEOF_INT128__
49  __int128_t numerator = (__int128_t) aNumerator * (__int128_t) aValue;
50 
51  if( ( numerator < 0 ) ^ ( aDenominator < 0 ) )
52  return ( numerator - aDenominator / 2 ) / aDenominator;
53  else
54  return ( numerator + aDenominator / 2 ) / aDenominator;
55 #else
56  int64_t r = 0;
57  int64_t sign = ( ( aNumerator < 0 ) ? -1 : 1 ) * ( aDenominator < 0 ? -1 : 1 ) *
58  ( aValue < 0 ? -1 : 1 );
59 
60  int64_t a = std::abs( aNumerator );
61  int64_t b = std::abs( aValue );
62  int64_t c = std::abs( aDenominator );
63 
64  r = c / 2;
65 
66  if( b <= INT_MAX && c <= INT_MAX )
67  {
68  if( a <= INT_MAX )
69  return sign * ( ( a * b + r ) / c );
70  else
71  return sign * ( a / c * b + ( a % c * b + r ) / c);
72  }
73  else
74  {
75  uint64_t a0 = a & 0xFFFFFFFF;
76  uint64_t a1 = a >> 32;
77  uint64_t b0 = b & 0xFFFFFFFF;
78  uint64_t b1 = b >> 32;
79  uint64_t t1 = a0 * b1 + a1 * b0;
80  uint64_t t1a = t1 << 32;
81  int i;
82 
83  a0 = a0 * b0 + t1a;
84  a1 = a1 * b1 + ( t1 >> 32 ) + ( a0 < t1a );
85  a0 += r;
86  a1 += a0 < (uint64_t)r;
87 
88  for( i = 63; i >= 0; i-- )
89  {
90  a1 += a1 + ( ( a0 >> i ) & 1 );
91  t1 += t1;
92 
93  if( (uint64_t) c <= a1 )
94  {
95  a1 -= c;
96  t1++;
97  }
98  }
99 
100  return t1 * sign;
101  }
102 #endif
103 }
int sign(T val)
Definition: util.h:101
int rescale(int aNumerator, int aValue, int aDenominator)
Definition: util.cpp:32