synalysis / elm-uint64

64-bit unsigned integer with division

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

This is a republished version of malaire/elm-uint64 which has been deleted. This new package is unmaintained and is purely meant for unblocking people who depend on it.

elm-uint64

64-bit unsigned integer using wrapping overflow.

import UInt64

-- `0 - 0xFF` wraps around to `0xFFFFFFFFFFFFFF01`
UInt64.sub UInt64.zero (UInt64.fromInt 0xFF)
    |> UInt64.toHexString
    --> "FFFFFFFFFFFFFF01"

-- ( 0xFFFFFFFFFFFFFFFF / 1e10, 0xFFFFFFFFFFFFFFFF % 1e10 )
UInt64.divMod UInt64.maxValue (UInt64.floor 1e10)
    |> Tuple.mapBoth UInt64.toFloat UInt64.toFloat
    --> ( 1844674407, 3709551615 )

UInt64.fromInt32s 0x11223344 0xAABBCCDD
    |> UInt64.rotateRightBy 20
    |> UInt64.toHexString
    --> "BCCDD11223344AAB"

Includes many conversion functions, for example converting UInt64 to String.

import UInt64
import UInt64.Digits as Digits

UInt64.fromInt 0x10F
    |> UInt64.toDigits Digits.binary
    |> Digits.padToPowerOfTwo '0'
    |> Digits.groupToString 4 ' '
    --> "0000 0001 0000 1111"

Changelog

Design Goals

Low-level

Includes relatively low-level functions. High-level functions using these low-level functions would then belong to other packages which depend on UInt64.

For example isPrime wouldn't belong to this package, but if someone needs add-with-carry or multiply-accumulate🢅 , those can be considered.

If you need a function which you think should be included, please create an issue🢅 mentioning use case for the function.

Fast

As fast as feasible, given the chosen implementation of three 24-bit unsigned integers. This goal is balanced with code complexity.

Safe Enough

Invalid arguments

Handling of invalid arguments is balanced with goal of speed. See Argument handling.

Testing

Every function, no matter how trivial, has at least one full-range fuzz test.

More tests are added as seemed necessary.

Performance

Here are some quick benchmark results. As usual I'm not interested in proper benchmarking, so take these with 2^64 grains of salt:

                                      runs / second
pow   : 64-bit number ^ 64-bit number: ~    200 000
pow   : 64-bit number ^ 251          : ~  1 500 000
divMod: 64-bit number / 40-bit number: ~  3 000 000
pow   : 64-bit number ^ 16           : >  8 000 000
divMod: 64-bit number / 29-bit number: > 10 000 000
add, sub, mul                        : > 20 000 000
bitwise shift/rotate by 1 bit        : > 25 000 000

Benchmarking was done 2020-06-26 with version 1.1.0, elm make --optimize, Elm 0.19.1, Firefox 68, Debian Linux and Core i5 3570K 3.4 GHz.

Some example benchmarks are included in benchmarks directory of source.

About

64-bit unsigned integer with division

License:MIT License


Languages

Language:Elm 100.0%