Skip to content

Commit

Permalink
Add more branchless functions
Browse files Browse the repository at this point in the history
  • Loading branch information
daedalus committed Feb 1, 2024
1 parent d96d769 commit 396d536
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions branchless.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
Sgn = lambda a: 1 * (a > 0) + (-1) * (a <= 0)
Abs = lambda a: a * (a > 0) + (-a) * (a <= 0)

Min = lambda a,b: a * (a < b) + b * (b <= a)
Max = lambda a,b: a * (a > b) + b * (b >= a)

Expand All @@ -9,12 +12,16 @@


def test():
print(Min(3,10))
print(Max(3,10))
print(Upper("dario"))
print(Upper2("dario"))
print(Lower("DARIO"))
print(Lower2("DARIO"))
assert(Sgn(-3) == -1)
assert(Sgn(3) == 1)
assert(Abs(-3) == 3)
assert(Abs(3) == 3)
assert(Min(3,10) == 3)
assert(Max(3,10) == 10)
assert(Upper("dario"))
assert(Upper2("dario"))
assert(Lower("DARIO"))
assert(Lower2("DARIO"))


test()

0 comments on commit 396d536

Please sign in to comment.