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