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, 10 16 movi v1, 38 17 call.short test, v0, v1 18 return 19} 20 21.function u1 test(i32 a0, i32 a1){ 22 call.short fannkuch, a0, a0 23 jne a1, if 24 ldai 0 25 return 26if: 27 ldai 1 28 return 29} 30 31.function i32 fannkuch(i32 a0){ 32 mov v0, a0 33 newarr v1, v0, i32[] #perm 34 newarr v2, v0, i32[] #perm1 35 newarr v3, v0, i32[] #count 36 newarr v4, v0, i32[] #maxPerm 37 movi v5, 0 #maxFlipsCount 38 movi v15, 1 39 lda a0 40 sub2 v15 41 sta v6 #m 42 call.short init, v2, v2 43 mov v7, a0 #r 44loop: 45loop2: 46 ldai 1 47 jeq v7, loop2_exit 48 lda v7 49 sub2 v15 50 sta v8 51 lda v7 52 starr v3, v8 53 inci v7, -1 54 lda v7 55 jmp loop2 56loop2_exit: 57 ldai 0 58 ldarr v2 59 jnez label 60endif: 61loop5: 62 lda v7 63 jeq a0, if3 64 ldai 0 65 ldarr v2 66 sta v14 #perm0 67 movi v13, 0 #i=0 68 ldai 0 69loop6: 70 jeq v7, loop6_exit 71 lda v13 72 addi 1 73 ldarr v2 74 starr v2, v13 75 inci v13, 1 76 lda v13 77 jmp loop6 78loop6_exit: 79 lda v14 80 starr v2, v7 81 lda v7 82 ldarr v3 83 sub2 v15 84 starr v3, v7 85 lda v7 86 ldarr v3 87 jgtz loop5_exit 88 inci v7, 1 89 lda v7 90 jmp loop5 91loop5_exit: 92 jmp loop 93if: 94 call.short init2, v1, v2 95 movi v10, 0 #flipsCount 96loop3: 97 ldai 0 98 ldarr v1 99 sta v11 #k 100 jeqz loop3_exit 101 addi 1 102 shri 1 103 sta v12 #k2 104 movi v13, 0 #loop_counter 105 ldai 0 106loop4: 107 jeq v12, loop4_exit 108 ldarr v1 109 sta v14 #temp 110 sub v11, v13 111 ldarr v1 112 starr v1, v13 113 sub v11, v13 114 sta v0 115 lda v14 116 starr v1, v0 117 inci v13, 1 118 lda v13 119 jmp loop4 120loop4_exit: 121 inci v10, 1 122 lda v10 123 jmp loop3 124loop3_exit: 125 lda v5 126 jlt v10, if2 127endif2: 128 jmp endif 129if2: 130 mov v5, v10 131 call.short init2, v4, v2 132 jmp endif2 133if3: 134 lda v5 135 return 136label: 137 lda v6 138 ldarr v2 139 jne v6, if 140 jmp endif 141} 142 143.function void init (i32[] a0){ 144 movi v1, 0 145 lenarr a0 146 sta v0 #sizeof(v0) 147 lda v1 #loop_counter 148loop: 149 jeq v0, loop_exit 150 lda v1 151 starr a0, v1 152 inci v1, 1 153 lda v1 154 jmp loop 155loop_exit: 156 return.void 157} 158 159.function void init2 (i32[] a0, i32[] a1){ 160 lenarr a0 161 sta v0 162 movi v2, 0 163 lda v2 164loop: 165 jeq v0, loop_exit 166 ldarr a1 167 starr a0, v2 168 inci v2, 1 169 lda v2 170 jmp loop 171loop_exit: 172 return.void 173} 174