MimIR
0.4-dev
MimIR is my Intermediate Representation
Toggle main menu visibility
Loading...
Searching...
No Matches
math.h
Go to the documentation of this file.
1
#pragma once
2
3
#include <
mim/axm.h
>
4
#include <
mim/world.h
>
5
6
#include "
mim/plug/math/autogen.h
"
7
8
namespace
mim::plug::math
{
9
10
/// @name Mode
11
///@{
12
// clang-format off
13
/// Allowed optimizations for a specific operation.
14
enum 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
42
/// Give Mode as mim::plug::math::Mode, mim::nat_t or const Def*.
43
using
VMode
= std::variant<Mode, nat_t, const Def*>;
44
45
/// mim::plug::math::VMode -> const Def*.
46
inline
const
Def
*
mode
(
World
& w,
VMode
m) {
47
if
(
auto
def = std::get_if<const Def*>(&m))
return
*def;
48
if
(
auto
nat = std::get_if<nat_t>(&m))
return
w.lit_nat(*nat);
49
return
w.lit_nat(std::to_underlying(std::get<Mode>(m)));
50
}
51
///@}
52
53
/// @name math.F
54
///@{
55
inline
const
Def
*
type_f
(
const
Def
* pe) {
56
World
& w = pe->world();
57
return
w.app(w.annex<
F
>(), pe);
58
}
59
inline
const
Def
*
type_f
(
World
& w,
nat_t
p,
nat_t
e) {
60
auto
lp = w.lit_nat(p);
61
auto
le = w.lit_nat(e);
62
return
type_f
(w.tuple({lp, le}));
63
}
64
template
<nat_t P, nat_t E>
65
inline
auto
match_f
(
const
Def
* def) {
66
if
(
auto
f_ty =
Axm::isa<F>
(def)) {
67
auto
[p, e] = f_ty->arg()->projs<2>([](
auto
op) {
return
Lit::isa
(op); });
68
if
(p && e && *p == P && *e == E)
return
f_ty;
69
}
70
return
Axm::IsA<F, App>
();
71
}
72
73
inline
auto
match_f16
(
const
Def
* def) {
return
match_f<10, 5>
(def); }
74
inline
auto
match_f32
(
const
Def
* def) {
return
match_f<23, 8>
(def); }
75
inline
auto
match_f64
(
const
Def
* def) {
return
match_f<52, 11>
(def); }
76
77
inline
std::optional<nat_t>
isa_f
(
const
Def
* def) {
78
if
(
auto
f_ty =
Axm::isa<F>
(def)) {
79
if
(
auto
[p, e] = f_ty->arg()->projs<2>([](
auto
op) { return Lit::isa(op); }); p && e) {
80
if
(*p == 10 && e == 5)
return
16;
81
if
(*p == 23 && e == 8)
return
32;
82
if
(*p == 52 && e == 11)
return
64;
83
}
84
}
85
return
{};
86
}
87
88
// clang-format off
89
template
<
class
R>
90
const
Lit
*
lit_f
(
World
& w, R val) {
91
static_assert
(std::is_floating_point_v<R>);
92
if
constexpr
(
sizeof
(R) == 2)
return
w.lit(w.annex<
F16
>(), std::bit_cast<u16>(val));
93
else
if
constexpr
(
sizeof
(R) == 4)
return
w.lit(w.annex<
F32
>(), std::bit_cast<u32>(val));
94
else
if
constexpr
(
sizeof
(R) == 8)
return
w.lit(w.annex<
F64
>(), std::bit_cast<u64>(val));
95
else
fe::unreachable();
96
}
97
98
inline
const
Lit
*
lit_f
(
World
& w,
nat_t
width,
mim::f64
val) {
99
switch
(width) {
100
#if defined(__STDCPP_FLOAT16_T__)
101
case
16: assert(
mim::f64
(mim::f16(val)) == val &&
"loosing precision"
);
return
lit_f
(w, mim::f16(val));
102
#endif
103
case
32: assert(
mim::f64
(
mim::f32
(val)) == val &&
"loosing precision"
);
return
lit_f
(w,
mim::f32
(val));
104
case
64: assert(
mim::f64
(
mim::f64
(val)) == val &&
"loosing precision"
);
return
lit_f
(w,
mim::f64
(val));
105
default
:
return
nullptr
;
106
}
107
}
108
// clang-format on
109
///@}
110
111
/// @name math.arith
112
///@{
113
inline
const
Def
*
op_rminus
(
VMode
m,
const
Def
* a) {
114
World
& w = a->world();
115
auto
s =
isa_f
(a->type());
116
if
(!s)
return
nullptr
;
117
if
(
auto
zero =
lit_f
(w, *s, -0.0))
return
w.call(
arith::sub
,
mode
(w, m),
Defs
{zero, a});
118
return
nullptr
;
119
}
120
///@}
121
122
}
// namespace mim::plug::math
123
124
namespace
mim
{
125
126
/// @name is_commutative/is_associative
127
///@{
128
// clang-format off
129
constexpr
bool
is_commutative
(
plug::math::extrema
) {
return
true
; }
130
constexpr
bool
is_commutative
(
plug::math::arith
id
) {
return
id
==
plug::math::arith::add
||
id
==
plug::math::arith::mul
; }
131
constexpr
bool
is_commutative
(
plug::math::cmp
id
) {
return
id
== plug::math::cmp ::e ||
id
== plug::math::cmp ::ne ; }
132
constexpr
bool
is_associative
(
plug::math::arith
id
) {
return
is_commutative
(
id
); }
133
// clang-format off
134
///@}
135
136
}
// namespace mim
137
138
#ifndef DOXYGEN
139
template
<>
struct
fe::is_bit_enum<
mim
::plug::math::Mode> : std::true_type {};
140
#endif
axm.h
mim::Axm::IsA
Definition
axm.h:74
mim::Axm::isa
static auto isa(const Def *def)
Definition
axm.h:112
mim::Def
Base class for all Defs.
Definition
def.h:273
mim::Lit
Definition
def.h:913
mim::Lit::isa
static std::optional< T > isa(const Def *def)
Definition
def.h:937
mim::World
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition
world.h:40
autogen.h
mim::plug::math
The math Plugin
Definition
math.h:8
mim::plug::math::lit_f
const Lit * lit_f(World &w, R val)
Definition
math.h:90
mim::plug::math::F32
F32
Definition
autogen.h:77
mim::plug::math::F
F
Definition
autogen.h:14
mim::plug::math::F16
F16
Definition
autogen.h:70
mim::plug::math::type_f
const Def * type_f(const Def *pe)
Definition
math.h:55
mim::plug::math::arith
arith
Definition
autogen.h:137
mim::plug::math::arith::add
@ add
Definition
autogen.h:138
mim::plug::math::arith::mul
@ mul
Definition
autogen.h:140
mim::plug::math::arith::sub
@ sub
Definition
autogen.h:139
mim::plug::math::match_f
auto match_f(const Def *def)
Definition
math.h:65
mim::plug::math::Mode
Mode
Allowed optimizations for a specific operation.
Definition
math.h:14
mim::plug::math::Mode::arcp
@ arcp
Allow Reciprocal.
Definition
math.h:25
mim::plug::math::Mode::fast
@ fast
All flags.
Definition
math.h:35
mim::plug::math::Mode::afn
@ afn
Approximate functions.
Definition
math.h:29
mim::plug::math::Mode::ninf
@ ninf
No Infs.
Definition
math.h:20
mim::plug::math::Mode::unsafe
@ unsafe
Mode::nsz | Mode::arcp | Mode::reassoc.
Definition
math.h:34
mim::plug::math::Mode::contract
@ contract
Allow floating-point contraction (e.g.
Definition
math.h:27
mim::plug::math::Mode::nsz
@ nsz
No Signed Zeros.
Definition
math.h:23
mim::plug::math::Mode::nnan
@ nnan
No NaNs.
Definition
math.h:17
mim::plug::math::Mode::top
@ top
Definition
math.h:15
mim::plug::math::Mode::finite
@ finite
Mode::nnan | Mode::ninf.
Definition
math.h:33
mim::plug::math::Mode::bot
@ bot
Alias for Mode::fast.
Definition
math.h:38
mim::plug::math::VMode
std::variant< Mode, nat_t, const Def * > VMode
Give Mode as mim::plug::math::Mode, mim::nat_t or const Def*.
Definition
math.h:43
mim::plug::math::match_f16
auto match_f16(const Def *def)
Definition
math.h:73
mim::plug::math::isa_f
std::optional< nat_t > isa_f(const Def *def)
Definition
math.h:77
mim::plug::math::op_rminus
const Def * op_rminus(VMode m, const Def *a)
Definition
math.h:113
mim::plug::math::extrema
extrema
Definition
autogen.h:159
mim::plug::math::mode
mode
Definition
autogen.h:119
mim::plug::math::F64
F64
Definition
autogen.h:84
mim::plug::math::cmp
cmp
Definition
autogen.h:309
mim::plug::math::match_f64
auto match_f64(const Def *def)
Definition
math.h:75
mim::plug::math::match_f32
auto match_f32(const Def *def)
Definition
math.h:74
mim
Definition
ast.h:16
mim::nat_t
u64 nat_t
Definition
types.h:37
mim::Defs
fe::View< const Def * > Defs
Definition
def.h:91
mim::f64
double f64
Definition
types.h:35
mim::is_commutative
constexpr bool is_commutative(Id)
Definition
axm.h:164
mim::f32
float f32
Definition
types.h:34
mim::is_associative
constexpr bool is_associative(Id id)
Definition
axm.h:170
world.h
include
mim
plug
math
math.h
Generated by
1.18.0