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# The following comment until the empty line must be a valid YAML document 15# containing exact copies of ISA specification assertions relevant to this test. 16#--- 17# - title: Load from array 18# description: > 19# Load an element from array using accumulator as an index and puts it into accumulator. 20# instructions: 21# - sig: ldarr vs 22# format: [op_vs_8] 23# type: obj 24# dtype: i32 25# etype: i32 26# - title: Store to array 27# description: > 28# Store accumulator content into array slot pointed by index. 29# instructions: 30# - sig: starr vs1, vs2 31# format: [op_vs1_4_vs2_4] 32# type: i32 33# dtype: none 34# etype: i32 35 36#int foo4(int n) { 37# int sum = 0; 38# if (n < 1) 39# return sum; 40# int *arr = new int[n]; 41# for (int i = 0; i < n; i++) { 42# arr[i] = 4; 43# sum += arr[i]; 44# } 45# return sum + foo4(n - 1); 46#} 47# 48#int foo3(int n) { 49# int sum = 0; 50# int *arr = new int[n]; 51# for (int i = 0; i < n; i++) { 52# arr[i] = 3; 53# sum += arr[i]; 54# } 55# n += 2; 56# sum += foo4(n); 57# return sum; 58#} 59# 60#int foo2(int n) { 61# int sum = 0; 62# int *arr = new int[n]; 63# for (int i = 0; i < n; i++) { 64# arr[i] = 2; 65# sum += arr[i]; 66# } 67# n += 2; 68# sum += foo3(n); 69# return sum; 70#} 71# 72#int foo1(int n) { 73# int sum = 0; 74# int *arr = new int[n]; 75# for (int i = 0; i < n; i++) { 76# arr[i] = 1; 77# sum += arr[i]; 78# } 79# n += 2; 80# sum += foo2(n); 81# return sum; 82#} 83# 84#int main() { 85# int n = 8; 86# if (foo1(n) != 484) 87# return 1; 88# return 0; 89#} 90 91.function i32 main(){ 92 movi v0, 8 93 movi v1, 484 94 call.short foo1, v0, v0 95 jne v1, exit_failure 96 ldai 0 97 return 98exit_failure: 99 ldai 1 100 return 101} 102 103.function i32 foo1(i32 a0){ 104 newarr v1, a0, i32[] 105 movi v0, 0 #loop_counter 106 movi v2, 0 #sum 107 lda v0 108loop: 109 jeq a0, loop_exit 110 ldai 1 111 starr v1, v0 112 lda v0 113 ldarr v1 114 add2 v2 115 sta v2 116 inci v0, 1 117 lda v0 118 jmp loop 119loop_exit: 120 lda a0 121 addi 2 122 sta a0 123 call.short foo2, a0, v2 124 add2 v2 125 return 126} 127 128.function i32 foo2(i32 a0){ 129 newarr v1, a0, i32[] 130 movi v0, 0 131 movi v2, 0 #sum 132 lda v0 133loop: 134 jeq a0, loop_exit 135 ldai 2 136 starr v1, v0 137 lda v0 138 ldarr v1 139 add2 v2 140 sta v2 141 inci v0, 1 142 lda v0 143 jmp loop 144loop_exit: 145 lda a0 146 addi 2 147 sta a0 148 call.short foo3, a0, a0 149 add2 v2 150 return 151} 152 153.function i32 foo3(i32 a0){ 154 newarr v1, a0, i32[] 155 movi v0, 0 #loop_counter 156 movi v2, 0 #sum 157 lda v0 158loop: 159 jeq a0, loop_exit 160 ldai 3 161 starr v1, v0 162 lda v0 163 ldarr v1 164 add2 v2 165 sta v2 166 inci v0, 1 167 lda v0 168 jmp loop 169loop_exit: 170 lda a0 171 addi 2 172 sta a0 173 call.short foo4, a0, a0 174 add2 v2 175 return 176} 177 178.function i32 foo4(i32 a0){ 179 newarr v1, a0, i32[] 180 movi v0, 0 #loop_counter 181 movi v2, 0 #sum 182 ldai 0 183 jeq a0, exit 184 lda v0 185loop: 186 jeq a0, loop_exit 187 ldai 4 188 starr v1, v0 189 lda v0 190 ldarr v1 191 add2 v2 192 sta v2 193 inci v0, 1 194 lda v0 195 jmp loop 196loop_exit: 197 inci a0, -1 198 call.short foo4, a0, a0 199 add2 v2 200 return 201exit: 202 ldai 0 203 return 204} 205