• 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 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_a, const uint8_t *vec_b, unsigned size)
42   // Argument locations:
43   //    vec_a (pointer) -> x0
44   //    vec_b (pointer) -> x1
45   //    size (integer) -> w2
46   // Result returned in vec_a.
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 
87 #ifndef TEST_EXAMPLES
88 #ifdef VIXL_INCLUDE_SIMULATOR_AARCH64
PrintVector(const uint8_t * vec,unsigned num)89 void PrintVector(const uint8_t* vec, unsigned num) {
90   unsigned i;
91   printf("( ");
92   if (num > 0) {
93     for (i = 0; i < num - 1; ++i) {
94       printf("%d, ", vec[i]);
95     }
96     printf("%d", vec[i]);
97   }
98   printf(" )\n");
99 }
100 #endif
101 
102 
main(void)103 int main(void) {
104   MacroAssembler masm;
105 
106   // Generate native code for the example function.
107   Label add2_vectors;
108   masm.Bind(&add2_vectors);
109   GenerateAdd2Vectors(&masm);
110   masm.FinalizeCode();
111 
112   // Initialize input data for the example function.
113   // clang-format: off
114   uint8_t vec_a[] = {0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12,
115                      13, 14, 15, 0,  1,  2,  3,  4,  5,  6,  7,  8,  9,
116                      10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
117   uint8_t vec_b[] = {16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28,
118                      29, 30, 31, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
119                      26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36};
120   // clang-format on
121   uint8_t vec_c[ARRAY_SIZE(vec_a)];
122 
123   // Check whether the number of elements in both vectors match.
124   VIXL_CHECK(ARRAY_SIZE(vec_a) == ARRAY_SIZE(vec_b));
125 
126   // Compute the result in C.
127   for (unsigned i = 0; i < ARRAY_SIZE(vec_a); i++) {
128     vec_c[i] = vec_a[i] + vec_b[i];
129   }
130 
131 #ifdef VIXL_INCLUDE_SIMULATOR_AARCH64
132   uintptr_t vec_a_addr = reinterpret_cast<uintptr_t>(vec_a);
133   uintptr_t vec_b_addr = reinterpret_cast<uintptr_t>(vec_b);
134 
135   // Configure register environment in the simulator.
136   Decoder decoder;
137   Simulator simulator(&decoder);
138   simulator.WriteXRegister(0, vec_a_addr);
139   simulator.WriteXRegister(1, vec_b_addr);
140   simulator.WriteXRegister(2, ARRAY_SIZE(vec_a));
141   PrintVector(vec_a, ARRAY_SIZE(vec_a));
142   printf(" +\n");
143   PrintVector(vec_b, ARRAY_SIZE(vec_b));
144 
145   // Run the example function in the simulator.
146   simulator.RunFrom(masm.GetLabelAddress<Instruction*>(&add2_vectors));
147   printf(" =\n");
148   PrintVector(vec_a, ARRAY_SIZE(vec_a));
149 
150   // Check that the computed value in NEON matches the C version.
151   for (unsigned i = 0; i < ARRAY_SIZE(vec_a); i++) {
152     VIXL_CHECK(vec_c[i] == vec_a[i]);
153   }
154 #else
155   USE(vec_c);
156 
157   // Placeholder to run test natively.
158   printf("Running tests natively is not supported yet.\n");
159 #endif  // VIXL_INCLUDE_SIMULATOR_AARCH64
160 
161   return 0;
162 }
163 #endif  // TEST_EXAMPLES
164