Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test on i386 #141

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ jobs:
- run: echo "commit message doesn't contain '[skip ci]'"

test:
needs: before
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
nim-version:
- '1.4.8'
Copy link
Contributor

@dlesnoff dlesnoff Dec 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might want to drop support for 1.4.8.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't mind that, 1.4 has quite some bugs (#59, #93, #94) and doesn't receive backports anymore.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for dropping 1.4.x

It can also be part of a separate PR if you will. Don't forget to also bump minimal supported version in the .nimble file.

- '1.6.12'
- '1.6.16'
- '2.0.0'
- 'devel'
needs: before
steps:
- uses: actions/checkout@v4
- uses: jiro4989/setup-nim-action@v1
Expand All @@ -30,3 +32,30 @@ jobs:
- run: nimble install -y
- run: nimble test
- run: nimble checkExamples

i386:
needs: before
runs-on: ubuntu-latest
container:
image: i386/ubuntu
strategy:
fail-fast: false
matrix:
nim-version:
- '1.4.8'
- '1.6.16'
- '2.0.0'
steps:
- uses: actions/checkout@v1
- name: Setup
run: |
apt-get update -y
apt-get install -y curl gcc g++ git xz-utils
git config --global --add safe.directory /__w/bigints/bigints
- name: Install Nim
run: |
curl -sSf https://nim-lang.org/download/nim-${{ matrix.nim-version }}-linux_x32.tar.xz | tar -xJ
realpath nim-${{ matrix.nim-version }}/bin >> $GITHUB_PATH
- run: nimble install -y
- run: nimble test
- run: nimble checkExamples
1 change: 0 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ nimble install https://github.com/nim-lang/bigints

## Current limitations and possible enhancements

* not expected to work on 32 bit
* arithmetic operations such as addition, multiplication and division are not optimized for performance (e.g. [Karatsuba multiplication](https://en.wikipedia.org/wiki/Karatsuba_algorithm) is not implemented)


Expand Down
2 changes: 1 addition & 1 deletion src/bigints.nim
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func initBigInt*(vals: sink seq[uint32], isNegative = false): BigInt =
## Initializes a `BigInt` from a sequence of `uint32` values.
runnableExamples:
let a = @[10'u32, 2'u32].initBigInt
let b = 10 + 2 shl 32
let b = 10 + 2'u64 shl 32
assert $a == $b
result.limbs = vals
result.isNegative = isNegative
Expand Down
28 changes: 23 additions & 5 deletions src/bigints/random.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,43 @@ import std/sequtils
import std/options
import std/random

# workaround for https://github.com/nim-lang/Nim/issues/16360
proc rand(r: var Rand, max: uint64): uint64 =
if max == 0:
return 0
elif max == uint64.high:
return r.next()
else:
var iters = 0
while true:
let x = next(r)
# avoid `mod` bias
if x <= uint64.high - (uint64.high mod max) or iters > 20:
return x mod (max + 1)
else:
inc iters

func rand*(r: var Rand, x: Slice[BigInt]): BigInt =
## Return a random `BigInt`, within the given range, using the given state.
assert(x.a <= x.b, "invalid range")
if x.a == x.b:
return x.a
let
spread = x.b - x.a
# number of bits *not* including leading bit
nbits = spread.fastLog2
# number of limbs to generate completely randomly
nFullLimbs = max(nbits div 32 - 1, 0)
# highest possible value of the top two limbs.
hi64Max = (spread shr (nFullLimbs*32)).toInt[:uint64].get()
hi64Max = (spread shr (nFullLimbs * 32)).toInt[:uint64].get()
while true:
# these limbs can be generated completely arbitrarily
var limbs = newSeqWith(nFullLimbs, r.rand(uint32.low..uint32.high))
var limbs = newSeqWith(nFullLimbs, uint32(r.next() and uint32.high)) # work around https://github.com/nim-lang/Nim/issues/16360
# generate the top two limbs more carefully. This all but guarantees
# that the entire number is in the correct range
let hi64 = r.rand(uint64.low..hi64Max)
limbs.add(cast[uint32](hi64))
limbs.add(cast[uint32](hi64 shr 32))
let hi64 = r.rand(hi64Max)
limbs.add(uint32(hi64 and uint32.high))
limbs.add(uint32(hi64 shr 32))
Comment on lines +40 to +42
Copy link
Contributor

@dlesnoff dlesnoff Dec 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works by generating a 64-bit number. I am surprised that this works well on a 32-bit architecture.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why wouldn't it? 32 bit architectures support 64 bit numbers perfectly well, they're just slower (because they take up two memory locations).

result = initBigInt(limbs)
if result <= spread:
break
Expand Down
11 changes: 8 additions & 3 deletions tests/trandom.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@ block: # check uniformity
doAssert(lo <= r)
doAssert(r <= hi)
total += r
let iBucket = (r-lo) div ((hi-lo) div initBigInt(nbuckets))
let iBucket = (r - lo) div ((hi - lo) div initBigInt(nbuckets))
buckets[iBucket.toInt[:int]().get()] += 1
let expected = trials / nbuckets
for x in buckets:
doAssert(trials/nbuckets*0.5 < float(x))
doAssert(float(x) < trials/nbuckets*1.5)
doAssert(expected * 0.5 < float(x))
doAssert(float(x) < expected * 1.5)

block: # single element range
let x = 1234567890.initBigInt
for _ in 1..100:
doAssert rand(x..x) == x