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