core/hint.rs
1#![stable(feature = "core_hint", since = "1.27.0")]
2
3//! Hints to compiler that affects how code should be emitted or optimized.
4//!
5//! Hints may be compile time or runtime.
6
7use crate::mem::MaybeUninit;
8use crate::{intrinsics, ub_checks};
9
10/// Informs the compiler that the site which is calling this function is not
11/// reachable, possibly enabling further optimizations.
12///
13/// # Safety
14///
15/// Reaching this function is *Undefined Behavior*.
16///
17/// As the compiler assumes that all forms of Undefined Behavior can never
18/// happen, it will eliminate all branches in the surrounding code that it can
19/// determine will invariably lead to a call to `unreachable_unchecked()`.
20///
21/// If the assumptions embedded in using this function turn out to be wrong -
22/// that is, if the site which is calling `unreachable_unchecked()` is actually
23/// reachable at runtime - the compiler may have generated nonsensical machine
24/// instructions for this situation, including in seemingly unrelated code,
25/// causing difficult-to-debug problems.
26///
27/// Use this function sparingly. Consider using the [`unreachable!`] macro,
28/// which may prevent some optimizations but will safely panic in case it is
29/// actually reached at runtime. Benchmark your code to find out if using
30/// `unreachable_unchecked()` comes with a performance benefit.
31///
32/// # Examples
33///
34/// `unreachable_unchecked()` can be used in situations where the compiler
35/// can't prove invariants that were previously established. Such situations
36/// have a higher chance of occurring if those invariants are upheld by
37/// external code that the compiler can't analyze.
38/// ```
39/// fn prepare_inputs(divisors: &mut Vec<u32>) {
40/// // Note to future-self when making changes: The invariant established
41/// // here is NOT checked in `do_computation()`; if this changes, you HAVE
42/// // to change `do_computation()`.
43/// divisors.retain(|divisor| *divisor != 0)
44/// }
45///
46/// /// # Safety
47/// /// All elements of `divisor` must be non-zero.
48/// unsafe fn do_computation(i: u32, divisors: &[u32]) -> u32 {
49/// divisors.iter().fold(i, |acc, divisor| {
50/// // Convince the compiler that a division by zero can't happen here
51/// // and a check is not needed below.
52/// if *divisor == 0 {
53/// // Safety: `divisor` can't be zero because of `prepare_inputs`,
54/// // but the compiler does not know about this. We *promise*
55/// // that we always call `prepare_inputs`.
56/// unsafe { std::hint::unreachable_unchecked() }
57/// }
58/// // The compiler would normally introduce a check here that prevents
59/// // a division by zero. However, if `divisor` was zero, the branch
60/// // above would reach what we explicitly marked as unreachable.
61/// // The compiler concludes that `divisor` can't be zero at this point
62/// // and removes the - now proven useless - check.
63/// acc / divisor
64/// })
65/// }
66///
67/// let mut divisors = vec![2, 0, 4];
68/// prepare_inputs(&mut divisors);
69/// let result = unsafe {
70/// // Safety: prepare_inputs() guarantees that divisors is non-zero
71/// do_computation(100, &divisors)
72/// };
73/// assert_eq!(result, 12);
74///
75/// ```
76///
77/// While using `unreachable_unchecked()` is perfectly sound in the following
78/// example, as the compiler is able to prove that a division by zero is not
79/// possible, benchmarking reveals that `unreachable_unchecked()` provides
80/// no benefit over using [`unreachable!`], while the latter does not introduce
81/// the possibility of Undefined Behavior.
82///
83/// ```
84/// fn div_1(a: u32, b: u32) -> u32 {
85/// use std::hint::unreachable_unchecked;
86///
87/// // `b.saturating_add(1)` is always positive (not zero),
88/// // hence `checked_div` will never return `None`.
89/// // Therefore, the else branch is unreachable.
90/// a.checked_div(b.saturating_add(1))
91/// .unwrap_or_else(|| unsafe { unreachable_unchecked() })
92/// }
93///
94/// assert_eq!(div_1(7, 0), 7);
95/// assert_eq!(div_1(9, 1), 4);
96/// assert_eq!(div_1(11, u32::MAX), 0);
97/// ```
98#[inline]
99#[stable(feature = "unreachable", since = "1.27.0")]
100#[rustc_const_stable(feature = "const_unreachable_unchecked", since = "1.57.0")]
101#[track_caller]
102#[coverage(off)] // Ferrocene addition: this function breaks llvm-cov
103pub const unsafe fn unreachable_unchecked() -> ! {
104 ub_checks::assert_unsafe_precondition!(
105 check_language_ub,
106 "hint::unreachable_unchecked must never be reached",
107 () => false
108 );
109 // SAFETY: the safety contract for `intrinsics::unreachable` must
110 // be upheld by the caller.
111 unsafe { intrinsics::unreachable() }
112}
113
114/// Makes a *soundness* promise to the compiler that `cond` holds.
115///
116/// This may allow the optimizer to simplify things, but it might also make the generated code
117/// slower. Either way, calling it will most likely make compilation take longer.
118///
119/// You may know this from other places as
120/// [`llvm.assume`](https://llvm.org/docs/LangRef.html#llvm-assume-intrinsic) or, in C,
121/// [`__builtin_assume`](https://clang.llvm.org/docs/LanguageExtensions.html#builtin-assume).
122///
123/// This promotes a correctness requirement to a soundness requirement. Don't do that without
124/// very good reason.
125///
126/// # Usage
127///
128/// This is a situational tool for micro-optimization, and is allowed to do nothing. Any use
129/// should come with a repeatable benchmark to show the value, with the expectation to drop it
130/// later should the optimizer get smarter and no longer need it.
131///
132/// The more complicated the condition, the less likely this is to be useful. For example,
133/// `assert_unchecked(foo.is_sorted())` is a complex enough value that the compiler is unlikely
134/// to be able to take advantage of it.
135///
136/// There's also no need to `assert_unchecked` basic properties of things. For example, the
137/// compiler already knows the range of `count_ones`, so there is no benefit to
138/// `let n = u32::count_ones(x); assert_unchecked(n <= u32::BITS);`.
139///
140/// `assert_unchecked` is logically equivalent to `if !cond { unreachable_unchecked(); }`. If
141/// ever you are tempted to write `assert_unchecked(false)`, you should instead use
142/// [`unreachable_unchecked()`] directly.
143///
144/// # Safety
145///
146/// `cond` must be `true`. It is immediate UB to call this with `false`.
147///
148/// # Example
149///
150/// ```
151/// use core::hint;
152///
153/// /// # Safety
154/// ///
155/// /// `p` must be nonnull and valid
156/// pub unsafe fn next_value(p: *const i32) -> i32 {
157/// // SAFETY: caller invariants guarantee that `p` is not null
158/// unsafe { hint::assert_unchecked(!p.is_null()) }
159///
160/// if p.is_null() {
161/// return -1;
162/// } else {
163/// // SAFETY: caller invariants guarantee that `p` is valid
164/// unsafe { *p + 1 }
165/// }
166/// }
167/// ```
168///
169/// Without the `assert_unchecked`, the above function produces the following with optimizations
170/// enabled:
171///
172/// ```asm
173/// next_value:
174/// test rdi, rdi
175/// je .LBB0_1
176/// mov eax, dword ptr [rdi]
177/// inc eax
178/// ret
179/// .LBB0_1:
180/// mov eax, -1
181/// ret
182/// ```
183///
184/// Adding the assertion allows the optimizer to remove the extra check:
185///
186/// ```asm
187/// next_value:
188/// mov eax, dword ptr [rdi]
189/// inc eax
190/// ret
191/// ```
192///
193/// This example is quite unlike anything that would be used in the real world: it is redundant
194/// to put an assertion right next to code that checks the same thing, and dereferencing a
195/// pointer already has the builtin assumption that it is nonnull. However, it illustrates the
196/// kind of changes the optimizer can make even when the behavior is less obviously related.
197#[track_caller]
198#[inline(always)]
199#[doc(alias = "assume")]
200#[stable(feature = "hint_assert_unchecked", since = "1.81.0")]
201#[rustc_const_stable(feature = "hint_assert_unchecked", since = "1.81.0")]
202pub const unsafe fn assert_unchecked(cond: bool) {
203 // SAFETY: The caller promised `cond` is true.
204 unsafe {
205 ub_checks::assert_unsafe_precondition!(
206 check_language_ub,
207 "hint::assert_unchecked must never be called when the condition is false",
208 (cond: bool = cond) => cond,
209 );
210 crate::intrinsics::assume(cond);
211 }
212}
213
214/// Emits a machine instruction to signal the processor that it is running in
215/// a busy-wait spin-loop ("spin lock").
216///
217/// Upon receiving the spin-loop signal the processor can optimize its behavior by,
218/// for example, saving power or switching hyper-threads.
219///
220/// This function is different from [`thread::yield_now`] which directly
221/// yields to the system's scheduler, whereas `spin_loop` does not interact
222/// with the operating system.
223///
224/// A common use case for `spin_loop` is implementing bounded optimistic
225/// spinning in a CAS loop in synchronization primitives. To avoid problems
226/// like priority inversion, it is strongly recommended that the spin loop is
227/// terminated after a finite amount of iterations and an appropriate blocking
228/// syscall is made.
229///
230/// **Note**: On platforms that do not support receiving spin-loop hints this
231/// function does not do anything at all.
232///
233/// # Examples
234///
235/// ```ignore-wasm
236/// use std::sync::atomic::{AtomicBool, Ordering};
237/// use std::sync::Arc;
238/// use std::{hint, thread};
239///
240/// // A shared atomic value that threads will use to coordinate
241/// let live = Arc::new(AtomicBool::new(false));
242///
243/// // In a background thread we'll eventually set the value
244/// let bg_work = {
245/// let live = live.clone();
246/// thread::spawn(move || {
247/// // Do some work, then make the value live
248/// do_some_work();
249/// live.store(true, Ordering::Release);
250/// })
251/// };
252///
253/// // Back on our current thread, we wait for the value to be set
254/// while !live.load(Ordering::Acquire) {
255/// // The spin loop is a hint to the CPU that we're waiting, but probably
256/// // not for very long
257/// hint::spin_loop();
258/// }
259///
260/// // The value is now set
261/// # fn do_some_work() {}
262/// do_some_work();
263/// bg_work.join()?;
264/// # Ok::<(), Box<dyn core::any::Any + Send + 'static>>(())
265/// ```
266///
267/// [`thread::yield_now`]: ../../std/thread/fn.yield_now.html
268#[inline(always)]
269#[stable(feature = "renamed_spin_loop", since = "1.49.0")]
270pub fn spin_loop() {
271 #[cfg(target_arch = "x86")]
272 {
273 // SAFETY: the `cfg` attr ensures that we only execute this on x86 targets.
274 unsafe { crate::arch::x86::_mm_pause() };
275 }
276
277 #[cfg(target_arch = "x86_64")]
278 {
279 // SAFETY: the `cfg` attr ensures that we only execute this on x86_64 targets.
280 unsafe { crate::arch::x86_64::_mm_pause() };
281 }
282
283 #[cfg(target_arch = "riscv32")]
284 {
285 crate::arch::riscv32::pause();
286 }
287
288 #[cfg(target_arch = "riscv64")]
289 {
290 crate::arch::riscv64::pause();
291 }
292
293 #[cfg(any(target_arch = "aarch64", target_arch = "arm64ec"))]
294 {
295 // SAFETY: the `cfg` attr ensures that we only execute this on aarch64 targets.
296 unsafe { crate::arch::aarch64::__isb(crate::arch::aarch64::SY) };
297 }
298
299 #[cfg(all(target_arch = "arm", target_feature = "v6"))]
300 {
301 // SAFETY: the `cfg` attr ensures that we only execute this on arm targets
302 // with support for the v6 feature.
303 unsafe { crate::arch::arm::__yield() };
304 }
305}
306
307/// An identity function that *__hints__* to the compiler to be maximally pessimistic about what
308/// `black_box` could do.
309///
310/// Unlike [`std::convert::identity`], a Rust compiler is encouraged to assume that `black_box` can
311/// use `dummy` in any possible valid way that Rust code is allowed to without introducing undefined
312/// behavior in the calling code. This property makes `black_box` useful for writing code in which
313/// certain optimizations are not desired, such as benchmarks.
314///
315/// <div class="warning">
316///
317/// Note however, that `black_box` is only (and can only be) provided on a "best-effort" basis. The
318/// extent to which it can block optimisations may vary depending upon the platform and code-gen
319/// backend used. Programs cannot rely on `black_box` for *correctness*, beyond it behaving as the
320/// identity function. As such, it **must not be relied upon to control critical program behavior.**
321/// This also means that this function does not offer any guarantees for cryptographic or security
322/// purposes.
323///
324/// This limitation is not specific to `black_box`; there is no mechanism in the entire Rust
325/// language that can provide the guarantees required for constant-time cryptography.
326/// (There is also no such mechanism in LLVM, so the same is true for every other LLVM-based compiler.)
327///
328/// </div>
329///
330/// [`std::convert::identity`]: crate::convert::identity
331///
332/// # When is this useful?
333///
334/// While not suitable in those mission-critical cases, `black_box`'s functionality can generally be
335/// relied upon for benchmarking, and should be used there. It will try to ensure that the
336/// compiler doesn't optimize away part of the intended test code based on context. For
337/// example:
338///
339/// ```
340/// fn contains(haystack: &[&str], needle: &str) -> bool {
341/// haystack.iter().any(|x| x == &needle)
342/// }
343///
344/// pub fn benchmark() {
345/// let haystack = vec!["abc", "def", "ghi", "jkl", "mno"];
346/// let needle = "ghi";
347/// for _ in 0..10 {
348/// contains(&haystack, needle);
349/// }
350/// }
351/// ```
352///
353/// The compiler could theoretically make optimizations like the following:
354///
355/// - The `needle` and `haystack` do not change, move the call to `contains` outside the loop and
356/// delete the loop
357/// - Inline `contains`
358/// - `needle` and `haystack` have values known at compile time, `contains` is always true. Remove
359/// the call and replace with `true`
360/// - Nothing is done with the result of `contains`: delete this function call entirely
361/// - `benchmark` now has no purpose: delete this function
362///
363/// It is not likely that all of the above happens, but the compiler is definitely able to make some
364/// optimizations that could result in a very inaccurate benchmark. This is where `black_box` comes
365/// in:
366///
367/// ```
368/// use std::hint::black_box;
369///
370/// // Same `contains` function.
371/// fn contains(haystack: &[&str], needle: &str) -> bool {
372/// haystack.iter().any(|x| x == &needle)
373/// }
374///
375/// pub fn benchmark() {
376/// let haystack = vec!["abc", "def", "ghi", "jkl", "mno"];
377/// let needle = "ghi";
378/// for _ in 0..10 {
379/// // Force the compiler to run `contains`, even though it is a pure function whose
380/// // results are unused.
381/// black_box(contains(
382/// // Prevent the compiler from making assumptions about the input.
383/// black_box(&haystack),
384/// black_box(needle),
385/// ));
386/// }
387/// }
388/// ```
389///
390/// This essentially tells the compiler to block optimizations across any calls to `black_box`. So,
391/// it now:
392///
393/// - Treats both arguments to `contains` as unpredictable: the body of `contains` can no longer be
394/// optimized based on argument values
395/// - Treats the call to `contains` and its result as volatile: the body of `benchmark` cannot
396/// optimize this away
397///
398/// This makes our benchmark much more realistic to how the function would actually be used, where
399/// arguments are usually not known at compile time and the result is used in some way.
400///
401/// # How to use this
402///
403/// In practice, `black_box` serves two purposes:
404///
405/// 1. It prevents the compiler from making optimizations related to the value returned by `black_box`
406/// 2. It forces the value passed to `black_box` to be calculated, even if the return value of `black_box` is unused
407///
408/// ```
409/// use std::hint::black_box;
410///
411/// let zero = 0;
412/// let five = 5;
413///
414/// // The compiler will see this and remove the `* five` call, because it knows that multiplying
415/// // any integer by 0 will result in 0.
416/// let c = zero * five;
417///
418/// // Adding `black_box` here disables the compiler's ability to reason about the first operand in the multiplication.
419/// // It is forced to assume that it can be any possible number, so it cannot remove the `* five`
420/// // operation.
421/// let c = black_box(zero) * five;
422/// ```
423///
424/// While most cases will not be as clear-cut as the above example, it still illustrates how
425/// `black_box` can be used. When benchmarking a function, you usually want to wrap its inputs in
426/// `black_box` so the compiler cannot make optimizations that would be unrealistic in real-life
427/// use.
428///
429/// ```
430/// use std::hint::black_box;
431///
432/// // This is a simple function that increments its input by 1. Note that it is pure, meaning it
433/// // has no side-effects. This function has no effect if its result is unused. (An example of a
434/// // function *with* side-effects is `println!()`.)
435/// fn increment(x: u8) -> u8 {
436/// x + 1
437/// }
438///
439/// // Here, we call `increment` but discard its result. The compiler, seeing this and knowing that
440/// // `increment` is pure, will eliminate this function call entirely. This may not be desired,
441/// // though, especially if we're trying to track how much time `increment` takes to execute.
442/// let _ = increment(black_box(5));
443///
444/// // Here, we force `increment` to be executed. This is because the compiler treats `black_box`
445/// // as if it has side-effects, and thus must compute its input.
446/// let _ = black_box(increment(black_box(5)));
447/// ```
448///
449/// There may be additional situations where you want to wrap the result of a function in
450/// `black_box` to force its execution. This is situational though, and may not have any effect
451/// (such as when the function returns a zero-sized type such as [`()` unit][unit]).
452///
453/// Note that `black_box` has no effect on how its input is treated, only its output. As such,
454/// expressions passed to `black_box` may still be optimized:
455///
456/// ```
457/// use std::hint::black_box;
458///
459/// // The compiler sees this...
460/// let y = black_box(5 * 10);
461///
462/// // ...as this. As such, it will likely simplify `5 * 10` to just `50`.
463/// let _0 = 5 * 10;
464/// let y = black_box(_0);
465/// ```
466///
467/// In the above example, the `5 * 10` expression is considered distinct from the `black_box` call,
468/// and thus is still optimized by the compiler. You can prevent this by moving the multiplication
469/// operation outside of `black_box`:
470///
471/// ```
472/// use std::hint::black_box;
473///
474/// // No assumptions can be made about either operand, so the multiplication is not optimized out.
475/// let y = black_box(5) * black_box(10);
476/// ```
477///
478/// During constant evaluation, `black_box` is treated as a no-op.
479#[inline]
480#[stable(feature = "bench_black_box", since = "1.66.0")]
481#[rustc_const_stable(feature = "const_black_box", since = "1.86.0")]
482pub const fn black_box<T>(dummy: T) -> T {
483 crate::intrinsics::black_box(dummy)
484}
485
486/// An identity function that causes an `unused_must_use` warning to be
487/// triggered if the given value is not used (returned, stored in a variable,
488/// etc) by the caller.
489///
490/// This is primarily intended for use in macro-generated code, in which a
491/// [`#[must_use]` attribute][must_use] either on a type or a function would not
492/// be convenient.
493///
494/// [must_use]: https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-must_use-attribute
495///
496/// # Example
497///
498/// ```
499/// #![feature(hint_must_use)]
500///
501/// use core::fmt;
502///
503/// pub struct Error(/* ... */);
504///
505/// #[macro_export]
506/// macro_rules! make_error {
507/// ($($args:expr),*) => {
508/// core::hint::must_use({
509/// let error = $crate::make_error(core::format_args!($($args),*));
510/// error
511/// })
512/// };
513/// }
514///
515/// // Implementation detail of make_error! macro.
516/// #[doc(hidden)]
517/// pub fn make_error(args: fmt::Arguments<'_>) -> Error {
518/// Error(/* ... */)
519/// }
520///
521/// fn demo() -> Option<Error> {
522/// if true {
523/// // Oops, meant to write `return Some(make_error!("..."));`
524/// Some(make_error!("..."));
525/// }
526/// None
527/// }
528/// #
529/// # // Make rustdoc not wrap the whole snippet in fn main, so that $crate::make_error works
530/// # fn main() {}
531/// ```
532///
533/// In the above example, we'd like an `unused_must_use` lint to apply to the
534/// value created by `make_error!`. However, neither `#[must_use]` on a struct
535/// nor `#[must_use]` on a function is appropriate here, so the macro expands
536/// using `core::hint::must_use` instead.
537///
538/// - We wouldn't want `#[must_use]` on the `struct Error` because that would
539/// make the following unproblematic code trigger a warning:
540///
541/// ```
542/// # struct Error;
543/// #
544/// fn f(arg: &str) -> Result<(), Error>
545/// # { Ok(()) }
546///
547/// #[test]
548/// fn t() {
549/// // Assert that `f` returns error if passed an empty string.
550/// // A value of type `Error` is unused here but that's not a problem.
551/// f("").unwrap_err();
552/// }
553/// ```
554///
555/// - Using `#[must_use]` on `fn make_error` can't help because the return value
556/// *is* used, as the right-hand side of a `let` statement. The `let`
557/// statement looks useless but is in fact necessary for ensuring that
558/// temporaries within the `format_args` expansion are not kept alive past the
559/// creation of the `Error`, as keeping them alive past that point can cause
560/// autotrait issues in async code:
561///
562/// ```
563/// # #![feature(hint_must_use)]
564/// #
565/// # struct Error;
566/// #
567/// # macro_rules! make_error {
568/// # ($($args:expr),*) => {
569/// # core::hint::must_use({
570/// # // If `let` isn't used, then `f()` produces a non-Send future.
571/// # let error = make_error(core::format_args!($($args),*));
572/// # error
573/// # })
574/// # };
575/// # }
576/// #
577/// # fn make_error(args: core::fmt::Arguments<'_>) -> Error {
578/// # Error
579/// # }
580/// #
581/// async fn f() {
582/// // Using `let` inside the make_error expansion causes temporaries like
583/// // `unsync()` to drop at the semicolon of that `let` statement, which
584/// // is prior to the await point. They would otherwise stay around until
585/// // the semicolon on *this* statement, which is after the await point,
586/// // and the enclosing Future would not implement Send.
587/// log(make_error!("look: {:p}", unsync())).await;
588/// }
589///
590/// async fn log(error: Error) {/* ... */}
591///
592/// // Returns something without a Sync impl.
593/// fn unsync() -> *const () {
594/// 0 as *const ()
595/// }
596/// #
597/// # fn test() {
598/// # fn assert_send(_: impl Send) {}
599/// # assert_send(f());
600/// # }
601/// ```
602#[unstable(feature = "hint_must_use", issue = "94745")]
603#[must_use] // <-- :)
604#[inline(always)]
605pub const fn must_use<T>(value: T) -> T {
606 value
607}
608
609/// Hints to the compiler that a branch condition is likely to be true.
610/// Returns the value passed to it.
611///
612/// It can be used with `if` or boolean `match` expressions.
613///
614/// When used outside of a branch condition, it may still influence a nearby branch, but
615/// probably will not have any effect.
616///
617/// It can also be applied to parts of expressions, such as `likely(a) && unlikely(b)`, or to
618/// compound expressions, such as `likely(a && b)`. When applied to compound expressions, it has
619/// the following effect:
620/// ```text
621/// likely(!a) => !unlikely(a)
622/// likely(a && b) => likely(a) && likely(b)
623/// likely(a || b) => a || likely(b)
624/// ```
625///
626/// See also the function [`cold_path()`] which may be more appropriate for idiomatic Rust code.
627///
628/// # Examples
629///
630/// ```
631/// #![feature(likely_unlikely)]
632/// use core::hint::likely;
633///
634/// fn foo(x: i32) {
635/// if likely(x > 0) {
636/// println!("this branch is likely to be taken");
637/// } else {
638/// println!("this branch is unlikely to be taken");
639/// }
640///
641/// match likely(x > 0) {
642/// true => println!("this branch is likely to be taken"),
643/// false => println!("this branch is unlikely to be taken"),
644/// }
645///
646/// // Use outside of a branch condition may still influence a nearby branch
647/// let cond = likely(x != 0);
648/// if cond {
649/// println!("this branch is likely to be taken");
650/// }
651/// }
652/// ```
653///
654///
655#[unstable(feature = "likely_unlikely", issue = "136873")]
656#[inline(always)]
657pub const fn likely(b: bool) -> bool {
658 crate::intrinsics::likely(b)
659}
660
661/// Hints to the compiler that a branch condition is unlikely to be true.
662/// Returns the value passed to it.
663///
664/// It can be used with `if` or boolean `match` expressions.
665///
666/// When used outside of a branch condition, it may still influence a nearby branch, but
667/// probably will not have any effect.
668///
669/// It can also be applied to parts of expressions, such as `likely(a) && unlikely(b)`, or to
670/// compound expressions, such as `unlikely(a && b)`. When applied to compound expressions, it has
671/// the following effect:
672/// ```text
673/// unlikely(!a) => !likely(a)
674/// unlikely(a && b) => a && unlikely(b)
675/// unlikely(a || b) => unlikely(a) || unlikely(b)
676/// ```
677///
678/// See also the function [`cold_path()`] which may be more appropriate for idiomatic Rust code.
679///
680/// # Examples
681///
682/// ```
683/// #![feature(likely_unlikely)]
684/// use core::hint::unlikely;
685///
686/// fn foo(x: i32) {
687/// if unlikely(x > 0) {
688/// println!("this branch is unlikely to be taken");
689/// } else {
690/// println!("this branch is likely to be taken");
691/// }
692///
693/// match unlikely(x > 0) {
694/// true => println!("this branch is unlikely to be taken"),
695/// false => println!("this branch is likely to be taken"),
696/// }
697///
698/// // Use outside of a branch condition may still influence a nearby branch
699/// let cond = unlikely(x != 0);
700/// if cond {
701/// println!("this branch is likely to be taken");
702/// }
703/// }
704/// ```
705#[unstable(feature = "likely_unlikely", issue = "136873")]
706#[inline(always)]
707pub const fn unlikely(b: bool) -> bool {
708 crate::intrinsics::unlikely(b)
709}
710
711/// Hints to the compiler that given path is cold, i.e., unlikely to be taken. The compiler may
712/// choose to optimize paths that are not cold at the expense of paths that are cold.
713///
714/// # Examples
715///
716/// ```
717/// #![feature(cold_path)]
718/// use core::hint::cold_path;
719///
720/// fn foo(x: &[i32]) {
721/// if let Some(first) = x.get(0) {
722/// // this is the fast path
723/// } else {
724/// // this path is unlikely
725/// cold_path();
726/// }
727/// }
728///
729/// fn bar(x: i32) -> i32 {
730/// match x {
731/// 1 => 10,
732/// 2 => 100,
733/// 3 => { cold_path(); 1000 }, // this branch is unlikely
734/// _ => { cold_path(); 10000 }, // this is also unlikely
735/// }
736/// }
737/// ```
738#[unstable(feature = "cold_path", issue = "136873")]
739#[inline(always)]
740pub const fn cold_path() {
741 crate::intrinsics::cold_path()
742}
743
744/// Returns either `true_val` or `false_val` depending on the value of
745/// `condition`, with a hint to the compiler that `condition` is unlikely to be
746/// correctly predicted by a CPU’s branch predictor.
747///
748/// This method is functionally equivalent to
749/// ```ignore (this is just for illustrative purposes)
750/// fn select_unpredictable<T>(b: bool, true_val: T, false_val: T) -> T {
751/// if b { true_val } else { false_val }
752/// }
753/// ```
754/// but might generate different assembly. In particular, on platforms with
755/// a conditional move or select instruction (like `cmov` on x86 or `csel`
756/// on ARM) the optimizer might use these instructions to avoid branches,
757/// which can benefit performance if the branch predictor is struggling
758/// with predicting `condition`, such as in an implementation of binary
759/// search.
760///
761/// Note however that this lowering is not guaranteed (on any platform) and
762/// should not be relied upon when trying to write cryptographic constant-time
763/// code. Also be aware that this lowering might *decrease* performance if
764/// `condition` is well-predictable. It is advisable to perform benchmarks to
765/// tell if this function is useful.
766///
767/// # Examples
768///
769/// Distribute values evenly between two buckets:
770/// ```
771/// use std::hash::BuildHasher;
772/// use std::hint;
773///
774/// fn append<H: BuildHasher>(hasher: &H, v: i32, bucket_one: &mut Vec<i32>, bucket_two: &mut Vec<i32>) {
775/// let hash = hasher.hash_one(&v);
776/// let bucket = hint::select_unpredictable(hash % 2 == 0, bucket_one, bucket_two);
777/// bucket.push(v);
778/// }
779/// # let hasher = std::collections::hash_map::RandomState::new();
780/// # let mut bucket_one = Vec::new();
781/// # let mut bucket_two = Vec::new();
782/// # append(&hasher, 42, &mut bucket_one, &mut bucket_two);
783/// # assert_eq!(bucket_one.len() + bucket_two.len(), 1);
784/// ```
785#[inline(always)]
786#[stable(feature = "select_unpredictable", since = "1.88.0")]
787pub fn select_unpredictable<T>(condition: bool, true_val: T, false_val: T) -> T {
788 // FIXME(https://github.com/rust-lang/unsafe-code-guidelines/issues/245):
789 // Change this to use ManuallyDrop instead.
790 let mut true_val = MaybeUninit::new(true_val);
791 let mut false_val = MaybeUninit::new(false_val);
792 // SAFETY: The value that is not selected is dropped, and the selected one
793 // is returned. This is necessary because the intrinsic doesn't drop the
794 // value that is not selected.
795 unsafe {
796 crate::intrinsics::select_unpredictable(!condition, &mut true_val, &mut false_val)
797 .assume_init_drop();
798 crate::intrinsics::select_unpredictable(condition, true_val, false_val).assume_init()
799 }
800}