# Copyright (c) 2021-2022 Huawei Device Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # # The following comment until the empty line must be a valid YAML document # containing exact copies of ISA specification assertions relevant to this test. #--- # - title: Load from array # description: > # Load an element from array using accumulator as an index and puts it into accumulator. # instructions: # - sig: ldarr vs # format: [op_vs_8] # type: obj # dtype: i32 # etype: i32 # - title: Store to array # description: > # Store accumulator content into array slot pointed by index. # instructions: # - sig: starr vs1, vs2 # format: [op_vs1_4_vs2_4] # type: i32 # dtype: none # etype: i32 #int foo4(int n) { # int sum = 0; # if (n < 1) # return sum; # int *arr = new int[n]; # for (int i = 0; i < n; i++) { # arr[i] = 4; # sum += arr[i]; # } # return sum + foo4(n - 1); #} # #int foo3(int n) { # int sum = 0; # int *arr = new int[n]; # for (int i = 0; i < n; i++) { # arr[i] = 3; # sum += arr[i]; # } # n += 2; # sum += foo4(n); # return sum; #} # #int foo2(int n) { # int sum = 0; # int *arr = new int[n]; # for (int i = 0; i < n; i++) { # arr[i] = 2; # sum += arr[i]; # } # n += 2; # sum += foo3(n); # return sum; #} # #int foo1(int n) { # int sum = 0; # int *arr = new int[n]; # for (int i = 0; i < n; i++) { # arr[i] = 1; # sum += arr[i]; # } # n += 2; # sum += foo2(n); # return sum; #} # #int main() { # int n = 8; # if (foo1(n) != 484) # return 1; # return 0; #} .function i32 main(){ movi v0, 8 movi v1, 484 call.short foo1, v0, v0 jne v1, exit_failure ldai 0 return exit_failure: ldai 1 return } .function i32 foo1(i32 a0){ newarr v1, a0, i32[] movi v0, 0 #loop_counter movi v2, 0 #sum lda v0 loop: jeq a0, loop_exit ldai 1 starr v1, v0 lda v0 ldarr v1 add2 v2 sta v2 inci v0, 1 lda v0 jmp loop loop_exit: lda a0 addi 2 sta a0 call.short foo2, a0, v2 add2 v2 return } .function i32 foo2(i32 a0){ newarr v1, a0, i32[] movi v0, 0 movi v2, 0 #sum lda v0 loop: jeq a0, loop_exit ldai 2 starr v1, v0 lda v0 ldarr v1 add2 v2 sta v2 inci v0, 1 lda v0 jmp loop loop_exit: lda a0 addi 2 sta a0 call.short foo3, a0, a0 add2 v2 return } .function i32 foo3(i32 a0){ newarr v1, a0, i32[] movi v0, 0 #loop_counter movi v2, 0 #sum lda v0 loop: jeq a0, loop_exit ldai 3 starr v1, v0 lda v0 ldarr v1 add2 v2 sta v2 inci v0, 1 lda v0 jmp loop loop_exit: lda a0 addi 2 sta a0 call.short foo4, a0, a0 add2 v2 return } .function i32 foo4(i32 a0){ newarr v1, a0, i32[] movi v0, 0 #loop_counter movi v2, 0 #sum ldai 0 jeq a0, exit lda v0 loop: jeq a0, loop_exit ldai 4 starr v1, v0 lda v0 ldarr v1 add2 v2 sta v2 inci v0, 1 lda v0 jmp loop loop_exit: inci a0, -1 call.short foo4, a0, a0 add2 v2 return exit: ldai 0 return }