u128.js: 128 bit integer math library for Javascript

2011-06-03 by Luigi Galli

Introduction

A Javascript library for 128 bit unsigned integer arithmetic. The current release (0.5.0 BETA) includes:
  • Conversion to/from hex string
  • Add / Sub / Mul /Div / Mod / Cmp
  • And / Or / Xor / Not / Shl / Shr
  • A handful of other functions, see the docs below

Compatiblity

Unit tests for the current release have successfully run on:
  • Safari 5
  • Chrome 11
  • Firefox 3.6
  • Opera 9.64 and 11.11
  • IE 6.0.2800.1106, IE 7 and IE 8
  • Opera/507 Nintendo DSI
  • NetFront 3.4

Performance

So far, no particular effort was made to optimize the library for performance and there is no specifically designed benchmark yet. As a rough indication: on an old intel iMac 2.8GHz the test suite runs in under 0.1s on Chrome 11 and in around ~0.4s on Firefox 3.6.

Downloads

License

No license. The library is released as public domain without any warranty.

Documentation and examples

The below example code illustrates all currently supported functions

/Volumes/wdata/WSDIR/w/4E71.ORG/WWW/b/snip/demo.html.html <script type="text/javascript" src="u128.js" ></script>
<script type="text/javascript">
        
        /*
        Namespace shorthand
        */
        var u = faultylabs.u128;
        
        /*
        Conversions to/from hex string:
        */
        var a    = u.hexval("000000bbaaaaaaaa33333333ffffffff")
        var astr = u.hex$(a)
        alert( astr == "000000bbaaaaaaaa33333333ffffffff" )
        
        /*
        Addition a + b
        */
        var a = u.hexval("1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")
        var b = u.hexval("0000000000000000000000000000001")
        var c = u.add(a, b)
        alert(u.hex$(c) == "02000000000000000000000000000000")

        /*
        Subtraction a - b
        */
        var a = u.hexval("b0000000000000000000000000000000")
        var b = u.hexval("00000000000000000000000000000001")
        var c = u.sub(a, b)
        alert(u.hex$(c) == "afffffffffffffffffffffffffffffff")

        /*
        Multiplication a * b
        */
        var a = u.hexval("78cd2bbafbd21e3") 
        var b = u.hexval("de36a45d123b885") 
        var c = u.mul(a, b)
        alert(u.hex$(c) == "0068dbb4c2baf8856f0df18732a6c2ef")

        /*
        Quotient a / b
        */
        var a = u.hexval("0068dbb4c2baf8856f0df18732a6c2ef") 
        var b = u.hexval("de36a45d123b885") 
        var c = u.div(a, b)
        alert(u.hex$(c) == "0000000000000000078cd2bbafbd21e3")

        /*
        Remainder a % b
        */
        var a = u.hexval("00000521aaaaaaa76666666bfffffff9") 
        var b = u.hexval("00000521aaaaaaa00000000000000000") 
        var c = u.mod(a, b)
        alert(u.hex$(c) == "00000000000000076666666bfffffff9")
        
        /* Comparison of a and b
          0 -> a == b
          1 -> a > b
         -1 -> a < b
        */
        var a = u.hexval("de36a45d123b885") 
        var b = u.hexval("68dbb4c2baf8856f0df18732a6c2ef") 
        var cmp = u.cmp(a, b)
        alert(cmp == -1)

        /*
        Binary AND  a & b
        Binary OR   a | b
        Binary XOR  a ^ b
        Binary NOT  ~a
        */
        var v55  = u.hexval("55555555555555555555555555555555") 
        var vAA  = u.hexval("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA") 
        var vFF  = u.hexval("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF") 
        var vOR  = u.or (v55, vAA)
        var vAND = u.and(vFF, v55)
        var vXOR = u.xor(vFF, v55)
        var vNOT = u.not(v55)
        alert(u.hex$(vOR)  == "ffffffffffffffffffffffffffffffff")
        alert(u.hex$(vAND) == "55555555555555555555555555555555")
        alert(u.hex$(vXOR) == "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
        alert(u.hex$(vNOT) == "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")

        /*
        Left shift
        */
        var a  = u.hexval("00BBAAAAAAAA33333333FFFFFFFF")
        var la = u.shl(a, 8)
        alert(u.hex$(la) == "0000bbaaaaaaaa33333333ffffffff00")

        /*
        Right shift
        */
        var a  = u.hexval("0000BBAAAAAAAA33333333FFFFFFFF11")
        var ra = u.shr(a, 8)
        alert(u.hex$(ra) == "000000bbaaaaaaaa33333333ffffffff")
        
        /*
        Bit test
        */
        var a   = u.hexval("10000000000000000000000")
        var bit = u.btest(a, 88)
        alert(bit == 1)

        /*
        Shortcuts for the values 1 and 0
        */
        var a   = u.zero()
        var b   = u.one()
        alert( 
                u.hex$(a) == "00000000000000000000000000000000" &&
                u.hex$(b) == "00000000000000000000000000000001"
        )
        
</script>

Feedback

Feedback, bug-reports, suggestions etc. are most welcome. Mail me at LG@4e71.org