xref: /netbsd-src/sys/external/bsd/compiler_rt/dist/lib/builtins/powidf2.c (revision 156cd5872b4a1450b5c9c406f078c770980674de)
1*156cd587Sjoerg /* ===-- powidf2.cpp - Implement __powidf2 ---------------------------------===
2*156cd587Sjoerg  *
3*156cd587Sjoerg  *                     The LLVM Compiler Infrastructure
4*156cd587Sjoerg  *
5*156cd587Sjoerg  * This file is dual licensed under the MIT and the University of Illinois Open
6*156cd587Sjoerg  * Source Licenses. See LICENSE.TXT for details.
7*156cd587Sjoerg  *
8*156cd587Sjoerg  * ===----------------------------------------------------------------------===
9*156cd587Sjoerg  *
10*156cd587Sjoerg  * This file implements __powidf2 for the compiler_rt library.
11*156cd587Sjoerg  *
12*156cd587Sjoerg  * ===----------------------------------------------------------------------===
13*156cd587Sjoerg  */
14*156cd587Sjoerg 
15*156cd587Sjoerg #include "int_lib.h"
16*156cd587Sjoerg 
17*156cd587Sjoerg /* Returns: a ^ b */
18*156cd587Sjoerg 
19*156cd587Sjoerg COMPILER_RT_ABI double
__powidf2(double a,si_int b)20*156cd587Sjoerg __powidf2(double a, si_int b)
21*156cd587Sjoerg {
22*156cd587Sjoerg     const int recip = b < 0;
23*156cd587Sjoerg     double r = 1;
24*156cd587Sjoerg     while (1)
25*156cd587Sjoerg     {
26*156cd587Sjoerg         if (b & 1)
27*156cd587Sjoerg             r *= a;
28*156cd587Sjoerg         b /= 2;
29*156cd587Sjoerg         if (b == 0)
30*156cd587Sjoerg             break;
31*156cd587Sjoerg         a *= a;
32*156cd587Sjoerg     }
33*156cd587Sjoerg     return recip ? 1/r : r;
34*156cd587Sjoerg }
35