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