Home | History | Annotate | Download | only in Expressions
      1 /* The contents of this file are subject to the Netscape Public
      2  * License Version 1.1 (the "License"); you may not use this file
      3  * except in compliance with the License. You may obtain a copy of
      4  * the License at http://www.mozilla.org/NPL/
      5  *
      6  * Software distributed under the License is distributed on an "AS
      7  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
      8  * implied. See the License for the specific language governing
      9  * rights and limitations under the License.
     10  *
     11  * The Original Code is Mozilla Communicator client code, released March
     12  * 31, 1998.
     13  *
     14  * The Initial Developer of the Original Code is Netscape Communications
     15  * Corporation. Portions created by Netscape are
     16  * Copyright (C) 1998 Netscape Communications Corporation. All
     17  * Rights Reserved.
     18  *
     19  * Contributor(s):
     20  *
     21  */
     22 /**
     23     File Name:          11.7.3.js
     24     ECMA Section:       11.7.3  The unsigned right shift operator ( >>> )
     25     Description:
     26     11.7.3 The unsigned right shift operator ( >>> )
     27     Performs a zero-filling bitwise right shift operation on the left argument
     28     by the amount specified by the right argument.
     29 
     30     The production ShiftExpression : ShiftExpression >>> AdditiveExpression is
     31     evaluated as follows:
     32 
     33     1.  Evaluate ShiftExpression.
     34     2.  Call GetValue(Result(1)).
     35     3.  Evaluate AdditiveExpression.
     36     4.  Call GetValue(Result(3)).
     37     5.  Call ToUint32(Result(2)).
     38     6.  Call ToUint32(Result(4)).
     39     7.  Mask out all but the least significant 5 bits of Result(6), that is,
     40         compute Result(6) & 0x1F.
     41     8.  Perform zero-filling right shift of Result(5) by Result(7) bits.
     42         Vacated bits are filled with zero. The result is an unsigned 32 bit
     43         integer.
     44     9.  Return Result(8).
     45 
     46     Author:             christine (at) netscape.com
     47     Date:               12 november 1997
     48 */
     49     var SECTION = "11.7.3";
     50     var VERSION = "ECMA_1";
     51     startTest();
     52     var testcases = getTestCases();
     53 
     54     writeHeaderToLog( SECTION + "  The unsigned right shift operator ( >>> )");
     55     test();
     56 
     57 function test() {
     58     for ( tc=0; tc < testcases.length; tc++ ) {
     59         testcases[tc].passed = writeTestCaseResult(
     60                             testcases[tc].expect,
     61                             testcases[tc].actual,
     62                             testcases[tc].description +" = "+
     63                             testcases[tc].actual );
     64 
     65         testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value ";
     66     }
     67     stopTest();
     68     return ( testcases );
     69 }
     70 function getTestCases() {
     71     var array = new Array();
     72     var item = 0;
     73 
     74     var addexp = 0;
     75     var power = 0;
     76 
     77     for ( power = 0; power <= 32; power++ ) {
     78         shiftexp = Math.pow( 2, power );
     79 
     80         for ( addexp = 0; addexp <= 32; addexp++ ) {
     81             array[item++] = new TestCase( SECTION,
     82                                     shiftexp + " >>> " + addexp,
     83                                     UnsignedRightShift( shiftexp, addexp ),
     84                                     shiftexp >>> addexp );
     85          }
     86     }
     87 
     88     return ( array );
     89 }
     90 
     91 function ToInteger( n ) {
     92     n = Number( n );
     93     var sign = ( n < 0 ) ? -1 : 1;
     94 
     95     if ( n != n ) {
     96         return 0;
     97     }
     98     if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY ) {
     99         return n;
    100     }
    101     return ( sign * Math.floor(Math.abs(n)) );
    102 }
    103 function ToInt32( n ) {
    104     n = Number( n );
    105     var sign = ( n < 0 ) ? -1 : 1;
    106 
    107     if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
    108         return 0;
    109     }
    110 
    111     n = (sign * Math.floor( Math.abs(n) )) % Math.pow(2,32);
    112     n = ( n >= Math.pow(2,31) ) ? n - Math.pow(2,32) : n;
    113 
    114     return ( n );
    115 }
    116 function ToUint32( n ) {
    117     n = Number( n );
    118     var sign = ( n < 0 ) ? -1 : 1;
    119 
    120     if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
    121         return 0;
    122     }
    123     n = sign * Math.floor( Math.abs(n) )
    124 
    125     n = n % Math.pow(2,32);
    126 
    127     if ( n < 0 ){
    128         n += Math.pow(2,32);
    129     }
    130 
    131     return ( n );
    132 }
    133 function ToUint16( n ) {
    134     var sign = ( n < 0 ) ? -1 : 1;
    135 
    136     if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
    137         return 0;
    138     }
    139 
    140     n = ( sign * Math.floor( Math.abs(n) ) ) % Math.pow(2,16);
    141 
    142     if (n <0) {
    143         n += Math.pow(2,16);
    144     }
    145 
    146     return ( n );
    147 }
    148 function Mask( b, n ) {
    149     b = ToUint32BitString( b );
    150     b = b.substring( b.length - n );
    151     b = ToUint32Decimal( b );
    152     return ( b );
    153 }
    154 function ToUint32BitString( n ) {
    155     var b = "";
    156     for ( p = 31; p >=0; p-- ) {
    157         if ( n >= Math.pow(2,p) ) {
    158             b += "1";
    159             n -= Math.pow(2,p);
    160         } else {
    161             b += "0";
    162         }
    163     }
    164     return b;
    165 }
    166 function ToInt32BitString( n ) {
    167     var b = "";
    168     var sign = ( n < 0 ) ? -1 : 1;
    169 
    170     b += ( sign == 1 ) ? "0" : "1";
    171 
    172     for ( p = 30; p >=0; p-- ) {
    173         if ( (sign == 1 ) ? sign * n >= Math.pow(2,p) : sign * n > Math.pow(2,p) ) {
    174             b += ( sign == 1 ) ? "1" : "0";
    175             n -= sign * Math.pow( 2, p );
    176         } else {
    177             b += ( sign == 1 ) ? "0" : "1";
    178         }
    179     }
    180 
    181     return b;
    182 }
    183 function ToInt32Decimal( bin ) {
    184     var r = 0;
    185     var sign;
    186 
    187     if ( Number(bin.charAt(0)) == 0 ) {
    188         sign = 1;
    189         r = 0;
    190     } else {
    191         sign = -1;
    192         r = -(Math.pow(2,31));
    193     }
    194 
    195     for ( var j = 0; j < 31; j++ ) {
    196         r += Math.pow( 2, j ) * Number(bin.charAt(31-j));
    197     }
    198 
    199     return r;
    200 }
    201 function ToUint32Decimal( bin ) {
    202     var r = 0;
    203 
    204 
    205     for ( l = bin.length; l < 32; l++ ) {
    206         bin = "0" + bin;
    207     }
    208 
    209     for ( j = 0; j < 32; j++ ) {
    210         r += Math.pow( 2, j ) * Number(bin.charAt(31-j));
    211 
    212     }
    213 
    214     return r;
    215 }
    216 function RShift( s, a ) {
    217     s = ToUint32BitString( s );
    218     for ( z = 0; z < a; z++ ) {
    219         s = "0" + s;
    220     }
    221     s = s.substring( 0, s.length - a );
    222 
    223     return ToUint32Decimal(s);
    224 }
    225 function UnsignedRightShift( s, a ) {
    226     s = ToUint32( s );
    227     a = ToUint32( a );
    228     a = Mask( a, 5 );
    229     return ( RShift( s, a ) );
    230 }
    231