Skip to content

Commit 63cd4a3

Browse files
committed
Auto merge of #51171 - faern:const-bswap-ctpop-cttz-ctlz, r=oli-obk
Make some std::intrinsics `const fn`s Making some rustc intrinsics (`ctpop`, `cttz`, `ctlz` and `bswap`) `const fn`s. This is a pre-step to being able to make `swap_bytes`, `to_be` and `from_be` constant functions. That in itself could be ergonomic and useful. But even better is that it would allow `Ipv4Addr::new` etc becoming `const fn`s as well. Which might be really useful since I find it quite common to want to define them as constants. r? @oli-obk
2 parents b7a9d4e + 97a0d46 commit 63cd4a3

File tree

3 files changed

+75
-4
lines changed

3 files changed

+75
-4
lines changed

src/librustc_mir/interpret/const_eval.rs

+43-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc::middle::const_val::{ConstEvalErr, ErrKind};
33
use rustc::middle::const_val::ErrKind::{TypeckError, CheckMatchError};
44
use rustc::mir;
55
use rustc::ty::{self, TyCtxt, Ty, Instance};
6-
use rustc::ty::layout::{self, LayoutOf};
6+
use rustc::ty::layout::{self, LayoutOf, Primitive};
77
use rustc::ty::subst::Subst;
88

99
use syntax::ast::Mutability;
@@ -307,7 +307,7 @@ impl<'mir, 'tcx> super::Machine<'mir, 'tcx> for CompileTimeEvaluator {
307307
fn call_intrinsic<'a>(
308308
ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>,
309309
instance: ty::Instance<'tcx>,
310-
_args: &[ValTy<'tcx>],
310+
args: &[ValTy<'tcx>],
311311
dest: Place,
312312
dest_layout: layout::TyLayout<'tcx>,
313313
target: mir::BasicBlock,
@@ -345,8 +345,28 @@ impl<'mir, 'tcx> super::Machine<'mir, 'tcx> for CompileTimeEvaluator {
345345
};
346346
ecx.write_scalar(dest, id_val, dest_layout.ty)?;
347347
}
348+
"ctpop" | "cttz" | "cttz_nonzero" | "ctlz" | "ctlz_nonzero" | "bswap" => {
349+
let ty = substs.type_at(0);
350+
let layout_of = ecx.layout_of(ty)?;
351+
let bits = ecx.value_to_scalar(args[0])?.to_bits(layout_of.size)?;
352+
let kind = match layout_of.abi {
353+
ty::layout::Abi::Scalar(ref scalar) => scalar.value,
354+
_ => Err(::rustc::mir::interpret::EvalErrorKind::TypeNotPrimitive(ty))?,
355+
};
356+
let out_val = if intrinsic_name.ends_with("_nonzero") {
357+
if bits == 0 {
358+
return err!(Intrinsic(format!("{} called on 0", intrinsic_name)));
359+
}
360+
numeric_intrinsic(intrinsic_name.trim_right_matches("_nonzero"), bits, kind)?
361+
} else {
362+
numeric_intrinsic(intrinsic_name, bits, kind)?
363+
};
364+
ecx.write_scalar(dest, out_val, ty)?;
365+
}
348366

349-
name => return Err(ConstEvalError::NeedsRfc(format!("calling intrinsic `{}`", name)).into()),
367+
name => return Err(
368+
ConstEvalError::NeedsRfc(format!("calling intrinsic `{}`", name)).into()
369+
),
350370
}
351371

352372
ecx.goto_block(target);
@@ -570,3 +590,23 @@ pub fn const_eval_provider<'a, 'tcx>(
570590
}
571591
})
572592
}
593+
594+
fn numeric_intrinsic<'tcx>(
595+
name: &str,
596+
bits: u128,
597+
kind: Primitive,
598+
) -> EvalResult<'tcx, Scalar> {
599+
let defined = match kind {
600+
Primitive::Int(integer, _) => integer.size().bits() as u8,
601+
_ => bug!("invalid `{}` argument: {:?}", name, bits),
602+
};
603+
let extra = 128 - defined as u128;
604+
let bits_out = match name {
605+
"ctpop" => bits.count_ones() as u128,
606+
"ctlz" => bits.leading_zeros() as u128 - extra,
607+
"cttz" => (bits << extra).trailing_zeros() as u128 - extra,
608+
"bswap" => (bits << extra).swap_bytes(),
609+
_ => bug!("not a numeric intrinsic: {}", name),
610+
};
611+
Ok(Scalar::Bits { bits: bits_out, defined })
612+
}

src/librustc_mir/transform/qualify_consts.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,15 @@ This does not pose a problem by itself because they can't be accessed directly."
907907
Abi::PlatformIntrinsic => {
908908
assert!(!self.tcx.is_const_fn(def_id));
909909
match &self.tcx.item_name(def_id).as_str()[..] {
910-
"size_of" | "min_align_of" | "type_id" => is_const_fn = Some(def_id),
910+
| "size_of"
911+
| "min_align_of"
912+
| "type_id"
913+
| "bswap"
914+
| "ctpop"
915+
| "cttz"
916+
| "cttz_nonzero"
917+
| "ctlz"
918+
| "ctlz_nonzero" => is_const_fn = Some(def_id),
911919

912920
name if name.starts_with("simd_shuffle") => {
913921
is_shuffle = true;

src/test/run-pass/ctfe/bswap-const.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(core_intrinsics)]
12+
13+
use std::intrinsics;
14+
15+
const SWAPPED_U8: u8 = unsafe { intrinsics::bswap(0x12_u8) };
16+
const SWAPPED_U16: u16 = unsafe { intrinsics::bswap(0x12_34_u16) };
17+
const SWAPPED_I32: i32 = unsafe { intrinsics::bswap(0x12_34_56_78_i32) };
18+
19+
fn main() {
20+
assert_eq!(SWAPPED_U8, 0x12);
21+
assert_eq!(SWAPPED_U16, 0x34_12);
22+
assert_eq!(SWAPPED_I32, 0x78_56_34_12);
23+
}

0 commit comments

Comments
 (0)