1/*******************************************************************************
2* Copyright 2018-2022 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#include <float.h>
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21
22#include "dnnl_common.hpp"
23#include "oneapi/dnnl/dnnl.h"
24
25#include "reorder.hpp"
26
27namespace reorder {
28
29const float int_max_exact = 1 << 24;
30const float f16_max_exact = 1 << 11;
31
32#define REG(dt, min, max) \
33 const dt_conf_s CONCAT2(_conf_, dt) = {CONCAT2(dnnl_, dt), min, max}; \
34 const dt_conf_t CONCAT2(conf_, dt) = &CONCAT2(_conf_, dt);
35
36REG(f32, -int_max_exact, int_max_exact);
37REG(f64, -int_max_exact, int_max_exact);
38REG(f16, -f16_max_exact, f16_max_exact);
39REG(bf16, -int_max_exact, int_max_exact);
40// Do not exceed max float value representable in integer. Otherwise, we get
41// a correctness issue caused by different computations in reference and the
42// library.
43REG(s32, INT_MIN, BENCHDNN_S32_TO_F32_SAT_CONST);
44REG(s8, INT8_MIN, INT8_MAX);
45REG(u8, 0, UINT8_MAX);
46
47#undef REG
48
49dt_conf_t dt2cfg(dnnl_data_type_t dt) {
50#define CASE(cfg) \
51 if (CONCAT2(dnnl_, cfg) == dt) return CONCAT2(conf_, cfg)
52 CASE(f32);
53 CASE(f64);
54 CASE(f16);
55 CASE(bf16);
56 CASE(s32);
57 CASE(s8);
58 CASE(u8);
59#undef CASE
60 SAFE_V(FAIL);
61 return conf_f32;
62}
63
64dnnl_data_type_t cfg2dt(dt_conf_t cfg) {
65#define CASE(_cfg) \
66 if (cfg == CONCAT2(conf_, _cfg)) return CONCAT2(dnnl_, _cfg)
67 CASE(f32);
68 CASE(f64);
69 CASE(f16);
70 CASE(bf16);
71 CASE(s32);
72 CASE(s8);
73 CASE(u8);
74#undef CASE
75 SAFE_V(FAIL);
76 return dnnl_f32;
77}
78
79} // namespace reorder
80