• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 __ masm->
30 
GenerateFactorial(MacroAssembler * masm)31 void GenerateFactorial(MacroAssembler* masm) {
32   // uint64_t factorial(uint64_t n)
33   // Argument location:
34   //   n -> x0
35 
36   Label loop, end;
37 
38   __ Mov(x1, x0);
39   __ Mov(x0, 1);  // Use x0 as the accumulator.
40 
41   __ Cbz(x1, &end);  // Nothing to do if the input is null.
42 
43   __ Bind(&loop);
44   __ Mul(x0, x0, x1);
45   __ Sub(x1, x1, 1);
46   __ Cbnz(x1, &loop);
47 
48   __ Bind(&end);
49   // The return value is in x0.
50   __ Ret();
51 }
52 
53 
54 #ifndef TEST_EXAMPLES
55 #ifdef VIXL_INCLUDE_SIMULATOR_AARCH64
main(void)56 int main(void) {
57   MacroAssembler masm;
58   Decoder decoder;
59   Simulator simulator(&decoder);
60 
61   // Generate the code for the example function.
62   Label factorial;
63   masm.Bind(&factorial);
64   GenerateFactorial(&masm);
65   masm.FinalizeCode();
66 
67   // Run the example function.
68   uint64_t input_val = 16;
69   simulator.WriteXRegister(0, input_val);
70   simulator.RunFrom(masm.GetLabelAddress<Instruction*>(&factorial));
71   printf("factorial(%" PRIu64 ") = %" PRId64 "\n",
72          input_val,
73          simulator.ReadXRegister(0));
74 
75   return 0;
76 }
77 #else
78 // Without the simulator there is nothing to test.
main(void)79 int main(void) { return 0; }
80 #endif  // VIXL_INCLUDE_SIMULATOR_AARCH64
81 #endif  // TEST_EXAMPLES
82