"magic_pdf/vscode:/vscode.git/clone" did not exist on "068fab7f815a2a4dba89b2bad2b53e7795b38559"
math_v2.hpp 3.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

6
#include <cmath>
Chao Liu's avatar
Chao Liu committed
7
8
9

#include "ck/utility/data_type.hpp"
#include "ck/utility/type.hpp"
10
11
12
13

namespace ck {
namespace math {

14
15
// math functions for the host,  some are implemented by calling C++ std functions

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

38
    uint16_t abs_xx = xx & 0x7fff;
39

40
    half_t abs_x = ck::bit_cast<half_t>(abs_xx);
41
42
43
44

    return abs_x;
};

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

53
static inline __host__ bool isnan(float x) { return std::isnan(x); };
54

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

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

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

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

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

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

84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
static inline __host__ float sqrt(float x) { return std::sqrt(x); };

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

// 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
108
109
110
111
112
113
114
115
116
#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

117
118
119
120
121
122
123
124
125
126
127
static inline __device__ half_t abs(half_t x) { return ::__habs(x); };

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

129
130
131
132
static inline __device__ bool isnan(int32_t x)
{
    (void)x;
    return false;
133
};
134

Adam Osewski's avatar
Adam Osewski committed
135
136
137
138
139
140
141
142
#ifdef CK_EXPERIMENTAL_BIT_INT_EXTENSION_INT4
static inline __device__ bool isnan(int4_t x)
{
    (void)x;
    return false;
};
#endif

143
144
145
146
147
148
static inline __device__ bool isnan(half_t x) { return ::__hisnan(x); };

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

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

149
150
} // namespace math
} // namespace ck