1 // Copyright 2014, VIXL authors
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 // * Redistributions of source code must retain the above copyright notice,
8 // this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above copyright notice,
10 // this list of conditions and the following disclaimer in the documentation
11 // and/or other materials provided with the distribution.
12 // * Neither the name of ARM Limited nor the names of its contributors may be
13 // used to endorse or promote products derived from this software without
14 // specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
17 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 #include "examples.h"
28
29 using namespace vixl;
30 using namespace vixl::aarch64;
31
32 #define ARRAY_SIZE(Array) (sizeof(Array) / sizeof((Array)[0]))
33 #define __ masm->
34
GenerateSumArray(MacroAssembler * masm)35 void GenerateSumArray(MacroAssembler* masm) {
36 // uint32_t sum_array(uint8_t* array, uint32_t size)
37 // Argument locations:
38 // array (pointer) -> x0
39 // size -> x1
40
41 Label loop, end;
42
43 __ Mov(x2, x0);
44 __ Mov(w0, 0);
45
46 // There's nothing to do if the array is empty.
47 __ Cbz(w1, &end);
48
49 // Go through the array and sum the elements.
50 __ Bind(&loop);
51
52 __ Ldrb(w3, MemOperand(x2, 1, PostIndex)); // w3 = *(x2++)
53 __ Add(w0, w0, w3);
54
55 __ Sub(w1, w1, 1);
56 __ Cbnz(w1, &loop);
57
58 __ Bind(&end);
59 __ Ret();
60 }
61
62
63 #ifndef TEST_EXAMPLES
64 #ifdef VIXL_INCLUDE_SIMULATOR_AARCH64
main(void)65 int main(void) {
66 MacroAssembler masm;
67 Decoder decoder;
68 Simulator simulator(&decoder);
69
70 // Generate the code for the example function.
71 Label sum_array;
72 masm.Bind(&sum_array);
73 GenerateSumArray(&masm);
74 masm.FinalizeCode();
75
76 // Run the example function.
77 uint8_t data[] = {2, 45, 63, 7, 245, 38};
78 uintptr_t data_addr = reinterpret_cast<uintptr_t>(data);
79 simulator.WriteXRegister(0, data_addr);
80 simulator.WriteXRegister(1, ARRAY_SIZE(data));
81 simulator.RunFrom(masm.GetLabelAddress<Instruction*>(&sum_array));
82
83 unsigned int i;
84 for (i = 0; i < ARRAY_SIZE(data) - 1; ++i) {
85 printf("%d + ", data[i]);
86 }
87 printf("%d = %d\n", data[i], simulator.ReadWRegister(0));
88
89 return 0;
90 }
91 #else
92 // Without the simulator there is nothing to test.
main(void)93 int main(void) { return 0; }
94 #endif // VIXL_INCLUDE_SIMULATOR_AARCH64
95 #endif // TEST_EXAMPLES
96