Iros
 
Loading...
Searching...
No Matches
abs.h
Go to the documentation of this file.
1#pragma once
2
5#include "di/meta/core.h"
6#include "di/meta/language.h"
7#include "di/util/bit_cast.h"
9
10namespace di::math {
11namespace detail {
12 struct AbsFunction {
13 template<typename T>
16 constexpr auto operator()(T&& value) const {
18 return function::tag_invoke(*this, util::forward<T>(value));
20 return value < 0 ? -value : value;
21 } else {
22 auto as_bytes = di::bit_cast<di::Array<byte, sizeof(T)>>(value);
23 auto& msb = [&] -> byte& {
24 if constexpr (Endian::Little == Endian::Native) {
25 return as_bytes[sizeof(T) - 1];
26 } else {
27 return as_bytes[0];
28 }
29 }();
30
31 msb &= ~(byte(1) << 7);
32
33 return di::bit_cast<meta::RemoveCVRef<T>>(as_bytes);
34 }
35 }
36 };
37}
38
39constexpr inline auto abs = detail::AbsFunction {};
40}
41
42namespace di {
43using math::abs;
44}
Definition language.h:232
Definition language.h:247
Definition tag_invoke.h:33
constexpr tag_invoke_detail::TagInvokeFn tag_invoke
Definition tag_invoke.h:22
Definition abs.h:11
Definition abs.h:10
constexpr auto abs
Definition abs.h:39
std::byte byte
Definition byte.h:64
Definition zstring_parser.h:9
constexpr auto as_bytes
Definition as_bytes.h:16
Definition span_fixed_size.h:37