Code Monkey home page Code Monkey logo

nim-nint128's Introduction

Nint128, as the name suggests, is a package to work with integers, signed and unsigned, of 128 bits.

Currently the package implements 128-bit operations in pure Nim. Therefore, it is highly portable.

For optimizations, you can use, when possible, the C extension for 128-bit integers present in the GCC and CLANG compiler or VCC compiler intrinsics. For more information, see this here.

Status

The basic implementation is ready, but not fully tested. Visit the tests directory to see what has been tested and what remains to be tested.

Some things may change and break down in the future.

Install

nimble install nint128

or

nimble install https://github.com/rockcavera/nim-nint128.git

Basic Use

import pkg/nint128

const
  u999 = u128("0b1111100111") ## Defines a constant from a binary string.
  uHigh = u128("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF") ## Defines a constant from a hexadecimal string.
  i333 = i128("0b101001101") ## Defines a constant from a binary string.
  iHigh = i128("0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF") ## Defines a constant from a hexadecimal string.

## Comparisons `UInt128`
echo uHigh == high(UInt128)
echo uHigh != low(UInt128)
echo uHigh > zero(UInt128)
echo uHigh >= one(UInt128)
echo uHigh < ten(UInt128)
echo uHigh <= u999

## Comparisons `Int128`
echo iHigh == high(Int128)
echo iHigh != low(Int128)
echo iHigh > zero(Int128)
echo iHigh >= one(Int128)
echo iHigh < ten(Int128)
echo iHigh <= i333
echo isNegative(iHigh)

## Initialization of `UInt128` and `Int128` variables.
var
  a, b: UInt128
  c, d: Int128

a = u128(1234567890) ## Transforms the `int` `1234567890` into `UInt128`
b = u128("18446744073709551616") ## Transforms `string` `18446744073709551616` into `UInt128`

c = i128(1234567890) ## Transforms the `int` `-13579` into `Int128`
d = i128("-18446744073709551616") ## Transforms `string` `-18446744073709551616` to `Int128`

## Basic arithmetic `UInt128`
a = a + b
b = b - u128(9223372036854775808'u64)
a = a * u999
b = uHigh div a
b = uHigh mod a
(a, b) = divmod(uHigh, a)

## Basic arithmetic `Int128`
c = c + d
d = d - i333
c = c * i128("-3")
d = iHigh div c
d = iHigh mod c
(c, d) = divmod(iHigh, c)

## Bitwise operation `UInt128`
a = a and b
a = a or b
a = a xor b
a = not(a)
a = a shl 12
b = b shr 64

## Bitwise operation `Int128`
c = c and d
c = c or d
c = c xor d
c = not(c)
c = c shl 12
d = d shr 64

Documentation

https://rockcavera.github.io/nim-nint128/theindex.html

License

  • The file div2n1n.nim, in the directory src/nint128/vendor/stint/, is authored by Status Research & Development GmbH, part of the stint package.
  • The file endians2.nim, in the directory src/nint128/vendor/stew/, is authored by Status Research & Development GmbH, part of the stew package.

nim-nint128's People

Contributors

rockcavera avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

foxoman

nim-nint128's Issues

Incorrect integer returned

playground.zip
The attached file demonstrates this chunk of code:
var a: Int128
a = i128("1231231298371293217931712312312983712932179317")
echo(a)

Returns this number:
30447654189836100383415788052960652661

Here were the release flags used:
nimble build --mm:orc --threads:on -r -d:release

It appears there is a discrepancy.

Add support for JS backend

Hey there, I've made a library (NULID) and I wanted to implement support for the JS backend, but it seems like nint128 doesn't support the JS backend, but it does support the NimVM, so I'm wondering if it'd be possible to also support JS at the same time?

Consider placement into the Nim std library?

I'm halfway through writing a 128-bit decimal library for the Nim standard library (see nim-lang/RFCs#308) based on IEEE 754-2008's 128-bit specification.

Internally, it uses a 113-bit uint decimal base. So I've been working on a bad hack to make the core arithmetic work as I concentrate more on the serialization and conformance to spec.

But, looking at your code, I'd much prefer to use the work you have already done here for the arithmetic. Unfortunately, that wouldn't work if nim-nint128 is an external library.

So, I selfishly ask: would you be interested in migrating your work to Nim's internal std library?

Disclaimer: I've been on a 6+ month hiatus on my decimal project. I won't be picking it back up till 2Q2022 as I've got too many customers at the moment.

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.