14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_cideclare interface ArkTools {
174514f5e3Sopenharmony_ci    isAOTCompiled(args: any): boolean;
184514f5e3Sopenharmony_ci}
194514f5e3Sopenharmony_cideclare function print(arg:any):string;
204514f5e3Sopenharmony_cifunction replace(a : number)
214514f5e3Sopenharmony_ci{
224514f5e3Sopenharmony_ci    return a;
234514f5e3Sopenharmony_ci}
244514f5e3Sopenharmony_ci
254514f5e3Sopenharmony_cifunction printZero(x: any) {
264514f5e3Sopenharmony_ci    if (Object.is(x, -0)) {
274514f5e3Sopenharmony_ci        print("-0");
284514f5e3Sopenharmony_ci    } else {
294514f5e3Sopenharmony_ci        print(x);
304514f5e3Sopenharmony_ci    }
314514f5e3Sopenharmony_ci}
324514f5e3Sopenharmony_ci
334514f5e3Sopenharmony_cifunction doRound(x: any): number {
344514f5e3Sopenharmony_ci    return Math.round(x);
354514f5e3Sopenharmony_ci}
364514f5e3Sopenharmony_ci
374514f5e3Sopenharmony_cifunction printRound(x: any) {
384514f5e3Sopenharmony_ci    try {
394514f5e3Sopenharmony_ci        print(doRound(x));
404514f5e3Sopenharmony_ci    } finally {
414514f5e3Sopenharmony_ci    }
424514f5e3Sopenharmony_ci}
434514f5e3Sopenharmony_ci
444514f5e3Sopenharmony_ci// Check without params
454514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.round, caller function name:func_main_0@builtinMathRound
464514f5e3Sopenharmony_ciprint(Math.round()); //: NaN
474514f5e3Sopenharmony_ci
484514f5e3Sopenharmony_ci// Check with single int param
494514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.round, caller function name:func_main_0@builtinMathRound
504514f5e3Sopenharmony_ciprint(Math.round(0)); //: 0
514514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.round, caller function name:func_main_0@builtinMathRound
524514f5e3Sopenharmony_ciprint(Math.round(3)); //: 3
534514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.round, caller function name:func_main_0@builtinMathRound
544514f5e3Sopenharmony_ciprint(Math.round(-5)); //: -5
554514f5e3Sopenharmony_ci
564514f5e3Sopenharmony_ci// Test large ints
574514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.round, caller function name:func_main_0@builtinMathRound
584514f5e3Sopenharmony_ciprint(Math.round(1073741824)) //: 1073741824
594514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.round, caller function name:func_main_0@builtinMathRound
604514f5e3Sopenharmony_ciprint(Math.round(1073741804)) //: 1073741804
614514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.round, caller function name:func_main_0@builtinMathRound
624514f5e3Sopenharmony_ciprint(Math.round(1073741784)) //: 1073741784
634514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.round, caller function name:func_main_0@builtinMathRound
644514f5e3Sopenharmony_ciprint(Math.round(-1073741804)) //: -1073741804
654514f5e3Sopenharmony_ci
664514f5e3Sopenharmony_ci// Check with single float param
674514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.round, caller function name:func_main_0@builtinMathRound
684514f5e3Sopenharmony_ciprint(Math.round(1.9e80)); //: 1.9e+80
694514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.round, caller function name:func_main_0@builtinMathRound
704514f5e3Sopenharmony_ci//aot: [trace] aot inline function name: #*#printZero@builtinMathRound caller function name: func_main_0@builtinMathRound
714514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Object.is, caller function name:#*#printZero@builtinMathRound
724514f5e3Sopenharmony_ciprintZero(Math.round(2.5)); //: 3
734514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.round, caller function name:func_main_0@builtinMathRound
744514f5e3Sopenharmony_ci//aot: [trace] aot inline function name: #*#printZero@builtinMathRound caller function name: func_main_0@builtinMathRound
754514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Object.is, caller function name:#*#printZero@builtinMathRound
764514f5e3Sopenharmony_ciprintZero(Math.round(1.5)); //: 2
774514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.round, caller function name:func_main_0@builtinMathRound
784514f5e3Sopenharmony_ci//aot: [trace] aot inline function name: #*#printZero@builtinMathRound caller function name: func_main_0@builtinMathRound
794514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Object.is, caller function name:#*#printZero@builtinMathRound
804514f5e3Sopenharmony_ciprintZero(Math.round(0.5)); //: 1
814514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.round, caller function name:func_main_0@builtinMathRound
824514f5e3Sopenharmony_ci//aot: [trace] aot inline function name: #*#printZero@builtinMathRound caller function name: func_main_0@builtinMathRound
834514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Object.is, caller function name:#*#printZero@builtinMathRound
844514f5e3Sopenharmony_ciprintZero(Math.round(0.2)); //: 0
854514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.round, caller function name:func_main_0@builtinMathRound
864514f5e3Sopenharmony_ci//aot: [trace] aot inline function name: #*#printZero@builtinMathRound caller function name: func_main_0@builtinMathRound
874514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Object.is, caller function name:#*#printZero@builtinMathRound
884514f5e3Sopenharmony_ciprintZero(Math.round(-0)); //: -0
894514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.round, caller function name:func_main_0@builtinMathRound
904514f5e3Sopenharmony_ci//aot: [trace] aot inline function name: #*#printZero@builtinMathRound caller function name: func_main_0@builtinMathRound
914514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Object.is, caller function name:#*#printZero@builtinMathRound
924514f5e3Sopenharmony_ciprintZero(Math.round(-1.9e-80)); //: -0
934514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.round, caller function name:func_main_0@builtinMathRound
944514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Object.is, caller function name:#*#printZero@builtinMathRound
954514f5e3Sopenharmony_ciprintZero(Math.round(-0.1)); //: -0
964514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.round, caller function name:func_main_0@builtinMathRound
974514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Object.is, caller function name:#*#printZero@builtinMathRound
984514f5e3Sopenharmony_ciprintZero(Math.round(-0.5)); //: -0
994514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.round, caller function name:func_main_0@builtinMathRound
1004514f5e3Sopenharmony_ciprint(Math.round(-0.7)); //: -1
1014514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.round, caller function name:func_main_0@builtinMathRound
1024514f5e3Sopenharmony_ciprint(Math.round(-1.2)); //: -1
1034514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.round, caller function name:func_main_0@builtinMathRound
1044514f5e3Sopenharmony_ciprint(Math.round(-1.5)); //: -1
1054514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.round, caller function name:func_main_0@builtinMathRound
1064514f5e3Sopenharmony_ciprint(Math.round(-2.1)); //: -2
1074514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.round, caller function name:func_main_0@builtinMathRound
1084514f5e3Sopenharmony_ciprint(Math.round(-2.49)); //: -2
1094514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.round, caller function name:func_main_0@builtinMathRound
1104514f5e3Sopenharmony_ciprint(Math.round(-2.5)); //: -2
1114514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.round, caller function name:func_main_0@builtinMathRound
1124514f5e3Sopenharmony_ciprint(Math.round(-2.7)); //: -3
1134514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.round, caller function name:func_main_0@builtinMathRound
1144514f5e3Sopenharmony_ciprint(Math.round(-1.9e80)); //: -1.9e+80
1154514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.round, caller function name:func_main_0@builtinMathRound
1164514f5e3Sopenharmony_ciprint(Math.round(1.9e-80)); //: 0
1174514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.round, caller function name:func_main_0@builtinMathRound
1184514f5e3Sopenharmony_ciprint(Math.round(0.5 - Number.EPSILON / 4)); //: 0
1194514f5e3Sopenharmony_ci
1204514f5e3Sopenharmony_ci
1214514f5e3Sopenharmony_ci// Check with special float params
1224514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.round, caller function name:func_main_0@builtinMathRound
1234514f5e3Sopenharmony_ciprint(Math.round(Infinity)); //: Infinity
1244514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.round, caller function name:func_main_0@builtinMathRound
1254514f5e3Sopenharmony_ciprint(Math.round(-Infinity)); //: -Infinity
1264514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.round, caller function name:func_main_0@builtinMathRound
1274514f5e3Sopenharmony_ciprint(Math.round(NaN)); //: NaN
1284514f5e3Sopenharmony_ci
1294514f5e3Sopenharmony_ci// Check with 2 params
1304514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.round, caller function name:func_main_0@builtinMathRound
1314514f5e3Sopenharmony_ciprint(Math.round(3, 0)); //: 3
1324514f5e3Sopenharmony_ci
1334514f5e3Sopenharmony_ci// Check with 3 params
1344514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.round, caller function name:func_main_0@builtinMathRound
1354514f5e3Sopenharmony_ciprint(Math.round(-3.5, 0, 0)); //: -3
1364514f5e3Sopenharmony_ci
1374514f5e3Sopenharmony_ci// Check with 4 params
1384514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.round, caller function name:func_main_0@builtinMathRound
1394514f5e3Sopenharmony_ciprint(Math.round(4.1, 0, 0, 0)); //: 4
1404514f5e3Sopenharmony_ci
1414514f5e3Sopenharmony_ci// Check with 5 params
1424514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.round, caller function name:func_main_0@builtinMathRound
1434514f5e3Sopenharmony_ciprint(Math.round(-4.1, 0, 0, 0, 0)); //: -4
1444514f5e3Sopenharmony_ci
1454514f5e3Sopenharmony_ci// Replace standard builtin
1464514f5e3Sopenharmony_cilet true_round = Math.round
1474514f5e3Sopenharmony_ciMath.round = replace
1484514f5e3Sopenharmony_ci
1494514f5e3Sopenharmony_ci// no deopt
1504514f5e3Sopenharmony_ciprint(Math.round(-1.001)); //: -1.001
1514514f5e3Sopenharmony_ciMath.round = true_round
1524514f5e3Sopenharmony_ci
1534514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.round, caller function name:#*#doRound@builtinMathRound
1544514f5e3Sopenharmony_ciprintRound(12.3); //: 12
1554514f5e3Sopenharmony_ci// Call standard builtin with non-number param
1564514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.round, caller function name:#*#doRound@builtinMathRound
1574514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotNumber2
1584514f5e3Sopenharmony_ciprintRound("abc"); //: NaN
1594514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.round, caller function name:#*#doRound@builtinMathRound
1604514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotNumber2
1614514f5e3Sopenharmony_ciprintRound("-12.9"); //: -13
1624514f5e3Sopenharmony_ci
1634514f5e3Sopenharmony_ciif (ArkTools.isAOTCompiled(printRound)) {
1644514f5e3Sopenharmony_ci    // Replace standard builtin after call to standard builtin was profiled
1654514f5e3Sopenharmony_ci    Math.round = replace
1664514f5e3Sopenharmony_ci}
1674514f5e3Sopenharmony_ciprintRound(-12.2); //pgo: -12
1684514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotCallTarget1
1694514f5e3Sopenharmony_ci//aot: -12.2
1704514f5e3Sopenharmony_ci
1714514f5e3Sopenharmony_ciprintRound("abc"); //pgo: NaN
1724514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotCallTarget1
1734514f5e3Sopenharmony_ci//aot: abc
1744514f5e3Sopenharmony_ci
1754514f5e3Sopenharmony_ciMath.round = true_round
1764514f5e3Sopenharmony_ci
1774514f5e3Sopenharmony_ci// Check IR correctness inside try-block
1784514f5e3Sopenharmony_citry {
1794514f5e3Sopenharmony_ci    //aot: [trace] aot inline builtin: Math.round, caller function name:#*#doRound@builtinMathRound
1804514f5e3Sopenharmony_ci    printRound(0.3) //: 0
1814514f5e3Sopenharmony_ci    //aot: [trace] aot inline builtin: Math.round, caller function name:#*#doRound@builtinMathRound
1824514f5e3Sopenharmony_ci    printRound(-12); //: -12
1834514f5e3Sopenharmony_ci    //aot: [trace] aot inline builtin: Math.round, caller function name:#*#doRound@builtinMathRound
1844514f5e3Sopenharmony_ci    //aot: [trace] Check Type: NotNumber2
1854514f5e3Sopenharmony_ci    printRound("abc"); //: NaN
1864514f5e3Sopenharmony_ci} catch (e) {
1874514f5e3Sopenharmony_ci}
1884514f5e3Sopenharmony_ci
1894514f5e3Sopenharmony_cilet obj = {
1904514f5e3Sopenharmony_ci    valueOf: () => { return -22.5; }
1914514f5e3Sopenharmony_ci};
1924514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.round, caller function name:func_main_0@builtinMathRound
1934514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotNumber2
1944514f5e3Sopenharmony_ciprint(Math.round(obj)); //: -22
195