naga/proc/overloads/
mathfunction.rs

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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//! Overload sets for [`ir::MathFunction`].

use crate::proc::overloads::any_overload_set::AnyOverloadSet;
use crate::proc::overloads::list::List;
use crate::proc::overloads::regular::regular;
use crate::proc::overloads::utils::{
    concrete_int_scalars, float_scalars, float_scalars_unimplemented_abstract, list, pairs, rule,
    scalar_or_vecn, triples, vector_sizes,
};
use crate::proc::overloads::OverloadSet;

use crate::ir;

impl ir::MathFunction {
    pub fn overloads(self) -> impl OverloadSet {
        use ir::MathFunction as Mf;

        let set: AnyOverloadSet = match self {
            // Component-wise unary numeric operations
            Mf::Abs | Mf::Sign => regular!(1, SCALAR|VECN of NUMERIC).into(),

            // Component-wise binary numeric operations
            Mf::Min | Mf::Max => regular!(2, SCALAR|VECN of NUMERIC).into(),

            // Component-wise ternary numeric operations
            Mf::Clamp => regular!(3, SCALAR|VECN of NUMERIC).into(),

            // Component-wise unary floating-point operations
            Mf::Sin
            | Mf::Cos
            | Mf::Tan
            | Mf::Asin
            | Mf::Acos
            | Mf::Atan
            | Mf::Sinh
            | Mf::Cosh
            | Mf::Tanh
            | Mf::Asinh
            | Mf::Acosh
            | Mf::Atanh
            | Mf::Saturate
            | Mf::Radians
            | Mf::Degrees
            | Mf::Ceil
            | Mf::Floor
            | Mf::Round
            | Mf::Fract
            | Mf::Trunc
            | Mf::Exp
            | Mf::Exp2
            | Mf::Log
            | Mf::Log2
            | Mf::Sqrt
            | Mf::InverseSqrt => regular!(1, SCALAR|VECN of FLOAT).into(),

            // Component-wise binary floating-point operations
            Mf::Atan2 | Mf::Pow | Mf::Step => regular!(2, SCALAR|VECN of FLOAT).into(),

            // Component-wise ternary floating-point operations
            Mf::Fma | Mf::SmoothStep => regular!(3, SCALAR|VECN of FLOAT).into(),

            // Component-wise unary concrete integer operations
            Mf::CountTrailingZeros
            | Mf::CountLeadingZeros
            | Mf::CountOneBits
            | Mf::ReverseBits
            | Mf::FirstTrailingBit
            | Mf::FirstLeadingBit => regular!(1, SCALAR|VECN of CONCRETE_INTEGER).into(),

            // Packing functions
            Mf::Pack4x8snorm | Mf::Pack4x8unorm => regular!(1, VEC4 of F32 -> U32).into(),
            Mf::Pack2x16snorm | Mf::Pack2x16unorm | Mf::Pack2x16float => {
                regular!(1, VEC2 of F32 -> U32).into()
            }
            Mf::Pack4xI8 => regular!(1, VEC4 of I32 -> U32).into(),
            Mf::Pack4xU8 => regular!(1, VEC4 of U32 -> U32).into(),

            // Unpacking functions
            Mf::Unpack4x8snorm | Mf::Unpack4x8unorm => regular!(1, SCALAR of U32 -> Vec4F).into(),
            Mf::Unpack2x16snorm | Mf::Unpack2x16unorm | Mf::Unpack2x16float => {
                regular!(1, SCALAR of U32 -> Vec2F).into()
            }
            Mf::Unpack4xI8 => regular!(1, SCALAR of U32 -> Vec4I).into(),
            Mf::Unpack4xU8 => regular!(1, SCALAR of U32 -> Vec4U).into(),

            // One-off operations
            Mf::Dot => regular!(2, VECN of NUMERIC -> Scalar).into(),
            Mf::Modf => regular!(1, SCALAR|VECN of FLOAT_ABSTRACT_UNIMPLEMENTED -> Modf).into(),
            Mf::Frexp => regular!(1, SCALAR|VECN of FLOAT_ABSTRACT_UNIMPLEMENTED -> Frexp).into(),
            Mf::Ldexp => ldexp().into(),
            Mf::Outer => outer().into(),
            Mf::Cross => regular!(2, VEC3 of FLOAT).into(),
            Mf::Distance => regular!(2, VECN of FLOAT_ABSTRACT_UNIMPLEMENTED -> Scalar).into(),
            Mf::Length => regular!(1, SCALAR|VECN of FLOAT_ABSTRACT_UNIMPLEMENTED -> Scalar).into(),
            Mf::Normalize => regular!(1, VECN of FLOAT_ABSTRACT_UNIMPLEMENTED).into(),
            Mf::FaceForward => regular!(3, VECN of FLOAT_ABSTRACT_UNIMPLEMENTED).into(),
            Mf::Reflect => regular!(2, VECN of FLOAT_ABSTRACT_UNIMPLEMENTED).into(),
            Mf::Refract => refract().into(),
            Mf::Mix => mix().into(),
            Mf::Inverse => regular!(1, MAT2X2|MAT3X3|MAT4X4 of FLOAT).into(),
            Mf::Transpose => transpose().into(),
            Mf::Determinant => regular!(1, MAT2X2|MAT3X3|MAT4X4 of FLOAT -> Scalar).into(),
            Mf::QuantizeToF16 => regular!(1, SCALAR|VECN of F32).into(),
            Mf::ExtractBits => extract_bits().into(),
            Mf::InsertBits => insert_bits().into(),
        };

        set
    }
}

