Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | |
michael@0 | 7 | /** |
michael@0 | 8 | File Name: 11.7.3.js |
michael@0 | 9 | ECMA Section: 11.7.3 The unsigned right shift operator ( >>> ) |
michael@0 | 10 | Description: |
michael@0 | 11 | 11.7.3 The unsigned right shift operator ( >>> ) |
michael@0 | 12 | Performs a zero-filling bitwise right shift operation on the left argument |
michael@0 | 13 | by the amount specified by the right argument. |
michael@0 | 14 | |
michael@0 | 15 | The production ShiftExpression : ShiftExpression >>> AdditiveExpression is |
michael@0 | 16 | evaluated as follows: |
michael@0 | 17 | |
michael@0 | 18 | 1. Evaluate ShiftExpression. |
michael@0 | 19 | 2. Call GetValue(Result(1)). |
michael@0 | 20 | 3. Evaluate AdditiveExpression. |
michael@0 | 21 | 4. Call GetValue(Result(3)). |
michael@0 | 22 | 5. Call ToUint32(Result(2)). |
michael@0 | 23 | 6. Call ToUint32(Result(4)). |
michael@0 | 24 | 7. Mask out all but the least significant 5 bits of Result(6), that is, |
michael@0 | 25 | compute Result(6) & 0x1F. |
michael@0 | 26 | 8. Perform zero-filling right shift of Result(5) by Result(7) bits. |
michael@0 | 27 | Vacated bits are filled with zero. The result is an unsigned 32 bit |
michael@0 | 28 | integer. |
michael@0 | 29 | 9. Return Result(8). |
michael@0 | 30 | |
michael@0 | 31 | Author: christine@netscape.com |
michael@0 | 32 | Date: 12 november 1997 |
michael@0 | 33 | */ |
michael@0 | 34 | var SECTION = "11.7.3"; |
michael@0 | 35 | var VERSION = "ECMA_1"; |
michael@0 | 36 | startTest(); |
michael@0 | 37 | |
michael@0 | 38 | writeHeaderToLog( SECTION + " The unsigned right shift operator ( >>> )"); |
michael@0 | 39 | |
michael@0 | 40 | var addexp = 0; |
michael@0 | 41 | var power = 0; |
michael@0 | 42 | |
michael@0 | 43 | for ( power = 0; power <= 32; power++ ) { |
michael@0 | 44 | shiftexp = Math.pow( 2, power ); |
michael@0 | 45 | |
michael@0 | 46 | for ( addexp = 0; addexp <= 32; addexp++ ) { |
michael@0 | 47 | new TestCase( SECTION, |
michael@0 | 48 | shiftexp + " >>> " + addexp, |
michael@0 | 49 | UnsignedRightShift( shiftexp, addexp ), |
michael@0 | 50 | shiftexp >>> addexp ); |
michael@0 | 51 | } |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | test(); |
michael@0 | 55 | |
michael@0 | 56 | |
michael@0 | 57 | function ToInteger( n ) { |
michael@0 | 58 | n = Number( n ); |
michael@0 | 59 | var sign = ( n < 0 ) ? -1 : 1; |
michael@0 | 60 | |
michael@0 | 61 | if ( n != n ) { |
michael@0 | 62 | return 0; |
michael@0 | 63 | } |
michael@0 | 64 | if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY ) { |
michael@0 | 65 | return n; |
michael@0 | 66 | } |
michael@0 | 67 | return ( sign * Math.floor(Math.abs(n)) ); |
michael@0 | 68 | } |
michael@0 | 69 | function ToInt32( n ) { |
michael@0 | 70 | n = Number( n ); |
michael@0 | 71 | var sign = ( n < 0 ) ? -1 : 1; |
michael@0 | 72 | |
michael@0 | 73 | if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) { |
michael@0 | 74 | return 0; |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | n = (sign * Math.floor( Math.abs(n) )) % Math.pow(2,32); |
michael@0 | 78 | n = ( n >= Math.pow(2,31) ) ? n - Math.pow(2,32) : n; |
michael@0 | 79 | |
michael@0 | 80 | return ( n ); |
michael@0 | 81 | } |
michael@0 | 82 | function ToUint32( n ) { |
michael@0 | 83 | n = Number( n ); |
michael@0 | 84 | var sign = ( n < 0 ) ? -1 : 1; |
michael@0 | 85 | |
michael@0 | 86 | if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) { |
michael@0 | 87 | return 0; |
michael@0 | 88 | } |
michael@0 | 89 | n = sign * Math.floor( Math.abs(n) ) |
michael@0 | 90 | |
michael@0 | 91 | n = n % Math.pow(2,32); |
michael@0 | 92 | |
michael@0 | 93 | if ( n < 0 ){ |
michael@0 | 94 | n += Math.pow(2,32); |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | return ( n ); |
michael@0 | 98 | } |
michael@0 | 99 | function ToUint16( n ) { |
michael@0 | 100 | var sign = ( n < 0 ) ? -1 : 1; |
michael@0 | 101 | |
michael@0 | 102 | if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) { |
michael@0 | 103 | return 0; |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | n = ( sign * Math.floor( Math.abs(n) ) ) % Math.pow(2,16); |
michael@0 | 107 | |
michael@0 | 108 | if (n <0) { |
michael@0 | 109 | n += Math.pow(2,16); |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | return ( n ); |
michael@0 | 113 | } |
michael@0 | 114 | function Mask( b, n ) { |
michael@0 | 115 | b = ToUint32BitString( b ); |
michael@0 | 116 | b = b.substring( b.length - n ); |
michael@0 | 117 | b = ToUint32Decimal( b ); |
michael@0 | 118 | return ( b ); |
michael@0 | 119 | } |
michael@0 | 120 | function ToUint32BitString( n ) { |
michael@0 | 121 | var b = ""; |
michael@0 | 122 | for ( p = 31; p >=0; p-- ) { |
michael@0 | 123 | if ( n >= Math.pow(2,p) ) { |
michael@0 | 124 | b += "1"; |
michael@0 | 125 | n -= Math.pow(2,p); |
michael@0 | 126 | } else { |
michael@0 | 127 | b += "0"; |
michael@0 | 128 | } |
michael@0 | 129 | } |
michael@0 | 130 | return b; |
michael@0 | 131 | } |
michael@0 | 132 | function ToInt32BitString( n ) { |
michael@0 | 133 | var b = ""; |
michael@0 | 134 | var sign = ( n < 0 ) ? -1 : 1; |
michael@0 | 135 | |
michael@0 | 136 | b += ( sign == 1 ) ? "0" : "1"; |
michael@0 | 137 | |
michael@0 | 138 | for ( p = 30; p >=0; p-- ) { |
michael@0 | 139 | if ( (sign == 1 ) ? sign * n >= Math.pow(2,p) : sign * n > Math.pow(2,p) ) { |
michael@0 | 140 | b += ( sign == 1 ) ? "1" : "0"; |
michael@0 | 141 | n -= sign * Math.pow( 2, p ); |
michael@0 | 142 | } else { |
michael@0 | 143 | b += ( sign == 1 ) ? "0" : "1"; |
michael@0 | 144 | } |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | return b; |
michael@0 | 148 | } |
michael@0 | 149 | function ToInt32Decimal( bin ) { |
michael@0 | 150 | var r = 0; |
michael@0 | 151 | var sign; |
michael@0 | 152 | |
michael@0 | 153 | if ( Number(bin.charAt(0)) == 0 ) { |
michael@0 | 154 | sign = 1; |
michael@0 | 155 | r = 0; |
michael@0 | 156 | } else { |
michael@0 | 157 | sign = -1; |
michael@0 | 158 | r = -(Math.pow(2,31)); |
michael@0 | 159 | } |
michael@0 | 160 | |
michael@0 | 161 | for ( var j = 0; j < 31; j++ ) { |
michael@0 | 162 | r += Math.pow( 2, j ) * Number(bin.charAt(31-j)); |
michael@0 | 163 | } |
michael@0 | 164 | |
michael@0 | 165 | return r; |
michael@0 | 166 | } |
michael@0 | 167 | function ToUint32Decimal( bin ) { |
michael@0 | 168 | var r = 0; |
michael@0 | 169 | |
michael@0 | 170 | |
michael@0 | 171 | for ( l = bin.length; l < 32; l++ ) { |
michael@0 | 172 | bin = "0" + bin; |
michael@0 | 173 | } |
michael@0 | 174 | |
michael@0 | 175 | for ( j = 0; j < 32; j++ ) { |
michael@0 | 176 | r += Math.pow( 2, j ) * Number(bin.charAt(31-j)); |
michael@0 | 177 | |
michael@0 | 178 | } |
michael@0 | 179 | |
michael@0 | 180 | return r; |
michael@0 | 181 | } |
michael@0 | 182 | function RShift( s, a ) { |
michael@0 | 183 | s = ToUint32BitString( s ); |
michael@0 | 184 | for ( z = 0; z < a; z++ ) { |
michael@0 | 185 | s = "0" + s; |
michael@0 | 186 | } |
michael@0 | 187 | s = s.substring( 0, s.length - a ); |
michael@0 | 188 | |
michael@0 | 189 | return ToUint32Decimal(s); |
michael@0 | 190 | } |
michael@0 | 191 | function UnsignedRightShift( s, a ) { |
michael@0 | 192 | s = ToUint32( s ); |
michael@0 | 193 | a = ToUint32( a ); |
michael@0 | 194 | a = Mask( a, 5 ); |
michael@0 | 195 | return ( RShift( s, a ) ); |
michael@0 | 196 | } |