@@ -597,3 +597,143 @@ pub const fn black_box<T>(dummy: T) -> T {
597
597
pub const fn must_use < T > ( value : T ) -> T {
598
598
value
599
599
}
600
+
601
+ /// Hints to the compiler that a branch condition is likely to be true.
602
+ /// Returns the value passed to it.
603
+ ///
604
+ /// It can be used with `if` or boolean `match` expressions.
605
+ ///
606
+ /// When used outside of a branch condition, it may still work if there is a branch close by, but
607
+ /// it is not guaranteed to have any effect.
608
+ ///
609
+ /// It can also be applied to parts of expressions, such as `likely(a) && unlikely(b)`, or to
610
+ /// compound expressions, such as `likely(a && b)`. When applied to compound expressions, it has
611
+ /// the following effect:
612
+ /// ```text
613
+ /// likely(!a) => !unlikely(a)
614
+ /// likely(a && b) => likely(a) && likely(b)
615
+ /// likely(a || b) => a || likely(b)
616
+ /// ```
617
+ ///
618
+ /// See also the function [`cold_path()`] which may be more appropriate for idiomatic Rust code.
619
+ ///
620
+ /// # Examples
621
+ ///
622
+ /// ```
623
+ /// #![feature(likely_unlikely)]
624
+ /// use core::hint::likely;
625
+ ///
626
+ /// fn foo(x: i32) {
627
+ /// if likely(x > 0) {
628
+ /// println!("this branch is likely to be taken");
629
+ /// } else {
630
+ /// println!("this branch is unlikely to be taken");
631
+ /// }
632
+ ///
633
+ /// match likely(x > 0) {
634
+ /// true => println!("this branch is likely to be taken"),
635
+ /// false => println!("this branch is unlikely to be taken"),
636
+ /// }
637
+ ///
638
+ /// // Use outside of a branch condition. This may still work if there is a branch close by,
639
+ /// // but it is not guaranteed to have any effect
640
+ /// let cond = likely(x != 0);
641
+ /// if cond {
642
+ /// println!("this branch is likely to be taken");
643
+ /// }
644
+ /// }
645
+ /// ```
646
+ ///
647
+ ///
648
+ #[ unstable( feature = "likely_unlikely" , issue = "26179" ) ]
649
+ #[ rustc_nounwind]
650
+ #[ inline( always) ]
651
+ pub const fn likely ( b : bool ) -> bool {
652
+ crate :: intrinsics:: likely ( b)
653
+ }
654
+
655
+ /// Hints to the compiler that a branch condition is unlikely to be true.
656
+ /// Returns the value passed to it.
657
+ ///
658
+ /// It can be used with `if` or boolean `match` expressions.
659
+ ///
660
+ /// When used outside of a branch condition, it may still work if there is a branch close by, but
661
+ /// it is not guaranteed to have any effect.
662
+ ///
663
+ /// It can also be applied to parts of expressions, such as `likely(a) && unlikely(b)`, or to
664
+ /// compound expressions, such as `unlikely(a && b)`. When applied to compound expressions, it has
665
+ /// the following effect:
666
+ /// ```text
667
+ /// unlikely(!a) => !likely(a)
668
+ /// unlikely(a && b) => a && unlikely(b)
669
+ /// unlikely(a || b) => unlikely(a) || unlikely(b)
670
+ /// ```
671
+ ///
672
+ /// See also the function [`cold_path()`] which may be more appropriate for idiomatic Rust code.
673
+ ///
674
+ /// # Examples
675
+ ///
676
+ /// ```
677
+ /// #![feature(likely_unlikely)]
678
+ /// use core::hint::unlikely;
679
+ ///
680
+ /// fn foo(x: i32) {
681
+ /// if unlikely(x > 0) {
682
+ /// println!("this branch is unlikely to be taken");
683
+ /// } else {
684
+ /// println!("this branch is likely to be taken");
685
+ /// }
686
+ ///
687
+ /// match unlikely(x > 0) {
688
+ /// true => println!("this branch is unlikely to be taken"),
689
+ /// false => println!("this branch is likely to be taken"),
690
+ /// }
691
+ ///
692
+ /// // Use outside of a branch condition. This may still work if there is a branch close by,
693
+ /// // but it is not guaranteed to have any effect
694
+ /// let cond = unlikely(x != 0);
695
+ /// if cond {
696
+ /// println!("this branch is likely to be taken");
697
+ /// }
698
+ /// }
699
+ /// ```
700
+ #[ unstable( feature = "likely_unlikely" , issue = "26179" ) ]
701
+ #[ rustc_nounwind]
702
+ #[ inline( always) ]
703
+ pub const fn unlikely ( b : bool ) -> bool {
704
+ crate :: intrinsics:: unlikely ( b)
705
+ }
706
+
707
+ /// Hints to the compiler that given path is cold, i.e., unlikely to be taken. The compiler may
708
+ /// choose to optimize paths that are not cold at the expense of paths that are cold.
709
+ ///
710
+ /// # Examples
711
+ ///
712
+ /// ```
713
+ /// #![feature(cold_path)]
714
+ /// use core::hint::cold_path;
715
+ ///
716
+ /// fn foo(x: &[i32]) {
717
+ /// if let Some(first) = x.get(0) {
718
+ /// // this is the fast path
719
+ /// } else {
720
+ /// // this path is unlikely
721
+ /// cold_path();
722
+ /// }
723
+ /// }
724
+ ///
725
+ /// fn bar(x: i32) -> i32 {
726
+ /// match x {
727
+ /// 1 => 10,
728
+ /// 2 => 100,
729
+ /// 3 => { cold_path(); 1000 }, // this branch is unlikely
730
+ /// _ => { cold_path(); 10000 }, // this is also unlikely
731
+ /// }
732
+ /// }
733
+ /// ```
734
+ #[ unstable( feature = "cold_path" , issue = "26179" ) ]
735
+ #[ rustc_nounwind]
736
+ #[ inline( always) ]
737
+ pub const fn cold_path ( ) {
738
+ crate :: intrinsics:: cold_path ( )
739
+ }
0 commit comments