Skip to content

Commit 836b137

Browse files
committed
[cfg_match] Library edition
1 parent 7d9fe91 commit 836b137

File tree

8 files changed

+247
-0
lines changed

8 files changed

+247
-0
lines changed

library/core/src/ffi/mod.rs

+117
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ pub type c_ptrdiff_t = isize;
9090
pub type c_ssize_t = isize;
9191

9292
mod c_char_definition {
93+
#[cfg(bootstrap)]
9394
cfg_if! {
9495
// These are the targets on which c_char is unsigned. Usually the
9596
// signedness is the same for all target_os values on a given architecture
@@ -178,9 +179,101 @@ mod c_char_definition {
178179
pub type c_char = i8;
179180
}
180181
}
182+
#[cfg(not(bootstrap))]
183+
crate::cfg_match! {
184+
// These are the targets on which c_char is unsigned. Usually the
185+
// signedness is the same for all target_os values on a given architecture
186+
// but there are some exceptions (see isSignedCharDefault() in clang).
187+
//
188+
// aarch64:
189+
// Section 10 "Arm C and C++ language mappings" in Procedure Call Standard for the Arm®
190+
// 64-bit Architecture (AArch64) says C/C++ char is unsigned byte.
191+
// https://github.com/ARM-software/abi-aa/blob/2024Q3/aapcs64/aapcs64.rst#arm-c-and-c-language-mappings
192+
// arm:
193+
// Section 8 "Arm C and C++ Language Mappings" in Procedure Call Standard for the Arm®
194+
// Architecture says C/C++ char is unsigned byte.
195+
// https://github.com/ARM-software/abi-aa/blob/2024Q3/aapcs32/aapcs32.rst#arm-c-and-c-language-mappings
196+
// csky:
197+
// Section 2.1.2 "Primary Data Type" in C-SKY V2 CPU Applications Binary Interface
198+
// Standards Manual says ANSI C char is unsigned byte.
199+
// https://github.com/c-sky/csky-doc/blob/9f7121f7d40970ba5cc0f15716da033db2bb9d07/C-SKY_V2_CPU_Applications_Binary_Interface_Standards_Manual.pdf
200+
// Note: this doesn't seem to match Clang's default (https://github.com/rust-lang/rust/issues/129945).
201+
// hexagon:
202+
// Section 3.1 "Basic data type" in Qualcomm Hexagon™ Application
203+
// Binary Interface User Guide says "By default, the `char` data type is unsigned."
204+
// https://docs.qualcomm.com/bundle/publicresource/80-N2040-23_REV_K_Qualcomm_Hexagon_Application_Binary_Interface_User_Guide.pdf
205+
// msp430:
206+
// Section 2.1 "Basic Types" in MSP430 Embedded Application Binary
207+
// Interface says "The char type is unsigned by default".
208+
// https://www.ti.com/lit/an/slaa534a/slaa534a.pdf
209+
// Note: this doesn't seem to match Clang's default (https://github.com/rust-lang/rust/issues/129945).
210+
// powerpc/powerpc64:
211+
// - PPC32 SysV: "Table 3-1 Scalar Types" in System V Application Binary Interface PowerPC
212+
// Processor Supplement says ANSI C char is unsigned byte
213+
// https://refspecs.linuxfoundation.org/elf/elfspec_ppc.pdf
214+
// - PPC64 ELFv1: Section 3.1.4 "Fundamental Types" in 64-bit PowerPC ELF Application
215+
// Binary Interface Supplement 1.9 says ANSI C is unsigned byte
216+
// https://refspecs.linuxfoundation.org/ELF/ppc64/PPC-elf64abi.html#FUND-TYPE
217+
// - PPC64 ELFv2: Section 2.1.2.2 "Fundamental Types" in 64-Bit ELF V2 ABI Specification
218+
// says char is unsigned byte
219+
// https://openpowerfoundation.org/specifications/64bitelfabi/
220+
// - AIX: XL C for AIX Language Reference says "By default, char behaves like an unsigned char."
221+
// https://www.ibm.com/docs/en/xl-c-aix/13.1.3?topic=specifiers-character-types
222+
// riscv32/riscv64:
223+
// C/C++ type representations section in RISC-V Calling Conventions
224+
// page in RISC-V ELF psABI Document says "char is unsigned."
225+
// https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/draft-20240829-13bfa9f54634cb60d86b9b333e109f077805b4b3/riscv-cc.adoc#cc-type-representations
226+
// s390x:
227+
// - ELF: "Table 1.1.: Scalar types" in ELF Application Binary Interface s390x Supplement
228+
// Version 1.6.1 categorize ISO C char in unsigned integer
229+
// https://github.com/IBM/s390x-abi/releases/tag/v1.6.1
230+
// - z/OS: XL C/C++ Language Reference says: "By default, char behaves like an unsigned char."
231+
// https://www.ibm.com/docs/en/zos/3.1.0?topic=specifiers-character-types
232+
// Xtensa:
233+
// - "The char type is unsigned by default for Xtensa processors."
234+
//
235+
// On the following operating systems, c_char is signed by default, regardless of architecture.
236+
// Darwin (macOS, iOS, etc.):
237+
// Apple targets' c_char is signed by default even on arm
238+
// https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Handle-data-types-and-data-alignment-properly
239+
// Windows:
240+
// Windows MSVC C++ Language Reference says "Microsoft-specific: Variables of type char
241+
// are promoted to int as if from type signed char by default, unless the /J compilation
242+
// option is used."
243+
// https://learn.microsoft.com/en-us/cpp/cpp/fundamental-types-cpp?view=msvc-170#character-types)
244+
// L4RE:
245+
// The kernel builds with -funsigned-char on all targets (but useserspace follows the
246+
// architecture defaults). As we only have a target for userspace apps so there are no
247+
// special cases for L4RE below.
248+
all(
249+
not(windows),
250+
not(target_vendor = "apple"),
251+
any(
252+
target_arch = "aarch64",
253+
target_arch = "arm",
254+
target_arch = "csky",
255+
target_arch = "hexagon",
256+
target_arch = "msp430",
257+
target_arch = "powerpc",
258+
target_arch = "powerpc64",
259+
target_arch = "riscv64",
260+
target_arch = "riscv32",
261+
target_arch = "s390x",
262+
target_arch = "xtensa",
263+
)
264+
) => {
265+
pub type c_char = u8;
266+
}
267+
_ => {
268+
// On every other target, c_char is signed.
269+
pub type c_char = i8;
270+
}
271+
}
181272
}
182273

