math_v2.hpp 4.08 KB
Newer Older
Chao Liu's avatar
Chao Liu committed
1
2
3
// SPDX-License-Identifier: MIT
// Copyright (c) 2018-2022, Advanced Micro Devices, Inc. All rights reserved.

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 {

16
17
// math functions for the host,  some are implemented by calling C++ std functions

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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)
{
38
    uint16_t xx = ck::bit_cast<uint16_t>(x);
39

40
    uint16_t abs_xx = xx & 0x7fff;
41

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

    return abs_x;
};

Adam Osewski's avatar
Adam Osewski committed
47
48
49
50
51
52
53
54
#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

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

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

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

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

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

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

Adam Osewski's avatar
Adam Osewski committed
78
79
80
81
82
83
84
85
#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
86
87
88
89
90
static inline __host__ half_t sqrt(half_t x)
{
    return static_cast<half_t>(std::sqrt(static_cast<float>(x)));
};

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

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

95
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); };

104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// 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
124
125
126
127
128
129
130
131
132
#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

133
134
135
136
137
138
139
140
141
142
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;
};
143
144
145
146
147
148
149
150
151
152

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;
};
153

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

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

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

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

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

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

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

184
185
186
187
188
189
190
191
192
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); };

193
194
} // namespace math
} // namespace ck