math_err.rs revision 572a5031a5d8602db0bec0b253428a034bd4dd59
1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma version(1)
18#pragma rs java_package_name(com.example.android.rs.matherr)
19
20typedef union
21{
22  float fv;
23  int32_t iv;
24} ieee_float_shape_type;
25
26/* Get a 32 bit int from a float.  */
27
28#define GET_FLOAT_WORD(i,d)         \
29do {                                \
30  ieee_float_shape_type gf_u;       \
31  gf_u.fv = (d);                    \
32  (i) = gf_u.iv;                    \
33} while (0)
34
35/* Set a float from a 32 bit int.  */
36
37#define SET_FLOAT_WORD(d,i)         \
38do {                                \
39  ieee_float_shape_type sf_u;       \
40  sf_u.iv = (i);                    \
41  (d) = sf_u.fv;                    \
42} while (0)
43
44
45static float fast_log2(float v) {
46    int32_t ibits;
47    GET_FLOAT_WORD(ibits, v);
48
49    int32_t e = (ibits >> 23) & 0xff;
50
51    ibits &= 0x7fffff;
52    ibits |= 127 << 23;
53
54    float ir;
55    SET_FLOAT_WORD(ir, ibits);
56
57    ir -= 1.5f;
58    float ir2 = ir*ir;
59    float adj2 = 0.405465108f + // -0.00009f +
60                 (0.666666667f * ir) -
61                 (0.222222222f * ir2) +
62                 (0.098765432f * ir*ir2) -
63                 (0.049382716f * ir2*ir2) +
64                 (0.026337449f * ir*ir2*ir2) -
65                 (0.014631916f * ir2*ir2*ir2);
66    adj2 *= (1.f / 0.693147181f);
67
68    return (float)(e - 127) + adj2;
69}
70
71void testExp2(const float *in, float *out) {
72    float i = *in;
73    if (i > (-125.f) && i < 125.f) {
74        *out = native_exp2(i);
75    } else {
76        *out = exp2(i);
77    }
78    *out = native_exp2(i);
79}
80
81void testLog2(const float *in, float *out) {
82    *out = fast_log2(*in);
83}
84
85