Skip to main content

naga/proc/
type_methods.rs

1//! Methods on or related to [`TypeInner`], [`Scalar`], [`ScalarKind`], and [`VectorSize`].
2//!
3//! [`TypeInner`]: crate::TypeInner
4//! [`Scalar`]: crate::Scalar
5//! [`ScalarKind`]: crate::ScalarKind
6//! [`VectorSize`]: crate::VectorSize
7
8use crate::{ir, valid::MAX_TYPE_SIZE};
9
10use super::TypeResolution;
11
12impl crate::ScalarKind {
13    pub const fn is_numeric(self) -> bool {
14        match self {
15            crate::ScalarKind::Sint
16            | crate::ScalarKind::Uint
17            | crate::ScalarKind::Float
18            | crate::ScalarKind::AbstractInt
19            | crate::ScalarKind::AbstractFloat => true,
20            crate::ScalarKind::Bool => false,
21        }
22    }
23}
24
25impl crate::Scalar {
26    pub const I16: Self = Self {
27        kind: crate::ScalarKind::Sint,
28        width: 2,
29    };
30    pub const U16: Self = Self {
31        kind: crate::ScalarKind::Uint,
32        width: 2,
33    };
34    pub const I32: Self = Self {
35        kind: crate::ScalarKind::Sint,
36        width: 4,
37    };
38    pub const U32: Self = Self {
39        kind: crate::ScalarKind::Uint,
40        width: 4,
41    };
42    pub const F16: Self = Self {
43        kind: crate::ScalarKind::Float,
44        width: 2,
45    };
46    pub const F32: Self = Self {
47        kind: crate::ScalarKind::Float,
48        width: 4,
49    };
50    pub const F64: Self = Self {
51        kind: crate::ScalarKind::Float,
52        width: 8,
53    };
54    pub const I64: Self = Self {
55        kind: crate::ScalarKind::Sint,
56        width: 8,
57    };
58    pub const U64: Self = Self {
59        kind: crate::ScalarKind::Uint,
60        width: 8,
61    };
62    pub const BOOL: Self = Self {
63        kind: crate::ScalarKind::Bool,
64        width: crate::BOOL_WIDTH,
65    };
66    pub const ABSTRACT_INT: Self = Self {
67        kind: crate::ScalarKind::AbstractInt,
68        width: crate::ABSTRACT_WIDTH,
69    };
70    pub const ABSTRACT_FLOAT: Self = Self {
71        kind: crate::ScalarKind::AbstractFloat,
72        width: crate::ABSTRACT_WIDTH,
73    };
74
75    pub const fn is_abstract(self) -> bool {
76        match self.kind {
77            crate::ScalarKind::AbstractInt | crate::ScalarKind::AbstractFloat => true,
78            crate::ScalarKind::Sint
79            | crate::ScalarKind::Uint
80            | crate::ScalarKind::Float
81            | crate::ScalarKind::Bool => false,
82        }
83    }
84
85    /// Construct a float `Scalar` with the given width.
86    ///
87    /// This is especially common when dealing with
88    /// `TypeInner::Matrix`, where the scalar kind is implicit.
89    pub const fn float(width: crate::Bytes) -> Self {
90        Self {
91            kind: crate::ScalarKind::Float,
92            width,
93        }
94    }
95
96    pub const fn to_inner_scalar(self) -> crate::TypeInner {
97        crate::TypeInner::Scalar(self)
98    }
99
100    pub const fn to_inner_vector(self, size: crate::VectorSize) -> crate::TypeInner {
101        crate::TypeInner::Vector { size, scalar: self }
102    }
103
104    pub const fn to_inner_atomic(self) -> crate::TypeInner {
105        crate::TypeInner::Atomic(self)
106    }
107}
108
109/// Produce all concrete integer [`ir::Scalar`]s.
110///
111/// Note that `I32` and `U32` must come first; this represents conversion rank
112/// in overload resolution.
113pub fn concrete_int_scalars() -> impl Iterator<Item = ir::Scalar> {
114    [
115        ir::Scalar::I32,
116        ir::Scalar::U32,
117        ir::Scalar::I16,
118        ir::Scalar::U16,
119        ir::Scalar::I64,
120        ir::Scalar::U64,
121    ]
122    .into_iter()
123}
124
125/// Produce all vector sizes.
126pub fn vector_sizes() -> impl Iterator<Item = ir::VectorSize> + Clone {
127    static SIZES: [ir::VectorSize; 3] = [
128        ir::VectorSize::Bi,
129        ir::VectorSize::Tri,
130        ir::VectorSize::Quad,
131    ];
132
133    SIZES.iter().cloned()
134}
135
136const POINTER_SPAN: u32 = 4;
137
138impl crate::TypeInner {
139    /// Return the scalar type of `self`.
140    ///
141    /// If `inner` is a scalar, vector, or matrix type, return
142    /// its scalar type. Otherwise, return `None`.
143    ///
144    /// Note that this doesn't inspect [`Array`] types, as required
145    /// for automatic conversions. For that, see [`scalar_for_conversions`].
146    ///
147    /// [`Array`]: crate::TypeInner::Array
148    /// [`scalar_for_conversions`]: crate::TypeInner::scalar_for_conversions
149    pub const fn scalar(&self) -> Option<crate::Scalar> {
150        use crate::TypeInner as Ti;
151        match *self {
152            Ti::Scalar(scalar) | Ti::Vector { scalar, .. } => Some(scalar),
153            Ti::Matrix { scalar, .. } => Some(scalar),
154            Ti::CooperativeMatrix { scalar, .. } => Some(scalar),
155            _ => None,
156        }
157    }
158
159    pub fn scalar_kind(&self) -> Option<crate::ScalarKind> {
160        self.scalar().map(|scalar| scalar.kind)
161    }
162
163    /// Returns the scalar width in bytes
164    pub fn scalar_width(&self) -> Option<u8> {
165        self.scalar().map(|scalar| scalar.width)
166    }
167
168    /// Return the leaf scalar type of `self`, as needed for automatic conversions.
169    ///
170    /// Unlike the [`scalar`] method, which only retrieves scalars for
171    /// [`Scalar`], [`Vector`], and [`Matrix`] this also looks into
172    /// [`Array`] types to find the leaf scalar.
173    ///
174    /// [`scalar`]: crate::TypeInner::scalar
175    /// [`Scalar`]: crate::TypeInner::Scalar
176    /// [`Vector`]: crate::TypeInner::Vector
177    /// [`Matrix`]: crate::TypeInner::Matrix
178    /// [`Array`]: crate::TypeInner::Array
179    pub fn scalar_for_conversions(
180        &self,
181        types: &crate::UniqueArena<crate::Type>,
182    ) -> Option<crate::Scalar> {
183        use crate::TypeInner as Ti;
184        match *self {
185            Ti::Scalar(scalar) | Ti::Vector { scalar, .. } | Ti::Matrix { scalar, .. } => {
186                Some(scalar)
187            }
188            Ti::Array { base, .. } => types[base].inner.scalar_for_conversions(types),
189            _ => None,
190        }
191    }
192
193    pub const fn pointer_space(&self) -> Option<crate::AddressSpace> {
194        match *self {
195            Self::Pointer { space, .. } => Some(space),
196            Self::ValuePointer { space, .. } => Some(space),
197            _ => None,
198        }
199    }
200
201    /// If `self` is a pointer type, return its base type.
202    pub const fn pointer_base_type(&self) -> Option<TypeResolution> {
203        match *self {
204            crate::TypeInner::Pointer { base, .. } => Some(TypeResolution::Handle(base)),
205            crate::TypeInner::ValuePointer {
206                size: None, scalar, ..
207            } => Some(TypeResolution::Value(crate::TypeInner::Scalar(scalar))),
208            crate::TypeInner::ValuePointer {
209                size: Some(size),
210                scalar,
211                ..
212            } => Some(TypeResolution::Value(crate::TypeInner::Vector {
213                size,
214                scalar,
215            })),
216            _ => None,
217        }
218    }
219
220    pub fn is_atomic_pointer(&self, types: &crate::UniqueArena<crate::Type>) -> bool {
221        match *self {
222            Self::Pointer { base, .. } => match types[base].inner {
223                Self::Atomic { .. } => true,
224                _ => false,
225            },
226            _ => false,
227        }
228    }
229
230    /// Attempt to calculate the size of this type. Returns `None` if the size
231    /// exceeds the limit of [`crate::valid::MAX_TYPE_SIZE`].
232    pub fn try_size(&self, gctx: super::GlobalCtx) -> Option<u32> {
233        match *self {
234            Self::Scalar(scalar) | Self::Atomic(scalar) => Some(scalar.width as u32),
235            Self::Vector { size, scalar } => Some(size as u32 * scalar.width as u32),
236            // matrices are treated as arrays of aligned columns
237            Self::Matrix {
238                columns,
239                rows,
240                scalar,
241            } => Some(super::Alignment::from(rows) * scalar.width as u32 * columns as u32),
242            Self::CooperativeMatrix {
243                columns,
244                rows,
245                scalar,
246                role: _,
247            } => Some(columns as u32 * rows as u32 * scalar.width as u32),
248            Self::Pointer { .. } | Self::ValuePointer { .. } => Some(POINTER_SPAN),
249            Self::Array {
250                base: _,
251                size,
252                stride,
253            } => {
254                let count = match size.resolve(gctx) {
255                    Ok(crate::proc::IndexableLength::Known(count)) => count,
256                    // any struct member or array element needing a size at pipeline-creation time
257                    // must have a creation-fixed footprint
258                    Err(_) => 0,
259                    // A dynamically-sized array has to have at least one element
260                    Ok(crate::proc::IndexableLength::Dynamic) => 1,
261                };
262                if count > MAX_TYPE_SIZE {
263                    // It shouldn't be possible to have an array of a zero-sized type, but
264                    // let's check just in case.
265                    None
266                } else {
267                    count
268                        .checked_mul(stride)
269                        .filter(|size| *size <= MAX_TYPE_SIZE)
270                }
271            }
272            Self::Struct { span, .. } => Some(span),
273            Self::Image { .. }
274            | Self::Sampler { .. }
275            | Self::AccelerationStructure { .. }
276            | Self::RayQuery { .. }
277            | Self::HitObject
278            | Self::BindingArray { .. } => Some(0),
279        }
280    }
281
282    /// Get the size of this type.
283    ///
284    /// Panics if the size exceeds the limit of [`crate::valid::MAX_TYPE_SIZE`].
285    /// Validated modules should not contain such types. Code working with
286    /// modules prior to validation should use [`Self::try_size`] and handle the
287    /// error appropriately.
288    pub fn size(&self, gctx: super::GlobalCtx) -> u32 {
289        self.try_size(gctx).expect("type is too large")
290    }
291
292    /// Return the canonical form of `self`, or `None` if it's already in
293    /// canonical form.
294    ///
295    /// Certain types have multiple representations in `TypeInner`. This
296    /// function converts all forms of equivalent types to a single
297    /// representative of their class, so that simply applying `Eq` to the
298    /// result indicates whether the types are equivalent, as far as Naga IR is
299    /// concerned.
300    pub fn canonical_form(
301        &self,
302        types: &crate::UniqueArena<crate::Type>,
303    ) -> Option<crate::TypeInner> {
304        use crate::TypeInner as Ti;
305        match *self {
306            Ti::Pointer { base, space } => match types[base].inner {
307                Ti::Scalar(scalar) => Some(Ti::ValuePointer {
308                    size: None,
309                    scalar,
310                    space,
311                }),
312                Ti::Vector { size, scalar } => Some(Ti::ValuePointer {
313                    size: Some(size),
314                    scalar,
315                    space,
316                }),
317                _ => None,
318            },
319            _ => None,
320        }
321    }
322
323    /// Compare value type `self` and `rhs` as types.
324    ///
325    /// This is mostly the same as `<TypeInner as Eq>::eq`, but it treats
326    /// [`ValuePointer`] and [`Pointer`] types as equivalent. This method
327    /// cannot be used for structs, because it cannot distinguish two
328    /// structs with different names but the same members. For structs,
329    /// use [`compare_types`].
330    ///
331    /// When you know that one side of the comparison is never a pointer or
332    /// struct, it's fine to not bother with canonicalization, and just
333    /// compare `TypeInner` values with `==`.
334    ///
335    /// # Panics
336    ///
337    /// If both `self` and `rhs` are structs.
338    ///
339    /// [`compare_types`]: crate::proc::compare_types
340    /// [`ValuePointer`]: ir::TypeInner::ValuePointer
341    /// [`Pointer`]: ir::TypeInner::Pointer
342    pub fn non_struct_equivalent(
343        &self,
344        rhs: &ir::TypeInner,
345        types: &crate::UniqueArena<crate::Type>,
346    ) -> bool {
347        let left = self.canonical_form(types);
348        let right = rhs.canonical_form(types);
349
350        let left_struct = matches!(*self, ir::TypeInner::Struct { .. });
351        let right_struct = matches!(*rhs, ir::TypeInner::Struct { .. });
352
353        assert!(!left_struct || !right_struct);
354
355        left.as_ref().unwrap_or(self) == right.as_ref().unwrap_or(rhs)
356    }
357
358    /// Returns true if `self` is runtime- or override-sized.
359    pub fn is_dynamically_sized(&self, types: &crate::UniqueArena<crate::Type>) -> bool {
360        use crate::TypeInner as Ti;
361        match *self {
362            Ti::Array {
363                size: crate::ArraySize::Constant(_),
364                ..
365            } => false,
366            Ti::Array {
367                size: crate::ArraySize::Pending(_) | crate::ArraySize::Dynamic,
368                ..
369            } => true,
370            Ti::Struct { ref members, .. } => members
371                .last()
372                .map(|last| types[last.ty].inner.is_dynamically_sized(types))
373                .unwrap_or(false),
374            _ => false,
375        }
376    }
377
378    /// True when host code may need a storage buffer byte size for this type, for bounds checks
379    /// or WGSL `arrayLength`. Includes runtime-sized arrays and `binding_array` of element types
380    /// that qualify.
381    ///
382    /// The Metal backend treats this like “needs array length metadata”: it decides which globals
383    /// get entries in the synthesized `_mslBufferSizes` struct that those entry points take.
384    ///
385    /// A struct matches only if its last field is a runtime-sized array. This method does not open
386    /// a trailing struct to look at its members. [`Self::is_dynamically_sized`] does.
387    pub fn needs_host_buffer_byte_size(&self, types: &crate::UniqueArena<crate::Type>) -> bool {
388        use crate::TypeInner as Ti;
389        match *self {
390            Ti::Struct { ref members, .. } => members.last().is_some_and(|m| {
391                matches!(
392                    types[m.ty].inner,
393                    Ti::Array {
394                        size: crate::ArraySize::Dynamic,
395                        ..
396                    }
397                )
398            }),
399            Ti::Array {
400                size: crate::ArraySize::Dynamic,
401                ..
402            } => true,
403            Ti::BindingArray { base, .. } => types[base].inner.needs_host_buffer_byte_size(types),
404            _ => false,
405        }
406    }
407
408    /// Returns true if `self` is a constructible type.
409    pub fn is_constructible(&self, types: &crate::UniqueArena<crate::Type>) -> bool {
410        use crate::TypeInner as Ti;
411        match *self {
412            Ti::Array { base, size, .. } => {
413                let fixed_size = match size {
414                    ir::ArraySize::Constant(_) => true,
415                    ir::ArraySize::Pending(_) | ir::ArraySize::Dynamic => false,
416                };
417                fixed_size && types[base].inner.is_constructible(types)
418            }
419            Ti::Struct { ref members, .. } => members
420                .iter()
421                .all(|member| types[member.ty].inner.is_constructible(types)),
422            Ti::Atomic(_)
423            | Ti::Pointer { .. }
424            | Ti::ValuePointer { .. }
425            | Ti::Image { .. }
426            | Ti::Sampler { .. }
427            | Ti::AccelerationStructure { .. }
428            | Ti::BindingArray { .. } => false,
429            Ti::Scalar(_)
430            | Ti::Vector { .. }
431            | Ti::Matrix { .. }
432            | Ti::RayQuery { .. }
433            | Ti::HitObject
434            | Ti::CooperativeMatrix { .. } => true,
435        }
436    }
437
438    pub const fn components(&self) -> Option<u32> {
439        Some(match *self {
440            Self::Vector { size, .. } => size as u32,
441            Self::Matrix { columns, .. } => columns as u32,
442            Self::Array {
443                size: crate::ArraySize::Constant(len),
444                ..
445            } => len.get(),
446            Self::Struct { ref members, .. } => members.len() as u32,
447            _ => return None,
448        })
449    }
450
451    pub fn component_type(&self, index: usize) -> Option<TypeResolution> {
452        Some(match *self {
453            Self::Vector { scalar, .. } => TypeResolution::Value(crate::TypeInner::Scalar(scalar)),
454            Self::Matrix { rows, scalar, .. } => {
455                TypeResolution::Value(crate::TypeInner::Vector { size: rows, scalar })
456            }
457            Self::Array {
458                base,
459                size: crate::ArraySize::Constant(_),
460                ..
461            } => TypeResolution::Handle(base),
462            Self::Struct { ref members, .. } => TypeResolution::Handle(members[index].ty),
463            _ => return None,
464        })
465    }
466
467    /// If the type is a scalar or vector (not a matrix), return a tuple of the vector
468    /// size (or `None` for scalars), and the scalar kind. Returns `None` for other types.
469    pub const fn vector_size_and_scalar(
470        &self,
471    ) -> Option<(Option<crate::VectorSize>, crate::Scalar)> {
472        match *self {
473            crate::TypeInner::Scalar(scalar) => Some((None, scalar)),
474            crate::TypeInner::Vector { size, scalar } => Some((Some(size), scalar)),
475            crate::TypeInner::Matrix { .. }
476            | crate::TypeInner::CooperativeMatrix { .. }
477            | crate::TypeInner::Atomic(_)
478            | crate::TypeInner::Pointer { .. }
479            | crate::TypeInner::ValuePointer { .. }
480            | crate::TypeInner::Array { .. }
481            | crate::TypeInner::Struct { .. }
482            | crate::TypeInner::Image { .. }
483            | crate::TypeInner::Sampler { .. }
484            | crate::TypeInner::AccelerationStructure { .. }
485            | crate::TypeInner::RayQuery { .. }
486            | crate::TypeInner::HitObject
487            | crate::TypeInner::BindingArray { .. } => None,
488        }
489    }
490
491    /// Return true if `self` is an abstract type.
492    ///
493    /// Use `types` to look up type handles. This is necessary to
494    /// recognize abstract arrays.
495    pub fn is_abstract(&self, types: &crate::UniqueArena<crate::Type>) -> bool {
496        match *self {
497            crate::TypeInner::Scalar(scalar)
498            | crate::TypeInner::Vector { scalar, .. }
499            | crate::TypeInner::Matrix { scalar, .. }
500            | crate::TypeInner::Atomic(scalar) => scalar.is_abstract(),
501            crate::TypeInner::Array { base, .. } => types[base].inner.is_abstract(types),
502            crate::TypeInner::CooperativeMatrix { .. }
503            | crate::TypeInner::ValuePointer { .. }
504            | crate::TypeInner::Pointer { .. }
505            | crate::TypeInner::Struct { .. }
506            | crate::TypeInner::Image { .. }
507            | crate::TypeInner::Sampler { .. }
508            | crate::TypeInner::AccelerationStructure { .. }
509            | crate::TypeInner::RayQuery { .. }
510            | crate::TypeInner::HitObject
511            | crate::TypeInner::BindingArray { .. } => false,
512        }
513    }
514
515    /// Return true if `self` is an external texture.
516    pub const fn is_external_image(&self) -> bool {
517        matches!(
518            self,
519            crate::TypeInner::Image {
520                class: crate::ImageClass::External,
521                ..
522            }
523        )
524    }
525
526    /// Determine whether `self` automatically converts to `goal`.
527    ///
528    /// If Naga IR's automatic conversions will convert `self` to
529    /// `goal`, then return a pair `(from, to)`, where `from` and `to`
530    /// are the scalar types of the leaf values of `self` and `goal`.
531    ///
532    /// If `self` and `goal` are the same type, this will simply return
533    /// a pair `(S, S)`.
534    ///
535    /// If the automatic conversions cannot convert `self` to `goal`,
536    /// return `None`.
537    ///
538    /// Naga IR's automatic conversions will convert:
539    ///
540    /// - [`AbstractInt`] scalars to [`AbstractFloat`] or any numeric scalar type
541    ///
542    /// - [`AbstractFloat`] scalars to any floating-point scalar type
543    ///
544    /// - A [`Vector`] `{ size, scalar: S }` to `{ size, scalar: T }`
545    ///   if they would convert `S` to `T`
546    ///
547    /// - An [`Array`] `{ base: S, size, stride }` to `{ base: T, size, stride }`
548    ///   if they would convert `S` to `T`
549    ///
550    /// [`AbstractInt`]: crate::ScalarKind::AbstractInt
551    /// [`AbstractFloat`]: crate::ScalarKind::AbstractFloat
552    /// [`Vector`]: crate::TypeInner::Vector
553    /// [`Array`]: crate::TypeInner::Array
554    pub fn automatically_converts_to(
555        &self,
556        goal: &Self,
557        types: &crate::UniqueArena<crate::Type>,
558    ) -> Option<(crate::Scalar, crate::Scalar)> {
559        use crate::ScalarKind as Sk;
560        use crate::TypeInner as Ti;
561
562        // Automatic conversions only change the scalar type of a value's leaves
563        // (e.g., `vec4<AbstractFloat>` to `vec4<f32>`), never the type
564        // constructors applied to those scalar types (e.g., never scalar to
565        // `vec4`, or `vec2` to `vec3`). So first we check that the type
566        // constructors match, extracting the leaf scalar types in the process.
567        let expr_scalar;
568        let goal_scalar;
569        match (self, goal) {
570            (&Ti::Scalar(expr), &Ti::Scalar(goal)) => {
571                expr_scalar = expr;
572                goal_scalar = goal;
573            }
574            (
575                &Ti::Vector {
576                    size: expr_size,
577                    scalar: expr,
578                },
579                &Ti::Vector {
580                    size: goal_size,
581                    scalar: goal,
582                },
583            ) if expr_size == goal_size => {
584                expr_scalar = expr;
585                goal_scalar = goal;
586            }
587            (
588                &Ti::Matrix {
589                    rows: expr_rows,
590                    columns: expr_columns,
591                    scalar: expr,
592                },
593                &Ti::Matrix {
594                    rows: goal_rows,
595                    columns: goal_columns,
596                    scalar: goal,
597                },
598            ) if expr_rows == goal_rows && expr_columns == goal_columns => {
599                expr_scalar = expr;
600                goal_scalar = goal;
601            }
602            (
603                &Ti::Array {
604                    base: expr_base,
605                    size: expr_size,
606                    stride: _,
607                },
608                &Ti::Array {
609                    base: goal_base,
610                    size: goal_size,
611                    stride: _,
612                },
613            ) if expr_size == goal_size => {
614                return types[expr_base]
615                    .inner
616                    .automatically_converts_to(&types[goal_base].inner, types);
617            }
618            _ => return None,
619        }
620
621        match (expr_scalar.kind, goal_scalar.kind) {
622            (Sk::AbstractFloat, Sk::Float) => {}
623            (Sk::AbstractInt, Sk::Sint | Sk::Uint | Sk::AbstractFloat | Sk::Float) => {}
624            _ => return None,
625        }
626
627        log::trace!("      okay: expr {expr_scalar:?}, goal {goal_scalar:?}");
628        Some((expr_scalar, goal_scalar))
629    }
630}
631
632/// Helper trait for providing the min and max values exactly representable by
633/// the integer type `Self` and floating point type `F`.
634pub trait IntFloatLimits<F>
635where
636    F: num_traits::Float,
637{
638    /// Returns the minimum value exactly representable by the integer type
639    /// `Self` and floating point type `F`.
640    fn min_float() -> F;
641    /// Returns the maximum value exactly representable by the integer type
642    /// `Self` and floating point type `F`.
643    fn max_float() -> F;
644}
645
646macro_rules! define_int_float_limits {
647    ($int:ty, $float:ty, $min:expr, $max:expr) => {
648        impl IntFloatLimits<$float> for $int {
649            fn min_float() -> $float {
650                $min
651            }
652            fn max_float() -> $float {
653                $max
654            }
655        }
656    };
657}
658
659// i16 range [-32768, 32767] fits exactly in f16 (max 65504), f32, and f64.
660// u16 range [0, 65535] fits exactly in f32 and f64. For f16, max exactly
661// representable is 65504 (f16::MAX).
662define_int_float_limits!(i16, half::f16, half::f16::MIN, half::f16::MAX);
663define_int_float_limits!(u16, half::f16, half::f16::ZERO, half::f16::MAX);
664define_int_float_limits!(i16, f32, -32768.0f32, 32767.0f32);
665define_int_float_limits!(u16, f32, 0.0f32, 65535.0f32);
666define_int_float_limits!(i16, f64, -32768.0f64, 32767.0f64);
667define_int_float_limits!(u16, f64, 0.0f64, 65535.0f64);
668define_int_float_limits!(i32, half::f16, half::f16::MIN, half::f16::MAX);
669define_int_float_limits!(u32, half::f16, half::f16::ZERO, half::f16::MAX);
670define_int_float_limits!(i64, half::f16, half::f16::MIN, half::f16::MAX);
671define_int_float_limits!(u64, half::f16, half::f16::ZERO, half::f16::MAX);
672define_int_float_limits!(i32, f32, -2147483648.0f32, 2147483520.0f32);
673define_int_float_limits!(u32, f32, 0.0f32, 4294967040.0f32);
674define_int_float_limits!(
675    i64,
676    f32,
677    -9223372036854775808.0f32,
678    9223371487098961920.0f32
679);
680define_int_float_limits!(u64, f32, 0.0f32, 18446742974197923840.0f32);
681define_int_float_limits!(i32, f64, -2147483648.0f64, 2147483647.0f64);
682define_int_float_limits!(u32, f64, 0.0f64, 4294967295.0f64);
683define_int_float_limits!(
684    i64,
685    f64,
686    -9223372036854775808.0f64,
687    9223372036854774784.0f64
688);
689define_int_float_limits!(u64, f64, 0.0f64, 18446744073709549568.0f64);
690
691/// Returns a tuple of [`crate::Literal`]s representing the minimum and maximum
692/// float values exactly representable by the provided float and integer types.
693/// Panics if `float` is not one of `F16`, `F32`, or `F64`, or `int` is
694/// not one of `I16`, `U16`, `I32`, `U32`, `I64`, or `U64`.
695pub fn min_max_float_representable_by(
696    float: crate::Scalar,
697    int: crate::Scalar,
698) -> (crate::Literal, crate::Literal) {
699    match (float, int) {
700        (crate::Scalar::F16, crate::Scalar::I16) => (
701            crate::Literal::F16(i16::min_float()),
702            crate::Literal::F16(i16::max_float()),
703        ),
704        (crate::Scalar::F16, crate::Scalar::U16) => (
705            crate::Literal::F16(u16::min_float()),
706            crate::Literal::F16(u16::max_float()),
707        ),
708        (crate::Scalar::F16, crate::Scalar::I32) => (
709            crate::Literal::F16(i32::min_float()),
710            crate::Literal::F16(i32::max_float()),
711        ),
712        (crate::Scalar::F16, crate::Scalar::U32) => (
713            crate::Literal::F16(u32::min_float()),
714            crate::Literal::F16(u32::max_float()),
715        ),
716        (crate::Scalar::F16, crate::Scalar::I64) => (
717            crate::Literal::F16(i64::min_float()),
718            crate::Literal::F16(i64::max_float()),
719        ),
720        (crate::Scalar::F16, crate::Scalar::U64) => (
721            crate::Literal::F16(u64::min_float()),
722            crate::Literal::F16(u64::max_float()),
723        ),
724        (crate::Scalar::F32, crate::Scalar::I16) => (
725            crate::Literal::F32(i16::min_float()),
726            crate::Literal::F32(i16::max_float()),
727        ),
728        (crate::Scalar::F32, crate::Scalar::U16) => (
729            crate::Literal::F32(u16::min_float()),
730            crate::Literal::F32(u16::max_float()),
731        ),
732        (crate::Scalar::F32, crate::Scalar::I32) => (
733            crate::Literal::F32(i32::min_float()),
734            crate::Literal::F32(i32::max_float()),
735        ),
736        (crate::Scalar::F32, crate::Scalar::U32) => (
737            crate::Literal::F32(u32::min_float()),
738            crate::Literal::F32(u32::max_float()),
739        ),
740        (crate::Scalar::F32, crate::Scalar::I64) => (
741            crate::Literal::F32(i64::min_float()),
742            crate::Literal::F32(i64::max_float()),
743        ),
744        (crate::Scalar::F32, crate::Scalar::U64) => (
745            crate::Literal::F32(u64::min_float()),
746            crate::Literal::F32(u64::max_float()),
747        ),
748        (crate::Scalar::F64, crate::Scalar::I16) => (
749            crate::Literal::F64(i16::min_float()),
750            crate::Literal::F64(i16::max_float()),
751        ),
752        (crate::Scalar::F64, crate::Scalar::U16) => (
753            crate::Literal::F64(u16::min_float()),
754            crate::Literal::F64(u16::max_float()),
755        ),
756        (crate::Scalar::F64, crate::Scalar::I32) => (
757            crate::Literal::F64(i32::min_float()),
758            crate::Literal::F64(i32::max_float()),
759        ),
760        (crate::Scalar::F64, crate::Scalar::U32) => (
761            crate::Literal::F64(u32::min_float()),
762            crate::Literal::F64(u32::max_float()),
763        ),
764        (crate::Scalar::F64, crate::Scalar::I64) => (
765            crate::Literal::F64(i64::min_float()),
766            crate::Literal::F64(i64::max_float()),
767        ),
768        (crate::Scalar::F64, crate::Scalar::U64) => (
769            crate::Literal::F64(u64::min_float()),
770            crate::Literal::F64(u64::max_float()),
771        ),
772        _ => unreachable!(),
773    }
774}
775
776/// Helper function that returns the string corresponding to the [`VectorSize`](crate::VectorSize)
777pub const fn vector_size_str(size: crate::VectorSize) -> &'static str {
778    match size {
779        crate::VectorSize::Bi => "2",
780        crate::VectorSize::Tri => "3",
781        crate::VectorSize::Quad => "4",
782    }
783}