1// Copyright 2016, 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 29using namespace vixl; 30using namespace vixl::aarch32; 31 32#define __ masm-> 33 34void GenerateApproximatePi(MacroAssembler* masm) { 35 // double ApproximatePi(uint32_t iterations) 36 // Very rough approximation of pi 37 // pi/4 = 1 - 1/3 + 1/5 - 1/7 + ... + (-1)^n / (2n + 1) 38 __ Cmp(r0, 0); 39 __ Bx(eq, lr); 40 __ Vpush(Untyped64, DRegisterList(d8, 8)); 41 __ Vldr(d0, 1.0); 42 __ Vldr(d1, 3.0); 43 __ Vldr(d2, 5.0); 44 __ Vldr(d3, 7.0); 45 46 47 __ Vmov(d4, 8.0); 48 __ Vmov(d5, 1.0); 49 50 __ Vmov(I64, d10, 0); // d10 = 0.0; 51 __ Vmov(I64, d11, 0); // d11 = 0.0; 52 __ Vmov(I64, d12, 0); // d12 = 0.0; 53 __ Vmov(I64, d13, 0); // d13 = 0.0 54 55 Label loop; 56 __ Bind(&loop); 57 58 __ Vdiv(F64, d6, d5, d0); 59 __ Vdiv(F64, d7, d5, d1); 60 __ Vdiv(F64, d8, d5, d2); 61 __ Vdiv(F64, d9, d5, d3); 62 63 __ Vadd(F64, d10, d10, d6); 64 __ Vadd(F64, d11, d11, d7); 65 __ Vadd(F64, d12, d12, d8); 66 __ Vadd(F64, d13, d13, d9); 67 68 __ Vadd(F64, d0, d0, d4); 69 __ Vadd(F64, d1, d1, d4); 70 __ Vadd(F64, d2, d2, d4); 71 __ Vadd(F64, d3, d3, d4); 72 73 __ Subs(r0, r0, 1); 74 __ B(ne, &loop); 75 76 __ Vmov(F64, d4, 4.0); 77 __ Vadd(F64, d10, d10, d12); 78 __ Vadd(F64, d11, d11, d13); 79 __ Vsub(F64, d10, d10, d11); 80 __ Vmul(F64, d0, d10, d4); 81 __ Vpop(Untyped64, DRegisterList(d8, 8)); 82 __ Bx(lr); 83} 84 85#ifndef TEST_EXAMPLES 86int main() { 87 MacroAssembler masm; 88 // Generate the code for the example function. 89 Label pi_approx; 90 masm.Bind(&pi_approx); 91 GenerateApproximatePi(&masm); 92 masm.FinalizeCode(); 93#ifdef VIXL_INCLUDE_SIMULATOR_AARCH32 94 // There is no simulator defined for VIXL AArch32. 95 printf("This example cannot be simulated\n"); 96#else 97 byte* code = masm.GetBuffer()->GetStartAddress<byte*>(); 98 uint32_t code_size = masm.GetSizeOfCodeGenerated(); 99 ExecutableMemory memory(code, code_size); 100 // Run the example function. 101 double (*pi_function)(uint32_t) = 102 memory.GetEntryPoint<double (*)(uint32_t)>(pi_approx, 103 masm.GetInstructionSetInUse()); 104 uint32_t repeat = 10000000; 105 double output_value = (*pi_function)(repeat); 106 printf("native: pi_approx(%u) = %3.10f\n", repeat, output_value); 107#endif 108 return 0; 109} 110#endif // TEST_EXAMPLES 111