caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 1 | |
| 2 | // inline utilities |
| 3 | /* Returns 0 if negative, 1 if zero, 2 if positive |
| 4 | */ |
| 5 | inline int side(double x) { |
| 6 | return (x > 0) + (x >= 0); |
| 7 | } |
| 8 | |
| 9 | /* Returns 1 if negative, 2 if zero, 4 if positive |
| 10 | */ |
| 11 | inline 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.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 17 | |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 18 | 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 |
| 22 | thus: |
| 23 | returned == 2 for (0, 3), (1, 2) |
| 24 | returned == 3 for (0, 1), (0, 2), (1, 3), (2, 3) |
| 25 | given 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 | */ |
| 29 | inline 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 | */ |
| 35 | inline int sign(double x) { |
| 36 | return (x > 0) - (x < 0); |
| 37 | } |
| 38 | |
| 39 | inline double interp(double A, double B, double t) { |
| 40 | return A + (B - A) * t; |
| 41 | } |