1 /* 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #ifndef SHARE_VM_RUNTIME_SHAREDRUNTIMEMATH_HPP 26 #define SHARE_VM_RUNTIME_SHAREDRUNTIMEMATH_HPP 27 28 #include <math.h> 29 30 // Used to access the lower/higher 32 bits of a double 31 typedef union { 32 double d; 33 struct { 34 #ifdef VM_LITTLE_ENDIAN 35 int lo; 36 int hi; 37 #else 38 int hi; 39 int lo; 40 #endif 41 } split; 42 } DoubleIntConv; 43 44 static inline int high(double d) { 45 DoubleIntConv x = { d }; 46 return x.split.hi; 47 } 48 49 static inline int low(double d) { 50 DoubleIntConv x = { d }; 51 return x.split.lo; 52 } 53 54 static inline void set_high(double* d, int high) { 55 DoubleIntConv conv = { *d }; 56 conv.split.hi = high; 57 *d = conv.d; 58 } 59 60 static inline void set_low(double* d, int low) { 61 DoubleIntConv conv = { *d }; 62 conv.split.lo = low; 63 *d = conv.d; 64 } 65 66 static double copysignA(double x, double y) { 67 DoubleIntConv convX = { x }; 68 convX.split.hi = (convX.split.hi & 0x7fffffff) | (high(y) & 0x80000000); 69 return convX.d; 70 } 71 72 /* 73 * ==================================================== 74 * Copyright (c) 1998 Oracle and/or its affiliates. All rights reserved. 75 * 76 * Developed at SunSoft, a Sun Microsystems, Inc. business. 77 * Permission to use, copy, modify, and distribute this 78 * software is freely granted, provided that this notice 79 * is preserved. 80 * ==================================================== 81 */ 82 83 /* 84 * scalbn (double x, int n) 85 * scalbn(x,n) returns x* 2**n computed by exponent 86 * manipulation rather than by actually performing an 87 * exponentiation or a multiplication. 88 */ 89 90 static const double 91 two54 = 1.80143985094819840000e+16, /* 0x43500000, 0x00000000 */ 92 twom54 = 5.55111512312578270212e-17, /* 0x3C900000, 0x00000000 */ 93 hugeX = 1.0e+300, 94 tiny = 1.0e-300; 95 96 static double scalbnA(double x, int n) { 97 int k,hx,lx; 98 hx = high(x); 99 lx = low(x); 100 k = (hx&0x7ff00000)>>20; /* extract exponent */ 101 if (k==0) { /* 0 or subnormal x */ 102 if ((lx|(hx&0x7fffffff))==0) return x; /* +-0 */ 103 x *= two54; 104 hx = high(x); 105 k = ((hx&0x7ff00000)>>20) - 54; 106 if (n< -50000) return tiny*x; /*underflow*/ 107 } 108 if (k==0x7ff) return x+x; /* NaN or Inf */ 109 k = k+n; 110 if (k > 0x7fe) return hugeX*copysignA(hugeX,x); /* overflow */ 111 if (k > 0) { /* normal result */ 112 set_high(&x, (hx&0x800fffff)|(k<<20)); 113 return x; 114 } 115 if (k <= -54) { 116 if (n > 50000) /* in case integer overflow in n+k */ 117 return hugeX*copysignA(hugeX,x); /*overflow*/ 118 else return tiny*copysignA(tiny,x); /*underflow*/ 119 } 120 k += 54; /* subnormal result */ 121 set_high(&x, (hx&0x800fffff)|(k<<20)); 122 return x*twom54; 123 } 124 125 #endif // SHARE_VM_RUNTIME_SHAREDRUNTIMEMATH_HPP