math_v2.hpp 2.5 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;
};

45
static inline __host__ bool isnan(float x) { return std::isnan(x); };
46

47
static inline __host__ bool isnan(double x) { return std::isnan(x); };
48

49
static inline __host__ bool isnan(int8_t x)
50
51
52
53
54
{
    (void)x;
    return false;
};

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

static inline __host__ bool isnan(half_t x)
{
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
    uint16_t xx = ck::bit_cast<uint16_t>(x);

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

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

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

104
105
106
107
static inline __device__ bool isnan(int32_t x)
{
    (void)x;
    return false;
108
};
109

110
111
112
113
114
115
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); };

116
117
} // namespace math
} // namespace ck