Skip to content

Commit 26383a6

Browse files
committed
[cfg_match] Library edition
1 parent 0cc4f4f commit 26383a6

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

276+
185277
mod c_int_definition {
278+
#[cfg(bootstrap)]
186279
cfg_if! {
187280
if #[cfg(any(target_arch = "avr", target_arch = "msp430"))] {
188281
pub(super) type c_int = i16;
@@ -192,9 +285,21 @@ mod c_int_definition {
192285
pub(super) type c_uint = u32;
193286
}
194287
}
288+
#[cfg(not(bootstrap))]
289+
crate::cfg_match! {
290+
any(target_arch = "avr", target_arch = "msp430") => {
291+
pub type c_int = i16;
292+
pub type c_uint = u16;
293+
}
294+
_ => {
295+
pub type c_int = i32;
296+
pub type c_uint = u32;
297+
}
298+
}
195299
}
196300

197301
mod c_long_definition {
302+
#[cfg(bootstrap)]
198303
cfg_if! {
199304
if #[cfg(all(target_pointer_width = "64", not(windows)))] {
200305
pub(super) type c_long = i64;
@@ -205,6 +310,18 @@ mod c_long_definition {
205310
pub(super) type c_ulong = u32;
206311
}
207312
}
313+
#[cfg(not(bootstrap))]
314+
crate::cfg_match! {
315+
all(target_pointer_width = "64", not(windows)) => {
316+
pub type c_long = i64;
317+
pub type c_ulong = u64;
318+
}
319+
_ => {
320+
// The minimal size of `long` in the C standard is 32 bits
321+
pub type c_long = i32;
322+
pub type c_ulong = u32;
323+
}
324+
}
208325
}
209326

210327
// 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
@@ -113,6 +113,7 @@
113113
#![feature(bigint_helper_methods)]
114114
#![feature(bstr)]
115115
#![feature(bstr_internals)]
116+
#![feature(cfg_match)]
116117
#![feature(const_carrying_mul_add)]
117118
#![feature(const_eval_select)]
118119
#![feature(core_intrinsics)]

library/core/src/num/f32.rs

+40
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,7 @@ impl f32 {
997997
#[stable(feature = "num_midpoint", since = "1.85.0")]
998998
#[rustc_const_stable(feature = "num_midpoint", since = "1.85.0")]
999999
pub const fn midpoint(self, other: f32) -> f32 {
1000+
#[cfg(bootstrap)]
10001001
cfg_if! {
10011002
// Allow faster implementation that have known good 64-bit float
10021003
// implementations. Falling back to the branchy code on targets that don't
@@ -1019,6 +1020,45 @@ impl f32 {
10191020
let abs_a = a.abs();
10201021
let abs_b = b.abs();
10211022

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