blob: b7e69fb65c45f38c2b3645fe026e30be1bb78619 [file] [log] [blame]
caryclark@google.com9e49fb62012-08-27 14:11:33 +00001/*
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 */
caryclark@google.com639df892012-01-10 21:46:10 +00007
8// inline utilities
9/* Returns 0 if negative, 1 if zero, 2 if positive
10*/
11inline int side(double x) {
12 return (x > 0) + (x >= 0);
13}
14
15/* Returns 1 if negative, 2 if zero, 4 if positive
16*/
17inline 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:
rmistry@google.comd6176b02012-08-23 18:14:13 +000023
caryclark@google.com639df892012-01-10 21:46:10 +000024 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
28thus:
29 returned == 2 for (0, 3), (1, 2)
30 returned == 3 for (0, 1), (0, 2), (1, 3), (2, 3)
31given 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*/
35inline 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*/
41inline int sign(double x) {
42 return (x > 0) - (x < 0);
43}
44
45inline double interp(double A, double B, double t) {
46 return A + (B - A) * t;
47}