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
//! Frozen binary-operation discriminants for primitive operation metadata.
// TAG RESERVATIONS: Add=0x01, Sub=0x02, Mul=0x03, Div=0x04, Mod=0x05,
// BitAnd=0x06, BitOr=0x07, BitXor=0x08, Shl=0x09, Shr=0x0A, Eq=0x0B,
// Ne=0x0C, Lt=0x0D, Gt=0x0E, Le=0x10, Ge=0x11, And=0x12, Or=0x13,
// AbsDiff=0x13, Min=0x14, Max=0x15, SaturatingAdd=0x16,
// SaturatingSub=0x17, SaturatingMul=0x18, Shuffle=0x19, Ballot=0x1A,
// WaveReduce=0x1B, WaveBroadcast=0x1C, RotateLeft=0x1D, WrappingAdd=0x1F, WrappingSub=0x20,
// RotateRight=0x1E, MulHigh=0x21, 0x22..=0x7F reserved, Opaque=0x80.
use crate::extension::ExtensionBinOpId;
/// Computational intensity class for a binary operation.
#[derive(
Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Deserialize, serde::Serialize,
)]
pub enum OpIntensity {
/// Zero-cost (bitcasts, aliasing).
Free,
/// Single-cycle ALU (Add, Sub, Bitwise).
Light,
/// Multi-cycle ALU (Mul, Div, Mod).
Medium,
/// High latency / Register heavy (transcendentals, subgroup ops).
Heavy,
}
/// Binary operation kind in the frozen data contract.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Deserialize, serde::Serialize)]
#[non_exhaustive]
pub enum BinOp {
/// Addition.
Add,
/// Subtraction.
Sub,
/// Multiplication.
Mul,
/// Division.
Div,
/// Remainder.
Mod,
/// Wrapping addition.
WrappingAdd,
/// Wrapping subtraction.
WrappingSub,
/// Bitwise AND.
BitAnd,
/// Bitwise OR.
BitOr,
/// Bitwise XOR.
BitXor,
/// Shift left.
Shl,
/// Shift right.
Shr,
/// Equality.
Eq,
/// Inequality.
Ne,
/// Less than.
Lt,
/// Greater than.
Gt,
/// Less than or equal.
Le,
/// Greater than or equal.
Ge,
/// Logical AND.
And,
/// Logical OR.
Or,
/// Unsigned absolute difference.
AbsDiff,
/// Minimum (f32).
Min,
/// Maximum (f32).
Max,
/// Saturating addition.
SaturatingAdd,
/// Saturating subtraction.
SaturatingSub,
/// Saturating multiplication.
SaturatingMul,
/// GPU subgroup shuffle.
Shuffle,
/// GPU subgroup ballot.
Ballot,
/// GPU subgroup reduction.
WaveReduce,
/// GPU subgroup broadcast.
WaveBroadcast,
/// Rotate-left.
RotateLeft,
/// Rotate-right.
RotateRight,
/// Unsigned multiply-high: upper 32 bits of `(left × right)` treated
/// as a 64-bit product. Enables Granlund-Montgomery strength reduction
/// of integer division by constant to 2 instructions.
MulHigh,
/// Extension-declared binary operator.
Opaque(ExtensionBinOpId),
}
impl BinOp {
/// Return the static computational intensity of this operation.
#[must_use]
pub fn intensity(&self) -> OpIntensity {
match self {
Self::Add
| Self::Sub
| Self::BitAnd
| Self::BitOr
| Self::BitXor
| Self::Shl
| Self::Shr
| Self::WrappingAdd
| Self::WrappingSub
| Self::RotateLeft
| Self::RotateRight
| Self::SaturatingAdd
| Self::SaturatingSub
| Self::SaturatingMul
| Self::AbsDiff => OpIntensity::Light,
Self::Ballot | Self::Shuffle | Self::WaveReduce | Self::WaveBroadcast => {
OpIntensity::Heavy
}
_ => OpIntensity::Medium,
}
}
}