Home | History | Annotate | Download | only in aarch64
      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 #define __ masm->
     30 
     31 void GenerateCrc32(MacroAssembler* masm) {
     32   // uint32_t crc32(const char *msg, size_t length)
     33   // Argument location:
     34   //   msg (pointer)    -> x0
     35   //   length (integer) -> x1
     36 
     37   // This example computes CRC32CB on an input array of a given size
     38   // and returns the resulting checksum in w0.
     39 
     40   Label loop, end;
     41 
     42   // Move input array to temp register so we can re-use w0 as return register.
     43   __ Mov(x2, x0);
     44 
     45   // Initial remainder for the checksum. If length=0, then this value will be
     46   // returned.
     47   __ Mov(w0, 0xffffffff);
     48 
     49   // Loop for iterating through the array, starting at msg[0].
     50   __ Bind(&loop);
     51 
     52   // If no more elements to process, then exit function.
     53   __ Cbz(x1, &end);
     54   __ Sub(x1, x1, 1);
     55 
     56   // Compute checksum for msg[i].
     57   __ Ldrb(w3, MemOperand(x2, 1, PostIndex));
     58   __ Crc32b(w0, w0, w3);
     59   __ B(&loop);
     60 
     61   __ Bind(&end);
     62 
     63   __ Ret();
     64 }
     65 
     66 #ifndef TEST_EXAMPLES
     67 
     68 void runExample(const char* msg) {
     69   MacroAssembler masm;
     70 
     71   // Generate the code for the example function.
     72   Label func;
     73   masm.Bind(&func);
     74   GenerateCrc32(&masm);
     75   masm.FinalizeCode();
     76 
     77 #ifdef VIXL_INCLUDE_SIMULATOR_AARCH64
     78   // Run example function in the simulator.
     79   uintptr_t msg_addr = reinterpret_cast<uintptr_t>(msg);
     80   size_t msg_size = strlen(msg);
     81   Decoder decoder;
     82   Simulator simulator(&decoder);
     83   simulator.WriteXRegister(0, msg_addr);
     84   simulator.WriteXRegister(1, msg_size);
     85   simulator.RunFrom(masm.GetLabelAddress<Instruction*>(&func));
     86   printf("crc32(\"%s\")=0x%x\n", msg, simulator.ReadWRegister(0));
     87 #else
     88   // Run example function natively.
     89   printf("Not yet implemented.\n");
     90   USE(msg);
     91 #endif  // VIXL_INCLUDE_SIMULATOR_AARCH64
     92 }
     93 
     94 
     95 int main(void) {
     96   runExample("Hello World!");
     97   runExample("do");
     98   runExample("1");
     99   runExample("");
    100 
    101   return 0;
    102 }
    103 #endif  // TEST_EXAMPLES
    104