Skip to content

Commit 1503d20

Browse files
committed
Add public-test-deps feature for better visibility control
1 parent 2956079 commit 1503d20

File tree

9 files changed

+69
-19
lines changed

9 files changed

+69
-19
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: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use core::ops;
22

33
use super::int::Int;
4+
use crate::public_test_dep;
45

56
pub mod add;
67
pub mod cmp;
@@ -11,9 +12,9 @@ pub mod mul;
1112
pub mod pow;
1213
pub mod sub;
1314

15+
public_test_dep! {
1416
/// Trait for some basic operations on floats
15-
#[doc(hidden)]
16-
pub trait Float:
17+
pub(crate) trait Float:
1718
Copy
1819
+ core::fmt::Debug
1920
+ PartialEq
@@ -99,6 +100,7 @@ pub trait Float:
99100
/// Returns if `self` is subnormal
100101
fn is_subnormal(self) -> bool;
101102
}
103+
}
102104

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

src/int/leading_zeros.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
use crate::public_test_dep;
2+
13
// Note: these functions happen to produce the correct `usize::leading_zeros(0)` value
24
// without a explicit zero check. Zero is probably common enough that it could warrant
35
// adding a zero check at the beginning, but `__clzsi2` has a precondition that `x != 0`.
46
// Compilers will insert the check for zero in cases where it is needed.
57

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

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

81+
public_test_dep! {
7882
/// Returns the number of leading binary zeros in `x`.
79-
#[doc(hidden)]
80-
pub fn usize_leading_zeros_riscv(x: usize) -> usize {
83+
pub(crate) fn usize_leading_zeros_riscv(x: usize) -> usize {
8184
let mut x = x;
8285
// the number of potential leading zeros
8386
let mut z = usize::MAX.count_ones() as usize;
@@ -126,6 +129,7 @@ pub fn usize_leading_zeros_riscv(x: usize) -> usize {
126129
// If `x != 0` then `x == 1` and subtracts one potential zero from `z`.
127130
z - x
128131
}
132+
}
129133

130134
intrinsics! {
131135
#[maybe_use_optimized_c_shim]

src/int/mod.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::public_test_dep;
12
use core::ops;
23

34
mod specialized_div_rem;
@@ -11,9 +12,9 @@ pub mod udiv;
1112

1213
pub use self::leading_zeros::__clzsi2;
1314

15+
public_test_dep! {
1416
/// Trait for some basic operations on integers
15-
#[doc(hidden)]
16-
pub trait Int:
17+
pub(crate) trait Int:
1718
Copy
1819
+ core::fmt::Debug
1920
+ PartialEq
@@ -81,6 +82,7 @@ pub trait Int:
8182
fn overflowing_add(self, other: Self) -> (Self, bool);
8283
fn leading_zeros(self) -> u32;
8384
}
85+
}
8486

8587
macro_rules! int_impl_common {
8688
($ty:ty) => {
@@ -255,10 +257,10 @@ int_impl!(i32, u32);
255257
int_impl!(i64, u64);
256258
int_impl!(i128, u128);
257259

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

@@ -271,11 +273,12 @@ pub trait DInt: Int {
271273
/// Constructs an integer using lower and higher half parts
272274
fn from_lo_hi(lo: Self::H, hi: Self::H) -> Self;
273275
}
276+
}
274277

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

@@ -291,6 +294,7 @@ pub trait HInt: Int {
291294
/// Widening multiplication. This cannot overflow.
292295
fn widen_mul(self, rhs: Self) -> Self::D;
293296
}
297+
}
294298

295299
macro_rules! impl_d_int {
296300
($($X:ident $D:ident),*) => {
@@ -353,11 +357,12 @@ impl_h_int!(
353357
i64 u64 i128
354358
);
355359

360+
public_test_dep! {
356361
/// Trait to express (possibly lossy) casting of integers
357-
#[doc(hidden)]
358-
pub trait CastInto<T: Copy>: Copy {
362+
pub(crate) trait CastInto<T: Copy>: Copy {
359363
fn cast(self) -> T;
360364
}
365+
}
361366

362367
macro_rules! cast_into {
363368
($ty:ty) => {

src/int/specialized_div_rem/delegate.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use crate::public_test_dep;
2+
13
/// Creates an unsigned division function that uses a combination of hardware division and
24
/// binary long division to divide integers larger than what hardware division by itself can do. This
35
/// function is intended for microarchitectures that have division hardware, but not fast enough
@@ -186,15 +188,17 @@ macro_rules! impl_delegate {
186188
};
187189
}
188190

191+
public_test_dep! {
189192
/// Returns `n / d` and sets `*rem = n % d`.
190193
///
191194
/// This specialization exists because:
192195
/// - The LLVM backend for 32-bit SPARC cannot compile functions that return `(u128, u128)`,
193196
/// so we have to use an old fashioned `&mut u128` argument to return the remainder.
194197
/// - 64-bit SPARC does not have u64 * u64 => u128 widening multiplication, which makes the
195198
/// 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 {
199+
// used on SPARC
200+
#[allow(dead_code)]
201+
pub(crate) fn u128_divide_sparc(duo: u128, div: u128, rem: &mut u128) -> u128 {
198202
use super::*;
199203
let duo_lo = duo as u64;
200204
let duo_hi = (duo >> 64) as u64;
@@ -315,3 +319,4 @@ pub fn u128_divide_sparc(duo: u128, div: u128, rem: &mut u128) -> u128 {
315319
}
316320
}
317321
}
322+
}

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/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/lib.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,23 @@ pub mod x86;
6666
pub mod x86_64;
6767

6868
pub mod probestack;
69+
70+
/// Changes the visibility to `pub` if feature "public-test-deps" is set
71+
#[doc(hidden)]
72+
#[cfg(not(feature = "public-test-deps"))]
73+
#[macro_export]
74+
macro_rules! public_test_dep {
75+
($(#[$($meta:meta)*])* pub(crate) $ident:ident $($tokens:tt)*) => {
76+
$(#[$($meta)*])* pub(crate) $ident $($tokens)*
77+
};
78+
}
79+
80+
/// Changes the visibility to `pub` if feature "public-test-deps" is set
81+
#[doc(hidden)]
82+
#[cfg(feature = "public-test-deps")]
83+
#[macro_export]
84+
macro_rules! public_test_dep {
85+
{$(#[$($meta:meta)*])* pub(crate) $ident:ident $($tokens:tt)*} => {
86+
$(#[$($meta)*])* pub $ident $($tokens)*
87+
};
88+
}

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)