Skip to content

Commit 2ad41ef

Browse files
committed
Add public-test-deps feature for better visibility control
1 parent 186517b commit 2ad41ef

File tree

13 files changed

+64
-29
lines changed

13 files changed

+64
-29
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ no-lang-items = []
6464
# Only used in the compiler's build system
6565
rustc-dep-of-std = ['compiler-builtins', 'core']
6666

67+
# This makes certain traits and function specializations public that
68+
# are not normally public but are required by the `testcrate`
69+
public-test-deps = []
70+
6771
[[example]]
6872
name = "intrinsics"
6973
required-features = ["compiler-builtins"]

src/float/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ pub mod mul;
1111
pub mod pow;
1212
pub mod sub;
1313

14+
public_test_dep! {
1415
/// Trait for some basic operations on floats
15-
#[doc(hidden)]
16-
pub trait Float:
16+
pub(crate) trait Float:
1717
Copy
1818
+ core::fmt::Debug
1919
+ PartialEq
@@ -99,6 +99,7 @@ pub trait Float:
9999
/// Returns if `self` is subnormal
100100
fn is_subnormal(self) -> bool;
101101
}
102+
}
102103

103104
macro_rules! float_impl {
104105
($ty:ident, $ity:ident, $sity:ident, $expty:ident, $bits:expr, $significand_bits:expr) => {

src/int/leading_zeros.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
// adding a zero check at the beginning, but `__clzsi2` has a precondition that `x != 0`.
44
// Compilers will insert the check for zero in cases where it is needed.
55

6+
public_test_dep! {
67
/// Returns the number of leading binary zeros in `x`.
7-
#[doc(hidden)]
8-
pub fn usize_leading_zeros_default(x: usize) -> usize {
8+
pub(crate) fn usize_leading_zeros_default(x: usize) -> usize {
99
// The basic idea is to test if the higher bits of `x` are zero and bisect the number
1010
// of leading zeros. It is possible for all branches of the bisection to use the same
1111
// code path by conditionally shifting the higher parts down to let the next bisection
@@ -69,15 +69,16 @@ pub fn usize_leading_zeros_default(x: usize) -> usize {
6969
// on x86_64, it is slightly faster to use the LUT, but this is probably because of OOO
7070
// execution effects. Changing to using a LUT and branching is risky for smaller cores.
7171
}
72+
}
7273

7374
// The above method does not compile well on RISC-V (because of the lack of predicated
7475
// instructions), producing code with many branches or using an excessively long
7576
// branchless solution. This method takes advantage of the set-if-less-than instruction on
7677
// RISC-V that allows `(x >= power-of-two) as usize` to be branchless.
7778

79+
public_test_dep! {
7880
/// Returns the number of leading binary zeros in `x`.
79-
#[doc(hidden)]
80-
pub fn usize_leading_zeros_riscv(x: usize) -> usize {
81+
pub(crate) fn usize_leading_zeros_riscv(x: usize) -> usize {
8182
let mut x = x;
8283
// the number of potential leading zeros
8384
let mut z = usize::MAX.count_ones() as usize;
@@ -126,6 +127,7 @@ pub fn usize_leading_zeros_riscv(x: usize) -> usize {
126127
// If `x != 0` then `x == 1` and subtracts one potential zero from `z`.
127128
z - x
128129
}
130+
}
129131

130132
intrinsics! {
131133
#[maybe_use_optimized_c_shim]

src/int/mod.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ pub mod udiv;
1111

1212
pub use self::leading_zeros::__clzsi2;
1313

14+
public_test_dep! {
1415
/// Trait for some basic operations on integers
15-
#[doc(hidden)]
16-
pub trait Int:
16+
pub(crate) trait Int:
1717
Copy
1818
+ core::fmt::Debug
1919
+ PartialEq
@@ -81,6 +81,7 @@ pub trait Int:
8181
fn overflowing_add(self, other: Self) -> (Self, bool);
8282
fn leading_zeros(self) -> u32;
8383
}
84+
}
8485

8586
macro_rules! int_impl_common {
8687
($ty:ty) => {
@@ -255,10 +256,10 @@ int_impl!(i32, u32);
255256
int_impl!(i64, u64);
256257
int_impl!(i128, u128);
257258

259+
public_test_dep! {
258260
/// Trait for integers twice the bit width of another integer. This is implemented for all
259261
/// primitives except for `u8`, because there is not a smaller primitive.
260-
#[doc(hidden)]
261-
pub trait DInt: Int {
262+
pub(crate) trait DInt: Int {
262263
/// Integer that is half the bit width of the integer this trait is implemented for
263264
type H: HInt<D = Self> + Int;
264265

@@ -271,11 +272,12 @@ pub trait DInt: Int {
271272
/// Constructs an integer using lower and higher half parts
272273
fn from_lo_hi(lo: Self::H, hi: Self::H) -> Self;
273274
}
275+
}
274276

277+
public_test_dep! {
275278
/// Trait for integers half the bit width of another integer. This is implemented for all
276279
/// primitives except for `u128`, because it there is not a larger primitive.
277-
#[doc(hidden)]
278-
pub trait HInt: Int {
280+
pub(crate) trait HInt: Int {
279281
/// Integer that is double the bit width of the integer this trait is implemented for
280282
type D: DInt<H = Self> + Int;
281283

@@ -291,6 +293,7 @@ pub trait HInt: Int {
291293
/// Widening multiplication. This cannot overflow.
292294
fn widen_mul(self, rhs: Self) -> Self::D;
293295
}
296+
}
294297

295298
macro_rules! impl_d_int {
296299
($($X:ident $D:ident),*) => {
@@ -353,11 +356,12 @@ impl_h_int!(
353356
i64 u64 i128
354357
);
355358

359+
public_test_dep! {
356360
/// Trait to express (possibly lossy) casting of integers
357-
#[doc(hidden)]
358-
pub trait CastInto<T: Copy>: Copy {
361+
pub(crate) trait CastInto<T: Copy>: Copy {
359362
fn cast(self) -> T;
360363
}
364+
}
361365

362366
macro_rules! cast_into {
363367
($ty:ty) => {

src/int/specialized_div_rem/asymmetric.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
/// assembly instruction that can divide a 128 bit integer by a 64 bit integer if the quotient fits
44
/// in 64 bits. The 128 bit version of this algorithm would use that fast hardware division to
55
/// construct a full 128 bit by 128 bit division.
6-
#[doc(hidden)]
7-
#[macro_export]
6+
#[allow(unused_macros)]
87
macro_rules! impl_asymmetric {
98
(
109
$fn:ident, // name of the unsigned division function

src/int/specialized_div_rem/binary_long.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
/// predicate instructions. For architectures with predicated instructions, one of the algorithms
55
/// described in the documentation of these functions probably has higher performance, and a custom
66
/// assembly routine should be used instead.
7-
#[doc(hidden)]
8-
#[macro_export]
7+
#[allow(unused_macros)]
98
macro_rules! impl_binary_long {
109
(
1110
$fn:ident, // name of the unsigned division function

src/int/specialized_div_rem/delegate.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
/// binary long division to divide integers larger than what hardware division by itself can do. This
33
/// function is intended for microarchitectures that have division hardware, but not fast enough
44
/// multiplication hardware for `impl_trifecta` to be faster.
5-
#[doc(hidden)]
6-
#[macro_export]
5+
#[allow(unused_macros)]
76
macro_rules! impl_delegate {
87
(
98
$fn:ident, // name of the unsigned division function
@@ -186,15 +185,17 @@ macro_rules! impl_delegate {
186185
};
187186
}
188187

188+
public_test_dep! {
189189
/// Returns `n / d` and sets `*rem = n % d`.
190190
///
191191
/// This specialization exists because:
192192
/// - The LLVM backend for 32-bit SPARC cannot compile functions that return `(u128, u128)`,
193193
/// so we have to use an old fashioned `&mut u128` argument to return the remainder.
194194
/// - 64-bit SPARC does not have u64 * u64 => u128 widening multiplication, which makes the
195195
/// delegate algorithm strategy the only reasonably fast way to perform `u128` division.
196-
#[doc(hidden)]
197-
pub fn u128_divide_sparc(duo: u128, div: u128, rem: &mut u128) -> u128 {
196+
// used on SPARC
197+
#[allow(dead_code)]
198+
pub(crate) fn u128_divide_sparc(duo: u128, div: u128, rem: &mut u128) -> u128 {
198199
use super::*;
199200
let duo_lo = duo as u64;
200201
let duo_hi = (duo >> 64) as u64;
@@ -315,3 +316,4 @@ pub fn u128_divide_sparc(duo: u128, div: u128, rem: &mut u128) -> u128 {
315316
}
316317
}
317318
}
319+
}

src/int/specialized_div_rem/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ mod binary_long;
5353

5454
#[macro_use]
5555
mod delegate;
56+
57+
// used on SPARC
58+
#[allow(unused_imports)]
59+
#[cfg(not(feature = "public-test-deps"))]
60+
pub(crate) use self::delegate::u128_divide_sparc;
61+
62+
#[cfg(feature = "public-test-deps")]
5663
pub use self::delegate::u128_divide_sparc;
5764

5865
#[macro_use]

src/int/specialized_div_rem/norm_shift.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/// Creates a function used by some division algorithms to compute the "normalization shift".
2-
#[doc(hidden)]
3-
#[macro_export]
2+
#[allow(unused_macros)]
43
macro_rules! impl_normalization_shift {
54
(
65
$name:ident, // name of the normalization shift function

src/int/specialized_div_rem/trifecta.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
/// larger than the largest hardware integer division supported. These functions use large radix
33
/// division algorithms that require both fast division and very fast widening multiplication on the
44
/// target microarchitecture. Otherwise, `impl_delegate` should be used instead.
5-
#[doc(hidden)]
6-
#[macro_export]
5+
#[allow(unused_macros)]
76
macro_rules! impl_trifecta {
87
(
98
$fn:ident, // name of the unsigned division function

src/int/udiv.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
pub use int::specialized_div_rem::u128_divide_sparc;
2-
use int::specialized_div_rem::*;
1+
#[cfg(not(feature = "public-test-deps"))]
2+
pub(crate) use int::specialized_div_rem::*;
3+
4+
#[cfg(feature = "public-test-deps")]
5+
pub use int::specialized_div_rem::*;
36

47
intrinsics! {
58
#[maybe_use_optimized_c_shim]

src/macros.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
//! Macros shared throughout the compiler-builtins implementation
22
3+
/// Changes the visibility to `pub` if feature "public-test-deps" is set
4+
#[cfg(not(feature = "public-test-deps"))]
5+
macro_rules! public_test_dep {
6+
($(#[$($meta:meta)*])* pub(crate) $ident:ident $($tokens:tt)*) => {
7+
$(#[$($meta)*])* pub(crate) $ident $($tokens)*
8+
};
9+
}
10+
11+
/// Changes the visibility to `pub` if feature "public-test-deps" is set
12+
#[cfg(feature = "public-test-deps")]
13+
macro_rules! public_test_dep {
14+
{$(#[$($meta:meta)*])* pub(crate) $ident:ident $($tokens:tt)*} => {
15+
$(#[$($meta)*])* pub $ident $($tokens)*
16+
};
17+
}
18+
319
/// The "main macro" used for defining intrinsics.
420
///
521
/// The compiler-builtins library is super platform-specific with tons of crazy

testcrate/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ rand_xoshiro = "0.6"
1717
[dependencies.compiler_builtins]
1818
path = ".."
1919
default-features = false
20-
features = ["no-lang-items"]
20+
features = ["no-lang-items", "public-test-deps"]
2121

2222
[target.'cfg(all(target_arch = "arm", not(any(target_env = "gnu", target_env = "musl")), target_os = "linux"))'.dev-dependencies]
2323
test = { git = "https://github.com/japaric/utest" }

0 commit comments

Comments
 (0)