Skip to content

Commit

Permalink
internal: get rid of architecture specific constants (#191)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericlagergren authored Apr 11, 2024
1 parent 93d5865 commit 00de7ca
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 14 deletions.
14 changes: 14 additions & 0 deletions internal/c/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@ import (
"math/big"
)

const (
is64bit = 1 << (^uintptr(0) >> 63) / 2 // 0 or 1
is32bit = is64bit &^ 1 // 0 or 1

maxScale32 = 425000000
maxScaleInf32 = 1000000001

maxScale64 = 999999999999999999
maxScaleInf64 = 2000000000000000001

MaxScale = maxScale32*is32bit + maxScale64*is64bit
MaxScaleInf = maxScaleInf32*is32bit + maxScaleInf64*is64bit
)

const Inflated uint64 = math.MaxUint64

var (
Expand Down
7 changes: 0 additions & 7 deletions internal/c/const32.go

This file was deleted.

7 changes: 0 additions & 7 deletions internal/c/const64.go

This file was deleted.

37 changes: 37 additions & 0 deletions internal/c/const_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package c

import (
"math/bits"
"testing"
)

func TestMaxScale(t *testing.T) {
check := func(t *testing.T, max int64, maxInf int64) {
if MaxScale != max {
t.Fatalf("MaxScale: got %d, expected %d", MaxScale, max)
}
if MaxScaleInf != maxInf {
t.Fatalf("MaxScaleInf: got %d, expected %d", MaxScaleInf, maxInf)
}
}
switch n := bits.UintSize; n {
case 32:
if is32bit != 1 {
t.Fatalf("is32bit: got %d, expected %d", is32bit, 1)
}
if is64bit != 0 {
t.Fatalf("is64bit: got %d, expected %d", is64bit, 0)
}
check(t, maxScale32, maxScaleInf32)
case 64:
if is32bit != 0 {
t.Fatalf("is32bit: got %d, expected %d", is32bit, 0)
}
if is64bit != 1 {
t.Fatalf("is64bit: got %d, expected %d", is64bit, 1)
}
check(t, maxScale64, maxScaleInf64)
default:
t.Fatalf("unknown bit size: %d", n)
}
}

0 comments on commit 00de7ca

Please sign in to comment.