22 #include "lib/integral.hpp" 73 CHECK (Rat(10,3) == 10_r/3);
74 CHECK (Rat(10,3) == boost::rational<int64_t>(10,3));
75 CHECK (rational_cast<float> (10_r/3) == 3.3333333f);
77 CHECK (2_r/3 + 3_r/4 == 17_r/12);
78 CHECK (2_r/3 * 3_r/4 == 1_r/2);
79 CHECK (2_r/3 /(3_r/4)== 8_r/9);
80 CHECK (2_r/3 / 3 /4 == 1_r/18);
86 CHECK (Rat(10,3).numerator() == int64_t(10));
87 CHECK (Rat(10,3).denominator() == int64_t(3));
88 CHECK (boost::rational<uint>(10,3).numerator() == uint(10));
89 CHECK (boost::rational<uint>(10,3).denominator() == uint(3));
90 CHECK (boost::rational<uint>(10,3) == rational_cast<boost::rational<uint>> (Rat(10,3)));
91 CHECK (boost::rational<uint>(11,3) != rational_cast<boost::rational<uint>> (Rat(10,3)));
104 const Rat MAXI = Rat{std::numeric_limits<int64_t>::max()};
105 const Rat MINI = Rat{1, std::numeric_limits<int64_t>::max()};
107 CHECK (rational_cast<int64_t>(MAXI) == std::numeric_limits<int64_t>::max());
108 CHECK (rational_cast<double> (MAXI) == 9.2233720368547758e+18);
112 CHECK (MAXI+1 < -MAXI);
114 CHECK (
util::toString(MAXI+1) ==
"-9223372036854775808sec"_expect);
115 CHECK (
util::toString(-MAXI) ==
"-9223372036854775807sec"_expect);
121 CHECK (
util::toString(MINI) ==
"1/9223372036854775807sec"_expect);
122 CHECK (
util::toString(-MINI) ==
"-1/9223372036854775807sec"_expect);
123 CHECK (
util::toString(1-MINI) ==
"9223372036854775806/9223372036854775807sec"_expect);
124 CHECK (
util::toString(1+MINI) ==
"-9223372036854775808/9223372036854775807sec"_expect);
126 CHECK ((MAXI-1)/MAXI == 1-MINI);
127 CHECK (MAXI/(MAXI-1) > 1);
128 CHECK (MAXI/(MAXI-1) - 1 > MINI);
129 CHECK (1 - MAXI/(MAXI-1) < -MINI);
130 CHECK (
util::toString(MAXI/(MAXI-1)) ==
"9223372036854775807/9223372036854775806sec"_expect);
131 CHECK (
util::toString(MAXI/(MAXI-1) - 1) ==
"1/9223372036854775806sec"_expect);
132 CHECK (
util::toString(1 - MAXI/(MAXI-1)) ==
"-1/9223372036854775806sec"_expect);
135 const Rat MIMI = -MAXI-1;
138 CHECK (
util::toString(1/MIMI) ==
"-1/-9223372036854775808sec"_expect);
142 NOTREACHED(
"expected boost::rational to flounder");
144 catch (std::exception& tilt)
146 CHECK (tilt.what() ==
string{
"bad rational: non-zero singular denominator"});
151 Rat poison = MAXI/49 / (MAXI/49-1);
152 Rat decoy = poison + 5;
155 CHECK (rational_cast<double>(decoy) == 6);
156 CHECK (rational_cast<double>(decoy+5) == 11);
157 CHECK (rational_cast<double>(decoy+50) == -42);
158 CHECK (rational_cast<double>(decoy+500) == 15.999999999999996);
161 CHECK ( can_represent_Sum(decoy,5));
162 CHECK (not can_represent_Sum(decoy,50));
165 CHECK ( can_represent_Sum(decoy,15));
166 CHECK (not can_represent_Sum(decoy,16));
167 CHECK (decoy+16 > 0);
168 CHECK (decoy+43 > 0);
169 CHECK (decoy+44 < 0);
172 CHECK (poison + 1_r/10 > 0);
173 CHECK (poison + 1_r/90 > 0);
174 CHECK (poison + 1_r/98 < 0);
175 CHECK ( can_represent_Sum(poison,1_r/10));
176 CHECK ( can_represent_Sum(poison,1_r/15));
177 CHECK (not can_represent_Sum(poison,1_r/16));
178 CHECK (not can_represent_Sum(poison,1_r/91));
179 CHECK (not can_represent_Sum(poison,1_r/100));
193 CHECK ( 5 ==
ilog2( int64_t(0b101010)));
194 CHECK ( 5 ==
ilog2(uint64_t(0b101010)));
195 CHECK ( 5 ==
ilog2( int32_t(0b101010)));
196 CHECK ( 5 ==
ilog2(uint32_t(0b101010)));
197 CHECK ( 5 ==
ilog2( int16_t(0b101010)));
198 CHECK ( 5 ==
ilog2(uint16_t(0b101010)));
199 CHECK ( 5 ==
ilog2( int8_t(0b101010)));
200 CHECK ( 5 ==
ilog2( uint8_t(0b101010)));
201 CHECK ( 5 ==
ilog2(
int (0b101010)));
202 CHECK ( 5 ==
ilog2( uint (0b101010)));
203 CHECK ( 5 ==
ilog2(
char (0b101010)));
204 CHECK ( 5 ==
ilog2( uchar (0b101010)));
205 CHECK ( 5 ==
ilog2(
long (0b101010)));
206 CHECK ( 5 ==
ilog2( ulong (0b101010)));
207 CHECK ( 5 ==
ilog2(
short (0b101010)));
208 CHECK ( 5 ==
ilog2( ushort (0b101010)));
210 CHECK (63 ==
ilog2(std::numeric_limits<uint64_t>::max()));
211 CHECK (62 ==
ilog2(std::numeric_limits< int64_t>::max()));
212 CHECK (31 ==
ilog2(std::numeric_limits<uint32_t>::max()));
213 CHECK (30 ==
ilog2(std::numeric_limits< int32_t>::max()));
214 CHECK (15 ==
ilog2(std::numeric_limits<uint16_t>::max()));
215 CHECK (14 ==
ilog2(std::numeric_limits< int16_t>::max()));
216 CHECK ( 7 ==
ilog2(std::numeric_limits< uint8_t>::max()));
217 CHECK ( 6 ==
ilog2(std::numeric_limits< int8_t>::max()));
219 CHECK ( 5 ==
ilog2(0b111111));
220 CHECK ( 5 ==
ilog2(0b101110));
221 CHECK ( 5 ==
ilog2(0b100100));
222 CHECK ( 5 ==
ilog2(0b100000));
224 CHECK ( 2 ==
ilog2(4));
225 CHECK ( 1 ==
ilog2(2));
226 CHECK ( 0 ==
ilog2(1));
227 CHECK (-1 ==
ilog2(0));
228 CHECK (-1 ==
ilog2(-1));
230 CHECK (-1 ==
ilog2(std::numeric_limits<uint64_t>::min()));
231 CHECK (-1 ==
ilog2(std::numeric_limits< int64_t>::min()));
232 CHECK (-1 ==
ilog2(std::numeric_limits<uint32_t>::min()));
233 CHECK (-1 ==
ilog2(std::numeric_limits< int32_t>::min()));
234 CHECK (-1 ==
ilog2(std::numeric_limits<uint16_t>::min()));
235 CHECK (-1 ==
ilog2(std::numeric_limits< int16_t>::min()));
236 CHECK (-1 ==
ilog2(std::numeric_limits< uint8_t>::min()));
237 CHECK (-1 ==
ilog2(std::numeric_limits< int8_t>::min()));
242 auto floatLog = [](
auto n)
244 return n <=0? -1 : ilogb(n);
246 auto bitshift = [](
auto n)
248 if (n <= 0)
return -1;
254 auto do_nothing = [](
auto n){
return n; };
256 array<uint64_t, 1000> numz;
259 n =
rani() * uint64_t(1 << 31);
260 CHECK (
ilog2(n) == floatLog(n));
261 CHECK (
ilog2(n) == bitshift(n));
265 auto microbenchmark = [&numz,&dump](
auto algo)
267 using std::chrono::system_clock;
268 using Dur = std::chrono::duration<double>;
269 const double SCALE = 1e9;
271 auto start = system_clock::now();
272 for (uint i=0; i<1000; ++i)
273 for (
auto const& n : numz)
275 Dur duration = system_clock::now () - start;
276 return duration.count()/(1000*1000) * SCALE;
281 auto time_ilog2 = microbenchmark(ilog2<int64_t>);
282 auto time_float = microbenchmark(floatLog);
283 auto time_shift = microbenchmark(bitshift);
284 auto time_ident = microbenchmark(do_nothing);
286 cout <<
"Microbenchmark integer-log2" <<endl
287 <<
"util::ilog2 :"<<time_ilog2<<
"ns"<<endl
288 <<
"std::ilogb :"<<time_float<<
"ns"<<endl
289 <<
"bit-shift :"<<time_shift<<
"ns"<<endl
290 <<
"identity :"<<time_ident<<
"ns"<<endl
291 <<
"(checksum="<<dump<<
")" <<endl;
294 CHECK (time_ilog2 < time_shift);
295 CHECK (time_ident < time_ilog2);
325 const int64_t
MAX{std::numeric_limits<int64_t>::max()};
326 const Rat MAXI = Rat{
MAX};
328 Rat poison = (MAXI-88)/(MAXI/7);
330 auto approx = [](Rat rat){
return rational_cast<
float> (rat); };
332 CHECK (poison+1 < 0);
333 CHECK (approx (poison ) == 6.99999952f);
334 CHECK (approx (poison+1) == -6);
335 CHECK (approx (poison+7) == -6.83047369e-17f);
336 CHECK (approx (poison+9_r/5) == 0.400000006f);
338 Rat sleazy =
reQuant (poison, 1 << 24);
339 CHECK (sleazy.denominator() == 1 << 24);
342 CHECK (sleazy+1 > 0);
343 CHECK (sleazy+7 > 0);
344 CHECK (approx (sleazy) == 7);
345 CHECK (approx (sleazy+1) == 8);
346 CHECK (approx (sleazy+7) == 14);
347 CHECK (approx (sleazy+9_r/5) == 8.80000019f);
349 CHECK (
util::toString (poison) ==
"9223372036854775719/1317624576693539401sec"_expect);
350 CHECK (
util::toString (poison+1) ==
"-7905747460161236496/1317624576693539401sec"_expect);
351 CHECK (
util::toString (sleazy) ==
"117440511/16777216sec"_expect);
352 CHECK (
util::toString (sleazy+1) ==
"134217727/16777216sec"_expect);
355 CHECK (
reQuant (1/poison,
MAX) == 1317624576693539413_r/9223372036854775807);
356 CHECK (
reQuant (-poison, 7777) == -54438_r/ 7777);
357 CHECK (
reQuant (poison, -7777) == -54438_r/-7777);
359 CHECK (approx ( 1/poison ) == 0.142857149f);
360 CHECK (approx (
reQuant (1/poison,
MAX)) == 0.142857149f);
361 CHECK (approx (
reQuant (poison, 7777)) == 6.99987125f);
int64_t reQuant(int64_t num, int64_t den, int64_t u)
Re-Quantise a number into a new grid, truncating to the next lower grid point.
Rational number support, based on boost::rational.
int rani(uint bound=_iBOUND())
#define MAX(A, B)
the inevitable MAX macro, sometimes still necessary in template code
void demonstrate_basics()
Simplistic test class runner.
A collection of frequently used helper functions to support unit testing.
Lumiera error handling (C++ interface).
constexpr int ilog2(I num)
Integral binary logarithm (disregarding fractional part)