Home | History | Annotate | Download | only in aarch64
      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 #define ARRAY_SIZE(Array) (sizeof(Array) / sizeof((Array)[0]))
     30 #define __ masm->
     31 
     32 void GenerateSumArray(MacroAssembler* masm) {
     33   // uint32_t sum_array(uint8_t* array, uint32_t size)
     34   //  Argument locations:
     35   //    array (pointer) -> x0
     36   //    size            -> x1
     37 
     38   Label loop, end;
     39 
     40   __ Mov(x2, x0);
     41   __ Mov(w0, 0);
     42 
     43   // There's nothing to do if the array is empty.
     44   __ Cbz(w1, &end);
     45 
     46   // Go through the array and sum the elements.
     47   __ Bind(&loop);
     48 
     49   __ Ldrb(w3, MemOperand(x2, 1, PostIndex));  // w3 = *(x2++)
     50   __ Add(w0, w0, w3);
     51 
     52   __ Sub(w1, w1, 1);
     53   __ Cbnz(w1, &loop);
     54 
     55   __ Bind(&end);
     56   __ Ret();
     57 }
     58 
     59 
     60 #ifndef TEST_EXAMPLES
     61 #ifdef VIXL_INCLUDE_SIMULATOR_AARCH64
     62 int main(void) {
     63   MacroAssembler masm;
     64   Decoder decoder;
     65   Simulator simulator(&decoder);
     66 
     67   // Generate the code for the example function.
     68   Label sum_array;
     69   masm.Bind(&sum_array);
     70   GenerateSumArray(&masm);
     71   masm.FinalizeCode();
     72 
     73   // Run the example function.
     74   uint8_t data[] = {2, 45, 63, 7, 245, 38};
     75   uintptr_t data_addr = reinterpret_cast<uintptr_t>(data);
     76   simulator.WriteXRegister(0, data_addr);
     77   simulator.WriteXRegister(1, ARRAY_SIZE(data));
     78   simulator.RunFrom(masm.GetLabelAddress<Instruction*>(&sum_array));
     79 
     80   unsigned int i;
     81   for (i = 0; i < ARRAY_SIZE(data) - 1; ++i) {
     82     printf("%d + ", data[i]);
     83   }
     84   printf("%d = %d\n", data[i], simulator.ReadWRegister(0));
     85 
     86   return 0;
     87 }
     88 #else
     89 // Without the simulator there is nothing to test.
     90 int main(void) { return 0; }
     91 #endif  // VIXL_INCLUDE_SIMULATOR_AARCH64
     92 #endif  // TEST_EXAMPLES
     93