1 // Copyright 2015, 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 // Macro to compute the number of elements in a vector.
33 #define ARRAY_SIZE(Array) (sizeof(Array) / sizeof((Array)[0]))
34 #define __ masm->
35
36 /*
37 * This example adds two vectors with 1-byte elements using NEON instructions,
38 * and returns the results in the first vector.
39 */
GenerateAdd2Vectors(MacroAssembler * masm)40 void GenerateAdd2Vectors(MacroAssembler* masm) {
41 // void add2_vectors(uint8_t *vec*, const uint8_t *vecB, unsigned size)
42 // Argument locations:
43 // vecA (pointer) -> x0
44 // vecB (pointer) -> x1
45 // size (integer) -> w2
46 // Result returned in vecA.
47
48 Label loop16, loopr, end;
49
50 // Loop to add vector elements in 16-byte chunks.
51 __ Bind(&loop16);
52
53 // Handle vectors smaller than 16-bytes in the remainder loop.
54 __ Cmp(w2, 16);
55 __ B(lo, &loopr);
56 __ Sub(w2, w2, 16);
57
58 // Add vectors in 16-byte chunks.
59 __ Ld1(v0.V16B(), MemOperand(x0));
60 __ Ld1(v1.V16B(), MemOperand(x1, 16, PostIndex));
61 __ Add(v0.V16B(), v0.V16B(), v1.V16B());
62 __ St1(v0.V16B(), MemOperand(x0, 16, PostIndex));
63
64 __ B(&loop16);
65
66 // Loop to add the remaining vector elements.
67 __ Bind(&loopr);
68
69 // If there are no more vector elements to process, then exit.
70 __ Cbz(w2, &end);
71 __ Sub(w2, w2, 1);
72
73 // Add remaining vector elements in 1-byte chunks.
74 __ Ldrb(w5, MemOperand(x0));
75 __ Ldrb(w6, MemOperand(x1, 1, PostIndex));
76 __ Add(w5, w5, w6);
77 __ Strb(w5, MemOperand(x0, 1, PostIndex));
78
79 __ B(&loopr);
80
81 __ Bind(&end);
82
83 __ Ret();
84 }
85
86
PrintVector(const uint8_t * vec,unsigned num)87 void PrintVector(const uint8_t* vec, unsigned num) {
88 unsigned i;
89 printf("( ");
90 if (num > 0) {
91 for (i = 0; i < num - 1; ++i) {
92 printf("%d, ", vec[i]);
93 }
94 printf("%d", vec[i]);
95 }
96 printf(" )\n");
97 }
98
99
100 #ifndef TEST_EXAMPLES
main(void)101 int main(void) {
102 MacroAssembler masm;
103
104 // Generate native code for the example function.
105 Label add2_vectors;
106 masm.Bind(&add2_vectors);
107 GenerateAdd2Vectors(&masm);
108 masm.FinalizeCode();
109
110 // Initialize input data for the example function.
111 // clang-format: off
112 uint8_t vecA[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
113 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
114 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
115 uint8_t vecB[] = {16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28,
116 29, 30, 31, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
117 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36};
118 // clang-format on
119 uint8_t vecC[ARRAY_SIZE(vecA)];
120
121 // Check whether the number of elements in both vectors match.
122 VIXL_CHECK(ARRAY_SIZE(vecA) == ARRAY_SIZE(vecB));
123
124 // Compute the result in C.
125 for (unsigned i = 0; i < ARRAY_SIZE(vecA); i++) {
126 vecC[i] = vecA[i] + vecB[i];
127 }
128
129 #ifdef VIXL_INCLUDE_SIMULATOR_AARCH64
130 uintptr_t vecA_addr = reinterpret_cast<uintptr_t>(vecA);
131 uintptr_t vecB_addr = reinterpret_cast<uintptr_t>(vecB);
132
133 // Configure register environment in the simulator.
134 Decoder decoder;
135 Simulator simulator(&decoder);
136 simulator.WriteXRegister(0, vecA_addr);
137 simulator.WriteXRegister(1, vecB_addr);
138 simulator.WriteXRegister(2, ARRAY_SIZE(vecA));
139 PrintVector(vecA, ARRAY_SIZE(vecA));
140 printf(" +\n");
141 PrintVector(vecB, ARRAY_SIZE(vecB));
142
143 // Run the example function in the simulator.
144 simulator.RunFrom(masm.GetLabelAddress<Instruction*>(&add2_vectors));
145 printf(" =\n");
146 PrintVector(vecA, ARRAY_SIZE(vecA));
147
148 // Check that the computed value in NEON matches the C version.
149 for (unsigned i = 0; i < ARRAY_SIZE(vecA); i++) {
150 VIXL_CHECK(vecC[i] == vecA[i]);
151 }
152 #else
153 USE(vecC);
154
155 // Placeholder to run test natively.
156 printf("Running tests natively is not supported yet.\n");
157 #endif // VIXL_INCLUDE_SIMULATOR_AARCH64
158
159 return 0;
160 }
161 #endif // TEST_EXAMPLES
162