1b78f13911bfe6eda303e91ef215c87a165aae8aeAlexandre Rames// Copyright 2016, VIXL authors
2ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl// All rights reserved.
3ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl//
4ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl// Redistribution and use in source and binary forms, with or without
5ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl// modification, are permitted provided that the following conditions are met:
6ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl//
7ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl//   * Redistributions of source code must retain the above copyright notice,
8ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl//     this list of conditions and the following disclaimer.
9ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl//   * Redistributions in binary form must reproduce the above copyright notice,
10ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl//     this list of conditions and the following disclaimer in the documentation
11ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl//     and/or other materials provided with the distribution.
12ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl//   * Neither the name of ARM Limited nor the names of its contributors may be
13ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl//     used to endorse or promote products derived from this software without
14ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl//     specific prior written permission.
15ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl//
16ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
17ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl
27ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl#include "examples.h"
28ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl
29ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl#define __ masm->
30ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl
31ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixlvoid GenerateAbs(MacroAssembler* masm) {
3288c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois  // int32_t abs(int32_t x)
33ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl  // Argument location:
3488c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois  //   x -> r0
35ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl
3688c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois  __ Cmp(r0, 0);
3788c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois  // If r0 is negative, negate r0.
3888c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois  __ Rsb(mi, r0, r0, 0);
3988c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois  __ Bx(lr);
40ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl}
41ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl
42ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl
43ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl#ifndef TEST_EXAMPLES
4488c46b84df005638546de5e4e965bdcc31352f48Pierre Langloisint main() {
4510dae1a549308bddc1931f29754d6a4459f70c9bJacob Bramley  MacroAssembler masm(A32);
46ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl  // Generate the code for the example function.
47ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl  Label abs;
48ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl  masm.Bind(&abs);
49ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl  GenerateAbs(&masm);
50ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl  masm.FinalizeCode();
511e85b7f2e8ad2bfb233de29405aade635ed207cePierre Langlois#ifdef VIXL_INCLUDE_SIMULATOR_AARCH32
5288c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois  // There is no simulator defined for VIXL AArch32.
5388c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois  printf("This example cannot be simulated\n");
5488c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois#else
556a049f97861bd71c69d81f643e42308d28c5de31Alexandre Rames  byte* code = masm.GetBuffer()->GetStartAddress<byte*>();
56919e3fe28a5024c53ede42922092bbc32e89dcb8Alexandre Rames  uint32_t code_size = masm.GetSizeOfCodeGenerated();
5788c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois  ExecutableMemory memory(code, code_size);
58ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl  // Run the example function.
5988c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois  int32_t (*abs_function)(int32_t) =
60963fa8ad3b32ffb62ab7804184e45c6627c080b4Georgia Kouveli      memory.GetEntryPoint<int32_t (*)(int32_t)>(abs,
61963fa8ad3b32ffb62ab7804184e45c6627c080b4Georgia Kouveli                                                 masm.GetInstructionSetInUse());
6288c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois  int32_t input_value = -42;
6388c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois  int32_t output_value = (*abs_function)(input_value);
6488c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois  printf("native: abs(%d) = %d\n", input_value, output_value);
6588c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois#endif
66ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl  return 0;
67ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl}
68c68cb64496485710cdb5b8480f8fee287058c93farmvixl#endif  // TEST_EXAMPLES
69