Skip to content

Commit 8122fb6

Browse files
committed
Add cfg(no_128_bit) to core: removes most u128/i128 operations
1 parent cef633d commit 8122fb6

File tree

10 files changed

+97
-14
lines changed

10 files changed

+97
-14
lines changed

library/core/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ description = "The Rust Core Library"
77
autotests = false
88
autobenches = false
99
# If you update this, be sure to update it in a bunch of other places too!
10-
# As of 2022, it was the ci/pgo.sh script and the core-no-fp-fmt-parse test.
10+
# As of 2023, it was the ci/pgo.sh script and the core-no-fp-fmt-parse/core-no-128-bit tests.
1111
edition = "2021"
1212

1313
[lib]

library/core/src/fmt/num.rs

+35-3
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,12 @@ integer! { i8, u8 }
176176
integer! { i16, u16 }
177177
integer! { i32, u32 }
178178
integer! { i64, u64 }
179+
#[cfg(not(no_128_bit))]
179180
integer! { i128, u128 }
181+
180182
macro_rules! debug {
181-
($($T:ident)*) => {$(
183+
($($( #[$cfg:meta] )? $T:ident)*) => {$(
184+
$( #[$cfg] )?
182185
#[stable(feature = "rust1", since = "1.0.0")]
183186
impl fmt::Debug for $T {
184187
#[inline]
@@ -195,8 +198,30 @@ macro_rules! debug {
195198
)*};
196199
}
197200
debug! {
198-
i8 i16 i32 i64 i128 isize
199-
u8 u16 u32 u64 u128 usize
201+
i8 i16 i32 i64 #[cfg(not(no_128_bit))] i128 isize
202+
u8 u16 u32 u64 #[cfg(not(no_128_bit))] u128 usize
203+
}
204+
205+
macro_rules! fake_debug {
206+
($($( #[$cfg:meta] )? $T:ident)*) => {$(
207+
$( #[$cfg] )?
208+
#[stable(feature = "rust1", since = "1.0.0")]
209+
impl fmt::Debug for $T {
210+
#[inline]
211+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
212+
// Debug formats are not stable so this is a legitimate implementation.
213+
// It would be possible to hexdump the contents instead, or to
214+
// fail or panic, but for an application which has specifically
215+
// recompiled core to avoid i128, the former seems unnecessary
216+
// and the latter needlessly harmful.
217+
f.write_str(stringify!($T))
218+
}
219+
}
220+
)*}
221+
}
222+
fake_debug! {
223+
#[cfg(no_128_bit)] i128
224+
#[cfg(no_128_bit)] u128
200225
}
201226

202227
// 2 digit decimal look up table
@@ -476,9 +501,11 @@ mod imp {
476501
impl_Exp!(i8, u8, i16, u16, i32, u32, isize, usize as u32 via to_u32 named exp_u32);
477502
impl_Exp!(i64, u64 as u64 via to_u64 named exp_u64);
478503
}
504+
#[cfg(not(no_128_bit))]
479505
impl_Exp!(i128, u128 as u128 via to_u128 named exp_u128);
480506

481507
/// Helper function for writing a u64 into `buf` going from last to first, with `curr`.
508+
#[cfg(not(no_128_bit))] // unused for `no_128_bit`
482509
fn parse_u64_into<const N: usize>(mut n: u64, buf: &mut [MaybeUninit<u8>; N], curr: &mut usize) {
483510
let buf_ptr = MaybeUninit::slice_as_mut_ptr(buf);
484511
let lut_ptr = DEC_DIGITS_LUT.as_ptr();
@@ -566,13 +593,15 @@ fn parse_u64_into<const N: usize>(mut n: u64, buf: &mut [MaybeUninit<u8>; N], cu
566593
}
567594

568595
#[stable(feature = "rust1", since = "1.0.0")]
596+
#[cfg(not(no_128_bit))]
569597
impl fmt::Display for u128 {
570598
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
571599
fmt_u128(*self, true, f)
572600
}
573601
}
574602

575603
#[stable(feature = "rust1", since = "1.0.0")]
604+
#[cfg(not(no_128_bit))]
576605
impl fmt::Display for i128 {
577606
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
578607
let is_nonnegative = *self >= 0;
@@ -590,6 +619,7 @@ impl fmt::Display for i128 {
590619
/// into at most 2 u64s, and then chunks by 10e16, 10e8, 10e4, 10e2, and then 10e1.
591620
/// It also has to handle 1 last item, as 10^40 > 2^128 > 10^39, whereas
592621
/// 10^20 > 2^64 > 10^19.
622+
#[cfg(not(no_128_bit))]
593623
fn fmt_u128(n: u128, is_nonnegative: bool, f: &mut fmt::Formatter<'_>) -> fmt::Result {
594624
// 2^128 is about 3*10^38, so 39 gives an extra byte of space
595625
let mut buf = [MaybeUninit::<u8>::uninit(); 39];
@@ -649,6 +679,7 @@ fn fmt_u128(n: u128, is_nonnegative: bool, f: &mut fmt::Formatter<'_>) -> fmt::R
649679
/// in Proc. of the SIGPLAN94 Conference on Programming Language Design and
650680
/// Implementation, 1994, pp. 61–72
651681
///
682+
#[cfg(not(no_128_bit))]
652683
fn udiv_1e19(n: u128) -> (u128, u64) {
653684
const DIV: u64 = 1e19 as u64;
654685
const FACTOR: u128 = 156927543384667019095894735580191660403;
@@ -665,6 +696,7 @@ fn udiv_1e19(n: u128) -> (u128, u64) {
665696

666697
/// Multiply unsigned 128 bit integers, return upper 128 bits of the result
667698
#[inline]
699+
#[cfg(not(no_128_bit))]
668700
fn u128_mulhi(x: u128, y: u128) -> u128 {
669701
let x_lo = x as u64;
670702
let x_hi = (x >> 64) as u64;

library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
not(test),
6565
any(not(feature = "miri-test-libstd"), test, doctest),
6666
no_fp_fmt_parse,
67+
no_128_bit,
6768
target_pointer_width = "16",
6869
target_pointer_width = "32",
6970
target_pointer_width = "64",

library/core/src/num/int_macros.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ macro_rules! int_impl {
33
$rot:expr, $rot_op:expr, $rot_result:expr, $swap_op:expr, $swapped:expr,
44
$reversed:expr, $le_bytes:expr, $be_bytes:expr,
55
$to_xe_bytes_doc:expr, $from_xe_bytes_doc:expr,
6-
$bound_condition:expr) => {
6+
$bound_condition:expr $(, #[$intrinsics_cfg:meta])?) => {
77
/// The smallest value that can be represented by this integer type
88
#[doc = concat!("(&minus;2<sup>", $BITS_MINUS_ONE, "</sup>", $bound_condition, ")")]
99
///
@@ -62,6 +62,7 @@ macro_rules! int_impl {
6262
#[doc = concat!("assert_eq!(", stringify!($SelfT), "::from_str_radix(\"A\", 16), Ok(10));")]
6363
/// ```
6464
#[stable(feature = "rust1", since = "1.0.0")]
65+
$( #[$intrinsics_cfg] )?
6566
pub fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError> {
6667
from_str_radix(src, radix)
6768
}

library/core/src/num/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ impl i128 {
268268
"[0x12, 0x90, 0x78, 0x56, 0x34, 0x12, 0x90, 0x78, \
269269
0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
270270
"[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, \
271-
0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12]", "", "", "" }
271+
0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12]", "", "", "", #[cfg(not(no_128_bit))] }
272272
}
273273

274274
#[cfg(target_pointer_width = "16")]
@@ -940,7 +940,7 @@ impl u128 {
940940
0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
941941
"[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, \
942942
0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12]",
943-
"", "", ""}
943+
"", "", "", #[cfg(not(no_128_bit))]}
944944
}
945945

946946
#[cfg(target_pointer_width = "16")]

library/core/src/num/nonzero.rs

+14-6
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ macro_rules! impl_nonzero_fmt {
2323
}
2424

2525
macro_rules! nonzero_integers {
26-
( $( #[$stability: meta] #[$const_new_unchecked_stability: meta] $Ty: ident($Int: ty); )+ ) => {
26+
( $( #[$stability: meta] #[$const_new_unchecked_stability: meta] $(#[$cfg: meta])? $Ty: ident($Int: ty); )+ ) => {
2727
$(
2828
/// An integer that is known not to equal zero.
2929
///
@@ -154,9 +154,12 @@ macro_rules! nonzero_integers {
154154
}
155155
}
156156

157+
$(#[$cfg])?
157158
impl_nonzero_fmt! {
158-
#[$stability] (Debug, Display, Binary, Octal, LowerHex, UpperHex) for $Ty
159+
#[$stability] (Display, Binary, Octal, LowerHex, UpperHex) for $Ty
159160
}
161+
162+
impl_nonzero_fmt!(#[$stability] (Debug) for $Ty);
160163
)+
161164
}
162165
}
@@ -166,13 +169,15 @@ nonzero_integers! {
166169
#[stable(feature = "nonzero", since = "1.28.0")] #[rustc_const_stable(feature = "nonzero", since = "1.28.0")] NonZeroU16(u16);
167170
#[stable(feature = "nonzero", since = "1.28.0")] #[rustc_const_stable(feature = "nonzero", since = "1.28.0")] NonZeroU32(u32);
168171
#[stable(feature = "nonzero", since = "1.28.0")] #[rustc_const_stable(feature = "nonzero", since = "1.28.0")] NonZeroU64(u64);
169-
#[stable(feature = "nonzero", since = "1.28.0")] #[rustc_const_stable(feature = "nonzero", since = "1.28.0")] NonZeroU128(u128);
172+
#[stable(feature = "nonzero", since = "1.28.0")] #[rustc_const_stable(feature = "nonzero", since = "1.28.0")]
173+
#[cfg(not(no_128_bit))] NonZeroU128(u128);
170174
#[stable(feature = "nonzero", since = "1.28.0")] #[rustc_const_stable(feature = "nonzero", since = "1.28.0")] NonZeroUsize(usize);
171175
#[stable(feature = "signed_nonzero", since = "1.34.0")] #[rustc_const_stable(feature = "signed_nonzero", since = "1.34.0")] NonZeroI8(i8);
172176
#[stable(feature = "signed_nonzero", since = "1.34.0")] #[rustc_const_stable(feature = "signed_nonzero", since = "1.34.0")] NonZeroI16(i16);
173177
#[stable(feature = "signed_nonzero", since = "1.34.0")] #[rustc_const_stable(feature = "signed_nonzero", since = "1.34.0")] NonZeroI32(i32);
174178
#[stable(feature = "signed_nonzero", since = "1.34.0")] #[rustc_const_stable(feature = "signed_nonzero", since = "1.34.0")] NonZeroI64(i64);
175-
#[stable(feature = "signed_nonzero", since = "1.34.0")] #[rustc_const_stable(feature = "signed_nonzero", since = "1.34.0")] NonZeroI128(i128);
179+
#[stable(feature = "signed_nonzero", since = "1.34.0")] #[rustc_const_stable(feature = "signed_nonzero", since = "1.34.0")]
180+
#[cfg(not(no_128_bit))] NonZeroI128(i128);
176181
#[stable(feature = "signed_nonzero", since = "1.34.0")] #[rustc_const_stable(feature = "signed_nonzero", since = "1.34.0")] NonZeroIsize(isize);
177182
}
178183

@@ -191,8 +196,11 @@ macro_rules! from_str_radix_nzint_impl {
191196
)*}
192197
}
193198

194-
from_str_radix_nzint_impl! { NonZeroU8 NonZeroU16 NonZeroU32 NonZeroU64 NonZeroU128 NonZeroUsize
195-
NonZeroI8 NonZeroI16 NonZeroI32 NonZeroI64 NonZeroI128 NonZeroIsize }
199+
from_str_radix_nzint_impl! { NonZeroU8 NonZeroU16 NonZeroU32 NonZeroU64 NonZeroUsize
200+
NonZeroI8 NonZeroI16 NonZeroI32 NonZeroI64 NonZeroIsize }
201+
202+
#[cfg(not(no_128_bit))]
203+
from_str_radix_nzint_impl! { NonZeroU128 NonZeroI128 }
196204

197205
macro_rules! nonzero_leading_trailing_zeros {
198206
( $( $Ty: ident($Uint: ty) , $LeadingTestExpr:expr ;)+ ) => {

library/core/src/num/uint_macros.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ macro_rules! uint_impl {
44
$rot:expr, $rot_op:expr, $rot_result:expr, $swap_op:expr, $swapped:expr,
55
$reversed:expr, $le_bytes:expr, $be_bytes:expr,
66
$to_xe_bytes_doc:expr, $from_xe_bytes_doc:expr,
7-
$bound_condition:expr) => {
7+
$bound_condition:expr $(, #[$intrinsics_cfg:meta])?) => {
88
/// The smallest value that can be represented by this integer type.
99
///
1010
/// # Examples
@@ -63,6 +63,7 @@ macro_rules! uint_impl {
6363
#[doc = concat!("assert_eq!(", stringify!($SelfT), "::from_str_radix(\"A\", 16), Ok(10));")]
6464
/// ```
6565
#[stable(feature = "rust1", since = "1.0.0")]
66+
$( #[$intrinsics_cfg] )?
6667
pub fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError> {
6768
from_str_radix(src, radix)
6869
}

src/bootstrap/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ const EXTRA_CHECK_CFGS: &[(Option<Mode>, &'static str, Option<&[&'static str]>)]
207207
(Some(Mode::Codegen), "parallel_compiler", None),
208208
(Some(Mode::Std), "stdarch_intel_sde", None),
209209
(Some(Mode::Std), "no_fp_fmt_parse", None),
210+
(Some(Mode::Std), "no_128_bit", None),
210211
(Some(Mode::Std), "no_global_oom_handling", None),
211212
(Some(Mode::Std), "no_rc", None),
212213
(Some(Mode::Std), "no_sync", None),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
include ../tools.mk
2+
3+
all:
4+
$(RUSTC) --edition=2021 --crate-type=rlib --crate-name=core ../../../library/core/src/lib.rs --cfg no_128_bit
5+
$(RUSTC) --edition=2021 --crate-type=staticlib --crate-name=demo --sysroot=. -C panic=abort lib.rs
6+
# Expect that objdump succeeds and grep fails. The grep pattern is for names like __multi3.
7+
# There is no pipefail on dash so echo a string that will fail the test if objump fails.
8+
(objdump -t $(TMPDIR)/libdemo.a || echo __objdumpfailedti) | (! grep -w '__[a-z]\+ti[0-9]\?')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#![feature(no_core)]
2+
3+
#![no_std]
4+
#![no_core] // supress compiler-builtins
5+
extern crate core;
6+
use core::prelude::rust_2021::*;
7+
use core::fmt::Write;
8+
9+
// An empty file might be sufficient here, but since formatting is one of the
10+
// features affected by no_128_bit it seems worth including some.
11+
12+
struct X(pub usize);
13+
impl core::fmt::Write for X {
14+
fn write_str(&mut self, s: &str) -> core::fmt::Result {
15+
self.0 += s.len();
16+
Ok(())
17+
}
18+
}
19+
20+
#[no_mangle]
21+
extern "C" fn demo() -> usize {
22+
let mut x = X(0);
23+
// Writes "i128 u128 foo" due to the removal of u/i128 formatting.
24+
core::write!(x, "{:?} {:?} {}", i128::MAX, u128::MIN, "foo").unwrap();
25+
x.0
26+
}
27+
28+
#[panic_handler]
29+
fn panic(_: &core::panic::PanicInfo) -> ! {
30+
loop {}
31+
}

0 commit comments

Comments
 (0)