ONE - On-device Neural Engine
Loading...
Searching...
No Matches
fp32.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
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
23#ifndef __NNFW_MISC_FP32_H__
24#define __NNFW_MISC_FP32_H__
25
26#include <cmath>
27#include <cfloat>
28#include <algorithm>
29#include <cstdint>
30
31namespace nnfw
32{
33namespace misc
34{
35namespace fp32
36{
37
44inline float relative_diff(float lhs, float rhs)
45{
46 const auto diff = std::fabs(lhs - rhs);
47 const auto base = std::max(std::fabs(lhs), std::fabs(rhs));
48
49 return diff / base;
50}
51
60inline bool epsilon_equal(float expected, float obtained, uint32_t tolerance = 1)
61{
62 if (std::isnan(expected) && std::isnan(obtained))
63 {
64 return true;
65 }
66
67 // Let's use relative epsilon comparision
68 const auto diff = std::fabs(expected - obtained);
69 const auto max = std::max(std::fabs(expected), std::fabs(obtained));
70
71 return diff <= (max * FLT_EPSILON * tolerance);
72}
73
82inline bool absolute_epsilon_equal(float expected, float obtained, float tolerance = 0.001)
83{
84 if (std::isnan(expected) && std::isnan(obtained))
85 {
86 return true;
87 }
88
89 // Let's use absolute epsilon comparision
90 const auto diff = std::fabs(expected - obtained);
91
92 return diff <= tolerance;
93}
94
95} // namespace fp32
96} // namespace misc
97} // namespace nnfw
98
99#endif // __NNFW_MISC_FP32_H__
bool epsilon_equal(float expected, float obtained, uint32_t tolerance=1)
Verify that an obtained float value is equal to the expected float value by using FLT_EPSILON.
Definition fp32.h:60
float relative_diff(float lhs, float rhs)
Get the difference between two float values as a relative value.
Definition fp32.h:44
bool absolute_epsilon_equal(float expected, float obtained, float tolerance=0.001)
Verify that an obtained float value is equal to the expected float value by comparing absolute tolera...
Definition fp32.h:82
Definition topk_v2.h:30