Skip to content

Commit e3864db

Browse files
authored
Rollup merge of rust-lang#125172 - tgross35:f16-f128-as-casting, r=compiler-errors
Fix assertion when attempting to convert `f16` and `f128` with `as` These types are currently rejected for `as` casts by the compiler. Remove this incorrect check and add codegen tests for all conversions involving these types.
2 parents a8a3117 + 488ddd3 commit e3864db

File tree

3 files changed

+397
-4
lines changed

3 files changed

+397
-4
lines changed

compiler/rustc_codegen_ssa/src/traits/builder.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,10 @@ pub trait BuilderMethods<'a, 'tcx>:
247247
} else {
248248
(in_ty, dest_ty)
249249
};
250-
assert!(matches!(self.cx().type_kind(float_ty), TypeKind::Float | TypeKind::Double));
250+
assert!(matches!(
251+
self.cx().type_kind(float_ty),
252+
TypeKind::Half | TypeKind::Float | TypeKind::Double | TypeKind::FP128
253+
));
251254
assert_eq!(self.cx().type_kind(int_ty), TypeKind::Integer);
252255

253256
if let Some(false) = self.cx().sess().opts.unstable_opts.saturating_float_casts {

tests/codegen/float/f128.rs

+194
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#![crate_type = "lib"]
44
#![feature(f128)]
5+
#![feature(f16)]
56
#![feature(core_intrinsics)]
67

78
// CHECK-LABEL: i1 @f128_eq(
@@ -127,3 +128,196 @@ pub fn f128_rem_assign(a: &mut f128, b: f128) {
127128
// CHECK-NEXT: store fp128 %{{.+}}, ptr %{{.+}}
128129
*a %= b
129130
}
131+
132+
/* float to float conversions */
133+
134+
// CHECK-LABEL: half @f128_as_f16(
135+
#[no_mangle]
136+
pub fn f128_as_f16(a: f128) -> f16 {
137+
// CHECK: fptrunc fp128 %{{.+}} to half
138+
a as f16
139+
}
140+
141+
// CHECK-LABEL: float @f128_as_f32(
142+
#[no_mangle]
143+
pub fn f128_as_f32(a: f128) -> f32 {
144+
// CHECK: fptrunc fp128 %{{.+}} to float
145+
a as f32
146+
}
147+
148+
// CHECK-LABEL: double @f128_as_f64(
149+
#[no_mangle]
150+
pub fn f128_as_f64(a: f128) -> f64 {
151+
// CHECK: fptrunc fp128 %{{.+}} to double
152+
a as f64
153+
}
154+
155+
// CHECK-LABEL: fp128 @f128_as_self(
156+
#[no_mangle]
157+
pub fn f128_as_self(a: f128) -> f128 {
158+
// CHECK: ret fp128 %{{.+}}
159+
a as f128
160+
}
161+
162+
// CHECK-LABEL: fp128 @f16_as_f128(
163+
#[no_mangle]
164+
pub fn f16_as_f128(a: f16) -> f128 {
165+
// CHECK: fpext half %{{.+}} to fp128
166+
a as f128
167+
}
168+
169+
// CHECK-LABEL: fp128 @f32_as_f128(
170+
#[no_mangle]
171+
pub fn f32_as_f128(a: f32) -> f128 {
172+
// CHECK: fpext float %{{.+}} to fp128
173+
a as f128
174+
}
175+
176+
// CHECK-LABEL: fp128 @f64_as_f128(
177+
#[no_mangle]
178+
pub fn f64_as_f128(a: f64) -> f128 {
179+
// CHECK: fpext double %{{.+}} to fp128
180+
a as f128
181+
}
182+
183+
/* float to int conversions */
184+
185+
// CHECK-LABEL: i8 @f128_as_u8(
186+
#[no_mangle]
187+
pub fn f128_as_u8(a: f128) -> u8 {
188+
// CHECK: call i8 @llvm.fptoui.sat.i8.f128(fp128 %{{.+}})
189+
a as u8
190+
}
191+
192+
#[no_mangle]
193+
pub fn f128_as_u16(a: f128) -> u16 {
194+
// CHECK: call i16 @llvm.fptoui.sat.i16.f128(fp128 %{{.+}})
195+
a as u16
196+
}
197+
198+
// CHECK-LABEL: i32 @f128_as_u32(
199+
#[no_mangle]
200+
pub fn f128_as_u32(a: f128) -> u32 {
201+
// CHECK: call i32 @llvm.fptoui.sat.i32.f128(fp128 %{{.+}})
202+
a as u32
203+
}
204+
205+
// CHECK-LABEL: i64 @f128_as_u64(
206+
#[no_mangle]
207+
pub fn f128_as_u64(a: f128) -> u64 {
208+
// CHECK: call i64 @llvm.fptoui.sat.i64.f128(fp128 %{{.+}})
209+
a as u64
210+
}
211+
212+
// CHECK-LABEL: i128 @f128_as_u128(
213+
#[no_mangle]
214+
pub fn f128_as_u128(a: f128) -> u128 {
215+
// CHECK: call i128 @llvm.fptoui.sat.i128.f128(fp128 %{{.+}})
216+
a as u128
217+
}
218+
219+
// CHECK-LABEL: i8 @f128_as_i8(
220+
#[no_mangle]
221+
pub fn f128_as_i8(a: f128) -> i8 {
222+
// CHECK: call i8 @llvm.fptosi.sat.i8.f128(fp128 %{{.+}})
223+
a as i8
224+
}
225+
226+
// CHECK-LABEL: i16 @f128_as_i16(
227+
#[no_mangle]
228+
pub fn f128_as_i16(a: f128) -> i16 {
229+
// CHECK: call i16 @llvm.fptosi.sat.i16.f128(fp128 %{{.+}})
230+
a as i16
231+
}
232+
// CHECK-LABEL: i32 @f128_as_i32(
233+
#[no_mangle]
234+
pub fn f128_as_i32(a: f128) -> i32 {
235+
// CHECK: call i32 @llvm.fptosi.sat.i32.f128(fp128 %{{.+}})
236+
a as i32
237+
}
238+
239+
// CHECK-LABEL: i64 @f128_as_i64(
240+
#[no_mangle]
241+
pub fn f128_as_i64(a: f128) -> i64 {
242+
// CHECK: call i64 @llvm.fptosi.sat.i64.f128(fp128 %{{.+}})
243+
a as i64
244+
}
245+
246+
// CHECK-LABEL: i128 @f128_as_i128(
247+
#[no_mangle]
248+
pub fn f128_as_i128(a: f128) -> i128 {
249+
// CHECK: call i128 @llvm.fptosi.sat.i128.f128(fp128 %{{.+}})
250+
a as i128
251+
}
252+
253+
/* int to float conversions */
254+
255+
// CHECK-LABEL: fp128 @u8_as_f128(
256+
#[no_mangle]
257+
pub fn u8_as_f128(a: u8) -> f128 {
258+
// CHECK: uitofp i8 %{{.+}} to fp128
259+
a as f128
260+
}
261+
262+
// CHECK-LABEL: fp128 @u16_as_f128(
263+
#[no_mangle]
264+
pub fn u16_as_f128(a: u16) -> f128 {
265+
// CHECK: uitofp i16 %{{.+}} to fp128
266+
a as f128
267+
}
268+
269+
// CHECK-LABEL: fp128 @u32_as_f128(
270+
#[no_mangle]
271+
pub fn u32_as_f128(a: u32) -> f128 {
272+
// CHECK: uitofp i32 %{{.+}} to fp128
273+
a as f128
274+
}
275+
276+
// CHECK-LABEL: fp128 @u64_as_f128(
277+
#[no_mangle]
278+
pub fn u64_as_f128(a: u64) -> f128 {
279+
// CHECK: uitofp i64 %{{.+}} to fp128
280+
a as f128
281+
}
282+
283+
// CHECK-LABEL: fp128 @u128_as_f128(
284+
#[no_mangle]
285+
pub fn u128_as_f128(a: u128) -> f128 {
286+
// CHECK: uitofp i128 %{{.+}} to fp128
287+
a as f128
288+
}
289+
290+
// CHECK-LABEL: fp128 @i8_as_f128(
291+
#[no_mangle]
292+
pub fn i8_as_f128(a: i8) -> f128 {
293+
// CHECK: sitofp i8 %{{.+}} to fp128
294+
a as f128
295+
}
296+
297+
// CHECK-LABEL: fp128 @i16_as_f128(
298+
#[no_mangle]
299+
pub fn i16_as_f128(a: i16) -> f128 {
300+
// CHECK: sitofp i16 %{{.+}} to fp128
301+
a as f128
302+
}
303+
304+
// CHECK-LABEL: fp128 @i32_as_f128(
305+
#[no_mangle]
306+
pub fn i32_as_f128(a: i32) -> f128 {
307+
// CHECK: sitofp i32 %{{.+}} to fp128
308+
a as f128
309+
}
310+
311+
// CHECK-LABEL: fp128 @i64_as_f128(
312+
#[no_mangle]
313+
pub fn i64_as_f128(a: i64) -> f128 {
314+
// CHECK: sitofp i64 %{{.+}} to fp128
315+
a as f128
316+
}
317+
318+
// CHECK-LABEL: fp128 @i128_as_f128(
319+
#[no_mangle]
320+
pub fn i128_as_f128(a: i128) -> f128 {
321+
// CHECK: sitofp i128 %{{.+}} to fp128
322+
a as f128
323+
}

0 commit comments

Comments
 (0)