forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfmod_fast.cpp
More file actions
160 lines (134 loc) · 4.78 KB
/
Copy pathfmod_fast.cpp
File metadata and controls
160 lines (134 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include "exprs/function/fmod_fast.h"
#include <string.h>
#include <cmath>
#include "common/compiler_util.h"
namespace doris::fmod_fast {
namespace {
#if defined(__x86_64__) && (defined(__GNUC__) || defined(__clang__))
#define DORIS_HAS_X87_FMOD_FAST 1
ALWAYS_INLINE inline double fmod_x87_fprem(double a, double b) {
double r;
asm volatile(
"fldl %[b]\n\t"
"fldl %[a]\n\t"
"1:\n\t"
"fprem\n\t"
"fnstsw %%ax\n\t"
"testb $4, %%ah\n\t"
"jne 1b\n\t"
"fstp %%st(1)\n\t"
"fstpl %[r]\n\t"
: [r] "=m"(r)
: [a] "m"(a), [b] "m"(b)
: "ax", "cc", "st");
return r;
}
#else
#define DORIS_HAS_X87_FMOD_FAST 0
#endif
ALWAYS_INLINE inline double fmod_double(double a, double b) {
#if DORIS_HAS_X87_FMOD_FAST
if (b != 0.0 && std::isfinite(a) && std::isfinite(b)) {
double abs_a = std::fabs(a);
double abs_b = std::fabs(b);
if (abs_a < abs_b) {
return a;
}
if (abs_a == abs_b) {
return std::copysign(0.0, a);
}
return fmod_x87_fprem(a, b);
}
#endif
return std::fmod(a, b);
}
ALWAYS_INLINE inline float fmod_float(float a, float b) {
return static_cast<float>(fmod_double(static_cast<double>(a), static_cast<double>(b)));
}
ALWAYS_INLINE inline double fmod_value(double a, double b) {
return fmod_double(a, b);
}
ALWAYS_INLINE inline float fmod_value(float a, float b) {
return fmod_float(a, b);
}
template <typename T>
ALWAYS_INLINE inline void vector_vector_impl(const T* lhs, const T* rhs, T* result,
uint8_t* null_map, size_t size) {
for (size_t i = 0; i < size; ++i) {
uint8_t is_null = rhs[i] == T(0);
null_map[i] = is_null;
T adjusted_rhs = rhs[i] + static_cast<T>(is_null);
result[i] = fmod_value(lhs[i], adjusted_rhs);
}
}
template <typename T>
ALWAYS_INLINE inline void vector_constant_impl(const T* lhs, T rhs, T* result, uint8_t* null_map,
size_t size) {
uint8_t is_null = rhs == T(0);
memset(null_map, is_null, size);
if (is_null) {
return;
}
for (size_t i = 0; i < size; ++i) {
result[i] = fmod_value(lhs[i], rhs);
}
}
template <typename T>
ALWAYS_INLINE inline void constant_vector_impl(T lhs, const T* rhs, T* result, uint8_t* null_map,
size_t size) {
for (size_t i = 0; i < size; ++i) {
uint8_t is_null = rhs[i] == T(0);
null_map[i] = is_null;
T adjusted_rhs = rhs[i] + static_cast<T>(is_null);
result[i] = fmod_value(lhs, adjusted_rhs);
}
}
} // namespace
bool is_x87_fast_path_enabled() {
return DORIS_HAS_X87_FMOD_FAST;
}
double scalar(double a, double b) {
return fmod_double(a, b);
}
float scalar(float a, float b) {
return fmod_float(a, b);
}
void vector_vector(const double* lhs, const double* rhs, double* result, uint8_t* null_map,
size_t size) {
vector_vector_impl(lhs, rhs, result, null_map, size);
}
void vector_vector(const float* lhs, const float* rhs, float* result, uint8_t* null_map,
size_t size) {
vector_vector_impl(lhs, rhs, result, null_map, size);
}
void vector_constant(const double* lhs, double rhs, double* result, uint8_t* null_map,
size_t size) {
vector_constant_impl(lhs, rhs, result, null_map, size);
}
void vector_constant(const float* lhs, float rhs, float* result, uint8_t* null_map, size_t size) {
vector_constant_impl(lhs, rhs, result, null_map, size);
}
void constant_vector(double lhs, const double* rhs, double* result, uint8_t* null_map,
size_t size) {
constant_vector_impl(lhs, rhs, result, null_map, size);
}
void constant_vector(float lhs, const float* rhs, float* result, uint8_t* null_map, size_t size) {
constant_vector_impl(lhs, rhs, result, null_map, size);
}
} // namespace doris::fmod_fast