1# Copyright (c) 2021-2022 Huawei Device Co., Ltd. 2# Licensed under the Apache License, Version 2.0 (the "License"); 3# you may not use this file except in compliance with the License. 4# You may obtain a copy of the License at 5# 6# http://www.apache.org/licenses/LICENSE-2.0 7# 8# Unless required by applicable law or agreed to in writing, software 9# distributed under the License is distributed on an "AS IS" BASIS, 10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11# See the License for the specific language governing permissions and 12# limitations under the License. 13 14.function u1 main(){ 15 movi v0, 3 16 movi v1, 2000000 17 movi v2, 1854053 18 call test, v0, v1, v2, v0 19 return 20} 21 22.function u1 test(i32 a0, i32 a1, i32 a2){ 23 call.short sieve, a0, a1 24 jne a2, exit_failure 25 ldai 0 26 return 27exit_failure: 28 ldai 1 29 return 30} 31 32.function i32 sieve(i32 a0, i32 a1){ 33 movi v2, 0 #sum 34 movi v3, 1 #loop_counter 35 ldai 1 36loop: 37 jgt a0, loop_exit 38 ldai 1 39 shl2 v3 40 mul2 a1 41 addi 1 42 sta v4 #m+1 43 newarr v5, v4, u1[] #flags 44 inci v4, -1 45 call.short nsieve, v4, v5 46 add2 v2 47 sta v2 48 inci v3, 1 49 lda v3 50 jmp loop 51loop_exit: 52 lda v2 53 return 54} 55 56.function i32 nsieve(i32 a0, u1[] a1){ 57 call.short init, a1, a1 58 movi v2, 0 #count 59 movi v3, 2 #loop_counter 60 ldai 2 61loop: 62 jgt a0, loop_exit 63 ldarr.8 a1 64 jnez if 65endif: 66 inci v3, 1 67 lda v3 68 jmp loop 69loop_exit: 70 lda v2 71 return 72if: 73 lda v3 74 add2 v3 75 sta v4 #loop2_counter 76loop2: 77 jgt a0, loop2_exit 78 ldai 0 79 starr.8 a1, v4 80 lda v4 81 add2 v3 82 sta v4 83 jmp loop2 84loop2_exit: 85 inci v2, 1 86 lda v2 87 jmp endif 88} 89 90.function void init(u1[] a0){ 91 lenarr a0 92 sta v0 #sizeof(a0) 93 movi v1, 2 #loop_counter 94 ldai 2 95loop: 96 jeq v0, loop_exit 97 ldai 1 98 starr.8 a0, v1 99 inci v1, 1 100 lda v1 101 jmp loop 102loop_exit: 103 lda.obj a0 104 return.void 105} 106