include/ck/utility/mxf4_utils.hpp Source File

include/ck/utility/mxf4_utils.hpp Source File#

Composable Kernel: include/ck/utility/mxf4_utils.hpp Source File
mxf4_utils.hpp
Go to the documentation of this file.
1 // SPDX-License-Identifier: MIT
2 // Copyright (c) 2024, Advanced Micro Devices, Inc. All rights reserved.
3 
4 #pragma once
5 
8 
9 namespace ck::utils {
10 
11 template <>
12 __host__ __device__ inline bool is_nan<f4_t>(e8m0_bexp_t const scale,
13  f4_t const dataBytes [[maybe_unused]])
14 {
15  // no need to check for data as it does not have NaN representation
16  return scale == NumericLimits<e8m0_bexp_t>::QuietNaN();
17 }
18 
19 // no infinity representation in ocp_e2m1_mxfp4 will always return false
20 template <>
21 __host__ __device__ inline bool is_inf<f4_t>(e8m0_bexp_t const scale [[maybe_unused]],
22  f4_t const data [[maybe_unused]])
23 {
24  // no inf representation for ocp_e2m1_mxfp4
25  return false;
26 }
27 
28 template <>
29 __host__ __device__ inline bool is_zero<f4_t>(e8m0_bexp_t const scale, f4_t const data)
30 {
31  if(is_nan<f4_t>(scale, data))
32  return false;
33 
34  // no need to check for scale as it does not have a 0 representation
35  f4_t result = (data & 0b00001111) & NumericUtils<f4_t>::set_sign_mask;
36 
37  return result == 0b0;
38 }
39 
40 template <>
41 __host__ __device__ inline float to_float<f4_t>(e8m0_bexp_t const scale, f4_t const data)
42 {
43  if(is_nan<f4_t>(scale, data))
44  return std::numeric_limits<float>::quiet_NaN();
45 
46  if(is_zero<f4_t>(scale, data))
47  return 0.0f;
48 
49  f4_t prepared_data = data & 0b00001111;
50 
51  int scale_exp = get_exponent_value<e8m0_bexp_t>(scale);
52 
53  return convert_to_float<f4_t>(prepared_data, scale_exp);
54 }
55 
56 template <>
57 __host__ __device__ inline f4_t sat_convert_to_type<f4_t>(float value)
58 {
59  cvt t;
60  t.value_float = value;
61  uint32_t sign = t.value_bitwise >> 31;
62 
63  if(std::isnan(value))
64  {
65 
68  }
69 
70  if(std::abs(value) > NumericLimits<f4_t>::Max()) // covers inf case as well
73 
74  f4_t res = convert_to_type<f4_t>(value);
75 
78  return value < 0 ? NumericUtils<f4_t>::negative_zero_mask
80 
81  return res;
82 }
83 
84 template <>
85 __host__ __device__ inline f4_t sat_convert_to_type_sr<f4_t>(float value, uint32_t seed)
86 {
87  cvt t;
88  t.value_float = value;
89  uint32_t sign = t.value_bitwise >> 31;
90 
91  if(std::isnan(value))
94 
95  if(std::abs(value) > NumericLimits<f4_t>::Max()) // covers inf case as well
98 
99  f4_t res = convert_to_type_sr<f4_t>(value, seed);
100 
103  return value < 0 ? NumericUtils<f4_t>::negative_zero_mask
105 
106  return res;
107 }
108 
109 } // namespace ck::utils
Definition: check_err.hpp:24
__host__ __device__ bool is_nan< f4_t >(e8m0_bexp_t const scale, f4_t const dataBytes[[maybe_unused]])
Definition: mxf4_utils.hpp:12
__host__ __device__ f4_t sat_convert_to_type_sr< f4_t >(float value, uint32_t seed)
Definition: mxf4_utils.hpp:85
__host__ __device__ int get_exponent_value< e8m0_bexp_t >(e8m0_bexp_t x)
Definition: e8m0.hpp:73
__host__ __device__ bool is_zero< f4_t >(e8m0_bexp_t const scale, f4_t const data)
Definition: mxf4_utils.hpp:29
__host__ __device__ float to_float< f4_t >(e8m0_bexp_t const scale, f4_t const data)
Definition: mxf4_utils.hpp:41
__host__ __device__ bool is_inf< f4_t >(e8m0_bexp_t const scale[[maybe_unused]], f4_t const data[[maybe_unused]])
Definition: mxf4_utils.hpp:21
__host__ __device__ f4_t sat_convert_to_type< f4_t >(float value)
Definition: mxf4_utils.hpp:57
unsigned _BitInt(4) f4_t
Definition: data_type.hpp:27
Definition: data_type.hpp:2831
__host__ static constexpr __device__ T QuietNaN()
Definition: data_type.hpp:2835
Definition: data_type.hpp:3078
Unsigned representation of a conventional biased Float32 exponent.
Definition: e8m0.hpp:25
Definition: mxfp_utils.hpp:9
float value_float
Definition: mxfp_utils.hpp:10
uint32_t value_bitwise
Definition: mxfp_utils.hpp:11