Skip to content

Commit f465ee7

Browse files
committed
Changed to buildins for ceil and floor
1 parent 3f6c801 commit f465ee7

1 file changed

Lines changed: 14 additions & 48 deletions

File tree

klib/math.hpp

Lines changed: 14 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -218,33 +218,16 @@ namespace klib {
218218
typename = std::enable_if_t<std::is_floating_point_v<T>>
219219
>
220220
constexpr T ceil(T arg) {
221-
if (isinf(arg)) [[unlikely]] {
222-
return arg;
221+
// use the build in ceil functions
222+
if constexpr (sizeof(T) <= sizeof(float)) {
223+
return __builtin_ceilf(arg);
223224
}
224-
225-
if (arg == 0.0) [[unlikely]] {
226-
return arg;
227-
}
228-
229-
if (isnan(arg)) [[unlikely]] {
230-
return arg;
231-
}
232-
233-
if (static_cast<int64_t>(arg) == arg) {
234-
return arg;
225+
else if constexpr (sizeof(T) <= sizeof(double)) {
226+
return __builtin_ceil(arg);
235227
}
236-
237-
// Negative ceiling
238-
if (arg < 0.0) {
239-
return static_cast<T>(
240-
static_cast<int64_t>(arg)
241-
);
228+
else {
229+
return __builtin_ceill(arg);
242230
}
243-
244-
// Positive ceiling
245-
return static_cast<T>(
246-
static_cast<int64_t>(arg) + 1
247-
);
248231
}
249232

250233
/**
@@ -262,33 +245,16 @@ namespace klib {
262245
typename = std::enable_if_t<std::is_floating_point_v<T>>
263246
>
264247
constexpr T floor(const T arg) {
265-
if (isinf(arg)) [[unlikely]] {
266-
return arg;
248+
// use the build in floor functions
249+
if constexpr (sizeof(T) <= sizeof(float)) {
250+
return __builtin_floorf(arg);
267251
}
268-
269-
if (arg == 0.0) [[unlikely]] {
270-
return arg;
271-
}
272-
273-
if (isnan(arg)) [[unlikely]] {
274-
return arg;
275-
}
276-
277-
if (static_cast<int64_t>(arg) == arg) {
278-
return arg;
252+
else if constexpr (sizeof(T) <= sizeof(double)) {
253+
return __builtin_floor(arg);
279254
}
280-
281-
// Negative flooring
282-
if (arg < 0.0) {
283-
return static_cast<T>(
284-
static_cast<int64_t>(arg) - 1
285-
);
255+
else {
256+
return __builtin_floorl(arg);
286257
}
287-
288-
// Positive flooring
289-
return static_cast<T>(
290-
static_cast<int64_t>(arg)
291-
);
292258
}
293259

294260
/**

0 commit comments

Comments
 (0)