"vscode:/vscode.git/clone" did not exist on "60e85da55e3111c8d43ca8562e4b05014dad8e39"
math_v2.hpp 4.11 KB
Newer Older
Chao Liu's avatar
Chao Liu committed
1
// SPDX-License-Identifier: MIT
Illia Silin's avatar
Illia Silin committed
2
// Copyright (c) 2018-2023, Advanced Micro Devices, Inc. All rights reserved.
Chao Liu's avatar
Chao Liu committed
3

Chao Liu's avatar
Chao Liu committed
4
#pragma once
5

Qianfeng's avatar
Qianfeng committed
6
#ifndef __HIP_DEVICE_COMPILE__
7
#include <cmath>
Qianfeng's avatar
Qianfeng committed
8
#endif
Chao Liu's avatar
Chao Liu committed
9
10
11

#include "ck/utility/data_type.hpp"
#include "ck/utility/type.hpp"
12
13
14
15

namespace ck {
namespace math {

Umang Yadav's avatar
Umang Yadav committed
16
#ifndef __HIPCC_RTC__
17
18
// math functions for the host,  some are implemented by calling C++ std functions

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
static inline __host__ float abs(float x) { return std::abs(x); };

static inline __host__ double abs(double x) { return std::abs(x); };

static inline __host__ int8_t abs(int8_t x)
{
    int8_t sgn = x >> (8 - 1);

    return (x ^ sgn) - sgn;
};

static inline __host__ int32_t abs(int32_t x)
{
    int32_t sgn = x >> (32 - 1);

    return (x ^ sgn) - sgn;
};

static inline __host__ half_t abs(half_t x)
{
39
    uint16_t xx = ck::bit_cast<uint16_t>(x);
40

41
    uint16_t abs_xx = xx & 0x7fff;
42

43
    half_t abs_x = ck::bit_cast<half_t>(abs_xx);
44
45
46
47

    return abs_x;
};

Adam Osewski's avatar
Adam Osewski committed
48
49
50
51
52
53
54
55
#ifdef CK_EXPERIMENTAL_BIT_INT_EXTENSION_INT4
static inline __host__ int4_t abs(int4_t x)
{
    int4_t sgn = x >> (4 - 1);
    return (x ^ sgn) - sgn;
}
#endif

56
static inline __host__ bool isnan(float x) { return std::isnan(x); };
57

58
static inline __host__ bool isnan(double x) { return std::isnan(x); };
59

60
static inline __host__ bool isnan(int8_t x)
61
62
63
64
65
{
    (void)x;
    return false;
};

66
static inline __host__ bool isnan(int32_t x)
67
68
69
70
71
72
73
{
    (void)x;
    return false;
};

static inline __host__ bool isnan(half_t x)
{
74
75
76
77
78
    uint16_t xx = ck::bit_cast<uint16_t>(x);

    return (xx & 0x7FFF) > 0x7C00;
};

Adam Osewski's avatar
Adam Osewski committed
79
80
81
82
83
84
85
86
#ifdef CK_EXPERIMENTAL_BIT_INT_EXTENSION_INT4
static inline __host__ bool isnan(int4_t x)
{
    (void)x;
    return false;
};
#endif

rocking5566's avatar
rocking5566 committed
87
88
89
90
91
static inline __host__ half_t sqrt(half_t x)
{
    return static_cast<half_t>(std::sqrt(static_cast<float>(x)));
};

92
93
94
95
static inline __host__ float sqrt(float x) { return std::sqrt(x); };

static inline __host__ double sqrt(double x) { return std::sqrt(x); };

96
97
98
99
100
101
102
103
static inline __host__ half_t tanh(half_t x)
{
    return static_cast<half_t>(std::tanh(static_cast<float>(x)));
};

static inline __host__ float tanh(float x) { return std::tanh(x); };

static inline __host__ double tanh(double x) { return std::tanh(x); };
Umang Yadav's avatar
Umang Yadav committed
104
#endif
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// math functions for the HIP kernel,  some are implemented by calling hip builtin functions

static inline __device__ float abs(float x) { return ::abs(x); };

static inline __device__ double abs(double x) { return ::abs(x); };

static inline __device__ int8_t abs(int8_t x)
{
    int8_t sgn = x >> (8 - 1);

    return (x ^ sgn) - sgn;
};

static inline __device__ int32_t abs(int32_t x)
{
    int32_t sgn = x >> (32 - 1);

    return (x ^ sgn) - sgn;
};

Adam Osewski's avatar
Adam Osewski committed
125
126
127
128
129
130
131
132
133
#ifdef CK_EXPERIMENTAL_BIT_INT_EXTENSION_INT4
static inline __device__ int4_t abs(int4_t x)
{
    int4_t sgn = x >> (4 - 1);

    return (x ^ sgn) - sgn;
};
#endif

134
135
136
137
138
139
140
141
142
143
static inline __device__ half_t abs(half_t x)
{
    uint16_t xx = ck::bit_cast<uint16_t>(x);

    uint16_t abs_xx = xx & 0x7fff;

    half_t abs_x = ck::bit_cast<half_t>(abs_xx);

    return abs_x;
};
144
145
146
147
148
149
150
151
152
153

static inline __device__ bool isnan(float x) { return ::isnan(x); };

static inline __device__ bool isnan(double x) { return ::isnan(x); };

static inline __device__ bool isnan(int8_t x)
{
    (void)x;
    return false;
};
154

155
156
157
158
static inline __device__ bool isnan(int32_t x)
{
    (void)x;
    return false;
159
};
160

Adam Osewski's avatar
Adam Osewski committed
161
162
163
164
165
166
167
168
#ifdef CK_EXPERIMENTAL_BIT_INT_EXTENSION_INT4
static inline __device__ bool isnan(int4_t x)
{
    (void)x;
    return false;
};
#endif

169
170
171
172
173
174
static inline __device__ bool isnan(half_t x)
{
    uint16_t xx = ck::bit_cast<uint16_t>(x);

    return (xx & 0x7FFF) > 0x7C00;
};
175

rocking5566's avatar
rocking5566 committed
176
177
178
179
180
static inline __device__ half_t sqrt(half_t x)
{
    return static_cast<half_t>(__builtin_amdgcn_sqrtf(static_cast<float>(x)));
};

181
static inline __device__ float sqrt(float x) { return __builtin_amdgcn_sqrtf(x); };
182

183
static inline __device__ double sqrt(double x) { return __builtin_amdgcn_sqrt(x); };
184

185
186
187
188
189
190
191
192
193
static inline __device__ half_t tanh(half_t x)
{
    return static_cast<half_t>(::tanhf(static_cast<float>(x)));
};

static inline __device__ float tanh(float x) { return ::tanhf(x); };

static inline __device__ double tanh(double x) { return ::tanh(x); };

194
195
} // namespace math
} // namespace ck