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
17package com.example.android.rs.matherr;
18
19import android.content.res.Resources;
20import android.renderscript.*;
21import java.lang.Float;
22import java.lang.Math;
23
24public class MathErr {
25    private RenderScript mRS;
26    private Allocation mAllocationSrc;
27    private Allocation mAllocationRes;
28    private ScriptC_math_err mScript;
29    private java.util.Random mRand = new java.util.Random();
30
31    private final int BUF_SIZE = 4096;
32    float mSrc[] = new float[BUF_SIZE];
33    float mRef[] = new float[BUF_SIZE];
34    float mRes[] = new float[BUF_SIZE];
35
36    MathErr(RenderScript rs) {
37        mRS = rs;
38        mScript = new ScriptC_math_err(mRS);
39
40        mAllocationSrc = Allocation.createSized(rs, Element.F32(rs), BUF_SIZE);
41        mAllocationRes = Allocation.createSized(rs, Element.F32(rs), BUF_SIZE);
42
43        testExp2();
44        testLog2();
45    }
46
47    void buildRand() {
48        for (int i=0; i < BUF_SIZE; i++) {
49            mSrc[i] = (((float)i) / 9) - 200;
50            //mSrc[i] = Float.intBitsToFloat(mRand.nextInt());
51        }
52        mAllocationSrc.copyFrom(mSrc);
53    }
54
55    void logErr() {
56        mAllocationRes.copyTo(mRes);
57        for (int i=0; i < BUF_SIZE; i++) {
58            int err = Float.floatToRawIntBits(mRef[i]) - Float.floatToRawIntBits(mRes[i]);
59            err = Math.abs(err);
60            if (err > 8096) {
61                android.util.Log.v("err", "error " + err + " src " + mSrc[i] + " ref " + mRef[i] + " res " + mRes[i]);
62            }
63        }
64    }
65
66    void testExp2() {
67        android.util.Log.v("err", "testing exp2");
68        buildRand();
69        mScript.forEach_testExp2(mAllocationSrc, mAllocationRes);
70        for (int i=0; i < BUF_SIZE; i++) {
71            mRef[i] = (float)Math.pow(2.f, mSrc[i]);
72        }
73        logErr();
74    }
75
76    void testLog2() {
77        android.util.Log.v("err", "testing log2");
78        buildRand();
79        mScript.forEach_testLog2(mAllocationSrc, mAllocationRes);
80        for (int i=0; i < BUF_SIZE; i++) {
81            mRef[i] = (float)Math.log(mSrc[i]) * 1.442695041f;
82        }
83        logErr();
84    }
85
86}
87