MathLibrary.cpp 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "RuntimeLibraryPch.h"
  6. #include "Language/JavascriptMathOperators.h"
  7. #include "Math/CrtSSE2Math.h"
  8. #include <math.h>
  9. #if defined(_M_IX86) || defined(_M_X64)
  10. #pragma intrinsic(_mm_round_sd)
  11. #endif
  12. const LPCWSTR UCrtC99MathApis::LibraryName = _u("api-ms-win-crt-math-l1-1-0.dll");
  13. void UCrtC99MathApis::Ensure()
  14. {
  15. if (m_isInit)
  16. {
  17. return;
  18. }
  19. DelayLoadLibrary::EnsureFromSystemDirOnly();
  20. if (IsAvailable())
  21. {
  22. m_pfnlog2 = (PFNMathFn)GetFunction("log2");
  23. m_pfnlog1p = (PFNMathFn)GetFunction("log1p");
  24. m_pfnexpm1 = (PFNMathFn)GetFunction("expm1");
  25. m_pfnacosh = (PFNMathFn)GetFunction("acosh");
  26. m_pfnasinh = (PFNMathFn)GetFunction("asinh");
  27. m_pfnatanh = (PFNMathFn)GetFunction("atanh");
  28. m_pfntrunc = (PFNMathFn)GetFunction("trunc");
  29. m_pfncbrt = (PFNMathFn)GetFunction("cbrt");
  30. if (m_pfnlog2 == nullptr ||
  31. m_pfnlog1p == nullptr ||
  32. m_pfnexpm1 == nullptr ||
  33. m_pfnacosh == nullptr ||
  34. m_pfnasinh == nullptr ||
  35. m_pfnatanh == nullptr ||
  36. m_pfntrunc == nullptr ||
  37. m_pfncbrt == nullptr)
  38. {
  39. // If any of the APIs fail to load then presume the entire module is bogus and free it
  40. FreeLibrary(m_hModule);
  41. m_hModule = nullptr;
  42. }
  43. }
  44. }
  45. namespace Js
  46. {
  47. const double Math::PI = 3.1415926535897931;
  48. const double Math::E = 2.7182818284590451;
  49. const double Math::LN10 = 2.3025850929940459;
  50. const double Math::LN2 = 0.69314718055994529;
  51. const double Math::LOG2E = 1.4426950408889634;
  52. const double Math::LOG10E = 0.43429448190325182;
  53. const double Math::SQRT1_2 = 0.70710678118654757;
  54. const double Math::SQRT2 = 1.4142135623730951;
  55. const double Math::EPSILON = 2.2204460492503130808472633361816e-16;
  56. const double Math::MAX_SAFE_INTEGER = 9007199254740991.0;
  57. const double Math::MIN_SAFE_INTEGER = -9007199254740991.0;
  58. ///----------------------------------------------------------------------------
  59. /// Abs() returns the absolute value of the given number, as described in
  60. /// (ES5.0: S15.8.2.1).
  61. ///----------------------------------------------------------------------------
  62. Var Math::Abs(RecyclableObject* function, CallInfo callInfo, ...)
  63. {
  64. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  65. ARGUMENTS(args, callInfo);
  66. ScriptContext* scriptContext = function->GetScriptContext();
  67. Assert(!(callInfo.Flags & CallFlags_New));
  68. if (args.Info.Count >= 2)
  69. {
  70. Var arg = args[1];
  71. if (TaggedInt::Is(arg))
  72. {
  73. #if defined(_M_X64_OR_ARM64)
  74. __int64 result = ::_abs64(TaggedInt::ToInt32(arg));
  75. #else
  76. __int32 result = ::abs(TaggedInt::ToInt32(arg));
  77. #endif
  78. return JavascriptNumber::ToVar(result, scriptContext);
  79. }
  80. double x = JavascriptConversion::ToNumber(args[1], scriptContext);
  81. double result = Math::Abs(x);
  82. return JavascriptNumber::ToVarNoCheck(result, scriptContext);
  83. }
  84. else
  85. {
  86. return scriptContext->GetLibrary()->GetNaN();
  87. }
  88. }
  89. double Math::Abs(double x)
  90. {
  91. // ::fabs if linked from UCRT changes FPU ctrl word for NaN input
  92. if (NumberUtilities::IsNan(x))
  93. {
  94. // canonicalize to 0xFFF8000..., so we can tag correctly on x64.
  95. return NumberConstants::NaN;
  96. }
  97. return ::fabs(x);
  98. }
  99. ///----------------------------------------------------------------------------
  100. /// Acos() returns the arc-cosine of the given angle in radians, as described
  101. /// in (ES5.0: S15.8.2.2).
  102. ///----------------------------------------------------------------------------
  103. Var Math::Acos(RecyclableObject* function, CallInfo callInfo, ...)
  104. {
  105. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  106. ARGUMENTS(args, callInfo);
  107. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  108. ScriptContext* scriptContext = function->GetScriptContext();
  109. Assert(!(callInfo.Flags & CallFlags_New));
  110. if (args.Info.Count >= 2)
  111. {
  112. double x = JavascriptConversion::ToNumber(args[1], scriptContext);
  113. double result = Math::Acos(x);
  114. return JavascriptNumber::ToVarNoCheck(result, scriptContext);
  115. }
  116. else
  117. {
  118. return scriptContext->GetLibrary()->GetNaN();
  119. }
  120. }
  121. double Math::Acos(double x)
  122. {
  123. double result;
  124. #if defined(_M_IX86) && defined(_WIN32)
  125. // This is for perf, not for functionality
  126. // If non Win32 CRT implementation already support SSE2,
  127. // then we get most of the perf already.
  128. if (AutoSystemInfo::Data.SSE2Available())
  129. {
  130. _asm {
  131. movsd xmm0, x
  132. call dword ptr [__libm_sse2_acos]
  133. movsd result, xmm0
  134. }
  135. }
  136. else
  137. #endif
  138. {
  139. result = ::acos(x);
  140. }
  141. return result;
  142. }
  143. ///----------------------------------------------------------------------------
  144. /// Asin() returns the arc-sine of the given angle in radians, as described in
  145. /// (ES5.0: S15.8.2.3).
  146. ///----------------------------------------------------------------------------
  147. Var Math::Asin(RecyclableObject* function, CallInfo callInfo, ...)
  148. {
  149. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  150. ARGUMENTS(args, callInfo);
  151. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  152. ScriptContext* scriptContext = function->GetScriptContext();
  153. Assert(!(callInfo.Flags & CallFlags_New));
  154. if (args.Info.Count >= 2)
  155. {
  156. double x = JavascriptConversion::ToNumber(args[1], scriptContext);
  157. double result = Math::Asin(x);
  158. return JavascriptNumber::ToVarNoCheck(result, scriptContext);
  159. }
  160. else
  161. {
  162. return scriptContext->GetLibrary()->GetNaN();
  163. }
  164. }
  165. double Math::Asin(double x)
  166. {
  167. double result;
  168. #if defined(_M_IX86) && defined(_WIN32)
  169. // This is for perf, not for functionality
  170. // If non Win32 CRT implementation already support SSE2,
  171. // then we get most of the perf already.
  172. if (AutoSystemInfo::Data.SSE2Available())
  173. {
  174. _asm {
  175. movsd xmm0, x
  176. call dword ptr [__libm_sse2_asin]
  177. movsd result, xmm0
  178. }
  179. }
  180. else
  181. #endif
  182. {
  183. result = ::asin(x);
  184. }
  185. return result;
  186. }
  187. ///----------------------------------------------------------------------------
  188. /// Atan() returns the arc-tangent of the given angle in radians, as described
  189. /// in (ES5.0: S15.8.2.4).
  190. ///----------------------------------------------------------------------------
  191. Var Math::Atan(RecyclableObject* function, CallInfo callInfo, ...)
  192. {
  193. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  194. ARGUMENTS(args, callInfo);
  195. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  196. ScriptContext* scriptContext = function->GetScriptContext();
  197. Assert(!(callInfo.Flags & CallFlags_New));
  198. if (args.Info.Count >= 2)
  199. {
  200. double x = JavascriptConversion::ToNumber(args[1], scriptContext);
  201. double result = Math::Atan(x);
  202. return JavascriptNumber::ToVarNoCheck(result, scriptContext);
  203. }
  204. else
  205. {
  206. return scriptContext->GetLibrary()->GetNaN();
  207. }
  208. }
  209. double Math::Atan(double x)
  210. {
  211. double result;
  212. #if defined(_M_IX86) && defined(_WIN32)
  213. // This is for perf, not for functionality
  214. // If non Win32 CRT implementation already support SSE2,
  215. // then we get most of the perf already.
  216. if (AutoSystemInfo::Data.SSE2Available())
  217. {
  218. _asm {
  219. movsd xmm0, x
  220. call dword ptr [__libm_sse2_atan]
  221. movsd result, xmm0
  222. }
  223. }
  224. else
  225. #endif
  226. {
  227. result = ::atan(x);
  228. }
  229. return result;
  230. }
  231. ///----------------------------------------------------------------------------
  232. /// Atan2() returns the arc-tangent of the angle described by the given x and y
  233. /// lengths, as described in (ES5.0: S15.8.2.5).
  234. ///----------------------------------------------------------------------------
  235. Var Math::Atan2(RecyclableObject* function, CallInfo callInfo, ...)
  236. {
  237. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  238. ARGUMENTS(args, callInfo);
  239. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  240. ScriptContext* scriptContext = function->GetScriptContext();
  241. Assert(!(callInfo.Flags & CallFlags_New));
  242. if (args.Info.Count >= 3)
  243. {
  244. double x = JavascriptConversion::ToNumber(args[1], scriptContext);
  245. double y = JavascriptConversion::ToNumber(args[2], scriptContext);
  246. double result = Math::Atan2(x,y);
  247. return JavascriptNumber::ToVarNoCheck(result, scriptContext);
  248. }
  249. else
  250. {
  251. return scriptContext->GetLibrary()->GetNaN();
  252. }
  253. }
  254. double Math::Atan2( double x, double y )
  255. {
  256. double result;
  257. #if defined(_M_IX86) && defined(_WIN32)
  258. // This is for perf, not for functionality
  259. // If non Win32 CRT implementation already support SSE2,
  260. // then we get most of the perf already.
  261. if (AutoSystemInfo::Data.SSE2Available())
  262. {
  263. _asm
  264. {
  265. movsd xmm0, x
  266. movsd xmm1, y
  267. call dword ptr[__libm_sse2_atan2]
  268. movsd result, xmm0
  269. }
  270. }
  271. else
  272. #endif
  273. {
  274. result = ::atan2(x, y);
  275. }
  276. return result;
  277. }
  278. ///----------------------------------------------------------------------------
  279. /// Ceil() returns the smallest (closest to -Inf) number value that is not less
  280. /// than x and is equal to an integer, as described in (ES5.0: S15.8.2.6).
  281. ///----------------------------------------------------------------------------
  282. #pragma warning(push)
  283. #pragma warning(disable:4700) // uninitialized local variable 'output' used, for call to _mm_ceil_sd
  284. Var Math::Ceil(RecyclableObject* function, CallInfo callInfo, ...)
  285. {
  286. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  287. ARGUMENTS(args, callInfo);
  288. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  289. ScriptContext* scriptContext = function->GetScriptContext();
  290. Assert(!(callInfo.Flags & CallFlags_New));
  291. if (args.Info.Count >= 2)
  292. {
  293. Var inputArg = args[1];
  294. if (TaggedInt::Is(inputArg))
  295. {
  296. return inputArg;
  297. }
  298. double x = JavascriptConversion::ToNumber(inputArg, scriptContext);
  299. #if defined(_M_ARM32_OR_ARM64)
  300. if (Js::JavascriptNumber::IsNan(x))
  301. {
  302. return scriptContext->GetLibrary()->GetNaN();
  303. }
  304. #endif
  305. #if (defined(_M_IX86) || defined(_M_X64)) \
  306. && (__SSE4_1__ || _WIN32) // _mm_ceil_sd needs this
  307. if (AutoSystemInfo::Data.SSE4_1Available())
  308. {
  309. __m128d input, output;
  310. input = _mm_load_sd(&x);
  311. output = _mm_ceil_sd(input, input);
  312. int intResult = _mm_cvtsd_si32(output);
  313. if (TaggedInt::IsOverflow(intResult) || intResult == 0 || intResult == 0x80000000)
  314. {
  315. double dblResult;
  316. _mm_store_sd(&dblResult, output);
  317. if (intResult == 0 && !JavascriptNumber::IsNegZero(dblResult))
  318. {
  319. return JavascriptNumber::ToVar(0, scriptContext);
  320. }
  321. Assert(dblResult == ::ceil(x) || Js::JavascriptNumber::IsNan(dblResult));
  322. return JavascriptNumber::ToVarNoCheck(dblResult, scriptContext);
  323. }
  324. else
  325. {
  326. return JavascriptNumber::ToVar(intResult, scriptContext);
  327. }
  328. }
  329. else
  330. #endif
  331. {
  332. double result = ::ceil(x);
  333. intptr_t intResult = (intptr_t)result;
  334. if (TaggedInt::IsOverflow(intResult) || JavascriptNumber::IsNegZero(result))
  335. {
  336. return JavascriptNumber::ToVarNoCheck(result, scriptContext);
  337. }
  338. else
  339. {
  340. return JavascriptNumber::ToVar(intResult, scriptContext);
  341. }
  342. }
  343. }
  344. else
  345. {
  346. return scriptContext->GetLibrary()->GetNaN();
  347. }
  348. }
  349. #pragma warning(pop)
  350. ///----------------------------------------------------------------------------
  351. /// Cos() returns the cosine of the given angle in radians, as described in
  352. /// (ES5.0: S15.8.2.7).
  353. ///----------------------------------------------------------------------------
  354. Var Math::Cos(RecyclableObject* function, CallInfo callInfo, ...)
  355. {
  356. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  357. ARGUMENTS(args, callInfo);
  358. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  359. ScriptContext* scriptContext = function->GetScriptContext();
  360. Assert(!(callInfo.Flags & CallFlags_New));
  361. if (args.Info.Count >= 2)
  362. {
  363. double x = JavascriptConversion::ToNumber(args[1], scriptContext);
  364. double result = Math::Cos(x);
  365. return JavascriptNumber::ToVarNoCheck(result, scriptContext);
  366. }
  367. else
  368. {
  369. return scriptContext->GetLibrary()->GetNaN();
  370. }
  371. }
  372. double Math::Cos(double x)
  373. {
  374. double result;
  375. #if defined(_M_IX86) && defined(_WIN32)
  376. // This is for perf, not for functionality
  377. // If non Win32 CRT implementation already support SSE2,
  378. // then we get most of the perf already.
  379. if (AutoSystemInfo::Data.SSE2Available())
  380. {
  381. _asm {
  382. movsd xmm0, x
  383. call dword ptr [__libm_sse2_cos]
  384. movsd result, xmm0
  385. }
  386. }
  387. else
  388. #endif
  389. {
  390. result = ::cos(x);
  391. }
  392. return result;
  393. }
  394. ///----------------------------------------------------------------------------
  395. /// Exp() returns e^x, as described in (ES5.0: S15.8.2.8).
  396. ///----------------------------------------------------------------------------
  397. Var Math::Exp(RecyclableObject* function, CallInfo callInfo, ...)
  398. {
  399. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  400. ARGUMENTS(args, callInfo);
  401. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  402. ScriptContext* scriptContext = function->GetScriptContext();
  403. Assert(!(callInfo.Flags & CallFlags_New));
  404. if (args.Info.Count >= 2)
  405. {
  406. double x = JavascriptConversion::ToNumber(args[1], scriptContext);
  407. double result = Math::Exp(x);
  408. return JavascriptNumber::ToVarNoCheck(result, scriptContext);
  409. }
  410. else
  411. {
  412. return scriptContext->GetLibrary()->GetNaN();
  413. }
  414. }
  415. double Math::Exp(double x)
  416. {
  417. double result;
  418. #if defined(_M_IX86) && defined(_WIN32)
  419. // This is for perf, not for functionality
  420. // If non Win32 CRT implementation already support SSE2,
  421. // then we get most of the perf already.
  422. if (AutoSystemInfo::Data.SSE2Available())
  423. {
  424. _asm {
  425. movsd xmm0, x
  426. call dword ptr [__libm_sse2_exp]
  427. movsd result, xmm0
  428. }
  429. }
  430. else
  431. #endif
  432. {
  433. result = ::exp(x);
  434. }
  435. return result;
  436. }
  437. ///----------------------------------------------------------------------------
  438. /// Floor() returns the greatest (closest to +Inf) number value that is not
  439. /// greater than x and is equal to an integer, as described in
  440. /// (ES5.0: S15.8.2.9).
  441. ///----------------------------------------------------------------------------
  442. Var Math::Floor(RecyclableObject* function, CallInfo callInfo, ...)
  443. {
  444. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  445. ARGUMENTS(args, callInfo);
  446. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  447. ScriptContext* scriptContext = function->GetScriptContext();
  448. Assert(!(callInfo.Flags & CallFlags_New));
  449. if (args.Info.Count >= 2)
  450. {
  451. Var input = args[1];
  452. if (TaggedInt::Is(input))
  453. {
  454. return input;
  455. }
  456. double x = JavascriptConversion::ToNumber(input, scriptContext);
  457. #if defined(_M_ARM32_OR_ARM64)
  458. if (Js::JavascriptNumber::IsNan(x))
  459. {
  460. return scriptContext->GetLibrary()->GetNaN();
  461. }
  462. #endif
  463. return Math::FloorDouble(x, scriptContext);
  464. }
  465. else
  466. {
  467. return scriptContext->GetLibrary()->GetNaN();
  468. }
  469. }
  470. #pragma warning(push)
  471. #pragma warning(disable:4700) // // uninitialized local variable 'output' used, for call to _mm_floor_sd
  472. Var inline Math::FloorDouble(double d, ScriptContext *scriptContext)
  473. {
  474. // xplat-todo: use intrinsics here on linux
  475. #ifdef _MSC_VER
  476. #if defined(_M_IX86) || defined(_M_X64)
  477. if (AutoSystemInfo::Data.SSE4_1Available())
  478. {
  479. __m128d input, output;
  480. int intResult;
  481. input = _mm_load_sd(&d);
  482. if (d >= 0.0)
  483. {
  484. output = input;
  485. }
  486. else
  487. {
  488. output = _mm_floor_sd(input, input);
  489. }
  490. intResult = _mm_cvttsd_si32(output);
  491. if (TaggedInt::IsOverflow(intResult) || intResult == 0x80000000 || JavascriptNumber::IsNegZero(d))
  492. {
  493. double dblResult;
  494. if (d >= 0.0)
  495. {
  496. output = _mm_floor_sd(output, input);
  497. }
  498. _mm_store_sd(&dblResult, output);
  499. Assert(dblResult == ::floor(d) || Js::JavascriptNumber::IsNan(dblResult));
  500. return JavascriptNumber::ToVarNoCheck(dblResult, scriptContext);
  501. }
  502. else
  503. {
  504. Assert(intResult == (int)::floor(d));
  505. return JavascriptNumber::ToVar(intResult, scriptContext);
  506. }
  507. }
  508. else
  509. #endif
  510. #endif
  511. {
  512. intptr_t intResult;
  513. if (d >= 0.0)
  514. {
  515. intResult = (intptr_t)d;
  516. }
  517. else
  518. {
  519. d = ::floor(d);
  520. intResult = (intptr_t)d;
  521. }
  522. if (TaggedInt::IsOverflow(intResult) || JavascriptNumber::IsNegZero(d))
  523. {
  524. return JavascriptNumber::ToVarNoCheck(::floor(d), scriptContext);
  525. }
  526. else
  527. {
  528. return JavascriptNumber::ToVar(intResult, scriptContext);
  529. }
  530. }
  531. }
  532. #pragma warning(pop)
  533. ///----------------------------------------------------------------------------
  534. /// Log() returns the natural logarithm of x, as described in
  535. /// (ES5.0: S15.8.2.10).
  536. ///----------------------------------------------------------------------------
  537. Var Math::Log(RecyclableObject* function, CallInfo callInfo, ...)
  538. {
  539. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  540. ARGUMENTS(args, callInfo);
  541. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  542. ScriptContext* scriptContext = function->GetScriptContext();
  543. Assert(!(callInfo.Flags & CallFlags_New));
  544. if (args.Info.Count >= 2)
  545. {
  546. double x = JavascriptConversion::ToNumber(args[1], scriptContext);
  547. double result = Math::Log(x);
  548. return JavascriptNumber::ToVarNoCheck(result, scriptContext);
  549. }
  550. else
  551. {
  552. return scriptContext->GetLibrary()->GetNaN();
  553. }
  554. }
  555. double Math::Log(double x)
  556. {
  557. double result;
  558. #if defined(_M_IX86) && defined(_WIN32)
  559. // This is for perf, not for functionality
  560. // If non Win32 CRT implementation already support SSE2,
  561. // then we get most of the perf already.
  562. if (AutoSystemInfo::Data.SSE2Available())
  563. {
  564. _asm {
  565. movsd xmm0, x
  566. call dword ptr [__libm_sse2_log]
  567. movsd result, xmm0
  568. }
  569. }
  570. else
  571. #endif
  572. {
  573. result = ::log(x);
  574. }
  575. return result;
  576. }
  577. ///----------------------------------------------------------------------------
  578. /// Max() returns the maximum of a series of numbers, as described in
  579. /// (ES5.0: S15.8.2.11):
  580. /// - Arg:0 = "this"
  581. /// - Arg:1-n = Values to compare
  582. ///----------------------------------------------------------------------------
  583. Var Math::Max(RecyclableObject* function, CallInfo callInfo, ...)
  584. {
  585. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  586. ARGUMENTS(args, callInfo);
  587. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  588. ScriptContext* scriptContext = function->GetScriptContext();
  589. Assert(!(callInfo.Flags & CallFlags_New));
  590. if (args.Info.Count <= 1)
  591. {
  592. return scriptContext->GetLibrary()->GetNegativeInfinite();
  593. }
  594. else if (args.Info.Count == 2)
  595. {
  596. double result = JavascriptConversion::ToNumber(args[1], scriptContext);
  597. return JavascriptNumber::ToVarNoCheck(result, scriptContext);
  598. }
  599. else if (args.Info.Count == 3)
  600. {
  601. if (TaggedInt::Is(args[1]) && TaggedInt::Is(args[2]))
  602. {
  603. return TaggedInt::ToVarUnchecked(max(TaggedInt::ToInt32(args[1]), TaggedInt::ToInt32(args[2])));
  604. }
  605. }
  606. double current = JavascriptConversion::ToNumber(args[1], scriptContext);
  607. if(JavascriptNumber::IsNan(current))
  608. {
  609. return scriptContext->GetLibrary()->GetNaN();
  610. }
  611. for (uint idxArg = 2; idxArg < args.Info.Count; idxArg++)
  612. {
  613. double compare = JavascriptConversion::ToNumber(args[idxArg], scriptContext);
  614. if(JavascriptNumber::IsNan(compare))
  615. {
  616. return scriptContext->GetLibrary()->GetNaN();
  617. }
  618. if((JavascriptNumber::IsNegZero(current) && compare == 0) ||
  619. current < compare )
  620. {
  621. current = compare;
  622. }
  623. }
  624. return JavascriptNumber::ToVarNoCheck(current, scriptContext);
  625. }
  626. ///----------------------------------------------------------------------------
  627. /// Min() returns the minimum of a series of numbers, as described in
  628. /// (ES5.0: S15.8.2.12):
  629. /// - Arg:0 = "this"
  630. /// - Arg:1-n = Values to compare
  631. ///----------------------------------------------------------------------------
  632. Var Math::Min(RecyclableObject* function, CallInfo callInfo, ...)
  633. {
  634. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  635. ARGUMENTS(args, callInfo);
  636. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  637. ScriptContext* scriptContext = function->GetScriptContext();
  638. Assert(!(callInfo.Flags & CallFlags_New));
  639. if (args.Info.Count <= 1)
  640. {
  641. return scriptContext->GetLibrary()->GetPositiveInfinite();
  642. }
  643. else if (args.Info.Count == 2)
  644. {
  645. double result = JavascriptConversion::ToNumber(args[1], scriptContext);
  646. return JavascriptNumber::ToVarNoCheck(result, scriptContext);
  647. }
  648. else if (args.Info.Count == 3)
  649. {
  650. if (TaggedInt::Is(args[1]) && TaggedInt::Is(args[2]))
  651. {
  652. return TaggedInt::ToVarUnchecked(min(TaggedInt::ToInt32(args[1]), TaggedInt::ToInt32(args[2])));
  653. }
  654. }
  655. double current = JavascriptConversion::ToNumber(args[1], scriptContext);
  656. if(JavascriptNumber::IsNan(current))
  657. {
  658. return scriptContext->GetLibrary()->GetNaN();
  659. }
  660. for (uint idxArg = 2; idxArg < args.Info.Count; idxArg++)
  661. {
  662. double compare = JavascriptConversion::ToNumber(args[idxArg], scriptContext);
  663. if(JavascriptNumber::IsNan(compare))
  664. {
  665. return scriptContext->GetLibrary()->GetNaN();
  666. }
  667. if((JavascriptNumber::IsNegZero(compare) && current == 0) ||
  668. current > compare )
  669. {
  670. current = compare;
  671. }
  672. }
  673. return JavascriptNumber::ToVarNoCheck(current, scriptContext);
  674. }
  675. ///----------------------------------------------------------------------------
  676. /// Pow() returns x ^ y, as described in (ES5.0: S15.8.2.13).
  677. ///----------------------------------------------------------------------------
  678. Var Math::Pow(RecyclableObject* function, CallInfo callInfo, ...)
  679. {
  680. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  681. ARGUMENTS(args, callInfo);
  682. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  683. ScriptContext* scriptContext = function->GetScriptContext();
  684. Assert(!(callInfo.Flags & CallFlags_New));
  685. if (args.Info.Count >= 3)
  686. {
  687. double x = JavascriptConversion::ToNumber(args[1], scriptContext);
  688. double y = JavascriptConversion::ToNumber(args[2], scriptContext);
  689. double result = Math::Pow( x, y );
  690. return JavascriptNumber::ToVarNoCheck(result, scriptContext);
  691. }
  692. else
  693. {
  694. return scriptContext->GetLibrary()->GetNaN();
  695. }
  696. }
  697. double Math::Pow( double x, double y )
  698. {
  699. double result = 0;
  700. #if defined(_M_IX86) && defined(_WIN32) // TODO: xplat support
  701. // We can't just use "if (0 == y)" because NaN compares
  702. // equal to 0 according to our compilers.
  703. if( 0 == NumberUtilities::LuLoDbl( y ) && 0 == ( NumberUtilities::LuHiDbl( y ) & 0x7FFFFFFF ) )
  704. {
  705. // Result is 1 even if x is NaN.
  706. result = 1;
  707. }
  708. else if( 1.0 == Math::Abs( x ) && !NumberUtilities::IsFinite( y ) )
  709. {
  710. result = JavascriptNumber::NaN;
  711. }
  712. else
  713. {
  714. int32 intY;
  715. // range [-8, 8] is from JavascriptNumber::DirectPowDoubleInt
  716. if (JavascriptNumber::TryGetInt32Value(y, &intY) && intY >= -8 && intY <= 8)
  717. {
  718. result = JavascriptNumber::DirectPowDoubleInt(x, intY);
  719. }
  720. else if( AutoSystemInfo::Data.SSE2Available() )
  721. {
  722. _asm {
  723. movsd xmm0, x
  724. movsd xmm1, y
  725. call dword ptr[__libm_sse2_pow]
  726. movsd result, xmm0
  727. }
  728. }
  729. else
  730. {
  731. result = ::pow( x, y );
  732. }
  733. }
  734. #else
  735. result = JavascriptNumber::DirectPow( x, y );
  736. #endif
  737. return result;
  738. }
  739. ///----------------------------------------------------------------------------
  740. /// Random() returns a random number 0 <= n < 1.0, as described in
  741. /// (ES5.0: S15.8.2.14).
  742. ///----------------------------------------------------------------------------
  743. Var Math::Random(RecyclableObject* function, CallInfo callInfo, ...)
  744. {
  745. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  746. AssertMsg(callInfo.Count > 0, "Should always have implicit 'this'");
  747. ScriptContext* scriptContext = function->GetScriptContext();
  748. Assert(!(callInfo.Flags & CallFlags_New));
  749. double res = JavascriptMath::Random(scriptContext);
  750. return JavascriptNumber::ToVarNoCheck(res, scriptContext);
  751. }
  752. ///----------------------------------------------------------------------------
  753. /// Round() returns the number value that is closest to x and is equal to an
  754. /// integer, as described in (ES5.0: S15.8.2.15).
  755. ///----------------------------------------------------------------------------
  756. Var Math::Round(RecyclableObject* function, CallInfo callInfo, ...)
  757. {
  758. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  759. ARGUMENTS(args, callInfo);
  760. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  761. ScriptContext* scriptContext = function->GetScriptContext();
  762. Assert(!(callInfo.Flags & CallFlags_New));
  763. if (args.Info.Count >= 2)
  764. {
  765. Var input = args[1];
  766. if (TaggedInt::Is(input))
  767. {
  768. return input;
  769. }
  770. double x = JavascriptConversion::ToNumber(args[1], scriptContext);
  771. if(JavascriptNumber::IsNan(x))
  772. {
  773. return scriptContext->GetLibrary()->GetNaN();
  774. }
  775. // for doubles, if x >= 2^52 or <= -2^52, x must be an integer, and adding 0.5 will overflow the
  776. // integer to the next one. Therefore, we directly return x.
  777. if (x == 0.0 || !NumberUtilities::IsFinite(x) || x >= 4503599627370496.0 || x <= -4503599627370496.0)
  778. {
  779. // 0.0 catches the -0 case...
  780. return JavascriptNumber::ToVarNoCheck(x, scriptContext);
  781. }
  782. if (x > 0 && x < 0.5) {
  783. return JavascriptNumber::ToVarNoCheck((double)Js::JavascriptNumber::k_Zero, scriptContext);
  784. }
  785. if(x < 0 && x >= -0.5)
  786. {
  787. return scriptContext->GetLibrary()->GetNegativeZero();
  788. }
  789. return Math::FloorDouble(x+0.5, scriptContext);
  790. }
  791. else
  792. {
  793. return scriptContext->GetLibrary()->GetNaN();
  794. }
  795. }
  796. ///----------------------------------------------------------------------------
  797. /// Sin() returns the sine of the given angle in radians, as described in
  798. /// (ES5.0: S15.8.2.16).
  799. ///----------------------------------------------------------------------------
  800. Var Math::Sin(RecyclableObject* function, CallInfo callInfo, ...)
  801. {
  802. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  803. ARGUMENTS(args, callInfo);
  804. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  805. ScriptContext* scriptContext = function->GetScriptContext();
  806. Assert(!(callInfo.Flags & CallFlags_New));
  807. if (args.Info.Count >= 2)
  808. {
  809. double x = JavascriptConversion::ToNumber(args[1], scriptContext);
  810. double result = Math::Sin(x);
  811. return JavascriptNumber::ToVarNoCheck(result, scriptContext);
  812. }
  813. else
  814. {
  815. return scriptContext->GetLibrary()->GetNaN();
  816. }
  817. }
  818. double Math::Sin(double x)
  819. {
  820. double result;
  821. #if defined(_M_IX86) && defined(_WIN32)
  822. // This is for perf, not for functionality
  823. // If non Win32 CRT implementation already support SSE2,
  824. // then we get most of the perf already.
  825. if (AutoSystemInfo::Data.SSE2Available())
  826. {
  827. _asm {
  828. movsd xmm0, x
  829. call dword ptr [__libm_sse2_sin]
  830. movsd result, xmm0
  831. }
  832. }
  833. else
  834. #endif
  835. {
  836. result = ::sin(x);
  837. }
  838. return result;
  839. }
  840. ///----------------------------------------------------------------------------
  841. /// Sqrt() returns the square-root of the given number, as described in
  842. /// (ES5.0: S15.8.2.17).
  843. ///----------------------------------------------------------------------------
  844. Var Math::Sqrt(RecyclableObject* function, CallInfo callInfo, ...)
  845. {
  846. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  847. ARGUMENTS(args, callInfo);
  848. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  849. ScriptContext* scriptContext = function->GetScriptContext();
  850. Assert(!(callInfo.Flags & CallFlags_New));
  851. if (args.Info.Count >= 2)
  852. {
  853. Var arg = args[1];
  854. double x = JavascriptConversion::ToNumber(arg, scriptContext);
  855. double result = ::sqrt(x);
  856. if (TaggedInt::Is(arg))
  857. {
  858. return JavascriptNumber::ToVarIntCheck(result, scriptContext);
  859. }
  860. else
  861. {
  862. return JavascriptNumber::ToVarNoCheck(result, scriptContext);
  863. }
  864. }
  865. else
  866. {
  867. return scriptContext->GetLibrary()->GetNaN();
  868. }
  869. }
  870. ///----------------------------------------------------------------------------
  871. /// Tan() returns the tangent of the given angle, in radians, as described in
  872. /// (ES5.0: S15.8.2.18).
  873. ///----------------------------------------------------------------------------
  874. Var Math::Tan(RecyclableObject* function, CallInfo callInfo, ...)
  875. {
  876. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  877. ARGUMENTS(args, callInfo);
  878. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  879. ScriptContext* scriptContext = function->GetScriptContext();
  880. Assert(!(callInfo.Flags & CallFlags_New));
  881. if (args.Info.Count >= 2)
  882. {
  883. double x = JavascriptConversion::ToNumber(args[1], scriptContext);
  884. double result = Math::Tan( x );
  885. return JavascriptNumber::ToVarNoCheck(result, scriptContext);
  886. }
  887. else
  888. {
  889. return scriptContext->GetLibrary()->GetNaN();
  890. }
  891. }
  892. double Math::Tan( double x )
  893. {
  894. double result = 0;
  895. #if defined(_M_IX86) && defined(_WIN32)
  896. // This is for perf, not for functionality
  897. // If non Win32 CRT implementation already support SSE2,
  898. // then we get most of the perf already.
  899. if( AutoSystemInfo::Data.SSE2Available() )
  900. {
  901. _asm {
  902. movsd xmm0, x
  903. call dword ptr[__libm_sse2_tan]
  904. movsd result, xmm0
  905. }
  906. }
  907. else
  908. #endif
  909. {
  910. result = ::tan( x );
  911. }
  912. return result;
  913. }
  914. ///----------------------------------------------------------------------------
  915. /// Log10() returns the base 10 logarithm of the given number, as described in
  916. /// (ES6.0: S20.2.2.21).
  917. ///----------------------------------------------------------------------------
  918. Var Math::Log10(RecyclableObject* function, CallInfo callInfo, ...)
  919. {
  920. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  921. ARGUMENTS(args, callInfo);
  922. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  923. ScriptContext* scriptContext = function->GetScriptContext();
  924. Assert(!(callInfo.Flags & CallFlags_New));
  925. CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Log10);
  926. if (args.Info.Count >= 2)
  927. {
  928. double x = JavascriptConversion::ToNumber(args[1], scriptContext);
  929. double result = Math::Log10(x);
  930. return JavascriptNumber::ToVarNoCheck(result, scriptContext);
  931. }
  932. else
  933. {
  934. return scriptContext->GetLibrary()->GetNaN();
  935. }
  936. }
  937. double Math::Log10(double x)
  938. {
  939. double result;
  940. #if defined(_M_IX86) && defined(_WIN32)
  941. // This is for perf, not for functionality
  942. // If non Win32 CRT implementation already support SSE2,
  943. // then we get most of the perf already.
  944. if (AutoSystemInfo::Data.SSE2Available())
  945. {
  946. _asm {
  947. movsd xmm0, x
  948. call dword ptr [__libm_sse2_log10]
  949. movsd result, xmm0
  950. }
  951. }
  952. else
  953. #endif
  954. {
  955. result = ::log10(x);
  956. }
  957. return result;
  958. }
  959. ///----------------------------------------------------------------------------
  960. /// Log2() returns the base 2 logarithm of the given number, as described in
  961. /// (ES6.0: S20.2.2.22).
  962. ///----------------------------------------------------------------------------
  963. Var Math::Log2(RecyclableObject* function, CallInfo callInfo, ...)
  964. {
  965. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  966. ARGUMENTS(args, callInfo);
  967. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  968. ScriptContext* scriptContext = function->GetScriptContext();
  969. Assert(!(callInfo.Flags & CallFlags_New));
  970. CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Log2);
  971. if (args.Info.Count >= 2)
  972. {
  973. double x = JavascriptConversion::ToNumber(args[1], scriptContext);
  974. double result = Math::Log2(x, scriptContext);
  975. return JavascriptNumber::ToVarNoCheck(result, scriptContext);
  976. }
  977. else
  978. {
  979. return scriptContext->GetLibrary()->GetNaN();
  980. }
  981. }
  982. double Math::Log2(double x, ScriptContext* scriptContext)
  983. {
  984. #if defined(WHEN_UCRT_IS_LINKED_IN_BUILD) || !defined(_WIN32)
  985. return ::log2( x );
  986. #else
  987. // TODO: THE FALLBACK IS NOT ACCURATE; Universal CRT is available on Threshold so we should never fallback but ideally we would link at build time to these APIs instead of loading them at runtime
  988. UCrtC99MathApis* ucrtC99MathApis = scriptContext->GetThreadContext()->GetUCrtC99MathApis();
  989. return ucrtC99MathApis->IsAvailable() ?
  990. ucrtC99MathApis->log2(x) :
  991. Math::Log( x ) / Math::LN2;
  992. #endif
  993. }
  994. ///----------------------------------------------------------------------------
  995. /// Log1p() returns the natural logarithm of one plus the given number, as
  996. /// described in (ES6.0: S20.2.2.20).
  997. ///----------------------------------------------------------------------------
  998. Var Math::Log1p(RecyclableObject* function, CallInfo callInfo, ...)
  999. {
  1000. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  1001. ARGUMENTS(args, callInfo);
  1002. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  1003. ScriptContext* scriptContext = function->GetScriptContext();
  1004. Assert(!(callInfo.Flags & CallFlags_New));
  1005. CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Log1p);
  1006. if (args.Info.Count >= 2)
  1007. {
  1008. double x = JavascriptConversion::ToNumber(args[1], scriptContext);
  1009. #if defined(WHEN_UCRT_IS_LINKED_IN_BUILD) || !defined(_WIN32)
  1010. double result = ::log1p(x);
  1011. #else
  1012. // TODO: THE FALLBACK IS NOT ACCURATE; Universal CRT is available on Threshold so we should never fallback but ideally we would link at build time to these APIs instead of loading them at runtime
  1013. UCrtC99MathApis* ucrtC99MathApis = scriptContext->GetThreadContext()->GetUCrtC99MathApis();
  1014. double result = ucrtC99MathApis->IsAvailable() ?
  1015. ucrtC99MathApis->log1p(x):
  1016. (JavascriptNumber::IsNegZero(x)) ? x : Math::Log(1.0 + x);
  1017. #endif
  1018. return JavascriptNumber::ToVarNoCheck(result, scriptContext);
  1019. }
  1020. else
  1021. {
  1022. return scriptContext->GetLibrary()->GetNaN();
  1023. }
  1024. }
  1025. ///----------------------------------------------------------------------------
  1026. /// Expm1() returns the result of subtracting one from the exponential
  1027. /// function of the given number, as described in (ES6.0: S20.2.2.14).
  1028. ///----------------------------------------------------------------------------
  1029. Var Math::Expm1(RecyclableObject* function, CallInfo callInfo, ...)
  1030. {
  1031. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  1032. ARGUMENTS(args, callInfo);
  1033. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  1034. ScriptContext* scriptContext = function->GetScriptContext();
  1035. Assert(!(callInfo.Flags & CallFlags_New));
  1036. CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Expm1);
  1037. if (args.Info.Count >= 2)
  1038. {
  1039. double x = JavascriptConversion::ToNumber(args[1], scriptContext);
  1040. #if defined(WHEN_UCRT_IS_LINKED_IN_BUILD) || !defined(_WIN32)
  1041. double result = ::expm1(x);
  1042. #else
  1043. // TODO: THE FALLBACK IS NOT ACCURATE; Universal CRT is available on Threshold so we should never fallback but ideally we would link at build time to these APIs instead of loading them at runtime
  1044. UCrtC99MathApis* ucrtC99MathApis = scriptContext->GetThreadContext()->GetUCrtC99MathApis();
  1045. double result = ucrtC99MathApis->IsAvailable() ?
  1046. ucrtC99MathApis->expm1(x) :
  1047. (JavascriptNumber::IsNegZero(x)) ? x : Math::Exp(x) - 1.0;
  1048. #endif
  1049. return JavascriptNumber::ToVarNoCheck(result, scriptContext);
  1050. }
  1051. else
  1052. {
  1053. return scriptContext->GetLibrary()->GetNaN();
  1054. }
  1055. }
  1056. ///----------------------------------------------------------------------------
  1057. /// Cosh() returns the hyperbolic cosine of the given number, as described in
  1058. /// (ES6.0: S20.2.2.12).
  1059. ///----------------------------------------------------------------------------
  1060. Var Math::Cosh(RecyclableObject* function, CallInfo callInfo, ...)
  1061. {
  1062. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  1063. ARGUMENTS(args, callInfo);
  1064. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  1065. ScriptContext* scriptContext = function->GetScriptContext();
  1066. Assert(!(callInfo.Flags & CallFlags_New));
  1067. CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Cosh);
  1068. if (args.Info.Count >= 2)
  1069. {
  1070. double x = JavascriptConversion::ToNumber(args[1], scriptContext);
  1071. double result = ::cosh(x);
  1072. return JavascriptNumber::ToVarNoCheck(result, scriptContext);
  1073. }
  1074. else
  1075. {
  1076. return scriptContext->GetLibrary()->GetNaN();
  1077. }
  1078. }
  1079. ///----------------------------------------------------------------------------
  1080. /// Sinh() returns the hyperbolic sine of the given number, as described in
  1081. /// (ES6.0: S20.2.2.30).
  1082. ///----------------------------------------------------------------------------
  1083. Var Math::Sinh(RecyclableObject* function, CallInfo callInfo, ...)
  1084. {
  1085. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  1086. ARGUMENTS(args, callInfo);
  1087. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  1088. ScriptContext* scriptContext = function->GetScriptContext();
  1089. Assert(!(callInfo.Flags & CallFlags_New));
  1090. CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Sinh);
  1091. if (args.Info.Count >= 2)
  1092. {
  1093. double x = JavascriptConversion::ToNumber(args[1], scriptContext);
  1094. double result = ::sinh(x);
  1095. return JavascriptNumber::ToVarNoCheck(result, scriptContext);
  1096. }
  1097. else
  1098. {
  1099. return scriptContext->GetLibrary()->GetNaN();
  1100. }
  1101. }
  1102. ///----------------------------------------------------------------------------
  1103. /// Tanh() returns the hyperbolic tangent of the given number, as described in
  1104. /// (ES6.0: S20.2.2.33).
  1105. ///----------------------------------------------------------------------------
  1106. Var Math::Tanh(RecyclableObject* function, CallInfo callInfo, ...)
  1107. {
  1108. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  1109. ARGUMENTS(args, callInfo);
  1110. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  1111. ScriptContext* scriptContext = function->GetScriptContext();
  1112. Assert(!(callInfo.Flags & CallFlags_New));
  1113. CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Tanh);
  1114. if (args.Info.Count >= 2)
  1115. {
  1116. double x = JavascriptConversion::ToNumber(args[1], scriptContext);
  1117. double result = ::tanh(x);
  1118. return JavascriptNumber::ToVarNoCheck(result, scriptContext);
  1119. }
  1120. else
  1121. {
  1122. return scriptContext->GetLibrary()->GetNaN();
  1123. }
  1124. }
  1125. ///----------------------------------------------------------------------------
  1126. /// Acosh() returns the inverse hyperbolic cosine of the given number, as
  1127. /// described in (ES6.0: S20.2.2.3).
  1128. ///----------------------------------------------------------------------------
  1129. Var Math::Acosh(RecyclableObject* function, CallInfo callInfo, ...)
  1130. {
  1131. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  1132. ARGUMENTS(args, callInfo);
  1133. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  1134. ScriptContext* scriptContext = function->GetScriptContext();
  1135. Assert(!(callInfo.Flags & CallFlags_New));
  1136. CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Acosh);
  1137. if (args.Info.Count >= 2)
  1138. {
  1139. double x = JavascriptConversion::ToNumber(args[1], scriptContext);
  1140. #if defined(WHEN_UCRT_IS_LINKED_IN_BUILD) || !defined(_WIN32)
  1141. double result = ::acosh(x);
  1142. return JavascriptNumber::ToVarNoCheck(result, scriptContext);
  1143. #else
  1144. // TODO: THE FALLBACK IS NOT ACCURATE; Universal CRT is available on Threshold so we should never fallback but ideally we would link at build time to these APIs instead of loading them at runtime
  1145. UCrtC99MathApis* ucrtC99MathApis = scriptContext->GetThreadContext()->GetUCrtC99MathApis();
  1146. if (ucrtC99MathApis->IsAvailable())
  1147. {
  1148. return JavascriptNumber::ToVarNoCheck(ucrtC99MathApis->acosh(x), scriptContext);
  1149. }
  1150. else if (x >= 1.0)
  1151. {
  1152. // Can be smarter about large values of x, e.g. as x -> Infinity, sqrt(x^2 - 1) -> x
  1153. // Therefore for large x, log(x+x) is sufficient, but how to decide what a large x is?
  1154. // Also ln(x+x) = ln 2x = ln 2 + ln x
  1155. double result = (x == 1.0) ? 0.0 : Math::Log(x + ::sqrt(x*x - 1.0));
  1156. return JavascriptNumber::ToVarNoCheck(result, scriptContext);
  1157. }
  1158. else
  1159. {
  1160. return scriptContext->GetLibrary()->GetNaN();
  1161. }
  1162. #endif
  1163. }
  1164. else
  1165. {
  1166. return scriptContext->GetLibrary()->GetNaN();
  1167. }
  1168. }
  1169. ///----------------------------------------------------------------------------
  1170. /// Asinh() returns the inverse hyperbolic sine of the given number, as
  1171. /// described in (ES6.0: S20.2.2.5).
  1172. ///----------------------------------------------------------------------------
  1173. Var Math::Asinh(RecyclableObject* function, CallInfo callInfo, ...)
  1174. {
  1175. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  1176. ARGUMENTS(args, callInfo);
  1177. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  1178. ScriptContext* scriptContext = function->GetScriptContext();
  1179. Assert(!(callInfo.Flags & CallFlags_New));
  1180. CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Asinh);
  1181. if (args.Info.Count >= 2)
  1182. {
  1183. double x = JavascriptConversion::ToNumber(args[1], scriptContext);
  1184. #if defined(WHEN_UCRT_IS_LINKED_IN_BUILD) || !defined(_WIN32)
  1185. double result = ::asinh(x);
  1186. return JavascriptNumber::ToVarNoCheck(result, scriptContext);
  1187. #else
  1188. // TODO: THE FALLBACK IS NOT ACCURATE; Universal CRT is available on Threshold so we should never fallback but ideally we would link at build time to these APIs instead of loading them at runtime
  1189. UCrtC99MathApis* ucrtC99MathApis = scriptContext->GetThreadContext()->GetUCrtC99MathApis();
  1190. if (ucrtC99MathApis->IsAvailable())
  1191. {
  1192. return JavascriptNumber::ToVarNoCheck(ucrtC99MathApis->asinh(x), scriptContext);
  1193. }
  1194. else
  1195. {
  1196. double result = JavascriptNumber::IsNegZero(x) ? x :
  1197. JavascriptNumber::IsPosInf(x) ? JavascriptNumber::POSITIVE_INFINITY :
  1198. JavascriptNumber::IsNegInf(x) ? JavascriptNumber::NEGATIVE_INFINITY :
  1199. Math::Log(x + ::sqrt(x*x + 1.0));
  1200. return JavascriptNumber::ToVarNoCheck(result, scriptContext);
  1201. }
  1202. #endif
  1203. }
  1204. else
  1205. {
  1206. return scriptContext->GetLibrary()->GetNaN();
  1207. }
  1208. }
  1209. ///----------------------------------------------------------------------------
  1210. /// Atanh() returns the inverse hyperbolic tangent of the given number, as
  1211. /// described in (ES6.0: S20.2.2.7).
  1212. ///----------------------------------------------------------------------------
  1213. Var Math::Atanh(RecyclableObject* function, CallInfo callInfo, ...)
  1214. {
  1215. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  1216. ARGUMENTS(args, callInfo);
  1217. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  1218. ScriptContext* scriptContext = function->GetScriptContext();
  1219. Assert(!(callInfo.Flags & CallFlags_New));
  1220. CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Atanh);
  1221. if (args.Info.Count >= 2)
  1222. {
  1223. double x = JavascriptConversion::ToNumber(args[1], scriptContext);
  1224. #if defined(WHEN_UCRT_IS_LINKED_IN_BUILD) || !defined(_WIN32)
  1225. double result = ::atanh(x);
  1226. return JavascriptNumber::ToVarNoCheck(result, scriptContext);
  1227. #else
  1228. // TODO: THE FALLBACK IS NOT ACCURATE; Universal CRT is available on Threshold so we should never fallback but ideally we would link at build time to these APIs instead of loading them at runtime
  1229. UCrtC99MathApis* ucrtC99MathApis = scriptContext->GetThreadContext()->GetUCrtC99MathApis();
  1230. if (ucrtC99MathApis->IsAvailable())
  1231. {
  1232. return JavascriptNumber::ToVarNoCheck(ucrtC99MathApis->atanh(x), scriptContext);
  1233. }
  1234. else if (Math::Abs(x) < 1.0)
  1235. {
  1236. double result = (JavascriptNumber::IsNegZero(x)) ? x : Math::Log((1.0 + x) / (1.0 - x)) / 2.0;
  1237. return JavascriptNumber::ToVarNoCheck(result, scriptContext);
  1238. }
  1239. else
  1240. {
  1241. if (x == -1.0)
  1242. {
  1243. return scriptContext->GetLibrary()->GetNegativeInfinite();
  1244. }
  1245. else if (x == 1.0)
  1246. {
  1247. return scriptContext->GetLibrary()->GetPositiveInfinite();
  1248. }
  1249. return scriptContext->GetLibrary()->GetNaN();
  1250. }
  1251. #endif
  1252. }
  1253. else
  1254. {
  1255. return scriptContext->GetLibrary()->GetNaN();
  1256. }
  1257. }
  1258. ///----------------------------------------------------------------------------
  1259. /// Hypot() returns the square root of the sum of squares of the two or three
  1260. /// given numbers, as described in (ES6.0: S20.2.2.17).
  1261. ///----------------------------------------------------------------------------
  1262. Var Math::Hypot(RecyclableObject* function, CallInfo callInfo, ...)
  1263. {
  1264. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  1265. ARGUMENTS(args, callInfo);
  1266. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  1267. ScriptContext* scriptContext = function->GetScriptContext();
  1268. Assert(!(callInfo.Flags & CallFlags_New));
  1269. CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Hypot);
  1270. // ES6 20.2.2.18 Math.hypot(value1, value2, ...values)
  1271. // If no arguments are passed, the result is +0.
  1272. // If any argument is +∞, the result is +∞.
  1273. // If any argument is -∞, the result is +∞.
  1274. // If no argument is +∞ or -∞, and any argument is NaN, the result is NaN.
  1275. // If all arguments are either +0 or -0, the result is +0.
  1276. double result = JavascriptNumber::k_Zero; // If there are no arguments return value is positive zero.
  1277. if (args.Info.Count == 2)
  1278. {
  1279. // Special case for one argument
  1280. double x1 = JavascriptConversion::ToNumber(args[1], scriptContext);
  1281. if (JavascriptNumber::IsPosInf(x1) || JavascriptNumber::IsNegInf(x1))
  1282. {
  1283. result = JavascriptNumber::POSITIVE_INFINITY;
  1284. }
  1285. else
  1286. {
  1287. result = Math::Abs(x1);
  1288. }
  1289. }
  1290. else if (args.Info.Count == 3)
  1291. {
  1292. // CRT hypot call
  1293. double x1 = JavascriptConversion::ToNumber(args[1], scriptContext);
  1294. double x2 = JavascriptConversion::ToNumber(args[2], scriptContext);
  1295. if (JavascriptNumber::IsPosInf(x1) || JavascriptNumber::IsNegInf(x1) ||
  1296. JavascriptNumber::IsPosInf(x2) || JavascriptNumber::IsNegInf(x2))
  1297. {
  1298. result = JavascriptNumber::POSITIVE_INFINITY;
  1299. }
  1300. else if (JavascriptNumber::IsNan(x1) || JavascriptNumber::IsNan(x2))
  1301. {
  1302. result = JavascriptNumber::NaN;
  1303. }
  1304. else
  1305. {
  1306. result = ::hypot(x1, x2);
  1307. }
  1308. }
  1309. else if (args.Info.Count > 3)
  1310. {
  1311. // Uncommon case of more than 2 arguments for hypot
  1312. result = Math::HypotHelper(args, scriptContext);
  1313. }
  1314. return JavascriptNumber::ToVarNoCheck(result, scriptContext);
  1315. }
  1316. double Math::HypotHelper(Arguments args, ScriptContext *scriptContext)
  1317. {
  1318. // CRT does not have a multiple version of hypot, so we implement it here ourselves.
  1319. bool foundNaN = false;
  1320. double scale = 0;
  1321. double sum = 0;
  1322. //Ignore first argument which is this pointer
  1323. for (uint counter = 1; counter < args.Info.Count; counter++)
  1324. {
  1325. double doubleVal = JavascriptConversion::ToNumber(args[counter], scriptContext);
  1326. if (JavascriptNumber::IsPosInf(doubleVal) || JavascriptNumber::IsNegInf(doubleVal))
  1327. {
  1328. return JavascriptNumber::POSITIVE_INFINITY;
  1329. }
  1330. if (!foundNaN)
  1331. {
  1332. if (JavascriptNumber::IsNan(doubleVal))
  1333. {
  1334. //Even though we found NaN, we still need to validate none of the other arguments are +∞ or -∞
  1335. foundNaN = true;
  1336. }
  1337. else
  1338. {
  1339. doubleVal = Math::Abs(doubleVal);
  1340. if (scale < doubleVal)
  1341. {
  1342. sum = sum * (scale / doubleVal) * (scale / doubleVal) + 1; /* scale/scale === 1*/
  1343. //change the scale to new max value
  1344. scale = doubleVal;
  1345. }
  1346. else if (scale != 0)
  1347. {
  1348. sum += (doubleVal / scale) * (doubleVal / scale);
  1349. }
  1350. }
  1351. }
  1352. }
  1353. if (foundNaN)
  1354. {
  1355. return JavascriptNumber::NaN;
  1356. }
  1357. return scale * ::sqrt(sum);
  1358. }
  1359. ///----------------------------------------------------------------------------
  1360. /// Trunc() returns the integral part of the given number, removing any
  1361. /// fractional digits, as described in (ES6.0: S20.2.2.34).
  1362. ///----------------------------------------------------------------------------
  1363. Var Math::Trunc(RecyclableObject* function, CallInfo callInfo, ...)
  1364. {
  1365. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  1366. ARGUMENTS(args, callInfo);
  1367. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  1368. ScriptContext* scriptContext = function->GetScriptContext();
  1369. Assert(!(callInfo.Flags & CallFlags_New));
  1370. CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Trunc);
  1371. if (args.Info.Count >= 2)
  1372. {
  1373. double x = JavascriptConversion::ToNumber(args[1], scriptContext);
  1374. #if defined(WHEN_UCRT_IS_LINKED_IN_BUILD) || !defined(_WIN32)
  1375. double result = ::trunc(x);
  1376. #else
  1377. // TODO: THE FALLBACK IS NOT ACCURATE; Universal CRT is available on Threshold so we should never fallback but ideally we would link at build time to these APIs instead of loading them at runtime
  1378. UCrtC99MathApis* ucrtC99MathApis = scriptContext->GetThreadContext()->GetUCrtC99MathApis();
  1379. double result = ucrtC99MathApis->IsAvailable() ?
  1380. ucrtC99MathApis->trunc(x) :
  1381. (x < 0.0) ? ::ceil(x) : ::floor(x);
  1382. #endif
  1383. return JavascriptNumber::ToVarNoCheck(result, scriptContext);
  1384. }
  1385. else
  1386. {
  1387. return scriptContext->GetLibrary()->GetNaN();
  1388. }
  1389. }
  1390. ///----------------------------------------------------------------------------
  1391. /// Sign() returns the sign of the given number, indicating whether it is
  1392. /// positive, negative, or zero, as described in (ES6.0: S20.2.2.28).
  1393. ///----------------------------------------------------------------------------
  1394. Var Math::Sign(RecyclableObject* function, CallInfo callInfo, ...)
  1395. {
  1396. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  1397. ARGUMENTS(args, callInfo);
  1398. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  1399. ScriptContext* scriptContext = function->GetScriptContext();
  1400. Assert(!(callInfo.Flags & CallFlags_New));
  1401. CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Sign);
  1402. if (args.Info.Count >= 2)
  1403. {
  1404. double x = JavascriptConversion::ToNumber(args[1], scriptContext);
  1405. if (JavascriptNumber::IsNan(x))
  1406. {
  1407. return scriptContext->GetLibrary()->GetNaN();
  1408. }
  1409. else if (JavascriptNumber::IsNegZero(x))
  1410. {
  1411. return scriptContext->GetLibrary()->GetNegativeZero();
  1412. }
  1413. else
  1414. {
  1415. return TaggedInt::ToVarUnchecked(x == 0.0 ? 0 : x < 0.0 ? -1 : 1);
  1416. }
  1417. }
  1418. else
  1419. {
  1420. return scriptContext->GetLibrary()->GetNaN();
  1421. }
  1422. }
  1423. ///----------------------------------------------------------------------------
  1424. /// Cbrt() returns the cube root of the given number, as described in
  1425. /// (ES6.0: S20.2.2.9).
  1426. ///----------------------------------------------------------------------------
  1427. Var Math::Cbrt(RecyclableObject* function, CallInfo callInfo, ...)
  1428. {
  1429. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  1430. ARGUMENTS(args, callInfo);
  1431. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  1432. ScriptContext* scriptContext = function->GetScriptContext();
  1433. Assert(!(callInfo.Flags & CallFlags_New));
  1434. CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Cbrt);
  1435. if (args.Info.Count >= 2)
  1436. {
  1437. double x = JavascriptConversion::ToNumber(args[1], scriptContext);
  1438. #if defined(WHEN_UCRT_IS_LINKED_IN_BUILD) || !defined(_WIN32)
  1439. double result = ::cbrt(x);
  1440. #else
  1441. // TODO: THE FALLBACK IS NOT ACCURATE; Universal CRT is available on Threshold so we should never fallback but ideally we would link at build time to these APIs instead of loading them at runtime
  1442. UCrtC99MathApis* ucrtC99MathApis = scriptContext->GetThreadContext()->GetUCrtC99MathApis();
  1443. if (ucrtC99MathApis->IsAvailable())
  1444. {
  1445. return JavascriptNumber::ToVarNoCheck(ucrtC99MathApis->cbrt(x), scriptContext);
  1446. }
  1447. bool isNeg = x < 0.0;
  1448. if (isNeg)
  1449. {
  1450. x = -x;
  1451. }
  1452. double result = (x == 0.0) ? x : Math::Exp(Math::Log(x) / 3.0);
  1453. if (isNeg)
  1454. {
  1455. result = -result;
  1456. }
  1457. #endif
  1458. return JavascriptNumber::ToVarNoCheck(result, scriptContext);
  1459. }
  1460. else
  1461. {
  1462. return scriptContext->GetLibrary()->GetNaN();
  1463. }
  1464. }
  1465. ///----------------------------------------------------------------------------
  1466. /// Imul() returns the 32-bit integer multiplication of two given numbers, as
  1467. /// described in (ES6.0: S20.2.2.18).
  1468. ///----------------------------------------------------------------------------
  1469. Var Math::Imul(RecyclableObject* function, CallInfo callInfo, ...)
  1470. {
  1471. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  1472. ARGUMENTS(args, callInfo);
  1473. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  1474. ScriptContext* scriptContext = function->GetScriptContext();
  1475. Assert(!(callInfo.Flags & CallFlags_New));
  1476. CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Imul);
  1477. if (args.Info.Count >= 3)
  1478. {
  1479. int32 x = JavascriptConversion::ToInt32(args[1], scriptContext);
  1480. int32 y = JavascriptConversion::ToInt32(args[2], scriptContext);
  1481. int64 int64Result = (int64)x * (int64)y;
  1482. int32 result = (int32)int64Result;
  1483. return JavascriptNumber::ToVar(result, scriptContext);
  1484. }
  1485. else
  1486. {
  1487. // The arguments, if left unspecified default to undefined, and ToUint32(undefined) produces +0.
  1488. // Therefore we return +0, not NaN here like the other Math functions.
  1489. return TaggedInt::ToVarUnchecked(0);
  1490. }
  1491. }
  1492. ///----------------------------------------------------------------------------
  1493. /// Clz32() returns the leading number of zero bits of ToUint32(x), as
  1494. /// described in (ES6.0: S20.2.2.11).
  1495. ///----------------------------------------------------------------------------
  1496. Var Math::Clz32(RecyclableObject* function, CallInfo callInfo, ...)
  1497. {
  1498. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  1499. ARGUMENTS(args, callInfo);
  1500. ScriptContext* scriptContext = function->GetScriptContext();
  1501. Var value = args.Info.Count > 1 ? args[1] : scriptContext->GetLibrary()->GetUndefined();
  1502. Assert(!(callInfo.Flags & CallFlags_New));
  1503. CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Clz32);
  1504. uint32 uint32value = JavascriptConversion::ToUInt32(value, scriptContext);
  1505. DWORD index;
  1506. if (!_BitScanReverse(&index, uint32value))
  1507. {
  1508. return TaggedInt::ToVarUnchecked(32);
  1509. }
  1510. return TaggedInt::ToVarUnchecked(31 - index);
  1511. }
  1512. Var Math::Fround(RecyclableObject* function, CallInfo callInfo, ...)
  1513. {
  1514. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  1515. ARGUMENTS(args, callInfo);
  1516. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  1517. ScriptContext* scriptContext = function->GetScriptContext();
  1518. Assert(!(callInfo.Flags & CallFlags_New));
  1519. CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Fround);
  1520. if (args.Info.Count >= 2)
  1521. {
  1522. float x = (float) JavascriptConversion::ToNumber(args[1], scriptContext);
  1523. return JavascriptNumber::ToVarNoCheck((double) x, scriptContext);
  1524. }
  1525. else
  1526. {
  1527. return scriptContext->GetLibrary()->GetNaN();
  1528. }
  1529. }
  1530. } // namespace Js