1 //! Codegen `extern "platform-intrinsic"` intrinsics.
3 use rustc_middle::ty::subst::SubstsRef;
4 use rustc_span::Symbol;
5 use rustc_target::abi::Endian;
10 fn report_simd_type_validation_error(
11 fx: &mut FunctionCx<'_, '_, '_>,
16 fx.tcx.sess.span_err(span, &format!("invalid monomorphization of `{}` intrinsic: expected SIMD input type, found non-SIMD `{}`", intrinsic, ty));
17 // Prevent verifier error
18 fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
21 pub(super) fn codegen_simd_intrinsic_call<'tcx>(
22 fx: &mut FunctionCx<'_, '_, 'tcx>,
24 _substs: SubstsRef<'tcx>,
25 args: &[mir::Operand<'tcx>],
30 sym::simd_as | sym::simd_cast => {
31 intrinsic_args!(fx, args => (a); intrinsic);
33 if !a.layout().ty.is_simd() {
34 report_simd_type_validation_error(fx, intrinsic, span, a.layout().ty);
38 simd_for_each_lane(fx, a, ret, &|fx, lane_ty, ret_lane_ty, lane| {
39 let ret_lane_clif_ty = fx.clif_type(ret_lane_ty).unwrap();
41 let from_signed = type_sign(lane_ty);
42 let to_signed = type_sign(ret_lane_ty);
44 clif_int_or_float_cast(fx, lane, from_signed, ret_lane_clif_ty, to_signed)
48 sym::simd_eq | sym::simd_ne | sym::simd_lt | sym::simd_le | sym::simd_gt | sym::simd_ge => {
49 intrinsic_args!(fx, args => (x, y); intrinsic);
51 if !x.layout().ty.is_simd() {
52 report_simd_type_validation_error(fx, intrinsic, span, x.layout().ty);
56 // FIXME use vector instructions when possible
57 simd_pair_for_each_lane(fx, x, y, ret, &|fx, lane_ty, res_lane_ty, x_lane, y_lane| {
58 let res_lane = match (lane_ty.kind(), intrinsic) {
59 (ty::Uint(_), sym::simd_eq) => fx.bcx.ins().icmp(IntCC::Equal, x_lane, y_lane),
60 (ty::Uint(_), sym::simd_ne) => {
61 fx.bcx.ins().icmp(IntCC::NotEqual, x_lane, y_lane)
63 (ty::Uint(_), sym::simd_lt) => {
64 fx.bcx.ins().icmp(IntCC::UnsignedLessThan, x_lane, y_lane)
66 (ty::Uint(_), sym::simd_le) => {
67 fx.bcx.ins().icmp(IntCC::UnsignedLessThanOrEqual, x_lane, y_lane)
69 (ty::Uint(_), sym::simd_gt) => {
70 fx.bcx.ins().icmp(IntCC::UnsignedGreaterThan, x_lane, y_lane)
72 (ty::Uint(_), sym::simd_ge) => {
73 fx.bcx.ins().icmp(IntCC::UnsignedGreaterThanOrEqual, x_lane, y_lane)
76 (ty::Int(_), sym::simd_eq) => fx.bcx.ins().icmp(IntCC::Equal, x_lane, y_lane),
77 (ty::Int(_), sym::simd_ne) => {
78 fx.bcx.ins().icmp(IntCC::NotEqual, x_lane, y_lane)
80 (ty::Int(_), sym::simd_lt) => {
81 fx.bcx.ins().icmp(IntCC::SignedLessThan, x_lane, y_lane)
83 (ty::Int(_), sym::simd_le) => {
84 fx.bcx.ins().icmp(IntCC::SignedLessThanOrEqual, x_lane, y_lane)
86 (ty::Int(_), sym::simd_gt) => {
87 fx.bcx.ins().icmp(IntCC::SignedGreaterThan, x_lane, y_lane)
89 (ty::Int(_), sym::simd_ge) => {
90 fx.bcx.ins().icmp(IntCC::SignedGreaterThanOrEqual, x_lane, y_lane)
93 (ty::Float(_), sym::simd_eq) => {
94 fx.bcx.ins().fcmp(FloatCC::Equal, x_lane, y_lane)
96 (ty::Float(_), sym::simd_ne) => {
97 fx.bcx.ins().fcmp(FloatCC::NotEqual, x_lane, y_lane)
99 (ty::Float(_), sym::simd_lt) => {
100 fx.bcx.ins().fcmp(FloatCC::LessThan, x_lane, y_lane)
102 (ty::Float(_), sym::simd_le) => {
103 fx.bcx.ins().fcmp(FloatCC::LessThanOrEqual, x_lane, y_lane)
105 (ty::Float(_), sym::simd_gt) => {
106 fx.bcx.ins().fcmp(FloatCC::GreaterThan, x_lane, y_lane)
108 (ty::Float(_), sym::simd_ge) => {
109 fx.bcx.ins().fcmp(FloatCC::GreaterThanOrEqual, x_lane, y_lane)
115 bool_to_zero_or_max_uint(fx, res_lane_ty, res_lane)
119 // simd_shuffle32<T, U>(x: T, y: T, idx: [u32; 32]) -> U
120 _ if intrinsic.as_str().starts_with("simd_shuffle") => {
121 let (x, y, idx) = match args {
122 [x, y, idx] => (x, y, idx),
124 bug!("wrong number of args for intrinsic {intrinsic}");
127 let x = codegen_operand(fx, x);
128 let y = codegen_operand(fx, y);
130 if !x.layout().ty.is_simd() {
131 report_simd_type_validation_error(fx, intrinsic, span, x.layout().ty);
135 // If this intrinsic is the older "simd_shuffleN" form, simply parse the integer.
136 // If there is no suffix, use the index array length.
137 let n: u16 = if intrinsic == sym::simd_shuffle {
138 // Make sure this is actually an array, since typeck only checks the length-suffixed
139 // version of this intrinsic.
140 let idx_ty = fx.monomorphize(idx.ty(fx.mir, fx.tcx));
141 match idx_ty.kind() {
142 ty::Array(ty, len) if matches!(ty.kind(), ty::Uint(ty::UintTy::U32)) => len
143 .try_eval_usize(fx.tcx, ty::ParamEnv::reveal_all())
145 span_bug!(span, "could not evaluate shuffle index array length")
150 fx.tcx.sess.span_err(
153 "simd_shuffle index must be an array of `u32`, got `{}`",
157 // Prevent verifier error
158 fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
163 // FIXME remove this case
164 intrinsic.as_str()["simd_shuffle".len()..].parse().unwrap()
167 assert_eq!(x.layout(), y.layout());
168 let layout = x.layout();
170 let (lane_count, lane_ty) = layout.ty.simd_size_and_type(fx.tcx);
171 let (ret_lane_count, ret_lane_ty) = ret.layout().ty.simd_size_and_type(fx.tcx);
173 assert_eq!(lane_ty, ret_lane_ty);
174 assert_eq!(u64::from(n), ret_lane_count);
176 let total_len = lane_count * 2;
179 use rustc_middle::mir::interpret::*;
180 let idx_const = crate::constant::mir_operand_get_const_val(fx, idx)
181 .expect("simd_shuffle* idx not const");
183 let idx_bytes = match idx_const {
184 ConstValue::ByRef { alloc, offset } => {
185 let size = Size::from_bytes(
186 4 * ret_lane_count, /* size_of([u32; ret_lane_count]) */
190 .get_bytes_strip_provenance(fx, alloc_range(offset, size))
193 _ => unreachable!("{:?}", idx_const),
198 let i = usize::try_from(i).unwrap();
199 let idx = rustc_middle::mir::interpret::read_target_uint(
200 fx.tcx.data_layout.endian,
201 &idx_bytes[4 * i..4 * i + 4],
203 .expect("read_target_uint");
204 u16::try_from(idx).expect("try_from u32")
206 .collect::<Vec<u16>>()
209 for &idx in &indexes {
210 assert!(u64::from(idx) < total_len, "idx {} out of range 0..{}", idx, total_len);
213 for (out_idx, in_idx) in indexes.into_iter().enumerate() {
214 let in_lane = if u64::from(in_idx) < lane_count {
215 x.value_lane(fx, in_idx.into())
217 y.value_lane(fx, u64::from(in_idx) - lane_count)
219 let out_lane = ret.place_lane(fx, u64::try_from(out_idx).unwrap());
220 out_lane.write_cvalue(fx, in_lane);
224 sym::simd_insert => {
225 let (base, idx, val) = match args {
226 [base, idx, val] => (base, idx, val),
228 bug!("wrong number of args for intrinsic {intrinsic}");
231 let base = codegen_operand(fx, base);
232 let val = codegen_operand(fx, val);
235 let idx_const = if let Some(idx_const) =
236 crate::constant::mir_operand_get_const_val(fx, idx)
240 fx.tcx.sess.span_fatal(span, "Index argument for `simd_insert` is not a constant");
244 .try_to_bits(Size::from_bytes(4 /* u32*/))
245 .unwrap_or_else(|| panic!("kind not scalar: {:?}", idx_const));
246 let (lane_count, _lane_ty) = base.layout().ty.simd_size_and_type(fx.tcx);
247 if idx >= lane_count.into() {
248 fx.tcx.sess.span_fatal(
250 &format!("[simd_insert] idx {} >= lane_count {}", idx, lane_count),
254 ret.write_cvalue(fx, base);
255 let ret_lane = ret.place_field(fx, mir::Field::new(idx.try_into().unwrap()));
256 ret_lane.write_cvalue(fx, val);
259 sym::simd_extract => {
260 let (v, idx) = match args {
261 [v, idx] => (v, idx),
263 bug!("wrong number of args for intrinsic {intrinsic}");
266 let v = codegen_operand(fx, v);
268 if !v.layout().ty.is_simd() {
269 report_simd_type_validation_error(fx, intrinsic, span, v.layout().ty);
273 let idx_const = if let Some(idx_const) =
274 crate::constant::mir_operand_get_const_val(fx, idx)
278 fx.tcx.sess.span_warn(span, "Index argument for `simd_extract` is not a constant");
279 let trap_block = fx.bcx.create_block();
280 let dummy_block = fx.bcx.create_block();
281 let true_ = fx.bcx.ins().iconst(types::I8, 1);
282 fx.bcx.ins().brnz(true_, trap_block, &[]);
283 fx.bcx.ins().jump(dummy_block, &[]);
284 fx.bcx.switch_to_block(trap_block);
285 crate::trap::trap_unimplemented(
287 "Index argument for `simd_extract` is not a constant",
289 fx.bcx.switch_to_block(dummy_block);
294 .try_to_bits(Size::from_bytes(4 /* u32*/))
295 .unwrap_or_else(|| panic!("kind not scalar: {:?}", idx_const));
296 let (lane_count, _lane_ty) = v.layout().ty.simd_size_and_type(fx.tcx);
297 if idx >= lane_count.into() {
298 fx.tcx.sess.span_fatal(
300 &format!("[simd_extract] idx {} >= lane_count {}", idx, lane_count),
304 let ret_lane = v.value_lane(fx, idx.try_into().unwrap());
305 ret.write_cvalue(fx, ret_lane);
309 intrinsic_args!(fx, args => (a); intrinsic);
311 if !a.layout().ty.is_simd() {
312 report_simd_type_validation_error(fx, intrinsic, span, a.layout().ty);
320 &|fx, lane_ty, _ret_lane_ty, lane| match lane_ty.kind() {
321 ty::Int(_) => fx.bcx.ins().ineg(lane),
322 ty::Float(_) => fx.bcx.ins().fneg(lane),
338 intrinsic_args!(fx, args => (x, y); intrinsic);
340 // FIXME use vector instructions when possible
341 simd_pair_for_each_lane(fx, x, y, ret, &|fx, lane_ty, _ret_lane_ty, x_lane, y_lane| {
342 match (lane_ty.kind(), intrinsic) {
343 (ty::Uint(_), sym::simd_add) => fx.bcx.ins().iadd(x_lane, y_lane),
344 (ty::Uint(_), sym::simd_sub) => fx.bcx.ins().isub(x_lane, y_lane),
345 (ty::Uint(_), sym::simd_mul) => fx.bcx.ins().imul(x_lane, y_lane),
346 (ty::Uint(_), sym::simd_div) => fx.bcx.ins().udiv(x_lane, y_lane),
347 (ty::Uint(_), sym::simd_rem) => fx.bcx.ins().urem(x_lane, y_lane),
349 (ty::Int(_), sym::simd_add) => fx.bcx.ins().iadd(x_lane, y_lane),
350 (ty::Int(_), sym::simd_sub) => fx.bcx.ins().isub(x_lane, y_lane),
351 (ty::Int(_), sym::simd_mul) => fx.bcx.ins().imul(x_lane, y_lane),
352 (ty::Int(_), sym::simd_div) => fx.bcx.ins().sdiv(x_lane, y_lane),
353 (ty::Int(_), sym::simd_rem) => fx.bcx.ins().srem(x_lane, y_lane),
355 (ty::Float(_), sym::simd_add) => fx.bcx.ins().fadd(x_lane, y_lane),
356 (ty::Float(_), sym::simd_sub) => fx.bcx.ins().fsub(x_lane, y_lane),
357 (ty::Float(_), sym::simd_mul) => fx.bcx.ins().fmul(x_lane, y_lane),
358 (ty::Float(_), sym::simd_div) => fx.bcx.ins().fdiv(x_lane, y_lane),
359 (ty::Float(FloatTy::F32), sym::simd_rem) => fx.lib_call(
361 vec![AbiParam::new(types::F32), AbiParam::new(types::F32)],
362 vec![AbiParam::new(types::F32)],
365 (ty::Float(FloatTy::F64), sym::simd_rem) => fx.lib_call(
367 vec![AbiParam::new(types::F64), AbiParam::new(types::F64)],
368 vec![AbiParam::new(types::F64)],
372 (ty::Uint(_), sym::simd_shl) => fx.bcx.ins().ishl(x_lane, y_lane),
373 (ty::Uint(_), sym::simd_shr) => fx.bcx.ins().ushr(x_lane, y_lane),
374 (ty::Uint(_), sym::simd_and) => fx.bcx.ins().band(x_lane, y_lane),
375 (ty::Uint(_), sym::simd_or) => fx.bcx.ins().bor(x_lane, y_lane),
376 (ty::Uint(_), sym::simd_xor) => fx.bcx.ins().bxor(x_lane, y_lane),
378 (ty::Int(_), sym::simd_shl) => fx.bcx.ins().ishl(x_lane, y_lane),
379 (ty::Int(_), sym::simd_shr) => fx.bcx.ins().sshr(x_lane, y_lane),
380 (ty::Int(_), sym::simd_and) => fx.bcx.ins().band(x_lane, y_lane),
381 (ty::Int(_), sym::simd_or) => fx.bcx.ins().bor(x_lane, y_lane),
382 (ty::Int(_), sym::simd_xor) => fx.bcx.ins().bxor(x_lane, y_lane),
390 intrinsic_args!(fx, args => (a, b, c); intrinsic);
392 if !a.layout().ty.is_simd() {
393 report_simd_type_validation_error(fx, intrinsic, span, a.layout().ty);
396 assert_eq!(a.layout(), b.layout());
397 assert_eq!(a.layout(), c.layout());
398 assert_eq!(a.layout(), ret.layout());
400 let layout = a.layout();
401 let (lane_count, lane_ty) = layout.ty.simd_size_and_type(fx.tcx);
402 let res_lane_layout = fx.layout_of(lane_ty);
404 for lane in 0..lane_count {
405 let a_lane = a.value_lane(fx, lane).load_scalar(fx);
406 let b_lane = b.value_lane(fx, lane).load_scalar(fx);
407 let c_lane = c.value_lane(fx, lane).load_scalar(fx);
409 let res_lane = fx.bcx.ins().fma(a_lane, b_lane, c_lane);
410 let res_lane = CValue::by_val(res_lane, res_lane_layout);
412 ret.place_lane(fx, lane).write_cvalue(fx, res_lane);
416 sym::simd_fmin | sym::simd_fmax => {
417 intrinsic_args!(fx, args => (x, y); intrinsic);
419 if !x.layout().ty.is_simd() {
420 report_simd_type_validation_error(fx, intrinsic, span, x.layout().ty);
424 // FIXME use vector instructions when possible
425 simd_pair_for_each_lane(fx, x, y, ret, &|fx, lane_ty, _ret_lane_ty, x_lane, y_lane| {
426 match lane_ty.kind() {
428 _ => unreachable!("{:?}", lane_ty),
431 sym::simd_fmin => crate::num::codegen_float_min(fx, x_lane, y_lane),
432 sym::simd_fmax => crate::num::codegen_float_max(fx, x_lane, y_lane),
439 intrinsic_args!(fx, args => (a); intrinsic);
441 if !a.layout().ty.is_simd() {
442 report_simd_type_validation_error(fx, intrinsic, span, a.layout().ty);
450 &|fx, lane_ty, _ret_lane_ty, lane| match lane_ty.kind() {
451 ty::Float(FloatTy::F32) => fx.lib_call(
453 vec![AbiParam::new(types::F32)],
454 vec![AbiParam::new(types::F32)],
457 ty::Float(FloatTy::F64) => fx.lib_call(
459 vec![AbiParam::new(types::F64)],
460 vec![AbiParam::new(types::F64)],
463 _ => unreachable!("{:?}", lane_ty),
468 sym::simd_fabs | sym::simd_fsqrt | sym::simd_ceil | sym::simd_floor | sym::simd_trunc => {
469 intrinsic_args!(fx, args => (a); intrinsic);
471 if !a.layout().ty.is_simd() {
472 report_simd_type_validation_error(fx, intrinsic, span, a.layout().ty);
476 simd_for_each_lane(fx, a, ret, &|fx, lane_ty, _ret_lane_ty, lane| {
477 match lane_ty.kind() {
479 _ => unreachable!("{:?}", lane_ty),
482 sym::simd_fabs => fx.bcx.ins().fabs(lane),
483 sym::simd_fsqrt => fx.bcx.ins().sqrt(lane),
484 sym::simd_ceil => fx.bcx.ins().ceil(lane),
485 sym::simd_floor => fx.bcx.ins().floor(lane),
486 sym::simd_trunc => fx.bcx.ins().trunc(lane),
492 sym::simd_reduce_add_ordered | sym::simd_reduce_add_unordered => {
493 intrinsic_args!(fx, args => (v, acc); intrinsic);
494 let acc = acc.load_scalar(fx);
496 // FIXME there must be no acc param for integer vectors
497 if !v.layout().ty.is_simd() {
498 report_simd_type_validation_error(fx, intrinsic, span, v.layout().ty);
502 simd_reduce(fx, v, Some(acc), ret, &|fx, lane_ty, a, b| {
503 if lane_ty.is_floating_point() {
504 fx.bcx.ins().fadd(a, b)
506 fx.bcx.ins().iadd(a, b)
511 sym::simd_reduce_mul_ordered | sym::simd_reduce_mul_unordered => {
512 intrinsic_args!(fx, args => (v, acc); intrinsic);
513 let acc = acc.load_scalar(fx);
515 // FIXME there must be no acc param for integer vectors
516 if !v.layout().ty.is_simd() {
517 report_simd_type_validation_error(fx, intrinsic, span, v.layout().ty);
521 simd_reduce(fx, v, Some(acc), ret, &|fx, lane_ty, a, b| {
522 if lane_ty.is_floating_point() {
523 fx.bcx.ins().fmul(a, b)
525 fx.bcx.ins().imul(a, b)
530 sym::simd_reduce_all => {
531 intrinsic_args!(fx, args => (v); intrinsic);
533 if !v.layout().ty.is_simd() {
534 report_simd_type_validation_error(fx, intrinsic, span, v.layout().ty);
538 simd_reduce_bool(fx, v, ret, &|fx, a, b| fx.bcx.ins().band(a, b));
541 sym::simd_reduce_any => {
542 intrinsic_args!(fx, args => (v); intrinsic);
544 if !v.layout().ty.is_simd() {
545 report_simd_type_validation_error(fx, intrinsic, span, v.layout().ty);
549 simd_reduce_bool(fx, v, ret, &|fx, a, b| fx.bcx.ins().bor(a, b));
552 sym::simd_reduce_and => {
553 intrinsic_args!(fx, args => (v); intrinsic);
555 if !v.layout().ty.is_simd() {
556 report_simd_type_validation_error(fx, intrinsic, span, v.layout().ty);
560 simd_reduce(fx, v, None, ret, &|fx, _ty, a, b| fx.bcx.ins().band(a, b));
563 sym::simd_reduce_or => {
564 intrinsic_args!(fx, args => (v); intrinsic);
566 if !v.layout().ty.is_simd() {
567 report_simd_type_validation_error(fx, intrinsic, span, v.layout().ty);
571 simd_reduce(fx, v, None, ret, &|fx, _ty, a, b| fx.bcx.ins().bor(a, b));
574 sym::simd_reduce_xor => {
575 intrinsic_args!(fx, args => (v); intrinsic);
577 if !v.layout().ty.is_simd() {
578 report_simd_type_validation_error(fx, intrinsic, span, v.layout().ty);
582 simd_reduce(fx, v, None, ret, &|fx, _ty, a, b| fx.bcx.ins().bxor(a, b));
585 sym::simd_reduce_min => {
586 intrinsic_args!(fx, args => (v); intrinsic);
588 if !v.layout().ty.is_simd() {
589 report_simd_type_validation_error(fx, intrinsic, span, v.layout().ty);
593 simd_reduce(fx, v, None, ret, &|fx, ty, a, b| {
594 let lt = match ty.kind() {
595 ty::Int(_) => fx.bcx.ins().icmp(IntCC::SignedLessThan, a, b),
596 ty::Uint(_) => fx.bcx.ins().icmp(IntCC::UnsignedLessThan, a, b),
597 ty::Float(_) => return crate::num::codegen_float_min(fx, a, b),
600 fx.bcx.ins().select(lt, a, b)
604 sym::simd_reduce_max => {
605 intrinsic_args!(fx, args => (v); intrinsic);
607 if !v.layout().ty.is_simd() {
608 report_simd_type_validation_error(fx, intrinsic, span, v.layout().ty);
612 simd_reduce(fx, v, None, ret, &|fx, ty, a, b| {
613 let gt = match ty.kind() {
614 ty::Int(_) => fx.bcx.ins().icmp(IntCC::SignedGreaterThan, a, b),
615 ty::Uint(_) => fx.bcx.ins().icmp(IntCC::UnsignedGreaterThan, a, b),
616 ty::Float(_) => return crate::num::codegen_float_max(fx, a, b),
619 fx.bcx.ins().select(gt, a, b)
623 sym::simd_select => {
624 intrinsic_args!(fx, args => (m, a, b); intrinsic);
626 if !m.layout().ty.is_simd() {
627 report_simd_type_validation_error(fx, intrinsic, span, m.layout().ty);
630 if !a.layout().ty.is_simd() {
631 report_simd_type_validation_error(fx, intrinsic, span, a.layout().ty);
634 assert_eq!(a.layout(), b.layout());
636 let (lane_count, lane_ty) = a.layout().ty.simd_size_and_type(fx.tcx);
637 let lane_layout = fx.layout_of(lane_ty);
639 for lane in 0..lane_count {
640 let m_lane = m.value_lane(fx, lane).load_scalar(fx);
641 let a_lane = a.value_lane(fx, lane).load_scalar(fx);
642 let b_lane = b.value_lane(fx, lane).load_scalar(fx);
644 let m_lane = fx.bcx.ins().icmp_imm(IntCC::Equal, m_lane, 0);
646 CValue::by_val(fx.bcx.ins().select(m_lane, b_lane, a_lane), lane_layout);
648 ret.place_lane(fx, lane).write_cvalue(fx, res_lane);
652 sym::simd_select_bitmask => {
653 intrinsic_args!(fx, args => (m, a, b); intrinsic);
655 if !a.layout().ty.is_simd() {
656 report_simd_type_validation_error(fx, intrinsic, span, a.layout().ty);
659 assert_eq!(a.layout(), b.layout());
661 let (lane_count, lane_ty) = a.layout().ty.simd_size_and_type(fx.tcx);
662 let lane_layout = fx.layout_of(lane_ty);
664 let m = m.load_scalar(fx);
666 for lane in 0..lane_count {
667 let m_lane = fx.bcx.ins().ushr_imm(m, u64::from(lane) as i64);
668 let m_lane = fx.bcx.ins().band_imm(m_lane, 1);
669 let a_lane = a.value_lane(fx, lane).load_scalar(fx);
670 let b_lane = b.value_lane(fx, lane).load_scalar(fx);
672 let m_lane = fx.bcx.ins().icmp_imm(IntCC::Equal, m_lane, 0);
674 CValue::by_val(fx.bcx.ins().select(m_lane, b_lane, a_lane), lane_layout);
676 ret.place_lane(fx, lane).write_cvalue(fx, res_lane);
680 sym::simd_bitmask => {
681 intrinsic_args!(fx, args => (a); intrinsic);
683 let (lane_count, lane_ty) = a.layout().ty.simd_size_and_type(fx.tcx);
684 let lane_clif_ty = fx.clif_type(lane_ty).unwrap();
686 // The `fn simd_bitmask(vector) -> unsigned integer` intrinsic takes a
687 // vector mask and returns the most significant bit (MSB) of each lane in the form
689 // * an unsigned integer
690 // * an array of `u8`
691 // If the vector has less than 8 lanes, a u8 is returned with zeroed trailing bits.
693 // The bit order of the result depends on the byte endianness, LSB-first for little
694 // endian and MSB-first for big endian.
695 let expected_int_bits = lane_count.max(8);
696 let expected_bytes = expected_int_bits / 8 + ((expected_int_bits % 8 > 0) as u64);
698 match lane_ty.kind() {
699 ty::Int(_) | ty::Uint(_) => {}
701 fx.tcx.sess.span_fatal(
704 "invalid monomorphization of `simd_bitmask` intrinsic: \
705 vector argument `{}`'s element type `{}`, expected integer element \
715 Type::int_with_byte_size(u16::try_from(expected_bytes).unwrap()).unwrap();
716 let mut res = type_zero_value(&mut fx.bcx, res_type);
718 let lanes = match fx.tcx.sess.target.endian {
719 Endian::Big => Box::new(0..lane_count) as Box<dyn Iterator<Item = u64>>,
720 Endian::Little => Box::new((0..lane_count).rev()) as Box<dyn Iterator<Item = u64>>,
723 let a_lane = a.value_lane(fx, lane).load_scalar(fx);
725 // extract sign bit of an int
726 let a_lane_sign = fx.bcx.ins().ushr_imm(a_lane, i64::from(lane_clif_ty.bits() - 1));
728 // shift sign bit into result
729 let a_lane_sign = clif_intcast(fx, a_lane_sign, res_type, false);
730 res = fx.bcx.ins().ishl_imm(res, 1);
731 res = fx.bcx.ins().bor(res, a_lane_sign);
734 match ret.layout().ty.kind() {
735 ty::Uint(i) if i.bit_width() == Some(expected_int_bits) => {}
737 if matches!(elem.kind(), ty::Uint(ty::UintTy::U8))
738 && len.try_eval_usize(fx.tcx, ty::ParamEnv::reveal_all())
739 == Some(expected_bytes) => {}
741 fx.tcx.sess.span_fatal(
744 "invalid monomorphization of `simd_bitmask` intrinsic: \
745 cannot return `{}`, expected `u{}` or `[u8; {}]`",
754 let res = CValue::by_val(res, ret.layout());
755 ret.write_cvalue(fx, res);
758 sym::simd_saturating_add | sym::simd_saturating_sub => {
759 intrinsic_args!(fx, args => (x, y); intrinsic);
761 let bin_op = match intrinsic {
762 sym::simd_saturating_add => BinOp::Add,
763 sym::simd_saturating_sub => BinOp::Sub,
767 // FIXME use vector instructions when possible
768 simd_pair_for_each_lane_typed(fx, x, y, ret, &|fx, x_lane, y_lane| {
769 crate::num::codegen_saturating_int_binop(fx, bin_op, x_lane, y_lane)
777 fx.tcx.sess.span_fatal(span, &format!("Unknown SIMD intrinsic {}", intrinsic));