blob: a016093ed651c5fb620b7c5e7b851b24ac776c95 [file] [log] [blame]
Abe Mbise4bd2cb82017-09-27 18:39:19 +01001/*
2 * Copyright (c) 2017 ARM Limited.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#include "FixedPoint.h"
25
26#include "arm_compute/core/Types.h"
27#include "tests/validation/FixedPoint.h"
28#include "tests/validation/Helpers.h"
29
30namespace arm_compute
31{
32namespace test
33{
34namespace validation
35{
36namespace reference
37{
38template <typename T>
39SimpleTensor<T> fixed_point_operation(const SimpleTensor<T> &src, FixedPointOp op)
40{
41 SimpleTensor<T> result(src.shape(), src.data_type());
42
43 const int p = src.fixed_point_position();
44 switch(op)
45 {
46 case FixedPointOp::EXP:
47 for(int i = 0; i < src.num_elements(); ++i)
48 {
49 result[i] = fixed_point_arithmetic::exp(fixed_point_arithmetic::fixed_point<T>(src[i], p, true)).raw();
50 }
51 break;
52 case FixedPointOp::LOG:
53 for(int i = 0; i < src.num_elements(); ++i)
54 {
55 result[i] = fixed_point_arithmetic::log(fixed_point_arithmetic::fixed_point<T>(src[i], p, true)).raw();
56 }
57 break;
58 case FixedPointOp::INV_SQRT:
59 for(int i = 0; i < src.num_elements(); ++i)
60 {
61 result[i] = fixed_point_arithmetic::inv_sqrt(fixed_point_arithmetic::fixed_point<T>(src[i], p, true)).raw();
62 }
63 break;
64 case FixedPointOp::RECIPROCAL:
65 for(int i = 0; i < src.num_elements(); ++i)
66 {
67 result[i] = fixed_point_arithmetic::div(fixed_point_arithmetic::fixed_point<T>(1, p), fixed_point_arithmetic::fixed_point<T>(src[i], p, true)).raw();
68 }
69 break;
70 default:
71 ARM_COMPUTE_ERROR("Fixed point operation not supported");
72 break;
73 }
74
75 return result;
76}
77
78template SimpleTensor<int8_t> fixed_point_operation(const SimpleTensor<int8_t> &src, FixedPointOp op);
79template SimpleTensor<int16_t> fixed_point_operation(const SimpleTensor<int16_t> &src, FixedPointOp op);
80} // namespace reference
81} // namespace validation
82} // namespace test
83} // namespace arm_compute