Ardour  8.7-15-gadf511264b
integer_division.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2020 Paul Davis
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18 
19 #ifndef __libpbd_integer_division_h__
20 #define __libpbd_integer_division_h__
21 
22 #include <cstdint>
23 
24 #ifndef COMPILER_INT128_SUPPORT
25 #include <boost/multiprecision/cpp_int.hpp>
26 #include "pbd/error.h"
27 #endif
28 
29 #define PBD_IDIV_ASR(x) ((x) < 0 ? -1 : 0) // Compiles into a (N-1)-bit arithmetic shift right
30 
31 /* The value of PBD_IDIV_ROUNDING will have the same sign as the dividend (x) and half
32  * the magnitude of the divisor (y). Adding ROUNDING to the dividend thus
33  * increases its magnitude before the integer division truncates the resulting
34  * quotient.
35  */
36 
37 #define PBD_IDIV_ROUNDING(x,y) ( (y)/2 - (PBD_IDIV_ASR((x)^(y)) & (y)))
38 
39 template<typename T>
40 T int_div_round (T x, T y)
41 {
42  /* essentially ((x + (y/2)) / y) but handles signed/negative values correcvtly */
43  return (x + PBD_IDIV_ROUNDING(x,y)) / y ;
44 }
45 
46 namespace PBD {
47 
48 /* this computes v * (n/d) where v, n and d are all 64 bit integers, without
49  * overflow, and with appropriate rounding given that this is integer division.
50  */
51 
52 inline
53 int64_t muldiv_round (int64_t v, int64_t n, int64_t d)
54 {
55 #ifndef COMPILER_INT128_SUPPORT
56  boost::multiprecision::int512_t bignum = v;
57 
58  bignum *= n;
59  bignum += PBD_IDIV_ROUNDING (bignum, d);
60  bignum /= d;
61 
62  try {
63 
64  return bignum.convert_to<int64_t> ();
65 
66  } catch (...) {
67  fatal << "arithmetic overflow in timeline math\n" << endmsg;
68  /* NOTREACHED */
69  return 0;
70  }
71 
72 #else
73  __int128 _n (n);
74  __int128 _d (d);
75  __int128 _v (v);
76  __int128 vn (_v * _n);
77 
78  const int64_t hd = PBD_IDIV_ROUNDING (vn, d);
79 
80  /* this could overflow, but will not do so merely because we are
81  * multiplying two int64_t together and storing the result in an
82  * int64_t. Overflow will occur where (v*n)+hd > INT128_MAX (hard
83  * limit) or where v * n / d > INT64_T (i.e. n > d)
84  */
85 
86  return(int64_t) ((vn + hd) / _d);
87 #endif
88 }
89 
90 inline
91 int64_t muldiv_floor (int64_t v, int64_t n, int64_t d)
92 {
93 #ifndef COMPILER_INT128_SUPPORT
94  boost::multiprecision::int512_t bignum = v;
95 
96  bignum *= n;
97  bignum /= d;
98 
99  try {
100 
101  return bignum.convert_to<int64_t> ();
102 
103  } catch (...) {
104  fatal << "arithmetic overflow in timeline math\n" << endmsg;
105  /* NOTREACHED */
106  return 0;
107  }
108 
109 #else
110  __int128 _n (n);
111  __int128 _d (d);
112  __int128 _v (v);
113 
114  /* this could overflow, but will not do so merely because we are
115  * multiplying two int64_t together and storing the result in an
116  * int64_t. Overflow will occur where (v*n)+hd > INT128_MAX (hard
117  * limit) or where v * n / d > INT64_T (i.e. n > d)
118  */
119 
120  return(int64_t) ((_v * _n) / _d);
121 #endif
122 }
123 } /* namespace */
124 
125 #endif /* __libpbd_integer_division_h___ */
T int_div_round(T x, T y)
#define PBD_IDIV_ROUNDING(x, y)
Definition: axis_view.h:42
int64_t muldiv_floor(int64_t v, int64_t n, int64_t d)
int64_t muldiv_round(int64_t v, int64_t n, int64_t d)
Transmitter fatal
std::ostream & endmsg(std::ostream &ostr)
Definition: transmitter.h:72