blob: 6ed7f96695054a65c5578c4a347aa45767e8c0ed [file] [log] [blame]
caryclark@google.com639df892012-01-10 21:46:10 +00001
2// inline utilities
3/* Returns 0 if negative, 1 if zero, 2 if positive
4*/
5inline int side(double x) {
6 return (x > 0) + (x >= 0);
7}
8
9/* Returns 1 if negative, 2 if zero, 4 if positive
10*/
11inline int sideBit(double x) {
12 return 1 << side(x);
13}
14
15/* Given the set [0, 1, 2, 3], and two of the four members, compute an XOR mask
16 that computes the other two. Note that:
rmistry@google.comd6176b02012-08-23 18:14:13 +000017
caryclark@google.com639df892012-01-10 21:46:10 +000018 one ^ two == 3 for (0, 3), (1, 2)
19 one ^ two < 3 for (0, 1), (0, 2), (1, 3), (2, 3)
20 3 - (one ^ two) is either 0, 1, or 2
21 1 >> 3 - (one ^ two) is either 0 or 1
22thus:
23 returned == 2 for (0, 3), (1, 2)
24 returned == 3 for (0, 1), (0, 2), (1, 3), (2, 3)
25given that:
26 (0, 3) ^ 2 -> (2, 1) (1, 2) ^ 2 -> (3, 0)
27 (0, 1) ^ 3 -> (3, 2) (0, 2) ^ 3 -> (3, 1) (1, 3) ^ 3 -> (2, 0) (2, 3) ^ 3 -> (1, 0)
28*/
29inline int other_two(int one, int two) {
30 return 1 >> 3 - (one ^ two) ^ 3;
31}
32
33/* Returns -1 if negative, 0 if zero, 1 if positive
34*/
35inline int sign(double x) {
36 return (x > 0) - (x < 0);
37}
38
39inline double interp(double A, double B, double t) {
40 return A + (B - A) * t;
41}