Home | History | Annotate | Download | only in examples
      1 // Copyright 2014, ARM Limited
      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 BUF_SIZE (4096)
     31 #define __ masm->
     32 
     33 void GenerateSumArray(MacroAssembler* masm) {
     34   // uint32_t sum_array(uint8_t* array, uint32_t size)
     35   //  Argument locations:
     36   //    array (pointer) -> x0
     37   //    size            -> x1
     38 
     39   Label loop, end;
     40 
     41   __ Mov(x2, x0);
     42   __ Mov(w0, 0);
     43 
     44   // There's nothing to do if the array is empty.
     45   __ Cbz(w1, &end);
     46 
     47   // Go through the array and sum the elements.
     48   __ Bind(&loop);
     49 
     50   __ Ldrb(w3, MemOperand(x2, 1, PostIndex));  // w3 = *(x2++)
     51   __ Add(w0, w0, w3);
     52 
     53   __ Sub(w1, w1, 1);
     54   __ Cbnz(w1, &loop);
     55 
     56   __ Bind(&end);
     57   __ Ret();
     58 }
     59 
     60 
     61 #ifndef TEST_EXAMPLES
     62 #ifdef VIXL_INCLUDE_SIMULATOR
     63 int main(void) {
     64   // Create and initialize the assembler and the simulator.
     65   byte assm_buf[BUF_SIZE];
     66   MacroAssembler masm(assm_buf, BUF_SIZE);
     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.set_xreg(0, data_addr);
     80   simulator.set_xreg(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.wreg(0));
     88 
     89   return 0;
     90 }
     91 #else
     92 // Without the simulator there is nothing to test.
     93 int main(void) { return 0; }
     94 #endif  // VIXL_INCLUDE_SIMULATOR
     95 #endif  // TEST_EXAMPLES
     96