"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/cuint/build/uint64.js" (11 Apr 2017, 15254 Bytes) of package /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /**
    2     C-like unsigned 64 bits integers in Javascript
    3     Copyright (C) 2013, Pierre Curto
    4     MIT license
    5  */
    6 ;(function (root) {
    7 
    8     // Local cache for typical radices
    9     var radixPowerCache = {
   10         16: UINT64( Math.pow(16, 5) )
   11     ,   10: UINT64( Math.pow(10, 5) )
   12     ,   2:  UINT64( Math.pow(2, 5) )
   13     }
   14     var radixCache = {
   15         16: UINT64(16)
   16     ,   10: UINT64(10)
   17     ,   2:  UINT64(2)
   18     }
   19 
   20     /**
   21      *  Represents an unsigned 64 bits integer
   22      * @constructor
   23      * @param {Number} first low bits (8)
   24      * @param {Number} second low bits (8)
   25      * @param {Number} first high bits (8)
   26      * @param {Number} second high bits (8)
   27      * or
   28      * @param {Number} low bits (32)
   29      * @param {Number} high bits (32)
   30      * or
   31      * @param {String|Number} integer as a string        | integer as a number
   32      * @param {Number|Undefined} radix (optional, default=10)
   33      * @return 
   34      */
   35     function UINT64 (a00, a16, a32, a48) {
   36         if ( !(this instanceof UINT64) )
   37             return new UINT64(a00, a16, a32, a48)
   38 
   39         this.remainder = null
   40         if (typeof a00 == 'string')
   41             return fromString.call(this, a00, a16)
   42 
   43         if (typeof a16 == 'undefined')
   44             return fromNumber.call(this, a00)
   45 
   46         fromBits.apply(this, arguments)
   47     }
   48 
   49     /**
   50      * Set the current _UINT64_ object with its low and high bits
   51      * @method fromBits
   52      * @param {Number} first low bits (8)
   53      * @param {Number} second low bits (8)
   54      * @param {Number} first high bits (8)
   55      * @param {Number} second high bits (8)
   56      * or
   57      * @param {Number} low bits (32)
   58      * @param {Number} high bits (32)
   59      * @return ThisExpression
   60      */
   61     function fromBits (a00, a16, a32, a48) {
   62         if (typeof a32 == 'undefined') {
   63             this._a00 = a00 & 0xFFFF
   64             this._a16 = a00 >>> 16
   65             this._a32 = a16 & 0xFFFF
   66             this._a48 = a16 >>> 16
   67             return this
   68         }
   69 
   70         this._a00 = a00 | 0
   71         this._a16 = a16 | 0
   72         this._a32 = a32 | 0
   73         this._a48 = a48 | 0
   74 
   75         return this
   76     }
   77     UINT64.prototype.fromBits = fromBits
   78 
   79     /**
   80      * Set the current _UINT64_ object from a number
   81      * @method fromNumber
   82      * @param {Number} number
   83      * @return ThisExpression
   84      */
   85     function fromNumber (value) {
   86         this._a00 = value & 0xFFFF
   87         this._a16 = value >>> 16
   88         this._a32 = 0
   89         this._a48 = 0
   90 
   91         return this
   92     }
   93     UINT64.prototype.fromNumber = fromNumber
   94 
   95     /**
   96      * Set the current _UINT64_ object from a string
   97      * @method fromString
   98      * @param {String} integer as a string
   99      * @param {Number} radix (optional, default=10)
  100      * @return ThisExpression
  101      */
  102     function fromString (s, radix) {
  103         radix = radix || 10
  104 
  105         this._a00 = 0
  106         this._a16 = 0
  107         this._a32 = 0
  108         this._a48 = 0
  109 
  110         /*
  111             In Javascript, bitwise operators only operate on the first 32 bits 
  112             of a number, even though parseInt() encodes numbers with a 53 bits 
  113             mantissa.
  114             Therefore UINT64(<Number>) can only work on 32 bits.
  115             The radix maximum value is 36 (as per ECMA specs) (26 letters + 10 digits)
  116             maximum input value is m = 32bits as 1 = 2^32 - 1
  117             So the maximum substring length n is:
  118             36^(n+1) - 1 = 2^32 - 1
  119             36^(n+1) = 2^32
  120             (n+1)ln(36) = 32ln(2)
  121             n = 32ln(2)/ln(36) - 1
  122             n = 5.189644915687692
  123             n = 5
  124          */
  125         var radixUint = radixPowerCache[radix] || new UINT64( Math.pow(radix, 5) )
  126 
  127         for (var i = 0, len = s.length; i < len; i += 5) {
  128             var size = Math.min(5, len - i)
  129             var value = parseInt( s.slice(i, i + size), radix )
  130             this.multiply(
  131                     size < 5
  132                         ? new UINT64( Math.pow(radix, size) )
  133                         : radixUint
  134                 )
  135                 .add( new UINT64(value) )
  136         }
  137 
  138         return this
  139     }
  140     UINT64.prototype.fromString = fromString
  141 
  142     /**
  143      * Convert this _UINT64_ to a number (last 32 bits are dropped)
  144      * @method toNumber
  145      * @return {Number} the converted UINT64
  146      */
  147     UINT64.prototype.toNumber = function () {
  148         return (this._a16 * 65536) + this._a00
  149     }
  150 
  151     /**
  152      * Convert this _UINT64_ to a string
  153      * @method toString
  154      * @param {Number} radix (optional, default=10)
  155      * @return {String} the converted UINT64
  156      */
  157     UINT64.prototype.toString = function (radix) {
  158         radix = radix || 10
  159         var radixUint = radixCache[radix] || new UINT64(radix)
  160 
  161         if ( !this.gt(radixUint) ) return this.toNumber().toString(radix)
  162 
  163         var self = this.clone()
  164         var res = new Array(64)
  165         for (var i = 63; i >= 0; i--) {
  166             self.div(radixUint)
  167             res[i] = self.remainder.toNumber().toString(radix)
  168             if ( !self.gt(radixUint) ) break
  169         }
  170         res[i-1] = self.toNumber().toString(radix)
  171 
  172         return res.join('')
  173     }
  174 
  175     /**
  176      * Add two _UINT64_. The current _UINT64_ stores the result
  177      * @method add
  178      * @param {Object} other UINT64
  179      * @return ThisExpression
  180      */
  181     UINT64.prototype.add = function (other) {
  182         var a00 = this._a00 + other._a00
  183 
  184         var a16 = a00 >>> 16
  185         a16 += this._a16 + other._a16
  186 
  187         var a32 = a16 >>> 16
  188         a32 += this._a32 + other._a32
  189 
  190         var a48 = a32 >>> 16
  191         a48 += this._a48 + other._a48
  192 
  193         this._a00 = a00 & 0xFFFF
  194         this._a16 = a16 & 0xFFFF
  195         this._a32 = a32 & 0xFFFF
  196         this._a48 = a48 & 0xFFFF
  197 
  198         return this
  199     }
  200 
  201     /**
  202      * Subtract two _UINT64_. The current _UINT64_ stores the result
  203      * @method subtract
  204      * @param {Object} other UINT64
  205      * @return ThisExpression
  206      */
  207     UINT64.prototype.subtract = function (other) {
  208         return this.add( other.clone().negate() )
  209     }
  210 
  211     /**
  212      * Multiply two _UINT64_. The current _UINT64_ stores the result
  213      * @method multiply
  214      * @param {Object} other UINT64
  215      * @return ThisExpression
  216      */
  217     UINT64.prototype.multiply = function (other) {
  218         /*
  219             a = a00 + a16 + a32 + a48
  220             b = b00 + b16 + b32 + b48
  221             a*b = (a00 + a16 + a32 + a48)(b00 + b16 + b32 + b48)
  222                 = a00b00 + a00b16 + a00b32 + a00b48
  223                 + a16b00 + a16b16 + a16b32 + a16b48
  224                 + a32b00 + a32b16 + a32b32 + a32b48
  225                 + a48b00 + a48b16 + a48b32 + a48b48
  226 
  227             a16b48, a32b32, a48b16, a48b32 and a48b48 overflow the 64 bits
  228             so it comes down to:
  229             a*b = a00b00 + a00b16 + a00b32 + a00b48
  230                 + a16b00 + a16b16 + a16b32
  231                 + a32b00 + a32b16
  232                 + a48b00
  233                 = a00b00
  234                 + a00b16 + a16b00
  235                 + a00b32 + a16b16 + a32b00
  236                 + a00b48 + a16b32 + a32b16 + a48b00
  237          */
  238         var a00 = this._a00
  239         var a16 = this._a16
  240         var a32 = this._a32
  241         var a48 = this._a48
  242         var b00 = other._a00
  243         var b16 = other._a16
  244         var b32 = other._a32
  245         var b48 = other._a48
  246 
  247         var c00 = a00 * b00
  248 
  249         var c16 = c00 >>> 16
  250         c16 += a00 * b16
  251         var c32 = c16 >>> 16
  252         c16 &= 0xFFFF
  253         c16 += a16 * b00
  254 
  255         c32 += c16 >>> 16
  256         c32 += a00 * b32
  257         var c48 = c32 >>> 16
  258         c32 &= 0xFFFF
  259         c32 += a16 * b16
  260         c48 += c32 >>> 16
  261         c32 &= 0xFFFF
  262         c32 += a32 * b00
  263 
  264         c48 += c32 >>> 16
  265         c48 += a00 * b48
  266         c48 &= 0xFFFF
  267         c48 += a16 * b32
  268         c48 &= 0xFFFF
  269         c48 += a32 * b16
  270         c48 &= 0xFFFF
  271         c48 += a48 * b00
  272 
  273         this._a00 = c00 & 0xFFFF
  274         this._a16 = c16 & 0xFFFF
  275         this._a32 = c32 & 0xFFFF
  276         this._a48 = c48 & 0xFFFF
  277 
  278         return this
  279     }
  280 
  281     /**
  282      * Divide two _UINT64_. The current _UINT64_ stores the result.
  283      * The remainder is made available as the _remainder_ property on
  284      * the _UINT64_ object. It can be null, meaning there are no remainder.
  285      * @method div
  286      * @param {Object} other UINT64
  287      * @return ThisExpression
  288      */
  289     UINT64.prototype.div = function (other) {
  290         if ( (other._a16 == 0) && (other._a32 == 0) && (other._a48 == 0) ) {
  291             if (other._a00 == 0) throw Error('division by zero')
  292 
  293             // other == 1: this
  294             if (other._a00 == 1) {
  295                 this.remainder = new UINT64(0)
  296                 return this
  297             }
  298         }
  299 
  300         // other > this: 0
  301         if ( other.gt(this) ) {
  302             this.remainder = this.clone()
  303             this._a00 = 0
  304             this._a16 = 0
  305             this._a32 = 0
  306             this._a48 = 0
  307             return this
  308         }
  309         // other == this: 1
  310         if ( this.eq(other) ) {
  311             this.remainder = new UINT64(0)
  312             this._a00 = 1
  313             this._a16 = 0
  314             this._a32 = 0
  315             this._a48 = 0
  316             return this
  317         }
  318 
  319         // Shift the divisor left until it is higher than the dividend
  320         var _other = other.clone()
  321         var i = -1
  322         while ( !this.lt(_other) ) {
  323             // High bit can overflow the default 16bits
  324             // Its ok since we right shift after this loop
  325             // The overflown bit must be kept though
  326             _other.shiftLeft(1, true)
  327             i++
  328         }
  329 
  330         // Set the remainder
  331         this.remainder = this.clone()
  332         // Initialize the current result to 0
  333         this._a00 = 0
  334         this._a16 = 0
  335         this._a32 = 0
  336         this._a48 = 0
  337         for (; i >= 0; i--) {
  338             _other.shiftRight(1)
  339             // If shifted divisor is smaller than the dividend
  340             // then subtract it from the dividend
  341             if ( !this.remainder.lt(_other) ) {
  342                 this.remainder.subtract(_other)
  343                 // Update the current result
  344                 if (i >= 48) {
  345                     this._a48 |= 1 << (i - 48)
  346                 } else if (i >= 32) {
  347                     this._a32 |= 1 << (i - 32)
  348                 } else if (i >= 16) {
  349                     this._a16 |= 1 << (i - 16)
  350                 } else {
  351                     this._a00 |= 1 << i
  352                 }
  353             }
  354         }
  355 
  356         return this
  357     }
  358 
  359     /**
  360      * Negate the current _UINT64_
  361      * @method negate
  362      * @return ThisExpression
  363      */
  364     UINT64.prototype.negate = function () {
  365         var v = ( ~this._a00 & 0xFFFF ) + 1
  366         this._a00 = v & 0xFFFF
  367         v = (~this._a16 & 0xFFFF) + (v >>> 16)
  368         this._a16 = v & 0xFFFF
  369         v = (~this._a32 & 0xFFFF) + (v >>> 16)
  370         this._a32 = v & 0xFFFF
  371         this._a48 = (~this._a48 + (v >>> 16)) & 0xFFFF
  372 
  373         return this
  374     }
  375 
  376     /**
  377 
  378      * @method eq
  379      * @param {Object} other UINT64
  380      * @return {Boolean}
  381      */
  382     UINT64.prototype.equals = UINT64.prototype.eq = function (other) {
  383         return (this._a48 == other._a48) && (this._a00 == other._a00)
  384              && (this._a32 == other._a32) && (this._a16 == other._a16)
  385     }
  386 
  387     /**
  388      * Greater than (strict)
  389      * @method gt
  390      * @param {Object} other UINT64
  391      * @return {Boolean}
  392      */
  393     UINT64.prototype.greaterThan = UINT64.prototype.gt = function (other) {
  394         if (this._a48 > other._a48) return true
  395         if (this._a48 < other._a48) return false
  396         if (this._a32 > other._a32) return true
  397         if (this._a32 < other._a32) return false
  398         if (this._a16 > other._a16) return true
  399         if (this._a16 < other._a16) return false
  400         return this._a00 > other._a00
  401     }
  402 
  403     /**
  404      * Less than (strict)
  405      * @method lt
  406      * @param {Object} other UINT64
  407      * @return {Boolean}
  408      */
  409     UINT64.prototype.lessThan = UINT64.prototype.lt = function (other) {
  410         if (this._a48 < other._a48) return true
  411         if (this._a48 > other._a48) return false
  412         if (this._a32 < other._a32) return true
  413         if (this._a32 > other._a32) return false
  414         if (this._a16 < other._a16) return true
  415         if (this._a16 > other._a16) return false
  416         return this._a00 < other._a00
  417     }
  418 
  419     /**
  420      * Bitwise OR
  421      * @method or
  422      * @param {Object} other UINT64
  423      * @return ThisExpression
  424      */
  425     UINT64.prototype.or = function (other) {
  426         this._a00 |= other._a00
  427         this._a16 |= other._a16
  428         this._a32 |= other._a32
  429         this._a48 |= other._a48
  430 
  431         return this
  432     }
  433 
  434     /**
  435      * Bitwise AND
  436      * @method and
  437      * @param {Object} other UINT64
  438      * @return ThisExpression
  439      */
  440     UINT64.prototype.and = function (other) {
  441         this._a00 &= other._a00
  442         this._a16 &= other._a16
  443         this._a32 &= other._a32
  444         this._a48 &= other._a48
  445 
  446         return this
  447     }
  448 
  449     /**
  450      * Bitwise XOR
  451      * @method xor
  452      * @param {Object} other UINT64
  453      * @return ThisExpression
  454      */
  455     UINT64.prototype.xor = function (other) {
  456         this._a00 ^= other._a00
  457         this._a16 ^= other._a16
  458         this._a32 ^= other._a32
  459         this._a48 ^= other._a48
  460 
  461         return this
  462     }
  463 
  464     /**
  465      * Bitwise NOT
  466      * @method not
  467      * @return ThisExpression
  468      */
  469     UINT64.prototype.not = function() {
  470         this._a00 = ~this._a00 & 0xFFFF
  471         this._a16 = ~this._a16 & 0xFFFF
  472         this._a32 = ~this._a32 & 0xFFFF
  473         this._a48 = ~this._a48 & 0xFFFF
  474 
  475         return this
  476     }
  477 
  478     /**
  479      * Bitwise shift right
  480      * @method shiftRight
  481      * @param {Number} number of bits to shift
  482      * @return ThisExpression
  483      */
  484     UINT64.prototype.shiftRight = UINT64.prototype.shiftr = function (n) {
  485         n %= 64
  486         if (n >= 48) {
  487             this._a00 = this._a48 >> (n - 48)
  488             this._a16 = 0
  489             this._a32 = 0
  490             this._a48 = 0
  491         } else if (n >= 32) {
  492             n -= 32
  493             this._a00 = ( (this._a32 >> n) | (this._a48 << (16-n)) ) & 0xFFFF
  494             this._a16 = (this._a48 >> n) & 0xFFFF
  495             this._a32 = 0
  496             this._a48 = 0
  497         } else if (n >= 16) {
  498             n -= 16
  499             this._a00 = ( (this._a16 >> n) | (this._a32 << (16-n)) ) & 0xFFFF
  500             this._a16 = ( (this._a32 >> n) | (this._a48 << (16-n)) ) & 0xFFFF
  501             this._a32 = (this._a48 >> n) & 0xFFFF
  502             this._a48 = 0
  503         } else {
  504             this._a00 = ( (this._a00 >> n) | (this._a16 << (16-n)) ) & 0xFFFF
  505             this._a16 = ( (this._a16 >> n) | (this._a32 << (16-n)) ) & 0xFFFF
  506             this._a32 = ( (this._a32 >> n) | (this._a48 << (16-n)) ) & 0xFFFF
  507             this._a48 = (this._a48 >> n) & 0xFFFF
  508         }
  509 
  510         return this
  511     }
  512 
  513     /**
  514      * Bitwise shift left
  515      * @method shiftLeft
  516      * @param {Number} number of bits to shift
  517      * @param {Boolean} allow overflow
  518      * @return ThisExpression
  519      */
  520     UINT64.prototype.shiftLeft = UINT64.prototype.shiftl = function (n, allowOverflow) {
  521         n %= 64
  522         if (n >= 48) {
  523             this._a48 = this._a00 << (n - 48)
  524             this._a32 = 0
  525             this._a16 = 0
  526             this._a00 = 0
  527         } else if (n >= 32) {
  528             n -= 32
  529             this._a48 = (this._a16 << n) | (this._a00 >> (16-n))
  530             this._a32 = (this._a00 << n) & 0xFFFF
  531             this._a16 = 0
  532             this._a00 = 0
  533         } else if (n >= 16) {
  534             n -= 16
  535             this._a48 = (this._a32 << n) | (this._a16 >> (16-n))
  536             this._a32 = ( (this._a16 << n) | (this._a00 >> (16-n)) ) & 0xFFFF
  537             this._a16 = (this._a00 << n) & 0xFFFF
  538             this._a00 = 0
  539         } else {
  540             this._a48 = (this._a48 << n) | (this._a32 >> (16-n))
  541             this._a32 = ( (this._a32 << n) | (this._a16 >> (16-n)) ) & 0xFFFF
  542             this._a16 = ( (this._a16 << n) | (this._a00 >> (16-n)) ) & 0xFFFF
  543             this._a00 = (this._a00 << n) & 0xFFFF
  544         }
  545         if (!allowOverflow) {
  546             this._a48 &= 0xFFFF
  547         }
  548 
  549         return this
  550     }
  551 
  552     /**
  553      * Bitwise rotate left
  554      * @method rotl
  555      * @param {Number} number of bits to rotate
  556      * @return ThisExpression
  557      */
  558     UINT64.prototype.rotateLeft = UINT64.prototype.rotl = function (n) {
  559         n %= 64
  560         if (n == 0) return this
  561         if (n >= 32) {
  562             // A.B.C.D
  563             // B.C.D.A rotl(16)
  564             // C.D.A.B rotl(32)
  565             var v = this._a00
  566             this._a00 = this._a32
  567             this._a32 = v
  568             v = this._a48
  569             this._a48 = this._a16
  570             this._a16 = v
  571             if (n == 32) return this
  572             n -= 32
  573         }
  574 
  575         var high = (this._a48 << 16) | this._a32
  576         var low = (this._a16 << 16) | this._a00
  577 
  578         var _high = (high << n) | (low >>> (32 - n))
  579         var _low = (low << n) | (high >>> (32 - n))
  580 
  581         this._a00 = _low & 0xFFFF
  582         this._a16 = _low >>> 16
  583         this._a32 = _high & 0xFFFF
  584         this._a48 = _high >>> 16
  585 
  586         return this
  587     }
  588 
  589     /**
  590      * Bitwise rotate right
  591      * @method rotr
  592      * @param {Number} number of bits to rotate
  593      * @return ThisExpression
  594      */
  595     UINT64.prototype.rotateRight = UINT64.prototype.rotr = function (n) {
  596         n %= 64
  597         if (n == 0) return this
  598         if (n >= 32) {
  599             // A.B.C.D
  600             // D.A.B.C rotr(16)
  601             // C.D.A.B rotr(32)
  602             var v = this._a00
  603             this._a00 = this._a32
  604             this._a32 = v
  605             v = this._a48
  606             this._a48 = this._a16
  607             this._a16 = v
  608             if (n == 32) return this
  609             n -= 32
  610         }
  611 
  612         var high = (this._a48 << 16) | this._a32
  613         var low = (this._a16 << 16) | this._a00
  614 
  615         var _high = (high >>> n) | (low << (32 - n))
  616         var _low = (low >>> n) | (high << (32 - n))
  617 
  618         this._a00 = _low & 0xFFFF
  619         this._a16 = _low >>> 16
  620         this._a32 = _high & 0xFFFF
  621         this._a48 = _high >>> 16
  622 
  623         return this
  624     }
  625 
  626     /**
  627      * Clone the current _UINT64_
  628      * @method clone
  629      * @return {Object} cloned UINT64
  630      */
  631     UINT64.prototype.clone = function () {
  632         return new UINT64(this._a00, this._a16, this._a32, this._a48)
  633     }
  634 
  635     if (typeof define != 'undefined' && define.amd) {
  636         // AMD / RequireJS
  637         define([], function () {
  638             return UINT64
  639         })
  640     } else if (typeof module != 'undefined' && module.exports) {
  641         // Node.js
  642         module.exports = UINT64
  643     } else {
  644         // Browser
  645         root['UINT64'] = UINT64
  646     }
  647 
  648 })(this)