1/*******************************************************************************
2* Copyright 2021 Intel Corporation
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#ifndef REF_IO_HELPER_HPP
18#define REF_IO_HELPER_HPP
19
20#include <cassert>
21
22#include "common/c_types_map.hpp"
23#include "common/dnnl_traits.hpp"
24
25#include "cpu/simple_q10n.hpp"
26
27namespace dnnl {
28namespace impl {
29namespace cpu {
30namespace io {
31
32inline int load_int_value(data_type_t dt, const void *ptr, dim_t idx) {
33 assert(ptr);
34#define CASE(dt) \
35 case dt: \
36 return static_cast<int>( \
37 reinterpret_cast<const typename prec_traits<dt>::type *>( \
38 ptr)[idx]);
39
40 using namespace data_type;
41 switch (dt) {
42 CASE(s32);
43 CASE(s8);
44 CASE(u8);
45 default: assert(!"bad data_type");
46 }
47
48#undef CASE
49 return INT_MAX;
50}
51
52inline float load_float_value(data_type_t dt, const void *ptr, dim_t idx) {
53 assert(ptr);
54#define CASE(dt) \
55 case dt: \
56 return static_cast<float>( \
57 reinterpret_cast<const typename prec_traits<dt>::type *>( \
58 ptr)[idx]);
59
60 using namespace data_type;
61 switch (dt) {
62 CASE(bf16);
63 CASE(f16);
64 CASE(f32);
65 CASE(s32);
66 CASE(s8);
67 CASE(u8);
68 default: assert(!"bad data_type");
69 }
70
71#undef CASE
72 return NAN;
73}
74
75inline void store_float_value(data_type_t dt, float val, void *ptr, dim_t idx) {
76 assert(ptr);
77#define CASE(dt) \
78 case dt: { \
79 using type_ = typename prec_traits<dt>::type; \
80 *(reinterpret_cast<type_ *>(ptr) + idx) \
81 = cpu::saturate_and_round<type_>(val); \
82 } break;
83
84 using namespace data_type;
85 switch (dt) {
86 CASE(bf16);
87 CASE(f16);
88 CASE(f32);
89 CASE(s32);
90 CASE(s8);
91 CASE(u8);
92 default: assert(!"bad data_type");
93 }
94
95#undef CASE
96}
97
98} // namespace io
99} // namespace cpu
100} // namespace impl
101} // namespace dnnl
102
103#endif
104
105// vim: et ts=4 sw=4 cindent cino+=l0,\:4,N-s
106