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