274+
183275
mod c_int_definition {
276+
#[cfg(bootstrap)]
184277
cfg_if! {
185278
if #[cfg(any(target_arch = "avr", target_arch = "msp430"))] {
186279
pub type c_int = i16;
@@ -190,9 +283,21 @@ mod c_int_definition {
190283
pub type c_uint = u32;
191284
}
192285
}
286+
#[cfg(not(bootstrap))]
287+
crate::cfg_match! {
288+
any(target_arch = "avr", target_arch = "msp430") => {
289+
pub type c_int = i16;
290+
pub type c_uint = u16;
291+
}
292+
_ => {
293+
pub type c_int = i32;
294+
pub type c_uint = u32;
295+
}
296+
}
193297
}
194298

195299
mod c_long_definition {
300+
#[cfg(bootstrap)]
196301
cfg_if! {
197302
if #[cfg(all(target_pointer_width = "64", not(windows)))] {
198303
pub type c_long = i64;
@@ -203,6 +308,18 @@ mod c_long_definition {
203308
pub type c_ulong = u32;
204309
}
205310
}
311+
#[cfg(not(bootstrap))]
312+
crate::cfg_match! {
313+
all(target_pointer_width = "64", not(windows)) => {
314+
pub type c_long = i64;
315+
pub type c_ulong = u64;
316+
}
317+
_ => {
318+
// The minimal size of `long` in the C standard is 32 bits
319+
pub type c_long = i32;
320+
pub type c_ulong = u32;
321+
}
322+
}
206323
}
207324

208325
// N.B., for LLVM to recognize the void pointer type and by extension

library/core/src/internal_macros.rs

