-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package day248 | ||
|
||
// MaxIf is a typical max function using an if branch. | ||
func MaxIf(a, b int64) int64 { | ||
if a > b { | ||
return a | ||
} | ||
return b | ||
} | ||
|
||
// MaxSubtractAndShift finds the max using subtraction | ||
// and right shifting. | ||
func MaxSubtractAndShift(a, b int64) int64 { | ||
return a - ((a - b) & ((a - b) >> 63)) | ||
} | ||
|
||
// MaxXor uses xor and branching to calculate the max. | ||
// Go requires if branching to convert a bool to an int64. | ||
// NOTE: No benefit here due to branching if, might as well use MaxIf. | ||
// Benchmark perf shows actually worse than MaxIf as expected. | ||
func MaxXor(a, b int64) int64 { | ||
bit := int64(0) | ||
if a < b { | ||
bit = int64(-1) | ||
} | ||
return a ^ ((a ^ b) & bit) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package day248 | ||
|
||
import "testing" | ||
|
||
// nolint | ||
var testcases = []struct { | ||
a, b, max int64 | ||
}{ | ||
{10, 20, 20}, | ||
{20, 1, 20}, | ||
{30, 2, 30}, | ||
{1, -2, 1}, | ||
} | ||
|
||
func TestMaxIf(t *testing.T) { | ||
t.Parallel() | ||
for _, tc := range testcases { | ||
if max := MaxIf(tc.a, tc.b); max != tc.max { | ||
t.Errorf("Expected %v, got %v", tc.max, max) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkMaxIf(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
MaxIf(tc.a, tc.b) | ||
} | ||
} | ||
} | ||
|
||
func TestMaxSubtractAndShift(t *testing.T) { | ||
t.Parallel() | ||
for _, tc := range testcases { | ||
if max := MaxSubtractAndShift(tc.a, tc.b); max != tc.max { | ||
t.Errorf("Expected %v, got %v", tc.max, max) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkMaxSubtractAndShift(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
MaxSubtractAndShift(tc.a, tc.b) | ||
} | ||
} | ||
} | ||
|
||
func TestMaxXor(t *testing.T) { | ||
t.Parallel() | ||
for _, tc := range testcases { | ||
if max := MaxXor(tc.a, tc.b); max != tc.max { | ||
t.Errorf("Expected %v, got %v", tc.max, max) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkMaxXor(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
MaxXor(tc.a, tc.b) | ||
} | ||
} | ||
} |