// Integer comparisons compile to a direct compare-and-branch when the // operands are provably small, with a fast path and the general code // (big integers) behind it otherwise (issue #191): every operator, on // small, negative, boundary and big values, agrees on both backends. import std/io import std/list fn power :: b: Int e: Int -> Int { if e == 0 { 1 } else { b * power b (e - 1) } } // `n`'s callers pass small and big values alike: nothing is proven, so // both the fast path and the general one run. fn classify :: n: Int -> String { if n < 0 { "neg" } else { if n == 0 { "zero" } else { if n <= 4611686018427387903 { "small" } else { "big" } } } } fn compare_all :: x: Int y: Int -> String { show (x < y) <> " " <> show (x <= y) <> " " <> show (x == y) <> " " <> show (x != y) <> " " <> show (x >= y) <> " " <> show (x > y) } pub fn main :: -> () / { let max = 4611686018427387903 let min = -4611686018427387904 let big = power 2 70 list.each [0, 1, -1, 42, -42, max, min, max + 1, min - 1, big, -big] fn x { io.println (classify x) } // Boundary pairs: equal, one apart, straddling the small range. list.each [ (0, 0) , (max, max) , (max, max + 1) , (max + 1, max) , (min, min - 1) , (min - 1, min) , (big, big) , (-big, big) , (-1, 1) ] fn p { io.println (compare_all p.0 p.1) } // A comparison's result matched on, and one in a loop's guard. match big - big == 0 { True -> io.println "difference is small", False -> io.println "no" } io.println (show (count_by 0 10)) } fn count_by :: i: Int n: Int -> Int { if i <= n { count_by (i + 3) n } else { i } } //> zero //> small //> neg //> small //> neg //> small //> neg //> big //> neg //> big //> neg //> False True True False True False //> False True True False True False //> True True False True False False //> False False False True True True //> False False False True True True //> True True False True False False //> False True True False True False //> True True False True False False //> True True False True False False //> difference is small //> 12