|
|
@@ -264,3 +264,39 @@ pub const SUCCESS: u64 = 0;
|
|
|
|
|
|
/// The result of a program execution.
|
|
|
pub type ProgramResult = Result<(), program_error::ProgramError>;
|
|
|
+
|
|
|
+/// Module with functions to provide hints to the compiler about how code
|
|
|
+/// should be optimized.
|
|
|
+pub mod hint {
|
|
|
+ /// A "dummy" function with a hint to the compiler that it is unlikely to be
|
|
|
+ /// called.
|
|
|
+ ///
|
|
|
+ /// This function is used as a hint to the compiler to optimize other code paths
|
|
|
+ /// instead of the one where the function is used.
|
|
|
+ #[cold]
|
|
|
+ pub const fn cold_path() {}
|
|
|
+
|
|
|
+ /// Return the given `bool` value with a hint to the compiler that `true` is the
|
|
|
+ /// likely case.
|
|
|
+ #[inline(always)]
|
|
|
+ pub const fn likely(b: bool) -> bool {
|
|
|
+ if b {
|
|
|
+ true
|
|
|
+ } else {
|
|
|
+ cold_path();
|
|
|
+ false
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// Return a given `bool` value with a hint to the compiler that `false` is the
|
|
|
+ /// likely case.
|
|
|
+ #[inline(always)]
|
|
|
+ pub const fn unlikely(b: bool) -> bool {
|
|
|
+ if b {
|
|
|
+ cold_path();
|
|
|
+ true
|
|
|
+ } else {
|
|
|
+ false
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|