1/*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16declare interface ArkTools {
17    isAOTCompiled(args: any): boolean;
18}
19declare function print(arg:any):string;
20function replace(a : number)
21{
22    return a;
23}
24
25function printZero(x: any) {
26    if (Object.is(x, -0)) {
27        print("-0");
28    } else {
29        print(x);
30    }
31}
32
33function doFround(x: any): number {
34    return Math.fround(x);
35}
36
37function printFround(x: any) {
38    try {
39        print(doFround(x));
40    } finally {
41    }
42}
43
44// Check without params
45//aot: [trace] aot inline builtin: Math.fround, caller function name:func_main_0@builtinMathFround
46print(Math.fround()); //: NaN
47
48// Check with single int param
49//aot: [trace] aot inline builtin: Math.fround, caller function name:func_main_0@builtinMathFround
50print(Math.fround(0)); //: 0
51//aot: [trace] aot inline builtin: Math.fround, caller function name:func_main_0@builtinMathFround
52print(Math.fround(3)); //: 3
53//aot: [trace] aot inline builtin: Math.fround, caller function name:func_main_0@builtinMathFround
54print(Math.fround(-5)); //: -5
55
56// Test large ints
57//aot: [trace] aot inline builtin: Math.fround, caller function name:func_main_0@builtinMathFround
58print(Math.fround(1073741824)) //: 1073741824
59//aot: [trace] aot inline builtin: Math.fround, caller function name:func_main_0@builtinMathFround
60print(Math.fround(1073741804)) //: 1073741824
61//aot: [trace] aot inline builtin: Math.fround, caller function name:func_main_0@builtinMathFround
62print(Math.fround(1073741784)) //: 1073741760
63//aot: [trace] aot inline builtin: Math.fround, caller function name:func_main_0@builtinMathFround
64print(Math.fround(-1073741804)) //: -1073741824
65
66// Check with single float param
67//aot: [trace] aot inline builtin: Math.fround, caller function name:func_main_0@builtinMathFround
68print(Math.fround(1.9e80)); //: Infinity
69//aot: [trace] aot inline builtin: Math.fround, caller function name:func_main_0@builtinMathFround
70//aot: [trace] aot inline function name: #*#printZero@builtinMathFround caller function name: func_main_0@builtinMathFround
71//aot: [trace] aot inline builtin: Object.is, caller function name:#*#printZero@builtinMathFround
72printZero(Math.fround(2.5)); //: 2.5
73//aot: [trace] aot inline builtin: Math.fround, caller function name:func_main_0@builtinMathFround
74//aot: [trace] aot inline function name: #*#printZero@builtinMathFround caller function name: func_main_0@builtinMathFround
75//aot: [trace] aot inline builtin: Object.is, caller function name:#*#printZero@builtinMathFround
76printZero(Math.fround(1.5)); //: 1.5
77//aot: [trace] aot inline builtin: Math.fround, caller function name:func_main_0@builtinMathFround
78//aot: [trace] aot inline function name: #*#printZero@builtinMathFround caller function name: func_main_0@builtinMathFround
79//aot: [trace] aot inline builtin: Object.is, caller function name:#*#printZero@builtinMathFround
80printZero(Math.fround(0.5)); //: 0.5
81//aot: [trace] aot inline builtin: Math.fround, caller function name:func_main_0@builtinMathFround
82//aot: [trace] aot inline function name: #*#printZero@builtinMathFround caller function name: func_main_0@builtinMathFround
83//aot: [trace] aot inline builtin: Object.is, caller function name:#*#printZero@builtinMathFround
84printZero(Math.fround(0.2)); //: 0.20000000298023224
85//aot: [trace] aot inline builtin: Math.fround, caller function name:func_main_0@builtinMathFround
86//aot: [trace] aot inline function name: #*#printZero@builtinMathFround caller function name: func_main_0@builtinMathFround
87//aot: [trace] aot inline builtin: Object.is, caller function name:#*#printZero@builtinMathFround
88printZero(Math.fround(-0)); //: -0
89//aot: [trace] aot inline builtin: Math.fround, caller function name:func_main_0@builtinMathFround
90//aot: [trace] aot inline function name: #*#printZero@builtinMathFround caller function name: func_main_0@builtinMathFround
91//aot: [trace] aot inline builtin: Object.is, caller function name:#*#printZero@builtinMathFround
92printZero(Math.fround(-1.9e-80)); //: -0
93//aot: [trace] aot inline builtin: Math.fround, caller function name:func_main_0@builtinMathFround
94//aot: [trace] aot inline builtin: Object.is, caller function name:#*#printZero@builtinMathFround
95printZero(Math.fround(-0.1)); //: -0.10000000149011612
96//aot: [trace] aot inline builtin: Math.fround, caller function name:func_main_0@builtinMathFround
97print(Math.fround(-0.5)); //: -0.5
98//aot: [trace] aot inline builtin: Math.fround, caller function name:func_main_0@builtinMathFround
99print(Math.fround(-1.5)); //: -1.5
100//aot: [trace] aot inline builtin: Math.fround, caller function name:func_main_0@builtinMathFround
101print(Math.fround(-2.1)); //: -2.0999999046325684
102//aot: [trace] aot inline builtin: Math.fround, caller function name:func_main_0@builtinMathFround
103print(Math.fround(-2.49)); //: -2.490000009536743
104//aot: [trace] aot inline builtin: Math.fround, caller function name:func_main_0@builtinMathFround
105print(Math.fround(-2.5)); //: -2.5
106//aot: [trace] aot inline builtin: Math.fround, caller function name:func_main_0@builtinMathFround
107print(Math.fround(-2.7)); //: -2.700000047683716
108//aot: [trace] aot inline builtin: Math.fround, caller function name:func_main_0@builtinMathFround
109print(Math.fround(-1.9e80)); //: -Infinity
110//aot: [trace] aot inline builtin: Math.fround, caller function name:func_main_0@builtinMathFround
111print(Math.fround(1.9e-80)); //: 0
112
113// Check with special float params
114//aot: [trace] aot inline builtin: Math.fround, caller function name:func_main_0@builtinMathFround
115print(Math.fround(Infinity)); //: Infinity
116//aot: [trace] aot inline builtin: Math.fround, caller function name:func_main_0@builtinMathFround
117print(Math.fround(-Infinity)); //: -Infinity
118//aot: [trace] aot inline builtin: Math.fround, caller function name:func_main_0@builtinMathFround
119print(Math.fround(NaN)); //: NaN
120
121// Check with 2 params
122//aot: [trace] aot inline builtin: Math.fround, caller function name:func_main_0@builtinMathFround
123print(Math.fround(3, 0)); //: 3
124
125// Check with 3 params
126//aot: [trace] aot inline builtin: Math.fround, caller function name:func_main_0@builtinMathFround
127print(Math.fround(-3.5, 0, 0)); //: -3.5
128
129// Check with 4 params
130//aot: [trace] aot inline builtin: Math.fround, caller function name:func_main_0@builtinMathFround
131print(Math.fround(4.1, 0, 0, 0)); //: 4.099999904632568
132
133// Check with 5 params
134//aot: [trace] aot inline builtin: Math.fround, caller function name:func_main_0@builtinMathFround
135print(Math.fround(-4.1, 0, 0, 0, 0)); //: -4.099999904632568
136
137// Replace standard builtin
138let true_fround = Math.fround
139Math.fround = replace
140
141// no deopt
142print(Math.fround(-1.001)); //: -1.001
143Math.fround = true_fround
144
145//aot: [trace] aot inline builtin: Math.fround, caller function name:#*#doFround@builtinMathFround
146printFround(12.3); //: 12.300000190734863
147// Call standard builtin with non-number param
148//aot: [trace] aot inline builtin: Math.fround, caller function name:#*#doFround@builtinMathFround
149//aot: [trace] Check Type: NotNumber1
150printFround("abc"); //: NaN
151//aot: [trace] aot inline builtin: Math.fround, caller function name:#*#doFround@builtinMathFround
152//aot: [trace] Check Type: NotNumber1
153printFround("-12.9"); //: -12.899999618530273
154
155if (ArkTools.isAOTCompiled(printFround)) {
156    // Replace standard builtin after call to standard builtin was profiled
157    Math.fround = replace
158}
159printFround(-12.2); //pgo: -12.199999809265137
160//aot: [trace] Check Type: NotCallTarget1
161//aot: -12.2
162
163printFround("abc"); //pgo: NaN
164//aot: [trace] Check Type: NotCallTarget1
165//aot: abc
166
167Math.fround = true_fround
168
169// Check IR correctness inside try-block
170try {
171    //aot: [trace] aot inline builtin: Math.fround, caller function name:#*#doFround@builtinMathFround
172    printFround(0.3) //: 0.30000001192092896
173    //aot: [trace] aot inline builtin: Math.fround, caller function name:#*#doFround@builtinMathFround
174    printFround(-12); //: -12
175    //aot: [trace] aot inline builtin: Math.fround, caller function name:#*#doFround@builtinMathFround
176    //aot: [trace] Check Type: NotNumber1
177    printFround("abc"); //: NaN
178} catch (e) {
179}
180
181let obj = {
182    valueOf: () => { return -22.5; }
183};
184//aot: [trace] aot inline builtin: Math.fround, caller function name:func_main_0@builtinMathFround
185//aot: [trace] Check Type: NotNumber1
186print(Math.fround(obj)); //: -22.5
187