js/src/tests/ecma/Expressions/11.7.1.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

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.1.js
michael@0 9 ECMA Section: 11.7.1 The Left Shift Operator ( << )
michael@0 10 Description:
michael@0 11 Performs a bitwise left shift operation on the left argument by the amount
michael@0 12 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. Left shift Result(5) by Result(7) bits. The result is a signed 32 bit
michael@0 26 integer.
michael@0 27 9. Return Result(8).
michael@0 28
michael@0 29 Author: christine@netscape.com
michael@0 30 Date: 12 november 1997
michael@0 31 */
michael@0 32 var SECTION = "11.7.1";
michael@0 33 var VERSION = "ECMA_1";
michael@0 34 startTest();
michael@0 35
michael@0 36 writeHeaderToLog( SECTION + " The left shift operator ( << )");
michael@0 37
michael@0 38 for ( power = 0; power < 33; power++ ) {
michael@0 39 shiftexp = Math.pow( 2, power );
michael@0 40
michael@0 41 for ( addexp = 0; addexp < 33; addexp++ ) {
michael@0 42 new TestCase( SECTION,
michael@0 43 shiftexp + " << " + addexp,
michael@0 44 LeftShift( shiftexp, addexp ),
michael@0 45 shiftexp << addexp );
michael@0 46 }
michael@0 47 }
michael@0 48
michael@0 49 test();
michael@0 50
michael@0 51 function ToInteger( n ) {
michael@0 52 n = Number( n );
michael@0 53 var sign = ( n < 0 ) ? -1 : 1;
michael@0 54
michael@0 55 if ( n != n ) {
michael@0 56 return 0;
michael@0 57 }
michael@0 58 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY ) {
michael@0 59 return n;
michael@0 60 }
michael@0 61 return ( sign * Math.floor(Math.abs(n)) );
michael@0 62 }
michael@0 63 function ToInt32( n ) {
michael@0 64 n = Number( n );
michael@0 65 var sign = ( n < 0 ) ? -1 : 1;
michael@0 66
michael@0 67 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
michael@0 68 return 0;
michael@0 69 }
michael@0 70
michael@0 71 n = (sign * Math.floor( Math.abs(n) )) % Math.pow(2,32);
michael@0 72 n = ( n >= Math.pow(2,31) ) ? n - Math.pow(2,32) : n;
michael@0 73
michael@0 74 return ( n );
michael@0 75 }
michael@0 76 function ToUint32( n ) {
michael@0 77 n = Number( n );
michael@0 78 var sign = ( n < 0 ) ? -1 : 1;
michael@0 79
michael@0 80 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
michael@0 81 return 0;
michael@0 82 }
michael@0 83 n = sign * Math.floor( Math.abs(n) )
michael@0 84
michael@0 85 n = n % Math.pow(2,32);
michael@0 86
michael@0 87 if ( n < 0 ){
michael@0 88 n += Math.pow(2,32);
michael@0 89 }
michael@0 90
michael@0 91 return ( n );
michael@0 92 }
michael@0 93 function ToUint16( n ) {
michael@0 94 var sign = ( n < 0 ) ? -1 : 1;
michael@0 95
michael@0 96 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
michael@0 97 return 0;
michael@0 98 }
michael@0 99
michael@0 100 n = ( sign * Math.floor( Math.abs(n) ) ) % Math.pow(2,16);
michael@0 101
michael@0 102 if (n <0) {
michael@0 103 n += Math.pow(2,16);
michael@0 104 }
michael@0 105
michael@0 106 return ( n );
michael@0 107 }
michael@0 108 function Mask( b, n ) {
michael@0 109 b = ToUint32BitString( b );
michael@0 110 b = b.substring( b.length - n );
michael@0 111 b = ToUint32Decimal( b );
michael@0 112 return ( b );
michael@0 113 }
michael@0 114 function ToUint32BitString( n ) {
michael@0 115 var b = "";
michael@0 116 for ( p = 31; p >=0; p-- ) {
michael@0 117 if ( n >= Math.pow(2,p) ) {
michael@0 118 b += "1";
michael@0 119 n -= Math.pow(2,p);
michael@0 120 } else {
michael@0 121 b += "0";
michael@0 122 }
michael@0 123 }
michael@0 124 return b;
michael@0 125 }
michael@0 126 function ToInt32BitString( n ) {
michael@0 127 var b = "";
michael@0 128 var sign = ( n < 0 ) ? -1 : 1;
michael@0 129
michael@0 130 b += ( sign == 1 ) ? "0" : "1";
michael@0 131
michael@0 132 for ( p = 30; p >=0; p-- ) {
michael@0 133 if ( (sign == 1 ) ? sign * n >= Math.pow(2,p) : sign * n > Math.pow(2,p) ) {
michael@0 134 b += ( sign == 1 ) ? "1" : "0";
michael@0 135 n -= sign * Math.pow( 2, p );
michael@0 136 } else {
michael@0 137 b += ( sign == 1 ) ? "0" : "1";
michael@0 138 }
michael@0 139 }
michael@0 140
michael@0 141 return b;
michael@0 142 }
michael@0 143 function ToInt32Decimal( bin ) {
michael@0 144 var r = 0;
michael@0 145 var sign;
michael@0 146
michael@0 147 if ( Number(bin.charAt(0)) == 0 ) {
michael@0 148 sign = 1;
michael@0 149 r = 0;
michael@0 150 } else {
michael@0 151 sign = -1;
michael@0 152 r = -(Math.pow(2,31));
michael@0 153 }
michael@0 154
michael@0 155 for ( var j = 0; j < 31; j++ ) {
michael@0 156 r += Math.pow( 2, j ) * Number(bin.charAt(31-j));
michael@0 157 }
michael@0 158
michael@0 159 return r;
michael@0 160 }
michael@0 161 function ToUint32Decimal( bin ) {
michael@0 162 var r = 0;
michael@0 163
michael@0 164
michael@0 165 for ( l = bin.length; l < 32; l++ ) {
michael@0 166 bin = "0" + bin;
michael@0 167 }
michael@0 168
michael@0 169 for ( j = 0; j < 31; j++ ) {
michael@0 170 r += Math.pow( 2, j ) * Number(bin.charAt(31-j));
michael@0 171
michael@0 172 }
michael@0 173
michael@0 174 return r;
michael@0 175 }
michael@0 176 function LeftShift( s, a ) {
michael@0 177 var shift = ToInt32( s );
michael@0 178 var add = ToUint32( a );
michael@0 179 add = Mask( add, 5 );
michael@0 180 var exp = LShift( shift, add );
michael@0 181
michael@0 182 return ( exp );
michael@0 183 }
michael@0 184 function LShift( s, a ) {
michael@0 185 s = ToInt32BitString( s );
michael@0 186
michael@0 187 for ( var z = 0; z < a; z++ ) {
michael@0 188 s += "0";
michael@0 189 }
michael@0 190
michael@0 191 s = s.substring( a, s.length);
michael@0 192
michael@0 193 return ToInt32(ToInt32Decimal(s));
michael@0 194 }

mercurial