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 #define __ masm->
33
GenerateCrc32(MacroAssembler * masm)34 void GenerateCrc32(MacroAssembler* masm) {
35 // uint32_t crc32(const char *msg, size_t length)
36 // Argument location:
37 // msg (pointer) -> x0
38 // length (integer) -> x1
39
40 // This example computes CRC32CB on an input array of a given size
41 // and returns the resulting checksum in w0.
42
43 Label loop, end;
44
45 // Move input array to temp register so we can re-use w0 as return register.
46 __ Mov(x2, x0);
47
48 // Initial remainder for the checksum. If length=0, then this value will be
49 // returned.
50 __ Mov(w0, 0xffffffff);
51
52 // Loop for iterating through the array, starting at msg[0].
53 __ Bind(&loop);
54
55 // If no more elements to process, then exit function.
56 __ Cbz(x1, &end);
57 __ Sub(x1, x1, 1);
58
59 // Compute checksum for msg[i].
60 __ Ldrb(w3, MemOperand(x2, 1, PostIndex));
61 __ Crc32b(w0, w0, w3);
62 __ B(&loop);
63
64 __ Bind(&end);
65
66 __ Ret();
67 }
68
69 #ifndef TEST_EXAMPLES
70
runExample(const char * msg)71 void runExample(const char* msg) {
72 MacroAssembler masm;
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 VIXL_INCLUDE_SIMULATOR_AARCH64
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.WriteXRegister(0, msg_addr);
87 simulator.WriteXRegister(1, msg_size);
88 simulator.RunFrom(masm.GetLabelAddress<Instruction*>(&func));
89 printf("crc32(\"%s\")=0x%x\n", msg, simulator.ReadWRegister(0));
90 #else
91 // Run example function natively.
92 printf("Not yet implemented.\n");
93 USE(msg);
94 #endif // VIXL_INCLUDE_SIMULATOR_AARCH64
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