Thorin 1.9.0
The Higher ORder INtermediate representation
Loading...
Searching...
No Matches
math.h
Go to the documentation of this file.
1#pragma once
2
3#include <thorin/axiom.h>
4#include <thorin/world.h>
5
7
9
10/// @name Mode
11///@{
12// clang-format off
13/// Allowed optimizations for a specific operation.
14enum class Mode : nat_t {
15 top = 0,
16 none = top, ///< Alias for Mode::none.
17 nnan = 1 << 0, ///< No NaNs.
18 ///< Allow optimizations to assume the arguments and result are not NaN.
19 ///< Such optimizations are required to retain defined behavior over NaNs, but the value of the result is undefined.
20 ninf = 1 << 1, ///< No Infs.
21 ///< Allow optimizations to assume the arguments and result are not +/-Inf.
22 ///< Such optimizations are required to retain defined behavior over +/-Inf, but the value of the result is undefined.
23 nsz = 1 << 2, ///< No Signed Zeros.
24 ///< Allow optimizations to treat the sign of a zero argument or result as insignificant.
25 arcp = 1 << 3, ///< Allow Reciprocal.
26 ///< Allow optimizations to use the reciprocal of an argument rather than perform division.
27 contract = 1 << 4, ///< Allow floating-point contraction
28 ///< (e.g. fusing a multiply followed by an addition into a fused multiply-and-add).
29 afn = 1 << 5, ///< Approximate functions.
30 ///< Allow substitution of approximate calculations for functions (sin, log, sqrt, etc).
31 reassoc = 1 << 6, ///< Allow reassociation transformations for floating-point operations.
32 ///< This may dramatically change results in floating point.
33 finite = nnan | ninf, ///< Mode::nnan `|` Mode::ninf.
34 unsafe = nsz | arcp | reassoc, ///< Mode::nsz `|` Mode::arcp `|` Mode::reassoc
35 fast = nnan | ninf | nsz
36 | arcp | contract | afn
37 | reassoc, ///< All flags.
38 bot = fast, ///< Alias for Mode::fast.
39};
40// clang-format on
41
43
44/// Give Mode as thorin::plug::math::Mode, thorin::nat_t or Ref.
45using VMode = std::variant<Mode, nat_t, Ref>;
46
47/// thorin::plug::math::VMode -> Ref.
48inline Ref mode(World& w, VMode m) {
49 if (auto def = std::get_if<Ref>(&m)) return *def;
50 if (auto nat = std::get_if<nat_t>(&m)) return w.lit_nat(*nat);
51 return w.lit_nat((nat_t)std::get<Mode>(m));
52}
53///@}
54
55/// @name %%math.F
56///@{
57inline Ref type_f(Ref pe) {
58 World& w = pe->world();
59 return w.app(w.annex<F>(), pe);
60}
61inline Ref type_f(World& w, nat_t p, nat_t e) {
62 auto lp = w.lit_nat(p);
63 auto le = w.lit_nat(e);
64 return type_f(w.tuple({lp, le}));
65}
66template<nat_t P, nat_t E> inline auto match_f(Ref def) {
67 if (auto f_ty = match<F>(def)) {
68 auto [p, e] = f_ty->arg()->projs<2>([](auto op) { return Lit::isa(op); });
69 if (p && e && *p == P && *e == E) return f_ty;
70 }
71 return Match<F, App>();
72}
73
74inline auto match_f16(Ref def) { return match_f<10, 5>(def); }
75inline auto match_f32(Ref def) { return match_f<23, 8>(def); }
76inline auto match_f64(Ref def) { return match_f<52, 11>(def); }
77
78inline std::optional<nat_t> isa_f(Ref def) {
79 if (auto f_ty = match<F>(def)) {
80 if (auto [p, e] = f_ty->arg()->projs<2>([](auto op) { return Lit::isa(op); }); p && e) {
81 if (*p == 10 && e == 5) return 16;
82 if (*p == 23 && e == 8) return 32;
83 if (*p == 52 && e == 11) return 64;
84 }
85 }
86 return {};
87}
88
89// clang-format off
90template<class R>
91const Lit* lit_f(World& w, R val) {
92 static_assert(std::is_floating_point<R>() || std::is_same<R, thorin::f16>());
93 if constexpr (false) {}
94 else if constexpr (sizeof(R) == 2) return w.lit(w.annex<F16>(), thorin::bitcast<u16>(val));
95 else if constexpr (sizeof(R) == 4) return w.lit(w.annex<F32>(), thorin::bitcast<u32>(val));
96 else if constexpr (sizeof(R) == 8) return w.lit(w.annex<F64>(), thorin::bitcast<u64>(val));
97 else fe::unreachable();
98}
99
100inline const Lit* lit_f(World& w, nat_t width, thorin::f64 val) {
101 switch (width) {
102 case 16: assert(thorin::f64(thorin::f16(thorin::f32(val))) == val && "loosing precision"); return lit_f(w, thorin::f16(thorin::f32(val)));
103 case 32: assert(thorin::f64(thorin::f32( (val))) == val && "loosing precision"); return lit_f(w, thorin::f32( (val)));
104 case 64: assert(thorin::f64(thorin::f64( (val))) == val && "loosing precision"); return lit_f(w, thorin::f64( (val)));
105 default: fe::unreachable();
106 }
107}
108// clang-format on
109///@}
110
111/// @name %%math.arith
112///@{
113inline Ref op_rminus(VMode m, Ref a) {
114 World& w = a->world();
115 auto s = isa_f(a->type());
116 return w.call(arith::sub, mode(w, m), Defs{lit_f(w, *s, -0.0), a});
117}
118///@}
119
120} // namespace thorin::plug::math
121
122namespace thorin {
123
124/// @name is_commutative/is_associative
125///@{
126// clang-format off
127constexpr bool is_commutative(plug::math::extrema ) { return true; }
129constexpr bool is_commutative(plug::math::cmp id) { return id == plug::math::cmp ::e || id == plug::math::cmp ::ne ; }
130constexpr bool is_associative(plug::math::arith id) { return is_commutative(id); }
131// clang-format off
132///@}
133
134} // namespace thorin
static std::optional< T > isa(Ref def)
Definition def.h:726
Helper class to retrieve Infer::arg if present.
Definition def.h:87
This is a thin wrapper for std::span<T, N> with the following additional features:
Definition span.h:28
The World represents the whole program and manages creation of Thorin nodes (Defs).
Definition world.h:35
Definition span.h:103
The math Plugin
Definition math.h:8
std::optional< nat_t > isa_f(Ref def)
Definition math.h:78
const Lit * lit_f(World &w, R val)
Definition math.h:91
Mode
Allowed optimizations for a specific operation.
Definition math.h:14
@ arcp
Allow Reciprocal.
@ none
Alias for Mode::none.
@ afn
Approximate functions.
@ unsafe
Mode::nsz | Mode::arcp | Mode::reassoc.
@ reassoc
Allow reassociation transformations for floating-point operations.
@ contract
Allow floating-point contraction (e.g.
@ nsz
No Signed Zeros.
@ finite
Mode::nnan | Mode::ninf.
@ bot
Alias for Mode::fast.
auto match_f(Ref def)
Definition math.h:66
auto match_f64(Ref def)
Definition math.h:76
auto match_f32(Ref def)
Definition math.h:75
Ref op_rminus(VMode m, Ref a)
Definition math.h:113
Ref type_f(Ref pe)
Definition math.h:57
std::variant< Mode, nat_t, Ref > VMode
Give Mode as thorin::plug::math::Mode, thorin::nat_t or Ref.
Definition math.h:45
Ref mode(World &w, VMode m)
thorin::plug::math::VMode -> Ref.
Definition math.h:48
auto match_f16(Ref def)
Definition math.h:74
Definition cfg.h:11
constexpr bool is_associative(Id id)
Definition axiom.h:135
u64 nat_t
Definition types.h:44
half f16
Definition types.h:40
constexpr bool is_commutative(Id)
Definition axiom.h:132
float f32
Definition types.h:41
double f64
Definition types.h:42
#define THORIN_ENUM_OPERATORS(E)
Use this to declare all kind of bit and comparison operators for an enum E.
Definition util.h:189