fn ldexp() -> List {
    /// Construct the exponent scalar given the mantissa's inner.
    fn exponent_from_mantissa(mantissa: ir::Scalar) -> ir::Scalar {
        match mantissa.kind {
            ir::ScalarKind::AbstractFloat => ir::Scalar::ABSTRACT_INT,
            ir::ScalarKind::Float => ir::Scalar::I32,
            _ => unreachable!("not a float scalar"),
        }
    }

    list(
        // The ldexp mantissa argument can be any floating-point type.
        float_scalars_unimplemented_abstract().flat_map(|mantissa_scalar| {
            // The exponent type is the integer counterpart of the mantissa type.
            let exponent_scalar = exponent_from_mantissa(mantissa_scalar);
            // There are scalar and vector component-wise overloads.
            scalar_or_vecn(mantissa_scalar)
                .zip(scalar_or_vecn(exponent_scalar))
                .map(move |(mantissa, exponent)| {
                    let result = mantissa.clone();
                    rule([mantissa, exponent], result)
                })
        }),
    )
}

fn outer() -> List {
    list(
        triples(
            vector_sizes(),
            vector_sizes(),
            float_scalars_unimplemented_abstract(),
        )
        .map(|(cols, rows, scalar)| {
            let left = ir::TypeInner::Vector { size: cols, scalar };
            let right = ir::TypeInner::Vector { size: rows, scalar };
            let result = ir::TypeInner::Matrix {
                columns: cols,
                rows,
                scalar,
            };
            rule([left, right], result)
        }),
    )
}

fn refract() -> List {
    list(
        pairs(vector_sizes(), float_scalars_unimplemented_abstract()).map(|(size, scalar)| {
            let incident = ir::TypeInner::Vector { size, scalar };
            let normal = incident.clone();
            let ratio = ir::TypeInner::Scalar(scalar);
            let result = incident.clone();
            rule([incident, normal, ratio], result)
        }),
    )
}

fn transpose() -> List {
    list(
        triples(vector_sizes(), vector_sizes(), float_scalars()).map(|(a, b, scalar)| {
            let input = ir::TypeInner::Matrix {
                columns: a,
                rows: b,
                scalar,
            };
            let output = ir::TypeInner::Matrix {
                columns: b,
                rows: a,
                scalar,
            };
            rule([input], output)
        }),
    )
}

fn extract_bits() -> List {
    list(concrete_int_scalars().flat_map(|scalar| {
        scalar_or_vecn(scalar).map(|input| {
            let offset = ir::TypeInner::Scalar(ir::Scalar::U32);
            let count = ir::TypeInner::Scalar(ir::Scalar::U32);
            let output = input.clone();
            rule([input, offset, count], output)
        })
    }))
}

fn insert_bits() -> List {
    list(concrete_int_scalars().flat_map(|scalar| {
        scalar_or_vecn(scalar).map(|input| {
            let newbits = input.clone();
            let offset = ir::TypeInner::Scalar(ir::Scalar::U32);
            let count = ir::TypeInner::Scalar(ir::Scalar::U32);
            let output = input.clone();
            rule([input, newbits, offset, count], output)
        })
    }))
}

fn mix() -> List {
    list(float_scalars().flat_map(|scalar| {
        scalar_or_vecn(scalar).flat_map(move |input| {
            let scalar_ratio = ir::TypeInner::Scalar(scalar);
            [
                rule([input.clone(), input.clone(), input.clone()], input.clone()),
                rule([input.clone(), input.clone(), scalar_ratio], input),
            ]
        })
    }))
}