1# Copyright (c) 2021 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.record Math <external> 15.function f64 Math.sqrt(f64 a0) <external> 16.function f64 Math.absF64(f64 a0) <external> 17 18.function u1 main(){ 19 movi v0, 6 20 movi v1, 480 21 fmovi.64 v2, 8.909361304388094 22 call test, v0, v1, v2, v0 23 return 24} 25 26.function u1 test(i32 a0, i32 a1, f64 a2){ 27 fmovi.64 v3, 0.0 #total 28 mov v4, a0 #loop counter 29 lda a0 30loop: 31 jgt a1, loop_exit 32 call.short spectralnorm, v4, v4 33 fadd2.64 v3 34 sta.64 v3 35 lda v4 36 muli 2 37 sta v4 38 jmp loop 39loop_exit: 40 lda.64 v3 41 fsub2.64 a2 42 sta.64 v5 43 call.short Math.absF64, v5, v5 44 fldai.64 1e-13 45 fcmpl.64 v5 46 jltz exit_failure 47 ldai 0 48 return 49exit_failure: 50 ldai 1 51 return 52} 53 54.function f64 spectralnorm(i32 a0){ 55 mov v0, a0 56 newarr v1, v0, f64[] #u 57 newarr v2, v0, f64[] #w 58 newarr v3, v0, f64[] #v 59 fmovi.64 v4, 0.0 #vBv 60 fmovi.64 v5, 0.0 #vv 61 movi.64 v6, 0 #loop_counter 62 movi v20, 10 63 ldai 0 64loop: 65 jeq a0, loop_exit 66 fldai.64 1.0 67 fstarr.64 v1, v6 68 fldai.64 0.0 69 fstarr.64 v2, v6 70 fldai.64 0.0 71 fstarr.64 v3, v6 72 inci v6, 1 73 lda v6 74 jmp loop 75loop_exit: 76 movi v6, 0 77 ldai 0 78loop2: 79 jeq v20, loop2_exit 80 call AtAu, v1, v3, v2, v1 81 call AtAu, v3, v1, v2, v1 82 inci v6, 1 83 lda v6 84 jmp loop2 85loop2_exit: 86 movi v6, 0 87 ldai 0 88loop3: 89 jeq a0, loop3_exit 90 lda v6 91 fldarr.64 v1 92 sta.64 v7 93 lda.64 v6 94 fldarr.64 v3 95 fmul2.64 v7 96 fadd2.64 v4 97 sta.64 v4 98 lda v6 99 fldarr.64 v3 100 sta.64 v7 101 fmul2.64 v7 102 fadd2.64 v5 103 sta.64 v5 104 inci v6, 1 105 lda v6 106 jmp loop3 107loop3_exit: 108 lda.64 v4 109 fdiv2.64 v5 110 sta.64 v9 111 call.short Math.sqrt, v9, v9 112 return.64 113} 114 115.function void AtAu(f64[] a0, f64[] a1, f64[] a2){ 116 call.short Au, a0, a2 117 call.short Atu, a2, a1 118 return.void 119} 120 121.function void Atu(f64[] a0, f64[] a1){ 122 movi v2, 0 #loop_counter 123 lenarr a0 124 sta v0 125 lenarr a1 126 sta v1 127 ldai 0 128loop: 129 jeq v1, loop_exit 130 fmovi.64 v3, 0.0 #t 131 movi v4, 0 #loop2_counter 132 ldai 0 133loop2: 134 jeq v0, loop2_exit 135 call.short A, v4, v2 136 sta.64 v5 137 lda v4 138 fldarr.64 a0 139 fmul2.64 v5 140 fadd2.64 v3 141 sta.64 v3 142 inci v4, 1 143 lda v4 144 jmp loop2 145loop2_exit: 146 lda.64 v3 147 fstarr.64 a1, v2 148 inci v2, 1 149 lda v2 150 jmp loop 151loop_exit: 152 return.void 153} 154 155.function void Au(f64[] a0, f64[] a1){ 156 movi v2, 0 #loop_counter 157 lenarr a0 158 sta v0 159 lenarr a1 160 sta v1 161 ldai 0 162loop: 163 jeq v1, loop_exit 164 fmovi.64 v3, 0.0 #t 165 movi v4, 0 #loop2_counter 166 ldai 0 167loop2: 168 jeq v0, loop2_exit 169 call.short A, v2, v4 170 sta.64 v5 171 lda v4 172 fldarr.64 a0 173 fmul2.64 v5 174 fadd2.64 v3 175 sta.64 v3 176 inci v4, 1 177 lda v4 178 jmp loop2 179loop2_exit: 180 lda.64 v3 181 fstarr.64 a1, v2 182 inci v2, 1 183 lda v2 184 jmp loop 185loop_exit: 186 return.void 187} 188 189.function f64 A(i32 a0, i32 a1){ 190 fmovi.64 v10, 1.0 191 fmovi.64 v11, 2.0 192 lda a0 193 i32tof64 194 sta.64 a0 195 lda a1 196 i32tof64 197 sta.64 a1 198 fadd2.64 a0 199 fadd2.64 v10 200 sta.64 v2 201 lda.64 a0 202 fadd2.64 a1 203 fmul2.64 v2 204 fdiv2.64 v11 205 fadd2.64 a0 206 fadd2.64 v10 207 sta.64 v2 208 lda.64 v10 209 fdiv2.64 v2 210 return.64 211} 212