Path: blob/master/src/hotspot/os/windows/sharedRuntimeRem.cpp
40931 views
/*1* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "runtime/sharedRuntime.hpp"2627#ifdef _WIN6428// These are copied defines from fdlibm.h, this allows us to keep the code29// the same as in the JDK, for easier maintenance.3031#define __HI(x) *(1+(int*)&x)32#define __LO(x) *(int*)&x3334// This code is a copy of __ieee754_fmod() from the JDK's libfdlibm and is35// used as a workaround for issues with the Windows x64 CRT implementation36// of fmod. Microsoft has acknowledged that this is an issue in Visual Studio37// 2012 and forward, but has not provided a time frame for a fix other than that38// it'll not be fixed in Visual Studio 2013 or 2015.3940static const double one = 1.0, Zero[] = { 0.0, -0.0, };4142double SharedRuntime::fmod_winx64(double x, double y)43{44int n, hx, hy, hz, ix, iy, sx, i;45unsigned lx, ly, lz;4647hx = __HI(x); /* high word of x */48lx = __LO(x); /* low word of x */49hy = __HI(y); /* high word of y */50ly = __LO(y); /* low word of y */51sx = hx & 0x80000000; /* sign of x */52hx ^= sx; /* |x| */53hy &= 0x7fffffff; /* |y| */5455#pragma warning( disable : 4146 )56/* purge off exception values */57if ((hy | ly) == 0 || (hx >= 0x7ff00000) || /* y=0,or x not finite */58((hy | ((ly | -ly) >> 31))>0x7ff00000)) /* or y is NaN */59#pragma warning( default : 4146 )60return (x*y) / (x*y);61if (hx <= hy) {62if ((hx<hy) || (lx<ly)) return x; /* |x|<|y| return x */63if (lx == ly)64return Zero[(unsigned)sx >> 31]; /* |x|=|y| return x*0*/65}6667/* determine ix = ilogb(x) */68if (hx<0x00100000) { /* subnormal x */69if (hx == 0) {70for (ix = -1043, i = lx; i>0; i <<= 1) ix -= 1;71}72else {73for (ix = -1022, i = (hx << 11); i>0; i <<= 1) ix -= 1;74}75}76else ix = (hx >> 20) - 1023;7778/* determine iy = ilogb(y) */79if (hy<0x00100000) { /* subnormal y */80if (hy == 0) {81for (iy = -1043, i = ly; i>0; i <<= 1) iy -= 1;82}83else {84for (iy = -1022, i = (hy << 11); i>0; i <<= 1) iy -= 1;85}86}87else iy = (hy >> 20) - 1023;8889/* set up {hx,lx}, {hy,ly} and align y to x */90if (ix >= -1022)91hx = 0x00100000 | (0x000fffff & hx);92else { /* subnormal x, shift x to normal */93n = -1022 - ix;94if (n <= 31) {95hx = (hx << n) | (lx >> (32 - n));96lx <<= n;97}98else {99hx = lx << (n - 32);100lx = 0;101}102}103if (iy >= -1022)104hy = 0x00100000 | (0x000fffff & hy);105else { /* subnormal y, shift y to normal */106n = -1022 - iy;107if (n <= 31) {108hy = (hy << n) | (ly >> (32 - n));109ly <<= n;110}111else {112hy = ly << (n - 32);113ly = 0;114}115}116117/* fix point fmod */118n = ix - iy;119while (n--) {120hz = hx - hy; lz = lx - ly; if (lx<ly) hz -= 1;121if (hz<0){ hx = hx + hx + (lx >> 31); lx = lx + lx; }122else {123if ((hz | lz) == 0) /* return sign(x)*0 */124return Zero[(unsigned)sx >> 31];125hx = hz + hz + (lz >> 31); lx = lz + lz;126}127}128hz = hx - hy; lz = lx - ly; if (lx<ly) hz -= 1;129if (hz >= 0) { hx = hz; lx = lz; }130131/* convert back to floating value and restore the sign */132if ((hx | lx) == 0) /* return sign(x)*0 */133return Zero[(unsigned)sx >> 31];134while (hx<0x00100000) { /* normalize x */135hx = hx + hx + (lx >> 31); lx = lx + lx;136iy -= 1;137}138if (iy >= -1022) { /* normalize output */139hx = ((hx - 0x00100000) | ((iy + 1023) << 20));140__HI(x) = hx | sx;141__LO(x) = lx;142}143else { /* subnormal output */144n = -1022 - iy;145if (n <= 20) {146lx = (lx >> n) | ((unsigned)hx << (32 - n));147hx >>= n;148}149else if (n <= 31) {150lx = (hx << (32 - n)) | (lx >> n); hx = sx;151}152else {153lx = hx >> (n - 32); hx = sx;154}155__HI(x) = hx | sx;156__LO(x) = lx;157x *= one; /* create necessary signal */158}159return x; /* exact output */160}161162#endif163164165