1 /**
2 Copyright: Copyright (c) 2015-2016 Andrey Penechko.
3 License: $(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0).
4 Authors: Andrey Penechko.
5 */
6 
7 module voxelman.math.utils;
8 
9 import std.traits : isFloatingPoint;
10 import voxelman.math;
11 
12 void nansToZero(T, int size)(ref Vector!(T, size) vector)
13 	if (isFloatingPoint!T)
14 {
15 	foreach(ref item; vector.arrayof)
16 	{
17 		item = isNaN(item) ? 0 : item;
18 	}
19 }
20 
21 void nansToZero(T, int size)(ref T[size] vector)
22 	if (isFloatingPoint!T)
23 {
24 	foreach(ref item; vector)
25 	{
26 		item = isNaN(item) ? 0 : item;
27 	}
28 }
29 
30 T nextPOT(T)(T x) {
31 	--x;
32 	x |= x >> 1;
33 	x |= x >> 2;
34 	x |= x >> 4;
35 	static if (T.sizeof >= 16) x |= x >>  8;
36 	static if (T.sizeof >= 32) x |= x >> 16;
37 	static if (T.sizeof >= 64) x |= x >> 32;
38 	++x;
39 
40 	return x;
41 }
42 
43 unittest {
44 	assert(nextPOT(1) == 1);
45 	assert(nextPOT(2) == 2);
46 	assert(nextPOT(3) == 4);
47 	assert(nextPOT(4) == 4);
48 	assert(nextPOT(5) == 8);
49 	assert(nextPOT(10) == 16);
50 	assert(nextPOT(30) == 32);
51 	assert(nextPOT(250) == 256);
52 	assert(nextPOT(1<<15+1) == 1<<16);
53 	assert(nextPOT(1UL<<31+1) == 1UL<<32);
54 	assert(nextPOT(1UL<<49+1) == 1UL<<50);
55 }