Giter Club home page Giter Club logo

elm-int64's Introduction

elm-int64

An efficient 64-bit integer with correct overflow.

Bitwise operators in javascript can only use 32 bits. Sometimes, external protocols use 64-bit integers. This package implementes such integers with "correct" overflow behavior.

This is a low-level package focussed on speed. The 64-bit integers are represented as a 2-tuple of 32-bit numbers. There are helpers for conversion to string and elm/bytes Bytes.

import Int64 

Int64.add (Int64.fromInt 42) (Int64.fromInt 10)
    |> Int64.toUnsignedString
    --> "52"

Int64.subtract (Int64.fromInt 10) (Int64.fromInt 42)
    |> Int64.toSignedString
    --> "-32"

Int64.xor 
    (Int64.fromInt32s 0xDEADBEEF 0xBAAAAAAD) 
    (Int64.fromInt 42)
    |> Int64.toHex
    --> "deadbeefbaaaaa87"

Performance

Performance is roughly half for subtraction, addition and the bitwise operators. That is great considering int64 is really two 32-bit integers, so we're doing operations twice.

Reliability

This package is extensively tested with fuzz tests. Nonetheless, the logic is extremely tricky, so there might still be bugs.

Missing functionality

  1. Int64 -> Int

    This is unsafe, because Int cannot store all the information of a Int64. If you think the conversion is safe for you, use:

    Int64.fromInt 42
        |> Int64.toSignedString
        |> String.toInt
        --> Just 42
  2. String -> Int64

    And by extension converting 64-bit integers in e.g. JSON into Int64. This is tricky to implement, but PRs are welcome!

  3. Multiplication, integer division, modulo

    Would be nice to have, again PRs are welcome!

elm-int64's People

Contributors

folkertdev avatar martinsstewart avatar minibill avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

dullbananas

elm-int64's Issues

Conversion from int does not respect the JS safe range as documented

I am having some trouble working with this library. Conversion from int seems to break down for large numbers, even when the number is within the JS safe range.

Take a look at the following code: https://ellie-app.com/mMqXP4NnLsNa1
The value given to all of those operations is lower than MAX_SAFE_INTEGER, but the result is still not what was put in when going back to string through Int64.

The documentation for Int64.fromInt says it is guaranteed to work for integers in the safe JS range. Yet, this is not the behavior I'm seeing.

Or maybe toSignedString and/or toUnsignedString are broken? I don't know.

Left shift by 0 adds the lower 32 bit number to the higher number

Int64.fromInt32s 4 18 |> Int64.toHex -- "0000000400000012"

Int64.fromInt32s 4 18 |> Int64.shiftLeftBy 0 |> Int64.toHex -- "0000001600000012"

Int64.rotateLeftBy 0 does behave as expected though.

(thanks for the package though - will be useful to me once bugs ironed out)

Expose way to access the two wrapped ints

I'm currently trying to add int64 encoders and decoders to elm-protocol-buffers
and I'm struggling to implement the VarInt encoding.
The int32 version currently converts the number to base 128, right shifts and continues in a loop.

This is the code:

toVarIntEncoders : Int -> List Encode.Encoder
toVarIntEncoders value =
    let
        base128 =
            Bitwise.and 0x7F value

        higherBits =
            Bitwise.shiftRightZfBy 7 value
    in
    if higherBits /= 0x00 then
        Encode.unsignedInt8 (Bitwise.or 0x80 base128) :: toVarIntEncoders higherBits

    else
        [ Encode.unsignedInt8 base128 ]

I cannot do this with Int64, since the inner values are not exposed.
Using Int64.encode instead of Encode.unsignedInt8 has the correct first two bytes but too many zeros afterwards.

xor can make internal integers negative

Here is an example:

> minusOne = Int64.fromInt32s 0 -1
Int64 0 4294967295 : Int64.Int64
> xored = Int64.xor (Int64.fromInt 0) minusOne
Int64 0 -1 : Int64.Int64

The issue is that other operations do not work as expected anymore - comparing does not return EQ and == returns False.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.