• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.record Math <external>
15.record Globals{
16    i32 flag <static>
17}
18
19.function f64 Math.pow(f64 a0, f64 a1) <external>
20.function f64 Math.cos(f64 a0) <external>
21.function f64 Math.sin(f64 a0) <external>
22.function f64 Math.absF64(f64 a0) <external>
23
24.function i32 main(){
25    # set 32, 640, 42.688806559475509 for fast run
26    movi v0, 32
27    movi v1, 640
28    fmovi.64 v2, 42.688806559475509
29    call test, v0, v1, v2, v0
30    return
31}
32
33.function i32 test(i32 a0, i32 a1, f64 a2){
34    ldai 0
35    ststatic Globals.flag
36    fmovi.64 v3, 0.0               #total
37    mov v5, a0                 #loop counter
38loop:
39    jgt a1, loop_exit
40    call.short partial, v5, v0
41    fadd2.64 v3
42    sta.64 v3
43    lda v5
44    muli 2
45    sta v5
46    jmp loop
47loop_exit:
48    lda.64 v3
49    fsub2.64 a2
50    sta.64 v6
51    call.short Math.absF64, v6, v6
52    sta.64 v6
53    fldai.64 1e-13
54    fcmpl.64 v6
55    jlez exit_failure
56    ldstatic Globals.flag
57    jeqz exit_failure
58    ldai 0
59    return
60exit_failure:
61    ldai 1
62    return
63}
64
65.function f64 partial(i32 a0){
66    fmovi.64 v10, -0.5
67    fmovi.64 v11, 0.0                #a1
68    fmovi.64 v12, 0.0                #a2
69    fmovi.64 v13, 0.0                #a3
70    fmovi.64 v14, 0.0                #a4
71    fmovi.64 v15, 0.0                #a5
72    fmovi.64 v16, 0.0                #a6
73    fmovi.64 v17, 0.0                #a7
74    fmovi.64 v18, 0.0                #a8
75    fmovi.64 v19, 0.0                #a9
76    fmovi.64 v20, 3.0
77    fldai.64 2.0
78    fdiv2.64 v20
79    sta.64 v20                     #twothirds
80    fmovi.64 v21, -1.0              #alt
81    fmovi.64 v22, 0.0               #k2
82    fmovi.64 v23, 0.0               #k3
83    fmovi.64 v24, 0.0               #sk
84    fmovi.64 v25, 0.0               #ck
85    movi.64 v27, 0                 #flag
86    movi v28, 1                 #loop counter
87    mov v26, a0
88    ldai 1
89loop:
90    jgt v26, loop_exit
91    i32tof64
92    sta.64 v29
93    fmul2.64 v29
94    sta.64 v30                     #k2
95    fmul2.64 v29
96    sta.64 v31                     #k3
97    mov.64 v0, v29
98    call.short Math.sin, v0, v0
99    sta.64 v32                     #sk
100    call.short Math.cos, v0, v0
101    sta.64 v33                     #ck
102    lda.64 v21
103    fneg.64
104    sta.64 v21
105    mov.64 v0, v20
106    fldai.64 -1.0
107    fadd2.64 v29
108    sta.64 v1
109    call.short Math.pow, v0, v1
110    fadd2.64 v11
111    sta.64 v11
112    mov.64 v0, v29
113    call.short Math.pow, v0, v10
114    fadd2.64 v12
115    sta.64 v12
116    fldai.64 1.0
117    fadd2.64 v29
118    fmul2.64 v29
119    sta.64 v34
120    fldai.64 1.0
121    fdiv2.64 v34
122    fadd2.64 v13
123    sta.64 v13
124    lda.64 v31
125    fmul2.64 v32
126    fmul2.64 v32
127    sta.64 v34
128    fldai.64 1.0
129    fdiv2.64 v34
130    fadd2.64 v14
131    sta.64 v14
132    lda.64 v31
133    fmul2.64 v33
134    fmul2.64 v33
135    sta.64 v34
136    fldai.64 1.0
137    fdiv2.64 v34
138    fadd2.64 v15
139    sta.64 v15
140    fldai.64 1.0
141    fdiv2.64 v29
142    fadd2.64 v16
143    sta.64 v16
144    fldai.64 1.0
145    fdiv2.64 v30
146    fadd2.64 v17
147    sta.64 v17
148    lda.64 v21
149    fdiv2.64 v29
150    fadd2.64 v18
151    sta.64 v18
152    fldai.64 2.0
153    fmul2.64 v29
154    sta.64 v34
155    fldai.64 -1.0
156    fadd2.64 v34
157    sta.64 v34
158    lda.64 v21
159    fdiv2.64 v34
160    fadd2.64 v19
161    sta.64 v19
162    lda v28
163    addi 1
164    sta v28
165    jmp loop
166loop_exit:
167    lda.64 v11
168    fadd2.64 v12
169    fadd2.64 v13
170    fadd2.64 v14
171    fadd2.64 v15
172    fmovi.64 v5, 0.0
173    fcmpl.64 v5
174    jgtz if
175    ldai 2
176    ststatic Globals.flag
177endif:
178    lda.64 v16
179    fadd2.64 v17
180    fadd2.64 v18
181    fadd2.64 v19
182    return.64
183if:
184    ldai 1
185    ststatic Globals.flag
186    jmp endif
187}
188