"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/cuint/build/uint32.js" (8 Mar 2017, 9945 Bytes) of archive /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 32 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         36: UINT32( Math.pow(36, 5) )
   11     ,   16: UINT32( Math.pow(16, 7) )
   12     ,   10: UINT32( Math.pow(10, 9) )
   13     ,   2:  UINT32( Math.pow(2, 30) )
   14     }
   15     var radixCache = {
   16         36: UINT32(36)
   17     ,   16: UINT32(16)
   18     ,   10: UINT32(10)
   19     ,   2:  UINT32(2)
   20     }
   21 
   22     /**
   23      *  Represents an unsigned 32 bits integer
   24      * @constructor
   25      * @param {Number|String|Number} low bits     | integer as a string          | integer as a number
   26      * @param {Number|Number|Undefined} high bits | radix (optional, default=10)
   27      * @return 
   28      */
   29     function UINT32 (l, h) {
   30         if ( !(this instanceof UINT32) )
   31             return new UINT32(l, h)
   32 
   33         this._low = 0
   34         this._high = 0
   35         this.remainder = null
   36         if (typeof h == 'undefined')
   37             return fromNumber.call(this, l)
   38 
   39         if (typeof l == 'string')
   40             return fromString.call(this, l, h)
   41 
   42         fromBits.call(this, l, h)
   43     }
   44 
   45     /**
   46      * Set the current _UINT32_ object with its low and high bits
   47      * @method fromBits
   48      * @param {Number} low bits
   49      * @param {Number} high bits
   50      * @return ThisExpression
   51      */
   52     function fromBits (l, h) {
   53         this._low = l | 0
   54         this._high = h | 0
   55 
   56         return this
   57     }
   58     UINT32.prototype.fromBits = fromBits
   59 
   60     /**
   61      * Set the current _UINT32_ object from a number
   62      * @method fromNumber
   63      * @param {Number} number
   64      * @return ThisExpression
   65      */
   66     function fromNumber (value) {
   67         this._low = value & 0xFFFF
   68         this._high = value >>> 16
   69 
   70         return this
   71     }
   72     UINT32.prototype.fromNumber = fromNumber
   73 
   74     /**
   75      * Set the current _UINT32_ object from a string
   76      * @method fromString
   77      * @param {String} integer as a string
   78      * @param {Number} radix (optional, default=10)
   79      * @return ThisExpression
   80      */
   81     function fromString (s, radix) {
   82         var value = parseInt(s, radix || 10)
   83 
   84         this._low = value & 0xFFFF
   85         this._high = value >>> 16
   86 
   87         return this
   88     }
   89     UINT32.prototype.fromString = fromString
   90 
   91     /**
   92      * Convert this _UINT32_ to a number
   93      * @method toNumber
   94      * @return {Number} the converted UINT32
   95      */
   96     UINT32.prototype.toNumber = function () {
   97         return (this._high * 65536) + this._low
   98     }
   99 
  100     /**
  101      * Convert this _UINT32_ to a string
  102      * @method toString
  103      * @param {Number} radix (optional, default=10)
  104      * @return {String} the converted UINT32
  105      */
  106     UINT32.prototype.toString = function (radix) {
  107         return this.toNumber().toString(radix || 10)
  108     }
  109 
  110     /**
  111      * Add two _UINT32_. The current _UINT32_ stores the result
  112      * @method add
  113      * @param {Object} other UINT32
  114      * @return ThisExpression
  115      */
  116     UINT32.prototype.add = function (other) {
  117         var a00 = this._low + other._low
  118         var a16 = a00 >>> 16
  119 
  120         a16 += this._high + other._high
  121 
  122         this._low = a00 & 0xFFFF
  123         this._high = a16 & 0xFFFF
  124 
  125         return this
  126     }
  127 
  128     /**
  129      * Subtract two _UINT32_. The current _UINT32_ stores the result
  130      * @method subtract
  131      * @param {Object} other UINT32
  132      * @return ThisExpression
  133      */
  134     UINT32.prototype.subtract = function (other) {
  135         //TODO inline
  136         return this.add( other.clone().negate() )
  137     }
  138 
  139     /**
  140      * Multiply two _UINT32_. The current _UINT32_ stores the result
  141      * @method multiply
  142      * @param {Object} other UINT32
  143      * @return ThisExpression
  144      */
  145     UINT32.prototype.multiply = function (other) {
  146         /*
  147             a = a00 + a16
  148             b = b00 + b16
  149             a*b = (a00 + a16)(b00 + b16)
  150                 = a00b00 + a00b16 + a16b00 + a16b16
  151 
  152             a16b16 overflows the 32bits
  153          */
  154         var a16 = this._high
  155         var a00 = this._low
  156         var b16 = other._high
  157         var b00 = other._low
  158 
  159 /* Removed to increase speed under normal circumstances (i.e. not multiplying by 0 or 1)
  160         // this == 0 or other == 1: nothing to do
  161         if ((a00 == 0 && a16 == 0) || (b00 == 1 && b16 == 0)) return this
  162 
  163         // other == 0 or this == 1: this = other
  164         if ((b00 == 0 && b16 == 0) || (a00 == 1 && a16 == 0)) {
  165             this._low = other._low
  166             this._high = other._high
  167             return this
  168         }
  169 */
  170 
  171         var c16, c00
  172         c00 = a00 * b00
  173         c16 = c00 >>> 16
  174 
  175         c16 += a16 * b00
  176         c16 &= 0xFFFF       // Not required but improves performance
  177         c16 += a00 * b16
  178 
  179         this._low = c00 & 0xFFFF
  180         this._high = c16 & 0xFFFF
  181 
  182         return this
  183     }
  184 
  185     /**
  186      * Divide two _UINT32_. The current _UINT32_ stores the result.
  187      * The remainder is made available as the _remainder_ property on
  188      * the _UINT32_ object. It can be null, meaning there are no remainder.
  189      * @method div
  190      * @param {Object} other UINT32
  191      * @return ThisExpression
  192      */
  193     UINT32.prototype.div = function (other) {
  194         if ( (other._low == 0) && (other._high == 0) ) throw Error('division by zero')
  195 
  196         // other == 1
  197         if (other._high == 0 && other._low == 1) {
  198             this.remainder = new UINT32(0)
  199             return this
  200         }
  201 
  202         // other > this: 0
  203         if ( other.gt(this) ) {
  204             this.remainder = this.clone()
  205             this._low = 0
  206             this._high = 0
  207             return this
  208         }
  209         // other == this: 1
  210         if ( this.eq(other) ) {
  211             this.remainder = new UINT32(0)
  212             this._low = 1
  213             this._high = 0
  214             return this
  215         }
  216 
  217         // Shift the divisor left until it is higher than the dividend
  218         var _other = other.clone()
  219         var i = -1
  220         while ( !this.lt(_other) ) {
  221             // High bit can overflow the default 16bits
  222             // Its ok since we right shift after this loop
  223             // The overflown bit must be kept though
  224             _other.shiftLeft(1, true)
  225             i++
  226         }
  227 
  228         // Set the remainder
  229         this.remainder = this.clone()
  230         // Initialize the current result to 0
  231         this._low = 0
  232         this._high = 0
  233         for (; i >= 0; i--) {
  234             _other.shiftRight(1)
  235             // If shifted divisor is smaller than the dividend
  236             // then subtract it from the dividend
  237             if ( !this.remainder.lt(_other) ) {
  238                 this.remainder.subtract(_other)
  239                 // Update the current result
  240                 if (i >= 16) {
  241                     this._high |= 1 << (i - 16)
  242                 } else {
  243                     this._low |= 1 << i
  244                 }
  245             }
  246         }
  247 
  248         return this
  249     }
  250 
  251     /**
  252      * Negate the current _UINT32_
  253      * @method negate
  254      * @return ThisExpression
  255      */
  256     UINT32.prototype.negate = function () {
  257         var v = ( ~this._low & 0xFFFF ) + 1
  258         this._low = v & 0xFFFF
  259         this._high = (~this._high + (v >>> 16)) & 0xFFFF
  260 
  261         return this
  262     }
  263 
  264     /**
  265      * Equals
  266      * @method eq
  267      * @param {Object} other UINT32
  268      * @return {Boolean}
  269      */
  270     UINT32.prototype.equals = UINT32.prototype.eq = function (other) {
  271         return (this._low == other._low) && (this._high == other._high)
  272     }
  273 
  274     /**
  275      * Greater than (strict)
  276      * @method gt
  277      * @param {Object} other UINT32
  278      * @return {Boolean}
  279      */
  280     UINT32.prototype.greaterThan = UINT32.prototype.gt = function (other) {
  281         if (this._high > other._high) return true
  282         if (this._high < other._high) return false
  283         return this._low > other._low
  284     }
  285 
  286     /**
  287      * Less than (strict)
  288      * @method lt
  289      * @param {Object} other UINT32
  290      * @return {Boolean}
  291      */
  292     UINT32.prototype.lessThan = UINT32.prototype.lt = function (other) {
  293         if (this._high < other._high) return true
  294         if (this._high > other._high) return false
  295         return this._low < other._low
  296     }
  297 
  298     /**
  299      * Bitwise OR
  300      * @method or
  301      * @param {Object} other UINT32
  302      * @return ThisExpression
  303      */
  304     UINT32.prototype.or = function (other) {
  305         this._low |= other._low
  306         this._high |= other._high
  307 
  308         return this
  309     }
  310 
  311     /**
  312      * Bitwise AND
  313      * @method and
  314      * @param {Object} other UINT32
  315      * @return ThisExpression
  316      */
  317     UINT32.prototype.and = function (other) {
  318         this._low &= other._low
  319         this._high &= other._high
  320 
  321         return this
  322     }
  323 
  324     /**
  325      * Bitwise NOT
  326      * @method not
  327      * @return ThisExpression
  328      */
  329     UINT32.prototype.not = function() {
  330         this._low = ~this._low & 0xFFFF
  331         this._high = ~this._high & 0xFFFF
  332 
  333         return this
  334     }
  335 
  336     /**
  337      * Bitwise XOR
  338      * @method xor
  339      * @param {Object} other UINT32
  340      * @return ThisExpression
  341      */
  342     UINT32.prototype.xor = function (other) {
  343         this._low ^= other._low
  344         this._high ^= other._high
  345 
  346         return this
  347     }
  348 
  349     /**
  350      * Bitwise shift right
  351      * @method shiftRight
  352      * @param {Number} number of bits to shift
  353      * @return ThisExpression
  354      */
  355     UINT32.prototype.shiftRight = UINT32.prototype.shiftr = function (n) {
  356         if (n > 16) {
  357             this._low = this._high >> (n - 16)
  358             this._high = 0
  359         } else if (n == 16) {
  360             this._low = this._high
  361             this._high = 0
  362         } else {
  363             this._low = (this._low >> n) | ( (this._high << (16-n)) & 0xFFFF )
  364             this._high >>= n
  365         }
  366 
  367         return this
  368     }
  369 
  370     /**
  371      * Bitwise shift left
  372      * @method shiftLeft
  373      * @param {Number} number of bits to shift
  374      * @param {Boolean} allow overflow
  375      * @return ThisExpression
  376      */
  377     UINT32.prototype.shiftLeft = UINT32.prototype.shiftl = function (n, allowOverflow) {
  378         if (n > 16) {
  379             this._high = this._low << (n - 16)
  380             this._low = 0
  381             if (!allowOverflow) {
  382                 this._high &= 0xFFFF
  383             }
  384         } else if (n == 16) {
  385             this._high = this._low
  386             this._low = 0
  387         } else {
  388             this._high = (this._high << n) | (this._low >> (16-n))
  389             this._low = (this._low << n) & 0xFFFF
  390             if (!allowOverflow) {
  391                 // Overflow only allowed on the high bits...
  392                 this._high &= 0xFFFF
  393             }
  394         }
  395 
  396         return this
  397     }
  398 
  399     /**
  400      * Bitwise rotate left
  401      * @method rotl
  402      * @param {Number} number of bits to rotate
  403      * @return ThisExpression
  404      */
  405     UINT32.prototype.rotateLeft = UINT32.prototype.rotl = function (n) {
  406         var v = (this._high << 16) | this._low
  407         v = (v << n) | (v >>> (32 - n))
  408         this._low = v & 0xFFFF
  409         this._high = v >>> 16
  410 
  411         return this
  412     }
  413 
  414     /**
  415      * Bitwise rotate right
  416      * @method rotr
  417      * @param {Number} number of bits to rotate
  418      * @return ThisExpression
  419      */
  420     UINT32.prototype.rotateRight = UINT32.prototype.rotr = function (n) {
  421         var v = (this._high << 16) | this._low
  422         v = (v >>> n) | (v << (32 - n))
  423         this._low = v & 0xFFFF
  424         this._high = v >>> 16
  425 
  426         return this
  427     }
  428 
  429     /**
  430      * Clone the current _UINT32_
  431      * @method clone
  432      * @return {Object} cloned UINT32
  433      */
  434     UINT32.prototype.clone = function () {
  435         return new UINT32(this._low, this._high)
  436     }
  437 
  438     if (typeof define != 'undefined' && define.amd) {
  439         // AMD / RequireJS
  440         define([], function () {
  441             return UINT32
  442         })
  443     } else if (typeof module != 'undefined' && module.exports) {
  444         // Node.js
  445         module.exports = UINT32
  446     } else {
  447         // Browser
  448         root['UINT32'] = UINT32
  449     }
  450 
  451 })(this)