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 2211735 commit 53e9210
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions branchless.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
clamp = lambda x, min_,max_: min(max(x, min_), max_)


def stringcompare(a,b):
tmp = 0
for i in range(0, len(a)):
tmp += a[i] != b[i]
return tmp == 0


def test():
assert(IsOdd(3) == True)
assert(IsOdd(2) == False)
Expand All @@ -40,6 +47,11 @@ def test():
assert(Upper2("dario"))
assert(Lower("DARIO"))
assert(Lower2("DARIO"))
assert(stringcompare("dario","dario") == True)
assert(stringcompare("dario","dariO") == False)
assert(clamp(4,10,15) == 10)
assert(clamp(14,10,15) == 14)
assert(clamp(24,10,15) == 15)


test()

0 comments on commit 53e9210

Please sign in to comment.