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 doclz32(x: any): number {
264514f5e3Sopenharmony_ci    return Math.clz32(x);
274514f5e3Sopenharmony_ci}
284514f5e3Sopenharmony_ci
294514f5e3Sopenharmony_cifunction printclz32(x: any) {
304514f5e3Sopenharmony_ci    try {
314514f5e3Sopenharmony_ci        print(doclz32(x));
324514f5e3Sopenharmony_ci    } finally {
334514f5e3Sopenharmony_ci    }
344514f5e3Sopenharmony_ci}
354514f5e3Sopenharmony_ci
364514f5e3Sopenharmony_ci// Check without params
374514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.clz32, caller function name:func_main_0@builtinMathClz32
384514f5e3Sopenharmony_ciprint(Math.clz32()); //: 32
394514f5e3Sopenharmony_ci
404514f5e3Sopenharmony_ci// Check with single int param
414514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.clz32, caller function name:func_main_0@builtinMathClz32
424514f5e3Sopenharmony_ciprint(Math.clz32(0)); //: 32
434514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.clz32, caller function name:func_main_0@builtinMathClz32
444514f5e3Sopenharmony_ciprint(Math.clz32(1)); //: 31
454514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.clz32, caller function name:func_main_0@builtinMathClz32
464514f5e3Sopenharmony_ciprint(Math.clz32(1 << 31)) //: 0
474514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.clz32, caller function name:func_main_0@builtinMathClz32
484514f5e3Sopenharmony_ciprint(Math.clz32(1 << 30)) //: 1
494514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.clz32, caller function name:func_main_0@builtinMathClz32
504514f5e3Sopenharmony_ciprint(Math.clz32(-5)); //: 0
514514f5e3Sopenharmony_ci
524514f5e3Sopenharmony_ci// 0b11
534514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.clz32, caller function name:func_main_0@builtinMathClz32
544514f5e3Sopenharmony_ciprint(Math.clz32(3)); //: 30
554514f5e3Sopenharmony_ci
564514f5e3Sopenharmony_ci// MAX_UINT32 + 3
574514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.clz32, caller function name:func_main_0@builtinMathClz32
584514f5e3Sopenharmony_ciprint(Math.clz32(4294967298)); //: 30
594514f5e3Sopenharmony_ci
604514f5e3Sopenharmony_ci// Check with single float param
614514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.clz32, caller function name:func_main_0@builtinMathClz32
624514f5e3Sopenharmony_ciprint(Math.clz32(1.9999999999999)); //: 31
634514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.clz32, caller function name:func_main_0@builtinMathClz32
644514f5e3Sopenharmony_ciprint(Math.clz32(126.55555555555)); //: 25
654514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.clz32, caller function name:func_main_0@builtinMathClz32
664514f5e3Sopenharmony_ciprint(Math.clz32(126.999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999)) //: 25
674514f5e3Sopenharmony_ci
684514f5e3Sopenharmony_ci// Negative arguments
694514f5e3Sopenharmony_ci// -1073741824 (0xC0000000) ->
704514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.clz32, caller function name:func_main_0@builtinMathClz32
714514f5e3Sopenharmony_ciprint(Math.clz32((1 << 30) + (1 << 31))) //: 0
724514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.clz32, caller function name:func_main_0@builtinMathClz32
734514f5e3Sopenharmony_ciprint(Math.clz32(-5)) //: 0
744514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.clz32, caller function name:func_main_0@builtinMathClz32
754514f5e3Sopenharmony_ciprint(Math.clz32(-126.99999999999999999999999)) //: 0
764514f5e3Sopenharmony_ci
774514f5e3Sopenharmony_ci
784514f5e3Sopenharmony_ci// Corner cases
794514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.clz32, caller function name:func_main_0@builtinMathClz32
804514f5e3Sopenharmony_ciprint(Math.clz32(NaN)) //: 32
814514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.clz32, caller function name:func_main_0@builtinMathClz32
824514f5e3Sopenharmony_ciprint(Math.clz32(+0.0)) //: 32
834514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.clz32, caller function name:func_main_0@builtinMathClz32
844514f5e3Sopenharmony_ciprint(Math.clz32(-0.0)) //: 32
854514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.clz32, caller function name:func_main_0@builtinMathClz32
864514f5e3Sopenharmony_ciprint(Math.clz32(Infinity)); //: 32
874514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.clz32, caller function name:func_main_0@builtinMathClz32
884514f5e3Sopenharmony_ciprint(Math.clz32(-Infinity)); //: 32
894514f5e3Sopenharmony_ci
904514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.clz32, caller function name:#*#doclz32@builtinMathClz32
914514f5e3Sopenharmony_ciprintclz32(4); //: 29
924514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.clz32, caller function name:#*#doclz32@builtinMathClz32
934514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotNumber1
944514f5e3Sopenharmony_ciprintclz32("abc"); //: 32
954514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.clz32, caller function name:#*#doclz32@builtinMathClz32
964514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotNumber1
974514f5e3Sopenharmony_ciprintclz32("abcd"); //: 32
984514f5e3Sopenharmony_ci
994514f5e3Sopenharmony_cilet true_clz32 = Math.clz32
1004514f5e3Sopenharmony_ciif (ArkTools.isAOTCompiled(printclz32)) {
1014514f5e3Sopenharmony_ci    // Replace standard builtin after call to standard builtin was profiled
1024514f5e3Sopenharmony_ci    Math.clz32 = replace
1034514f5e3Sopenharmony_ci}
1044514f5e3Sopenharmony_ciprintclz32(-8); //pgo: 0
1054514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotCallTarget1
1064514f5e3Sopenharmony_ci//aot: -8
1074514f5e3Sopenharmony_ci
1084514f5e3Sopenharmony_ciprintclz32("abc"); //pgo: 32
1094514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotCallTarget1
1104514f5e3Sopenharmony_ci//aot: abc
1114514f5e3Sopenharmony_ci
1124514f5e3Sopenharmony_ciMath.clz32 = true_clz32
1134514f5e3Sopenharmony_ci
1144514f5e3Sopenharmony_ci// Check IR correctness inside try-block
1154514f5e3Sopenharmony_citry {
1164514f5e3Sopenharmony_ci    //aot: [trace] aot inline builtin: Math.clz32, caller function name:#*#doclz32@builtinMathClz32
1174514f5e3Sopenharmony_ci    printclz32(16); //: 27
1184514f5e3Sopenharmony_ci    //aot: [trace] aot inline builtin: Math.clz32, caller function name:#*#doclz32@builtinMathClz32
1194514f5e3Sopenharmony_ci    //aot: [trace] Check Type: NotNumber1
1204514f5e3Sopenharmony_ci    printclz32("abc"); //: 32
1214514f5e3Sopenharmony_ci} catch (e) {
1224514f5e3Sopenharmony_ci}
1234514f5e3Sopenharmony_ci
1244514f5e3Sopenharmony_cilet obj = {};
1254514f5e3Sopenharmony_ciobj.valueOf = (() => { return 32; })
1264514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: Math.clz32, caller function name:func_main_0@builtinMathClz32
1274514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotNumber1
1284514f5e3Sopenharmony_ciprint(Math.clz32(obj)); //: 26
1294514f5e3Sopenharmony_ci
1304514f5e3Sopenharmony_cifunction Throwing() {
1314514f5e3Sopenharmony_ci    this.value = 64;
1324514f5e3Sopenharmony_ci}
1334514f5e3Sopenharmony_ciThrowing.prototype.valueOf = function() {
1344514f5e3Sopenharmony_ci    if (this.value > 100) {
1354514f5e3Sopenharmony_ci        throw new Error("so big");
1364514f5e3Sopenharmony_ci    }
1374514f5e3Sopenharmony_ci    return this.value;
1384514f5e3Sopenharmony_ci}
1394514f5e3Sopenharmony_cilet throwingObj = new Throwing();
1404514f5e3Sopenharmony_ci
1414514f5e3Sopenharmony_citry {
1424514f5e3Sopenharmony_ci    print(Math.clz32(throwingObj)); //: 25
1434514f5e3Sopenharmony_ci    throwingObj.value = 128;
1444514f5e3Sopenharmony_ci    print(Math.clz32(throwingObj)); //: Error: so big
1454514f5e3Sopenharmony_ci} catch(e) {
1464514f5e3Sopenharmony_ci    print(e);
1474514f5e3Sopenharmony_ci} finally {
1484514f5e3Sopenharmony_ci    print(Math.clz32(obj)); //: 26
1494514f5e3Sopenharmony_ci}