• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2012 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 // inline utilities
9 /* Returns 0 if negative, 1 if zero, 2 if positive
10 */
side(double x)11 inline int side(double x) {
12     return (x > 0) + (x >= 0);
13 }
14 
15 /* Returns 1 if negative, 2 if zero, 4 if positive
16 */
sideBit(double x)17 inline int sideBit(double x) {
18     return 1 << side(x);
19 }
20 
21 /* Given the set [0, 1, 2, 3], and two of the four members, compute an XOR mask
22    that computes the other two. Note that:
23 
24    one ^ two == 3 for (0, 3), (1, 2)
25    one ^ two <  3 for (0, 1), (0, 2), (1, 3), (2, 3)
26    3 - (one ^ two) is either 0, 1, or 2
27    1 >> 3 - (one ^ two) is either 0 or 1
28 thus:
29    returned == 2 for (0, 3), (1, 2)
30    returned == 3 for (0, 1), (0, 2), (1, 3), (2, 3)
31 given that:
32    (0, 3) ^ 2 -> (2, 1)  (1, 2) ^ 2 -> (3, 0)
33    (0, 1) ^ 3 -> (3, 2)  (0, 2) ^ 3 -> (3, 1)  (1, 3) ^ 3 -> (2, 0)  (2, 3) ^ 3 -> (1, 0)
34 */
other_two(int one,int two)35 inline int other_two(int one, int two) {
36     return 1 >> 3 - (one ^ two) ^ 3;
37 }
38 
39 /* Returns -1 if negative, 0 if zero, 1 if positive
40 */
sign(double x)41 inline int sign(double x) {
42     return (x > 0) - (x < 0);
43 }
44 
interp(double A,double B,double t)45 inline double interp(double A, double B, double t) {
46     return A + (B - A) * t;
47 }
48