Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(824)

Side by Side Diff: src/harmony-math.js

Issue 157503002: A64: Synchronize with r18444. (Closed) Base URL: https://v8.googlecode.com/svn/branches/experimental/a64
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/handles.cc ('k') | src/heap.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 the V8 project authors. All rights reserved. 1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 29 matching lines...) Expand all
40 // ES6 draft 09-27-13, section 20.2.2.34. 40 // ES6 draft 09-27-13, section 20.2.2.34.
41 function MathTrunc(x) { 41 function MathTrunc(x) {
42 x = TO_NUMBER_INLINE(x); 42 x = TO_NUMBER_INLINE(x);
43 if (x > 0) return MathFloor(x); 43 if (x > 0) return MathFloor(x);
44 if (x < 0) return MathCeil(x); 44 if (x < 0) return MathCeil(x);
45 if (x === 0) return x; 45 if (x === 0) return x;
46 return NAN; 46 return NAN;
47 } 47 }
48 48
49 49
50 // ES6 draft 09-27-13, section 20.2.2.30.
51 function MathSinh(x) {
52 if (!IS_NUMBER(x)) x = NonNumberToNumber(x);
53 // Idempotent for NaN, +/-0 and +/-Infinity.
54 if (x === 0 || !NUMBER_IS_FINITE(x)) return x;
55 return (MathExp(x) - MathExp(-x)) / 2;
56 }
57
58
59 // ES6 draft 09-27-13, section 20.2.2.12.
60 function MathCosh(x) {
61 if (!IS_NUMBER(x)) x = NonNumberToNumber(x);
62 // Idempotent for NaN and +/-Infinity.
63 if (!NUMBER_IS_FINITE(x)) return x;
64 return (MathExp(x) + MathExp(-x)) / 2;
65 }
66
67
68 // ES6 draft 09-27-13, section 20.2.2.33.
69 function MathTanh(x) {
70 if (!IS_NUMBER(x)) x = NonNumberToNumber(x);
71 // Idempotent for +/-0.
72 if (x === 0) return x;
73 // Returns +/-1 for +/-Infinity.
74 if (!NUMBER_IS_FINITE(x)) return MathSign(x);
75 var exp1 = MathExp(x);
76 var exp2 = MathExp(-x);
77 return (exp1 - exp2) / (exp1 + exp2);
78 }
79
80
81 // ES6 draft 09-27-13, section 20.2.2.5.
82 function MathAsinh(x) {
83 if (!IS_NUMBER(x)) x = NonNumberToNumber(x);
84 // Idempotent for NaN, +/-0 and +/-Infinity.
85 if (x === 0 || !NUMBER_IS_FINITE(x)) return x;
86 if (x > 0) return MathLog(x + MathSqrt(x * x + 1));
87 // This is to prevent numerical errors caused by large negative x.
88 return -MathLog(-x + MathSqrt(x * x + 1));
89 }
90
91
92 // ES6 draft 09-27-13, section 20.2.2.3.
93 function MathAcosh(x) {
94 if (!IS_NUMBER(x)) x = NonNumberToNumber(x);
95 if (x < 1) return NAN;
96 // Idempotent for NaN and +Infinity.
97 if (!NUMBER_IS_FINITE(x)) return x;
98 return MathLog(x + MathSqrt(x + 1) * MathSqrt(x - 1));
99 }
100
101
102 // ES6 draft 09-27-13, section 20.2.2.7.
103 function MathAtanh(x) {
104 if (!IS_NUMBER(x)) x = NonNumberToNumber(x);
105 // Idempotent for +/-0.
106 if (x === 0) return x;
107 // Returns NaN for NaN and +/- Infinity.
108 if (!NUMBER_IS_FINITE(x)) return NAN;
109 return 0.5 * MathLog((1 + x) / (1 - x));
110 }
111
112
113 //ES6 draft 09-27-13, section 20.2.2.21.
114 function MathLog10(x) {
115 return MathLog(x) * 0.434294481903251828; // log10(x) = log(x)/log(10).
116 }
117
118
119 //ES6 draft 09-27-13, section 20.2.2.22.
120 function MathLog2(x) {
121 return MathLog(x) * 1.442695040888963407; // log2(x) = log(x)/log(2).
122 }
123
124
125 //ES6 draft 09-27-13, section 20.2.2.17.
126 function MathHypot(x, y) { // Function length is 2.
127 // We may want to introduce fast paths for two arguments and when
128 // normalization to avoid overflow is not necessary. For now, we
129 // simply assume the general case.
130 var length = %_ArgumentsLength();
131 var args = new InternalArray(length);
132 var max = 0;
133 for (var i = 0; i < length; i++) {
134 var n = %_Arguments(i);
135 if (!IS_NUMBER(n)) n = NonNumberToNumber(n);
136 if (n === INFINITY || n === -INFINITY) return INFINITY;
137 n = MathAbs(n);
138 if (n > max) max = n;
139 args[i] = n;
140 }
141
142 // Kahan summation to avoid rounding errors.
143 // Normalize the numbers to the largest one to avoid overflow.
144 if (max === 0) max = 1;
145 var sum = 0;
146 var compensation = 0;
147 for (var i = 0; i < length; i++) {
148 var n = args[i] / max;
149 var summand = n * n - compensation;
150 var preliminary = sum + summand;
151 compensation = (preliminary - sum) - summand;
152 sum = preliminary;
153 }
154 return MathSqrt(sum) * max;
155 }
156
157
50 function ExtendMath() { 158 function ExtendMath() {
51 %CheckIsBootstrapping(); 159 %CheckIsBootstrapping();
52 160
53 // Set up the non-enumerable functions on the Math object. 161 // Set up the non-enumerable functions on the Math object.
54 InstallFunctions($Math, DONT_ENUM, $Array( 162 InstallFunctions($Math, DONT_ENUM, $Array(
55 "sign", MathSign, 163 "sign", MathSign,
56 "trunc", MathTrunc 164 "trunc", MathTrunc,
165 "sinh", MathSinh,
166 "cosh", MathCosh,
167 "tanh", MathTanh,
168 "asinh", MathAsinh,
169 "acosh", MathAcosh,
170 "atanh", MathAtanh,
171 "log10", MathLog10,
172 "log2", MathLog2,
173 "hypot", MathHypot
57 )); 174 ));
58 } 175 }
59 176
60 ExtendMath(); 177 ExtendMath();
OLDNEW
« no previous file with comments | « src/handles.cc ('k') | src/heap.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698