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.2.js |
michael@0 | 9 | ECMA Section: 11.7.2 The signed right shift operator ( >> ) |
michael@0 | 10 | Description: |
michael@0 | 11 | Performs a sign-filling bitwise right shift operation on the left argument |
michael@0 | 12 | by the amount specified by the right argument. |
michael@0 | 13 | |
michael@0 | 14 | The production ShiftExpression : ShiftExpression >> AdditiveExpression is |
michael@0 | 15 | evaluated as follows: |
michael@0 | 16 | |
michael@0 | 17 | 1. Evaluate ShiftExpression. |
michael@0 | 18 | 2. Call GetValue(Result(1)). |
michael@0 | 19 | 3. Evaluate AdditiveExpression. |
michael@0 | 20 | 4. Call GetValue(Result(3)). |
michael@0 | 21 | 5. Call ToInt32(Result(2)). |
michael@0 | 22 | 6. Call ToUint32(Result(4)). |
michael@0 | 23 | 7. Mask out all but the least significant 5 bits of Result(6), that is, |
michael@0 | 24 | compute Result(6) & 0x1F. |
michael@0 | 25 | 8. Perform sign-extending right shift of Result(5) by Result(7) bits. The |
michael@0 | 26 | most significant bit is propagated. The result is a signed 32 bit |
michael@0 | 27 | integer. |
michael@0 | 28 | 9. Return Result(8). |
michael@0 | 29 | |
michael@0 | 30 | Author: christine@netscape.com |
michael@0 | 31 | Date: 12 november 1997 |
michael@0 | 32 | */ |
michael@0 | 33 | var SECTION = "11.7.2"; |
michael@0 | 34 | var VERSION = "ECMA_1"; |
michael@0 | 35 | startTest(); |
michael@0 | 36 | |
michael@0 | 37 | writeHeaderToLog( SECTION + " The signed right shift operator ( >> )"); |
michael@0 | 38 | |
michael@0 | 39 | var power = 0; |
michael@0 | 40 | var addexp = 0; |
michael@0 | 41 | |
michael@0 | 42 | for ( power = 0; power <= 32; power++ ) { |
michael@0 | 43 | shiftexp = Math.pow( 2, power ); |
michael@0 | 44 | |
michael@0 | 45 | for ( addexp = 0; addexp <= 32; addexp++ ) { |
michael@0 | 46 | new TestCase( SECTION, |
michael@0 | 47 | shiftexp + " >> " + addexp, |
michael@0 | 48 | SignedRightShift( shiftexp, addexp ), |
michael@0 | 49 | shiftexp >> addexp ); |
michael@0 | 50 | } |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | for ( power = 0; power <= 32; power++ ) { |
michael@0 | 54 | shiftexp = -Math.pow( 2, power ); |
michael@0 | 55 | |
michael@0 | 56 | for ( addexp = 0; addexp <= 32; addexp++ ) { |
michael@0 | 57 | new TestCase( SECTION, |
michael@0 | 58 | shiftexp + " >> " + addexp, |
michael@0 | 59 | SignedRightShift( shiftexp, addexp ), |
michael@0 | 60 | shiftexp >> addexp ); |
michael@0 | 61 | } |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | test(); |
michael@0 | 65 | |
michael@0 | 66 | function ToInteger( n ) { |
michael@0 | 67 | n = Number( n ); |
michael@0 | 68 | var sign = ( n < 0 ) ? -1 : 1; |
michael@0 | 69 | |
michael@0 | 70 | if ( n != n ) { |
michael@0 | 71 | return 0; |
michael@0 | 72 | } |
michael@0 | 73 | if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY ) { |
michael@0 | 74 | return n; |
michael@0 | 75 | } |
michael@0 | 76 | return ( sign * Math.floor(Math.abs(n)) ); |
michael@0 | 77 | } |
michael@0 | 78 | function ToInt32( n ) { |
michael@0 | 79 | n = Number( n ); |
michael@0 | 80 | var sign = ( n < 0 ) ? -1 : 1; |
michael@0 | 81 | |
michael@0 | 82 | if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) { |
michael@0 | 83 | return 0; |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | n = (sign * Math.floor( Math.abs(n) )) % Math.pow(2,32); |
michael@0 | 87 | n = ( n >= Math.pow(2,31) ) ? n - Math.pow(2,32) : n; |
michael@0 | 88 | |
michael@0 | 89 | return ( n ); |
michael@0 | 90 | } |
michael@0 | 91 | function ToUint32( n ) { |
michael@0 | 92 | n = Number( n ); |
michael@0 | 93 | var sign = ( n < 0 ) ? -1 : 1; |
michael@0 | 94 | |
michael@0 | 95 | if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) { |
michael@0 | 96 | return 0; |
michael@0 | 97 | } |
michael@0 | 98 | n = sign * Math.floor( Math.abs(n) ) |
michael@0 | 99 | |
michael@0 | 100 | n = n % Math.pow(2,32); |
michael@0 | 101 | |
michael@0 | 102 | if ( n < 0 ){ |
michael@0 | 103 | n += Math.pow(2,32); |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | return ( n ); |
michael@0 | 107 | } |
michael@0 | 108 | function ToUint16( n ) { |
michael@0 | 109 | var sign = ( n < 0 ) ? -1 : 1; |
michael@0 | 110 | |
michael@0 | 111 | if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) { |
michael@0 | 112 | return 0; |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | n = ( sign * Math.floor( Math.abs(n) ) ) % Math.pow(2,16); |
michael@0 | 116 | |
michael@0 | 117 | if (n <0) { |
michael@0 | 118 | n += Math.pow(2,16); |
michael@0 | 119 | } |
michael@0 | 120 | |
michael@0 | 121 | return ( n ); |
michael@0 | 122 | } |
michael@0 | 123 | function Mask( b, n ) { |
michael@0 | 124 | b = ToUint32BitString( b ); |
michael@0 | 125 | b = b.substring( b.length - n ); |
michael@0 | 126 | b = ToUint32Decimal( b ); |
michael@0 | 127 | return ( b ); |
michael@0 | 128 | } |
michael@0 | 129 | function ToUint32BitString( n ) { |
michael@0 | 130 | var b = ""; |
michael@0 | 131 | for ( p = 31; p >=0; p-- ) { |
michael@0 | 132 | if ( n >= Math.pow(2,p) ) { |
michael@0 | 133 | b += "1"; |
michael@0 | 134 | n -= Math.pow(2,p); |
michael@0 | 135 | } else { |
michael@0 | 136 | b += "0"; |
michael@0 | 137 | } |
michael@0 | 138 | } |
michael@0 | 139 | return b; |
michael@0 | 140 | } |
michael@0 | 141 | function ToInt32BitString( n ) { |
michael@0 | 142 | var b = ""; |
michael@0 | 143 | var sign = ( n < 0 ) ? -1 : 1; |
michael@0 | 144 | |
michael@0 | 145 | b += ( sign == 1 ) ? "0" : "1"; |
michael@0 | 146 | |
michael@0 | 147 | for ( p = 30; p >=0; p-- ) { |
michael@0 | 148 | if ( (sign == 1 ) ? sign * n >= Math.pow(2,p) : sign * n > Math.pow(2,p) ) { |
michael@0 | 149 | b += ( sign == 1 ) ? "1" : "0"; |
michael@0 | 150 | n -= sign * Math.pow( 2, p ); |
michael@0 | 151 | } else { |
michael@0 | 152 | b += ( sign == 1 ) ? "0" : "1"; |
michael@0 | 153 | } |
michael@0 | 154 | } |
michael@0 | 155 | |
michael@0 | 156 | return b; |
michael@0 | 157 | } |
michael@0 | 158 | function ToInt32Decimal( bin ) { |
michael@0 | 159 | var r = 0; |
michael@0 | 160 | var sign; |
michael@0 | 161 | |
michael@0 | 162 | if ( Number(bin.charAt(0)) == 0 ) { |
michael@0 | 163 | sign = 1; |
michael@0 | 164 | r = 0; |
michael@0 | 165 | } else { |
michael@0 | 166 | sign = -1; |
michael@0 | 167 | r = -(Math.pow(2,31)); |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | for ( var j = 0; j < 31; j++ ) { |
michael@0 | 171 | r += Math.pow( 2, j ) * Number(bin.charAt(31-j)); |
michael@0 | 172 | } |
michael@0 | 173 | |
michael@0 | 174 | return r; |
michael@0 | 175 | } |
michael@0 | 176 | function ToUint32Decimal( bin ) { |
michael@0 | 177 | var r = 0; |
michael@0 | 178 | |
michael@0 | 179 | for ( l = bin.length; l < 32; l++ ) { |
michael@0 | 180 | bin = "0" + bin; |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | for ( j = 0; j < 31; j++ ) { |
michael@0 | 184 | r += Math.pow( 2, j ) * Number(bin.charAt(31-j)); |
michael@0 | 185 | } |
michael@0 | 186 | |
michael@0 | 187 | return r; |
michael@0 | 188 | } |
michael@0 | 189 | function SignedRightShift( s, a ) { |
michael@0 | 190 | s = ToInt32( s ); |
michael@0 | 191 | a = ToUint32( a ); |
michael@0 | 192 | a = Mask( a, 5 ); |
michael@0 | 193 | return ( SignedRShift( s, a ) ); |
michael@0 | 194 | } |
michael@0 | 195 | function SignedRShift( s, a ) { |
michael@0 | 196 | s = ToInt32BitString( s ); |
michael@0 | 197 | |
michael@0 | 198 | var firstbit = s.substring(0,1); |
michael@0 | 199 | |
michael@0 | 200 | s = s.substring( 1, s.length ); |
michael@0 | 201 | |
michael@0 | 202 | for ( var z = 0; z < a; z++ ) { |
michael@0 | 203 | s = firstbit + s; |
michael@0 | 204 | } |
michael@0 | 205 | |
michael@0 | 206 | s = s.substring( 0, s.length - a); |
michael@0 | 207 | |
michael@0 | 208 | s = firstbit +s; |
michael@0 | 209 | |
michael@0 | 210 | |
michael@0 | 211 | return ToInt32(ToInt32Decimal(s)); |
michael@0 | 212 | } |