1/* 2 * Copyright (c) 2023-2025 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 16function main(): int { 17 let repetitions: int = 500000; 18 19 return execute_test(repetitions); 20} 21 22function release_main(): int { 23 let repetitions: int = 10000000; 24 25 return execute_test(repetitions); 26} 27 28function execute_test(repetitions: int): int { 29 30 let res: double = 0; 31 let pi: double = 3.14158; 32 33 res = CallSlowFunction(1, repetitions); 34 if (Absolute(res - pi) > 0.0001) { 35 console.println("Pi calculation went wrong") 36 return 1; 37 } 38 res = CallSlowFunction(2, repetitions); 39 if (Absolute(res - pi) > 0.0001) { 40 console.println("Pi calculation went wrong") 41 return 1; 42 } 43 res = CallSlowFunction(3, repetitions); 44 if (Absolute(res - pi) > 0.0001) { 45 console.println("Pi calculation went wrong") 46 return 1; 47 } 48 res = CallSlowFunction(4, repetitions); 49 if (Absolute(res - pi) > 0.0001) { 50 console.println("Pi calculation went wrong") 51 return 1; 52 } 53 54 return 0; 55} 56 57function Absolute(val: double): double { 58 if (val < 0) { 59 return val *= -1; 60 } 61 return val; 62} 63 64function CallSlowFunction(foo: int, repetitions: int): double { 65 let res: double = 0; 66 if (foo == 1) { 67 res = SlowETSFunction(repetitions); 68 } else if (foo == 2) { 69 res = SlowETSFunctionCopy1(repetitions); 70 } else if (foo == 3) { 71 res = SlowETSFunctionCopy2(repetitions); 72 } else if (foo == 4) { 73 res = SlowETSFunctionCopy3(repetitions); 74 } 75 return res; 76} 77 78// Doing copypaste because of compiler optimisations 79function SlowETSFunction(repetitions: int): double { 80 let ret: double = 0.0; 81 let sign: int = 1; 82 for (let i: int = 0; i < repetitions; i++) { 83 ret += sign / (2.0 * i + 1); 84 sign *= -1; 85 } 86 return 4 * ret; 87} 88 89function SlowETSFunctionCopy1(repetitions: int): double { 90 let ret: double = 0.0; 91 let sign: int = 1; 92 for (let i: int = 0; i < repetitions; i++) { 93 ret += sign / (2.0 * i + 1); 94 sign *= -1; 95 } 96 return 4 * ret; 97} 98 99function SlowETSFunctionCopy2(repetitions: int): double { 100 let ret: double = 0.0; 101 let sign: int = 1; 102 for (let i: int = 0; i < repetitions; i++) { 103 ret += sign / (2.0 * i + 1); 104 sign *= -1; 105 } 106 return 4 * ret; 107} 108 109function SlowETSFunctionCopy3(repetitions: int): double { 110 let ret: double = 0.0; 111 let sign: int = 1; 112 for (let i: int = 0; i < repetitions; i++) { 113 ret += sign / (2.0 * i + 1); 114 sign *= -1; 115 } 116 return 4 * ret; 117} 118