PageRenderTime 37ms CodeModel.GetById 24ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llcommon/is_approx_equal_fraction.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 79 lines | 26 code | 9 blank | 44 comment | 3 complexity | 4291aab442aeddf2ca44a40055bad366 MD5 | raw file
 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=viewerlgpl$
10 * Second Life Viewer Source Code
11 * Copyright (C) 2010, Linden Research, Inc.
12 * 
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation;
16 * version 2.1 of the License only.
17 * 
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 * Lesser General Public License for more details.
22 * 
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
26 * 
27 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
28 * $/LicenseInfo$
29 */
30
31#if ! defined(LL_IS_APPROX_EQUAL_FRACTION_H)
32#define LL_IS_APPROX_EQUAL_FRACTION_H
33
34#include "lldefs.h"
35#include <cmath>
36
37/**
38 * Originally llmath.h contained two complete implementations of
39 * is_approx_equal_fraction(), with signatures as below, bodies identical save
40 * where they specifically mentioned F32/F64. Unifying these into a template
41 * makes sense -- but to preserve the compiler's overload-selection behavior,
42 * we still wrap the template implementation with the specific overloaded
43 * signatures.
44 */
45template <typename FTYPE>
46inline BOOL is_approx_equal_fraction_impl(FTYPE x, FTYPE y, U32 frac_bits)
47{
48    BOOL ret = TRUE;
49    FTYPE diff = (FTYPE) fabs(x - y);
50
51    S32 diffInt = (S32) diff;
52    S32 diffFracTolerance = (S32) ((diff - (FTYPE) diffInt) * (1 << frac_bits));
53
54    // if integer portion is not equal, not enough bits were used for packing
55    // so error out since either the use case is not correct OR there is
56    // an issue with pack/unpack. should fail in either case.
57    // for decimal portion, make sure that the delta is no more than 1
58    // based on the number of bits used for packing decimal portion.
59    if (diffInt != 0 || diffFracTolerance > 1)
60    {
61        ret = FALSE;
62    }
63
64    return ret;
65}
66
67/// F32 flavor
68inline BOOL is_approx_equal_fraction(F32 x, F32 y, U32 frac_bits)
69{
70    return is_approx_equal_fraction_impl<F32>(x, y, frac_bits);
71}
72
73/// F64 flavor
74inline BOOL is_approx_equal_fraction(F64 x, F64 y, U32 frac_bits)
75{
76    return is_approx_equal_fraction_impl<F64>(x, y, frac_bits);
77}
78
79#endif /* ! defined(LL_IS_APPROX_EQUAL_FRACTION_H) */