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, 4 16 movi v1, 1000000 17 movi.64 v2, -92346246980479 18 call test, v0, v1, v2, v0 19 return 20} 21 22.function u1 test(i32 a0, i32 a1, i64 a2){ 23 call.short sieve, a0, a1 24 sta.obj v3 #array 25 movi.64 v4, 0 #sum 26 movi v5, 0 #loop counter 27 lenarr v3 28 sta v6 #lenarr 29 ldai.64 0 30loop: 31 jeq v6, loop_exit 32 ldarr v3 33 i32toi64 34 add2.64 v4 35 sta.64 v4 36 inci v5, 1 37 lda v5 38 jmp loop 39loop_exit: 40 lda.64 v4 41 cmp.64 a2 42 jnez exit_err 43 ldai 0 44 return 45exit_err: 46 ldai 1 47 return 48} 49 50.function i32[] sieve(i32 a0, i32 a1){ 51 shl a1, a0 52 addi 31 53 shri 5 54 sta v2 55 newarr v3, v2, i32[] 56 call primes, v3, a0, a1, v0 57 return.obj 58} 59 60.function i32[] primes(i32[] a0, i32 a1, i32 a2){ 61 shl a2, a1 62 sta v3 #m 63 call.short init, a0, a0 64 sta.obj a0 65 movi v0, 2 #loop counter 66 ldai 2 67loop: 68 jeq v3, loop_exit 69 lda v0 70 andi 31 71 sta v5 72 ldai 1 73 shl2 v5 74 sta v5 75 lda v0 76 shri 5 77 ldarr a0 78 and2 v5 79 jnez if 80endif: 81 inci v0, 1 82 lda v0 83 jmp loop 84loop_exit: 85 lda.obj a0 86 return.obj 87if: 88 add v0, v0 89 sta v1 #loop2_counter 90loop2: 91 jge v3, loop2_exit 92 andi 31 93 sta v5 94 ldai 1 95 shl2 v5 96 not 97 sta v5 98 lda v1 99 shri 5 100 sta v6 101 ldarr a0 102 and2 v5 103 starr a0, v6 104 lda v1 105 add2 v0 106 sta v1 107 jmp loop2 108loop2_exit: 109 jmp endif 110} 111 112.function i32[] init(i32[] a0){ 113 lenarr a0 114 sta v0 #sizeof(a0) 115 movi v1, 0 #loop counter 116 ldai 0 117loop: 118 jeq v0, loop_exit 119 ldai 0xffffffff 120 starr a0, v1 121 inci v1, 1 122 lda v1 123 jmp loop 124loop_exit: 125 lda.obj a0 126 return.obj 127} 128