blob: 2980e2f32484e96abaa75aa933bccaa0f546b005 [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#ifndef ARM_COMPUTE_TEST_FIXED_POINT_FIXTURE
25#define ARM_COMPUTE_TEST_FIXED_POINT_FIXTURE
26
27#include "arm_compute/core/TensorShape.h"
28#include "arm_compute/core/Types.h"
29#include "tests/AssetsLibrary.h"
30#include "tests/Globals.h"
31#include "tests/IAccessor.h"
32#include "tests/framework/Asserts.h"
33#include "tests/framework/Fixture.h"
34#include "tests/validation/CPP/FixedPoint.h"
35#include "tests/validation/Helpers.h"
36
37namespace arm_compute
38{
39namespace test
40{
41namespace validation
42{
43template <typename TensorType, typename AccessorType, typename T>
44class FixedPointValidationFixture : public framework::Fixture
45{
46public:
47 template <typename...>
48 void setup(TensorShape shape, DataType dt, FixedPointOp op, int fractional_bits)
49 {
50 _fractional_bits = fractional_bits;
51 _target = compute_target(shape, dt, op, fractional_bits);
52 _reference = compute_reference(shape, dt, op, fractional_bits);
53 }
54
55protected:
56 template <typename U>
57 void fill(U &&tensor, int min, int max, int i)
58 {
59 std::uniform_int_distribution<> distribution(min, max);
60 library->fill(tensor, distribution, i);
61 }
62
63 TensorType compute_target(const TensorShape &shape, DataType dt, FixedPointOp op, int fixed_point_position)
64 {
65 // Create tensors
66 TensorType src = create_tensor<TensorType>(shape, dt, 1, fixed_point_position);
67 TensorType dst = create_tensor<TensorType>(shape, dt, 1, fixed_point_position);
68
69 // Allocate tensors
70 src.allocator()->allocate();
71 dst.allocator()->allocate();
72
73 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
74 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
75
76 compute_target_impl<TensorType, AccessorType, T>(shape, dt, op, fixed_point_position, src, dst);
77
78 return dst;
79 }
80
81 SimpleTensor<T> compute_reference(const TensorShape &shape, DataType dt, FixedPointOp op, int fixed_point_position)
82 {
83 // Create reference
84 SimpleTensor<T> src{ shape, dt, 1, fixed_point_position };
85
86 // Fill reference
87 int min = 0;
88 int max = 0;
89 switch(op)
90 {
91 case FixedPointOp::EXP:
92 min = -(1 << (fixed_point_position - 1));
93 max = (1 << (fixed_point_position - 1));
94 break;
95 case FixedPointOp::INV_SQRT:
96 min = 1;
97 max = (dt == DataType::QS8) ? 0x7F : 0x7FFF;
98 break;
99 case FixedPointOp::LOG:
100 min = (1 << (fixed_point_position - 1));
101 max = (dt == DataType::QS8) ? 0x3F : 0x3FFF;
102 break;
103 case FixedPointOp::RECIPROCAL:
104 min = 15;
105 max = (dt == DataType::QS8) ? 0x7F : 0x7FFF;
106 break;
107 default:
108 ARM_COMPUTE_ERROR("Fixed point operation not supported");
109 break;
110 }
111 fill(src, min, max, 0);
112
113 return reference::fixed_point_operation<T>(src, op);
114 }
115
116 TensorType _target{};
117 SimpleTensor<T> _reference{};
118 int _fractional_bits{};
119};
120} // namespace validation
121} // namespace test
122} // namespace arm_compute
123#endif /* ARM_COMPUTE_TEST_FIXED_POINT_FIXTURE */