Skip to content

Commit f6f9233

Browse files
jimblandyteoxoy
authored andcommitted
[naga] Allow abstract scalars in modf and frexp results.
Allow `PredeclaredType::ModfResult` and `PredeclaredType::FrexpResult` to hold any sort of scalar, not just a floating-point scalar. This prepares Naga for implementing the `AbstractFloat` overloads for the `modf` and `frexp` builtin functions.
1 parent 6580528 commit f6f9233

File tree

7 files changed

+43
-60
lines changed

7 files changed

+43
-60
lines changed

naga/src/back/glsl/mod.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -747,14 +747,17 @@ impl<'a, W: Write> Writer<'a, W> {
747747
// Write functions to create special types.
748748
for (type_key, struct_ty) in self.module.special_types.predeclared_types.iter() {
749749
match type_key {
750-
&crate::PredeclaredType::ModfResult { size, width }
751-
| &crate::PredeclaredType::FrexpResult { size, width } => {
750+
&crate::PredeclaredType::ModfResult { size, scalar }
751+
| &crate::PredeclaredType::FrexpResult { size, scalar } => {
752752
let arg_type_name_owner;
753753
let arg_type_name = if let Some(size) = size {
754-
arg_type_name_owner =
755-
format!("{}vec{}", if width == 8 { "d" } else { "" }, size as u8);
754+
arg_type_name_owner = format!(
755+
"{}vec{}",
756+
if scalar.width == 8 { "d" } else { "" },
757+
size as u8
758+
);
756759
&arg_type_name_owner
757-
} else if width == 8 {
760+
} else if scalar.width == 8 {
758761
"double"
759762
} else {
760763
"float"

naga/src/back/hlsl/help.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -796,17 +796,17 @@ impl<W: Write> super::Writer<'_, W> {
796796
pub(super) fn write_special_functions(&mut self, module: &crate::Module) -> BackendResult {
797797
for (type_key, struct_ty) in module.special_types.predeclared_types.iter() {
798798
match type_key {
799-
&crate::PredeclaredType::ModfResult { size, width }
800-
| &crate::PredeclaredType::FrexpResult { size, width } => {
799+
&crate::PredeclaredType::ModfResult { size, scalar }
800+
| &crate::PredeclaredType::FrexpResult { size, scalar } => {
801801
let arg_type_name_owner;
802802
let arg_type_name = if let Some(size) = size {
803803
arg_type_name_owner = format!(
804804
"{}{}",
805-
if width == 8 { "double" } else { "float" },
805+
if scalar.width == 8 { "double" } else { "float" },
806806
size as u8
807807
);
808808
&arg_type_name_owner
809-
} else if width == 8 {
809+
} else if scalar.width == 8 {
810810
"double"
811811
} else {
812812
"float"

naga/src/back/msl/writer.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3830,17 +3830,17 @@ impl<W: Write> Writer<W> {
38303830
// Write functions to create special types.
38313831
for (type_key, struct_ty) in module.special_types.predeclared_types.iter() {
38323832
match type_key {
3833-
&crate::PredeclaredType::ModfResult { size, width }
3834-
| &crate::PredeclaredType::FrexpResult { size, width } => {
3833+
&crate::PredeclaredType::ModfResult { size, scalar }
3834+
| &crate::PredeclaredType::FrexpResult { size, scalar } => {
38353835
let arg_type_name_owner;
38363836
let arg_type_name = if let Some(size) = size {
38373837
arg_type_name_owner = format!(
38383838
"{NAMESPACE}::{}{}",
3839-
if width == 8 { "double" } else { "float" },
3839+
if scalar.width == 8 { "double" } else { "float" },
38403840
size as u8
38413841
);
38423842
&arg_type_name_owner
3843-
} else if width == 8 {
3843+
} else if scalar.width == 8 {
38443844
"double"
38453845
} else {
38463846
"float"

naga/src/front/type_gen.rs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -298,11 +298,11 @@ impl crate::Module {
298298
},
299299
}
300300
}
301-
crate::PredeclaredType::ModfResult { size, width } => {
301+
crate::PredeclaredType::ModfResult { size, scalar } => {
302302
let float_ty = self.types.insert(
303303
crate::Type {
304304
name: None,
305-
inner: crate::TypeInner::Scalar(crate::Scalar::float(width)),
305+
inner: crate::TypeInner::Scalar(scalar),
306306
},
307307
Span::UNDEFINED,
308308
);
@@ -311,23 +311,20 @@ impl crate::Module {
311311
let vec_ty = self.types.insert(
312312
crate::Type {
313313
name: None,
314-
inner: crate::TypeInner::Vector {
315-
size,
316-
scalar: crate::Scalar::float(width),
317-
},
314+
inner: crate::TypeInner::Vector { size, scalar },
318315
},
319316
Span::UNDEFINED,
320317
);
321-
(vec_ty, size as u32 * width as u32)
318+
(vec_ty, size as u32 * scalar.width as u32)
322319
} else {
323-
(float_ty, width as u32)
320+
(float_ty, scalar.width as u32)
324321
};
325322

326323
let mut type_name = "__modf_result_".to_string();
327324
if let Some(size) = size {
328325
let _ = write!(type_name, "vec{}_", size as u8);
329326
}
330-
let _ = write!(type_name, "f{}", width * 8);
327+
let _ = write!(type_name, "f{}", scalar.width * 8);
331328

332329
crate::Type {
333330
name: Some(type_name),
@@ -350,11 +347,11 @@ impl crate::Module {
350347
},
351348
}
352349
}
353-
crate::PredeclaredType::FrexpResult { size, width } => {
350+
crate::PredeclaredType::FrexpResult { size, scalar } => {
354351
let float_ty = self.types.insert(
355352
crate::Type {
356353
name: None,
357-
inner: crate::TypeInner::Scalar(crate::Scalar::float(width)),
354+
inner: crate::TypeInner::Scalar(scalar),
358355
},
359356
Span::UNDEFINED,
360357
);
@@ -364,7 +361,7 @@ impl crate::Module {
364361
name: None,
365362
inner: crate::TypeInner::Scalar(crate::Scalar {
366363
kind: crate::ScalarKind::Sint,
367-
width,
364+
width: scalar.width,
368365
}),
369366
},
370367
Span::UNDEFINED,
@@ -374,10 +371,7 @@ impl crate::Module {
374371
let vec_float_ty = self.types.insert(
375372
crate::Type {
376373
name: None,
377-
inner: crate::TypeInner::Vector {
378-
size,
379-
scalar: crate::Scalar::float(width),
380-
},
374+
inner: crate::TypeInner::Vector { size, scalar },
381375
},
382376
Span::UNDEFINED,
383377
);
@@ -388,22 +382,22 @@ impl crate::Module {
388382
size,
389383
scalar: crate::Scalar {
390384
kind: crate::ScalarKind::Sint,
391-
width,
385+
width: scalar.width,
392386
},
393387
},
394388
},
395389
Span::UNDEFINED,
396390
);
397-
(vec_float_ty, vec_int_ty, size as u32 * width as u32)
391+
(vec_float_ty, vec_int_ty, size as u32 * scalar.width as u32)
398392
} else {
399-
(float_ty, int_ty, width as u32)
393+
(float_ty, int_ty, scalar.width as u32)
400394
};
401395

402396
let mut type_name = "__frexp_result_".to_string();
403397
if let Some(size) = size {
404398
let _ = write!(type_name, "vec{}_", size as u8);
405399
}
406-
let _ = write!(type_name, "f{}", width * 8);
400+
let _ = write!(type_name, "f{}", scalar.width * 8);
407401

408402
crate::Type {
409403
name: Some(type_name),

naga/src/front/wgsl/lower/mod.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2304,22 +2304,18 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
23042304
args.finish()?;
23052305

23062306
if fun == crate::MathFunction::Modf || fun == crate::MathFunction::Frexp {
2307-
if let Some((size, width)) = match *resolve_inner!(ctx, arg) {
2308-
crate::TypeInner::Scalar(crate::Scalar { width, .. }) => {
2309-
Some((None, width))
2307+
if let Some((size, scalar)) = match *resolve_inner!(ctx, arg) {
2308+
crate::TypeInner::Scalar(scalar) => Some((None, scalar)),
2309+
crate::TypeInner::Vector { size, scalar, .. } => {
2310+
Some((Some(size), scalar))
23102311
}
2311-
crate::TypeInner::Vector {
2312-
size,
2313-
scalar: crate::Scalar { width, .. },
2314-
..
2315-
} => Some((Some(size), width)),
23162312
_ => None,
23172313
} {
23182314
ctx.module.generate_predeclared_type(
23192315
if fun == crate::MathFunction::Modf {
2320-
crate::PredeclaredType::ModfResult { size, width }
2316+
crate::PredeclaredType::ModfResult { size, scalar }
23212317
} else {
2322-
crate::PredeclaredType::FrexpResult { size, width }
2318+
crate::PredeclaredType::FrexpResult { size, scalar }
23232319
},
23242320
);
23252321
}

naga/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2217,11 +2217,11 @@ pub enum PredeclaredType {
22172217
AtomicCompareExchangeWeakResult(Scalar),
22182218
ModfResult {
22192219
size: Option<VectorSize>,
2220-
width: Bytes,
2220+
scalar: Scalar,
22212221
},
22222222
FrexpResult {
22232223
size: Option<VectorSize>,
2224-
width: Bytes,
2224+
scalar: Scalar,
22252225
},
22262226
}
22272227

naga/src/proc/typifier.rs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -668,19 +668,9 @@ impl<'a> ResolveContext<'a> {
668668
| Mf::Pow
669669
| Mf::QuantizeToF16 => res_arg.clone(),
670670
Mf::Modf | Mf::Frexp => {
671-
let (size, width) = match res_arg.inner_with(types) {
672-
&Ti::Scalar(crate::Scalar {
673-
kind: crate::ScalarKind::Float,
674-
width,
675-
}) => (None, width),
676-
&Ti::Vector {
677-
scalar:
678-
crate::Scalar {
679-
kind: crate::ScalarKind::Float,
680-
width,
681-
},
682-
size,
683-
} => (Some(size), width),
671+
let (size, scalar) = match res_arg.inner_with(types) {
672+
&Ti::Scalar(scalar) => (None, scalar),
673+
&Ti::Vector { scalar, size } => (Some(size), scalar),
684674
ref other => {
685675
return Err(ResolveError::IncompatibleOperands(format!(
686676
"{fun:?}({other:?}, _)"
@@ -691,9 +681,9 @@ impl<'a> ResolveContext<'a> {
691681
.special_types
692682
.predeclared_types
693683
.get(&if fun == Mf::Modf {
694-
crate::PredeclaredType::ModfResult { size, width }
684+
crate::PredeclaredType::ModfResult { size, scalar }
695685
} else {
696-
crate::PredeclaredType::FrexpResult { size, width }
686+
crate::PredeclaredType::FrexpResult { size, scalar }
697687
})
698688
.ok_or(ResolveError::MissingSpecialType)?;
699689
TypeResolution::Handle(*result)

0 commit comments

Comments
 (0)