+1
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ macro_rules! impl_fn_for_zst {
146146
/// ```
147147
// This is a copy of `cfg_if!` from the `cfg_if` crate.
148148
// The recursive invocations should use $crate if this is ever exported.
149+
#[cfg(bootstrap)]
149150
macro_rules! cfg_if {
150151
// match if/else chains with a final `else`
151152
(

library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
#![feature(array_ptr_get)]
111111
#![feature(asm_experimental_arch)]
112112
#![feature(bigint_helper_methods)]
113+
#![feature(cfg_match)]
113114
#![feature(const_carrying_mul_add)]
114115
#![feature(const_eval_select)]
115116
#![feature(core_intrinsics)]

library/core/src/num/f32.rs

+40
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,7 @@ impl f32 {
991991
#[stable(feature = "num_midpoint", since = "1.85.0")]
992992
#[rustc_const_stable(feature = "num_midpoint", since = "1.85.0")]
993993
pub const fn midpoint(self, other: f32) -> f32 {
994+
#[cfg(bootstrap)]
994995
cfg_if! {
995996
// Allow faster implementation that have known good 64-bit float
996997
// implementations. Falling back to the branchy code on targets that don't
@@ -1013,6 +1014,45 @@ impl f32 {
10131014
let abs_a = a.abs();
10141015
let abs_b = b.abs();
10151016

1017+
if abs_a <= HI && abs_b <= HI {
1018+
// Overflow is impossible
1019+
(a + b) / 2.
1020+
} else if abs_a < LO {
1021+
// Not safe to halve `a` (would underflow)
1022+
a + (b / 2.)
1023+
} else if abs_b < LO {
1024+
// Not safe to halve `b` (would underflow)
1025+
(a / 2.) + b
1026+
} else {
1027+
// Safe to halve `a` and `b`
1028+
(a / 2.) + (b / 2.)
1029+
}
1030+
}
1031+
}
1032+
#[cfg(not(bootstrap))]
1033+
crate::cfg_match! {
1034+
// Allow faster implementation that have known good 64-bit float
1035+
// implementations. Falling back to the branchy code on targets that don't
1036+
// have 64-bit hardware floats or buggy implementations.
1037+
// https://github.com/rust-lang/rust/pull/121062#issuecomment-2123408114
1038+
any(
1039+
target_arch = "x86_64",
1040+
target_arch = "aarch64",
1041+
all(any(target_arch = "riscv32", target_arch = "riscv64"), target_feature = "d"),
1042+
all(target_arch = "arm", target_feature = "vfp2"),
1043+
target_arch = "wasm32",
1044+
target_arch = "wasm64",
1045+
) => {
1046+
((self as f64 + other as f64) / 2.0) as f32
1047+
}
1048+
_ => {
1049+
const LO: f32 = f32::MIN_POSITIVE * 2.;
1050+
const HI: f32 = f32::MAX / 2.;
1051+
1052+
let (a, b) = (self, other);
1053+
let abs_a = a.abs();
1054+
let abs_b = b.abs();
1055+
10161056
if abs_a <= HI && abs_b <= HI {
10171057
// Overflow is impossible
10181058
(a + b) / 2.

library/core/src/slice/sort/select.rs

+10
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,23 @@ where
4141
let min_idx = min_index(v, &mut is_less).unwrap();
4242
v.swap(min_idx, index);
4343
} else {
44+
#[cfg(bootstrap)]
4445
cfg_if! {
4546
if #[cfg(feature = "optimize_for_size")] {
4647
median_of_medians(v, &mut is_less, index);
4748
} else {
4849
partition_at_index_loop(v, index, None, &mut is_less);
4950
}
5051
}
52+
#[cfg(not(bootstrap))]
53+
crate::cfg_match! {
54+
feature = "optimize_for_size" => {
55+
median_of_medians(v, &mut is_less, index);
56+
}
57+
_ => {
58+
partition_at_index_loop(v, index, None, &mut is_less);
59+
}
60+
}
5161
}
5262

5363
let (left, right) = v.split_at_mut(index);

library/core/src/slice/sort/stable/mod.rs

+45
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ pub fn sort<T, F: FnMut(&T, &T) -> bool, BufT: BufGuard<T>>(v: &mut [T], is_less
3939
return;
4040
}
4141

42+
#[cfg(bootstrap)]
4243
cfg_if! {
4344
if #[cfg(any(feature = "optimize_for_size", target_pointer_width = "16"))] {
4445
let alloc_len = len / 2;
@@ -79,6 +80,50 @@ pub fn sort<T, F: FnMut(&T, &T) -> bool, BufT: BufGuard<T>>(v: &mut [T], is_less
7980
driftsort_main::<T, F, BufT>(v, is_less);
8081
}
8182
}
83+
84+
#[cfg(not(bootstrap))]
85+
crate::cfg_match! {
86+
any(feature = "optimize_for_size", target_pointer_width = "16") => {
87+
let alloc_len = len / 2;
88+
89+
crate::cfg_match! {
90+
target_pointer_width = "16" => {
91+
let mut heap_buf = BufT::with_capacity(alloc_len);
92+
let scratch = heap_buf.as_uninit_slice_mut();
93+
}
94+
_ => {
95+
// For small inputs 4KiB of stack storage suffices, which allows us to avoid
96+
// calling the (de-)allocator. Benchmarks showed this was quite beneficial.
97+
let mut stack_buf = AlignedStorage::<T, 4096>::new();
98+
let stack_scratch = stack_buf.as_uninit_slice_mut();
99+
let mut heap_buf;
100+
let scratch = if stack_scratch.len() >= alloc_len {
101+
stack_scratch
102+
} else {
103+
heap_buf = BufT::with_capacity(alloc_len);
104+
heap_buf.as_uninit_slice_mut()
105+
};
106+
}
107+
}
108+
109+
tiny::mergesort(v, scratch, is_less);
110+
}
111+
_ => {
112+
// More advanced sorting methods than insertion sort are faster if called in
113+
// a hot loop for small inputs, but for general-purpose code the small
114+
// binary size of insertion sort is more important. The instruction cache in
115+
// modern processors is very valuable, and for a single sort call in general
116+
// purpose code any gains from an advanced method are cancelled by i-cache
117+
// misses during the sort, and thrashing the i-cache for surrounding code.
118+
const MAX_LEN_ALWAYS_INSERTION_SORT: usize = 20;
119+
if intrinsics::likely(len <= MAX_LEN_ALWAYS_INSERTION_SORT) {
120+
insertion_sort_shift_left(v, 1, is_less);
121+
return;
122+
}
123+
124+
driftsort_main::<T, F, BufT>(v, is_less);
125+
}
126+
}
82127
}
83128

84129
/// See [`sort`]

library/core/src/slice/sort/unstable/mod.rs

+23
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pub fn sort<T, F: FnMut(&T, &T) -> bool>(v: &mut [T], is_less: &mut F) {
3030
return;
3131
}
3232

33+
#[cfg(bootstrap)]
3334
cfg_if! {
3435
if #[cfg(any(feature = "optimize_for_size", target_pointer_width = "16"))] {
3536
heapsort::heapsort(v, is_less);
@@ -49,6 +50,28 @@ pub fn sort<T, F: FnMut(&T, &T) -> bool>(v: &mut [T], is_less: &mut F) {
4950
ipnsort(v, is_less);
5051
}
5152
}
53+
54+
#[cfg(not(bootstrap))]
55+
crate::cfg_match! {
56+
any(feature = "optimize_for_size", target_pointer_width = "16") => {
57+
heapsort::heapsort(v, is_less);
58+
}
59+
_ => {
60+
// More advanced sorting methods than insertion sort are faster if called in
61+
// a hot loop for small inputs, but for general-purpose code the small
62+
// binary size of insertion sort is more important. The instruction cache in
63+
// modern processors is very valuable, and for a single sort call in general
64+
// purpose code any gains from an advanced method are cancelled by i-cache
65+
// misses during the sort, and thrashing the i-cache for surrounding code.
66+
const MAX_LEN_ALWAYS_INSERTION_SORT: usize = 20;
67+
if intrinsics::likely(len <= MAX_LEN_ALWAYS_INSERTION_SORT) {
68+
insertion_sort_shift_left(v, 1, is_less);
69+
return;
70+
}
71+
72+
ipnsort(v, is_less);
73+
}
74+
}
5275
}
5376

5477
/// See [`sort`]

library/core/src/slice/sort/unstable/quicksort.rs

+10
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,23 @@ const fn inst_partition<T, F: FnMut(&T, &T) -> bool>() -> fn(&mut [T], &T, &mut
140140
if mem::size_of::<T>() <= MAX_BRANCHLESS_PARTITION_SIZE {
141141
// Specialize for types that are relatively cheap to copy, where branchless optimizations
142142
// have large leverage e.g. `u64` and `String`.
143+
#[cfg(bootstrap)]
143144
cfg_if! {
144145
if #[cfg(feature = "optimize_for_size")] {
145146
partition_lomuto_branchless_simple::<T, F>
146147
} else {
147148
partition_lomuto_branchless_cyclic::<T, F>
148149
}
149150
}
151+
#[cfg(not(bootstrap))]
152+
crate::cfg_match! {
153+
feature = "optimize_for_size" => {
154+
partition_lomuto_branchless_simple::<T, F>
155+
}
156+
_ => {
157+
partition_lomuto_branchless_cyclic::<T, F>
158+
}
159+
}
150160
} else {
151161
partition_hoare_branchy_cyclic::<T, F>
152162
}

0 commit comments

Comments
 (0)