lfoltan@7000: /* lfoltan@7000: * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. lfoltan@7000: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. lfoltan@7000: * lfoltan@7000: * This code is free software; you can redistribute it and/or modify it lfoltan@7000: * under the terms of the GNU General Public License version 2 only, as lfoltan@7000: * published by the Free Software Foundation. lfoltan@7000: * lfoltan@7000: * This code is distributed in the hope that it will be useful, but WITHOUT lfoltan@7000: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or lfoltan@7000: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License lfoltan@7000: * version 2 for more details (a copy is included in the LICENSE file that lfoltan@7000: * accompanied this code). lfoltan@7000: * lfoltan@7000: * You should have received a copy of the GNU General Public License version lfoltan@7000: * 2 along with this work; if not, write to the Free Software Foundation, lfoltan@7000: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. lfoltan@7000: * lfoltan@7000: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA lfoltan@7000: * or visit www.oracle.com if you need additional information or have any lfoltan@7000: * questions. lfoltan@7000: * lfoltan@7000: */ lfoltan@7000: lfoltan@7000: #ifndef SHARE_VM_RUNTIME_SHAREDRUNTIMEMATH_HPP lfoltan@7000: #define SHARE_VM_RUNTIME_SHAREDRUNTIMEMATH_HPP lfoltan@7000: lfoltan@7000: #include lfoltan@7000: thartmann@7002: // Used to access the lower/higher 32 bits of a double thartmann@7002: typedef union { thartmann@7002: double d; thartmann@7002: struct { lfoltan@7000: #ifdef VM_LITTLE_ENDIAN thartmann@7002: int lo; thartmann@7002: int hi; lfoltan@7000: #else thartmann@7002: int hi; thartmann@7002: int lo; lfoltan@7000: #endif thartmann@7002: } split; thartmann@7002: } DoubleIntConv; thartmann@7002: thartmann@7002: static inline int high(double d) { thartmann@7002: DoubleIntConv x = { d }; thartmann@7002: return x.split.hi; thartmann@7002: } thartmann@7002: thartmann@7002: static inline int low(double d) { thartmann@7002: DoubleIntConv x = { d }; thartmann@7002: return x.split.lo; thartmann@7002: } thartmann@7002: thartmann@7002: static inline void set_high(double* d, int high) { thartmann@7002: DoubleIntConv conv = { *d }; thartmann@7002: conv.split.hi = high; thartmann@7002: *d = conv.d; thartmann@7002: } thartmann@7002: thartmann@7002: static inline void set_low(double* d, int low) { thartmann@7002: DoubleIntConv conv = { *d }; thartmann@7002: conv.split.lo = low; thartmann@7002: *d = conv.d; thartmann@7002: } lfoltan@7000: lfoltan@7000: static double copysignA(double x, double y) { thartmann@7002: DoubleIntConv convX = { x }; thartmann@7002: convX.split.hi = (convX.split.hi & 0x7fffffff) | (high(y) & 0x80000000); thartmann@7002: return convX.d; lfoltan@7000: } lfoltan@7000: lfoltan@7000: /* lfoltan@7000: * ==================================================== lfoltan@7000: * Copyright (c) 1998 Oracle and/or its affiliates. All rights reserved. lfoltan@7000: * lfoltan@7000: * Developed at SunSoft, a Sun Microsystems, Inc. business. lfoltan@7000: * Permission to use, copy, modify, and distribute this lfoltan@7000: * software is freely granted, provided that this notice lfoltan@7000: * is preserved. lfoltan@7000: * ==================================================== lfoltan@7000: */ lfoltan@7000: lfoltan@7000: /* lfoltan@7000: * scalbn (double x, int n) lfoltan@7000: * scalbn(x,n) returns x* 2**n computed by exponent lfoltan@7000: * manipulation rather than by actually performing an lfoltan@7000: * exponentiation or a multiplication. lfoltan@7000: */ lfoltan@7000: lfoltan@7000: static const double lfoltan@7000: two54 = 1.80143985094819840000e+16, /* 0x43500000, 0x00000000 */ lfoltan@7000: twom54 = 5.55111512312578270212e-17, /* 0x3C900000, 0x00000000 */ lfoltan@7000: hugeX = 1.0e+300, lfoltan@7000: tiny = 1.0e-300; lfoltan@7000: thartmann@7002: static double scalbnA(double x, int n) { lfoltan@7000: int k,hx,lx; thartmann@7002: hx = high(x); thartmann@7002: lx = low(x); lfoltan@7000: k = (hx&0x7ff00000)>>20; /* extract exponent */ lfoltan@7000: if (k==0) { /* 0 or subnormal x */ lfoltan@7000: if ((lx|(hx&0x7fffffff))==0) return x; /* +-0 */ lfoltan@7000: x *= two54; thartmann@7002: hx = high(x); lfoltan@7000: k = ((hx&0x7ff00000)>>20) - 54; lfoltan@7000: if (n< -50000) return tiny*x; /*underflow*/ lfoltan@7000: } lfoltan@7000: if (k==0x7ff) return x+x; /* NaN or Inf */ lfoltan@7000: k = k+n; thartmann@7002: if (k > 0x7fe) return hugeX*copysignA(hugeX,x); /* overflow */ thartmann@7002: if (k > 0) { /* normal result */ thartmann@7002: set_high(&x, (hx&0x800fffff)|(k<<20)); thartmann@7002: return x; thartmann@7002: } lfoltan@7000: if (k <= -54) { lfoltan@7000: if (n > 50000) /* in case integer overflow in n+k */ lfoltan@7000: return hugeX*copysignA(hugeX,x); /*overflow*/ lfoltan@7000: else return tiny*copysignA(tiny,x); /*underflow*/ lfoltan@7000: } lfoltan@7000: k += 54; /* subnormal result */ thartmann@7002: set_high(&x, (hx&0x800fffff)|(k<<20)); lfoltan@7000: return x*twom54; lfoltan@7000: } lfoltan@7000: lfoltan@7000: #endif // SHARE_VM_RUNTIME_SHAREDRUNTIMEMATH_HPP