Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / tests / js1_5 / Regress / regress-452008.js
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  * http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  *
15  * The Original Code is JavaScript Engine testing utilities.
16  *
17  * The Initial Developer of the Original Code is
18  * Mozilla Foundation.
19  * Portions created by the Initial Developer are Copyright (C) 2008
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s): Marco Fabbri
23  *
24  * Alternatively, the contents of this file may be used under the terms of
25  * either the GNU General Public License Version 2 or later (the "GPL"), or
26  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27  * in which case the provisions of the GPL or the LGPL are applicable instead
28  * of those above. If you wish to allow use of your version of this file only
29  * under the terms of either the GPL or the LGPL, and not to allow others to
30  * use your version of this file under the terms of the MPL, indicate your
31  * decision by deleting the provisions above and replace them with the notice
32  * and other provisions required by the GPL or the LGPL. If you do not delete
33  * the provisions above, a recipient may use your version of this file under
34  * the terms of any one of the MPL, the GPL or the LGPL.
35  *
36  * ***** END LICENSE BLOCK ***** */
37
38 //-----------------------------------------------------------------------------
39 var BUGNUMBER = 452008;
40 var summary = 'Bad math with JIT';
41 var actual = '';
42 var expect = '';
43
44
45 //-----------------------------------------------------------------------------
46 test();
47 //-----------------------------------------------------------------------------
48
49 function test()
50 {
51   enterFunc ('test');
52   printBugNumber(BUGNUMBER);
53   printStatus (summary);
54
55   jit(true);
56  
57 // regression test for Bug 452008 - TM: SRP in Clipperz crypto library fails when JIT (TraceMonkey) is enabled. 
58
59   var x = [9385, 32112, 25383, 16317, 30138, 14565, 17812, 24500, 2719, 30174, 3546, 9096, 15352, 19120, 20648, 14334, 7426, 0, 0, 0];
60   var n = [27875, 25925, 30422, 12227, 27798, 32170, 10873, 21748, 30629, 26296, 20697, 5125, 4815, 2221, 14392, 23369, 5560, 2, 0, 0];
61   var np = 18229;
62   var expected = [18770, 31456, 17999, 32635, 27508, 29131, 2856, 16233, 5439, 27580, 7093, 18192, 30804, 5472, 8529, 28649, 14852, 0, 0, 0];
63
64 //globals
65   bpe=0;         //bits stored per array element
66   mask=0;        //AND this with an array element to chop it down to bpe bits
67
68 //initialize the global variables
69   for (bpe=0; (1<<(bpe+1)) > (1<<bpe); bpe++);  //bpe=number of bits in the mantissa on this platform
70   bpe>>=1;                   //bpe=number of bits in one element of the array representing the bigInt
71   mask=(1<<bpe)-1;           //AND the mask with an integer to get its bpe least significant bits
72
73
74 //the following global variables are scratchpad memory to
75 //reduce dynamic memory allocation in the inner loop
76   sa = new Array(0); //used in mont_()
77
78 //do x=y on bigInts x and y.  x must be an array at least as big as y (not counting the leading zeros in y).
79   function copy_(x,y) {
80     var i;
81     var k=x.length<y.length ? x.length : y.length;
82     for (i=0;i<k;i++)
83       x[i]=y[i];
84     for (i=k;i<x.length;i++)
85       x[i]=0;
86   }
87
88 //do x=y on bigInt x and integer y.
89   function copyInt_(x,n) {
90     var i,c;
91     for (c=n,i=0;i<x.length;i++) {
92       x[i]=c & mask;
93       c>>=bpe;
94     }
95   }
96
97 //is x > y? (x and y both nonnegative)
98   function greater(x,y) {
99     var i;
100     var k=(x.length<y.length) ? x.length : y.length;
101
102     for (i=x.length;i<y.length;i++)
103       if (y[i])
104         return 0;  //y has more digits
105
106     for (i=y.length;i<x.length;i++)
107       if (x[i])
108         return 1;  //x has more digits
109
110     for (i=k-1;i>=0;i--)
111       if (x[i]>y[i])
112         return 1;
113       else if (x[i]<y[i])
114         return 0;
115     return 0;
116   }
117
118
119 //do x=x*y*Ri mod n for bigInts x,y,n,
120 //  where Ri = 2**(-kn*bpe) mod n, and kn is the
121 //  number of elements in the n array, not
122 //  counting leading zeros.
123 //x must be large enough to hold the answer.
124 //It's OK if x and y are the same variable.
125 //must have:
126 //  x,y < n
127 //  n is odd
128 //  np = -(n^(-1)) mod radix
129   function mont_(x,y,n,np) {
130     var i,j,c,ui,t;
131     var kn=n.length;
132     var ky=y.length;
133
134     if (sa.length!=kn)
135       sa=new Array(kn);
136
137     for (;kn>0 && n[kn-1]==0;kn--); //ignore leading zeros of n
138     for (;ky>0 && y[ky-1]==0;ky--); //ignore leading zeros of y
139
140     copyInt_(sa,0);
141
142     //the following loop consumes 95% of the runtime for randTruePrime_() and powMod_() for large keys
143     for (i=0; i<kn; i++) {
144       t=sa[0]+x[i]*y[0];
145       ui=((t & mask) * np) & mask;  //the inner "& mask" is needed on Macintosh MSIE, but not windows MSIE
146       c=(t+ui*n[0]) >> bpe;
147       t=x[i];
148
149       //do sa=(sa+x[i]*y+ui*n)/b   where b=2**bpe
150       for (j=1;j<ky;j++) {
151         c+=sa[j]+t*y[j]+ui*n[j];
152         sa[j-1]=c & mask;
153         c>>=bpe;
154       }
155       for (;j<kn;j++) {
156         c+=sa[j]+ui*n[j];
157         sa[j-1]=c & mask;
158         c>>=bpe;
159       }
160       sa[j-1]=c & mask;
161     }
162
163     if (!greater(n,sa))
164       sub_(sa,n);
165     copy_(x,sa);
166   }
167
168   mont_(x, x, n, np);
169
170   var passed = expected.length == x.length;
171   for (var i = 0; i < expected.length; i++) {
172     if (passed)
173       passed = expected[i] == x[i];
174   }
175   print(passed);
176
177   jit(false);
178
179   expect = true;
180   actual = passed;
181
182   reportCompare(expect, actual, summary);
183
184   exitFunc ('test');
185 }