diff options
Diffstat (limited to 'linden/indra/llcommon/is_approx_equal_fraction.h')
-rw-r--r-- | linden/indra/llcommon/is_approx_equal_fraction.h | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/linden/indra/llcommon/is_approx_equal_fraction.h b/linden/indra/llcommon/is_approx_equal_fraction.h new file mode 100644 index 0000000..d369fbc --- /dev/null +++ b/linden/indra/llcommon/is_approx_equal_fraction.h | |||
@@ -0,0 +1,85 @@ | |||
1 | /** | ||
2 | * @file is_approx_equal_fraction.h | ||
3 | * @author Nat Goodspeed | ||
4 | * @date 2009-01-28 | ||
5 | * @brief lltut.h uses is_approx_equal_fraction(). Moved to this header | ||
6 | * file in llcommon so we can use lltut.h for llcommon tests without | ||
7 | * making llcommon depend on llmath. | ||
8 | * | ||
9 | * $LicenseInfo:firstyear=2009&license=viewergpl$ | ||
10 | * | ||
11 | * Copyright (c) 2009, Linden Research, Inc. | ||
12 | * | ||
13 | * Second Life Viewer Source Code | ||
14 | * The source code in this file ("Source Code") is provided by Linden Lab | ||
15 | * to you under the terms of the GNU General Public License, version 2.0 | ||
16 | * ("GPL"), unless you have obtained a separate licensing agreement | ||
17 | * ("Other License"), formally executed by you and Linden Lab. Terms of | ||
18 | * the GPL can be found in doc/GPL-license.txt in this distribution, or | ||
19 | * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2 | ||
20 | * | ||
21 | * There are special exceptions to the terms and conditions of the GPL as | ||
22 | * it is applied to this Source Code. View the full text of the exception | ||
23 | * in the file doc/FLOSS-exception.txt in this software distribution, or | ||
24 | * online at | ||
25 | * http://secondlifegrid.net/programs/open_source/licensing/flossexception | ||
26 | * | ||
27 | * By copying, modifying or distributing this software, you acknowledge | ||
28 | * that you have read and understood your obligations described above, | ||
29 | * and agree to abide by those obligations. | ||
30 | * | ||
31 | * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO | ||
32 | * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY, | ||
33 | * COMPLETENESS OR PERFORMANCE. | ||
34 | * $/LicenseInfo$ | ||
35 | */ | ||
36 | |||
37 | #if ! defined(LL_IS_APPROX_EQUAL_FRACTION_H) | ||
38 | #define LL_IS_APPROX_EQUAL_FRACTION_H | ||
39 | |||
40 | #include "lldefs.h" | ||
41 | #include <cmath> | ||
42 | |||
43 | /** | ||
44 | * Originally llmath.h contained two complete implementations of | ||
45 | * is_approx_equal_fraction(), with signatures as below, bodies identical save | ||
46 | * where they specifically mentioned F32/F64. Unifying these into a template | ||
47 | * makes sense -- but to preserve the compiler's overload-selection behavior, | ||
48 | * we still wrap the template implementation with the specific overloaded | ||
49 | * signatures. | ||
50 | */ | ||
51 | template <typename FTYPE> | ||
52 | inline BOOL is_approx_equal_fraction_impl(FTYPE x, FTYPE y, U32 frac_bits) | ||
53 | { | ||
54 | BOOL ret = TRUE; | ||
55 | FTYPE diff = (FTYPE) fabs(x - y); | ||
56 | |||
57 | S32 diffInt = (S32) diff; | ||
58 | S32 diffFracTolerance = (S32) ((diff - (FTYPE) diffInt) * (1 << frac_bits)); | ||
59 | |||
60 | // if integer portion is not equal, not enough bits were used for packing | ||
61 | // so error out since either the use case is not correct OR there is | ||
62 | // an issue with pack/unpack. should fail in either case. | ||
63 | // for decimal portion, make sure that the delta is no more than 1 | ||
64 | // based on the number of bits used for packing decimal portion. | ||
65 | if (diffInt != 0 || diffFracTolerance > 1) | ||
66 | { | ||
67 | ret = FALSE; | ||
68 | } | ||
69 | |||
70 | return ret; | ||
71 | } | ||
72 | |||
73 | /// F32 flavor | ||
74 | inline BOOL is_approx_equal_fraction(F32 x, F32 y, U32 frac_bits) | ||
75 | { | ||
76 | return is_approx_equal_fraction_impl<F32>(x, y, frac_bits); | ||
77 | } | ||
78 | |||
79 | /// F64 flavor | ||
80 | inline BOOL is_approx_equal_fraction(F64 x, F64 y, U32 frac_bits) | ||
81 | { | ||
82 | return is_approx_equal_fraction_impl<F64>(x, y, frac_bits); | ||
83 | } | ||
84 | |||
85 | #endif /* ! defined(LL_IS_APPROX_EQUAL_FRACTION_H) */ | ||