125c28e83SPiotr Jasiukajtis /* 225c28e83SPiotr Jasiukajtis * CDDL HEADER START 325c28e83SPiotr Jasiukajtis * 425c28e83SPiotr Jasiukajtis * The contents of this file are subject to the terms of the 525c28e83SPiotr Jasiukajtis * Common Development and Distribution License (the "License"). 625c28e83SPiotr Jasiukajtis * You may not use this file except in compliance with the License. 725c28e83SPiotr Jasiukajtis * 825c28e83SPiotr Jasiukajtis * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 925c28e83SPiotr Jasiukajtis * or http://www.opensolaris.org/os/licensing. 1025c28e83SPiotr Jasiukajtis * See the License for the specific language governing permissions 1125c28e83SPiotr Jasiukajtis * and limitations under the License. 1225c28e83SPiotr Jasiukajtis * 1325c28e83SPiotr Jasiukajtis * When distributing Covered Code, include this CDDL HEADER in each 1425c28e83SPiotr Jasiukajtis * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 1525c28e83SPiotr Jasiukajtis * If applicable, add the following below this CDDL HEADER, with the 1625c28e83SPiotr Jasiukajtis * fields enclosed by brackets "[]" replaced with your own identifying 1725c28e83SPiotr Jasiukajtis * information: Portions Copyright [yyyy] [name of copyright owner] 1825c28e83SPiotr Jasiukajtis * 1925c28e83SPiotr Jasiukajtis * CDDL HEADER END 2025c28e83SPiotr Jasiukajtis */ 2125c28e83SPiotr Jasiukajtis /* 2225c28e83SPiotr Jasiukajtis * Copyright 2011 Nexenta Systems, Inc. All rights reserved. 2325c28e83SPiotr Jasiukajtis */ 2425c28e83SPiotr Jasiukajtis /* 2525c28e83SPiotr Jasiukajtis * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 2625c28e83SPiotr Jasiukajtis * Use is subject to license terms. 2725c28e83SPiotr Jasiukajtis */ 2825c28e83SPiotr Jasiukajtis 29*ddc0e0b5SRichard Lowe #pragma weak __fmodf = fmodf 3025c28e83SPiotr Jasiukajtis 3125c28e83SPiotr Jasiukajtis #include "libm.h" 3225c28e83SPiotr Jasiukajtis 3325c28e83SPiotr Jasiukajtis /* INDENT OFF */ 3425c28e83SPiotr Jasiukajtis static const int 3525c28e83SPiotr Jasiukajtis is = (int)0x80000000, 3625c28e83SPiotr Jasiukajtis im = 0x007fffff, 3725c28e83SPiotr Jasiukajtis ii = 0x7f800000, 3825c28e83SPiotr Jasiukajtis iu = 0x00800000; 3925c28e83SPiotr Jasiukajtis /* INDENT ON */ 4025c28e83SPiotr Jasiukajtis 4125c28e83SPiotr Jasiukajtis static const float zero = 0.0; 4225c28e83SPiotr Jasiukajtis 4325c28e83SPiotr Jasiukajtis float 4425c28e83SPiotr Jasiukajtis fmodf(float x, float y) { 4525c28e83SPiotr Jasiukajtis float w; 4625c28e83SPiotr Jasiukajtis int hx, ix, iy, iz, k, ny, nd; 4725c28e83SPiotr Jasiukajtis 4825c28e83SPiotr Jasiukajtis hx = *(int *)&x; 4925c28e83SPiotr Jasiukajtis ix = hx & 0x7fffffff; 5025c28e83SPiotr Jasiukajtis iy = *(int *)&y & 0x7fffffff; 5125c28e83SPiotr Jasiukajtis 5225c28e83SPiotr Jasiukajtis /* purge off exception values */ 5325c28e83SPiotr Jasiukajtis if (ix >= ii || iy > ii || iy == 0) { 5425c28e83SPiotr Jasiukajtis w = x * y; 5525c28e83SPiotr Jasiukajtis w = w / w; 5625c28e83SPiotr Jasiukajtis } else if (ix <= iy) { 5725c28e83SPiotr Jasiukajtis if (ix < iy) 5825c28e83SPiotr Jasiukajtis w = x; /* return x if |x|<|y| */ 5925c28e83SPiotr Jasiukajtis else 6025c28e83SPiotr Jasiukajtis w = zero * x; /* return sign(x)*0.0 */ 6125c28e83SPiotr Jasiukajtis } else { 6225c28e83SPiotr Jasiukajtis /* INDENT OFF */ 6325c28e83SPiotr Jasiukajtis /* 6425c28e83SPiotr Jasiukajtis * scale x,y to "normal" with 6525c28e83SPiotr Jasiukajtis * ny = exponent of y 6625c28e83SPiotr Jasiukajtis * nd = exponent of x minus exponent of y 6725c28e83SPiotr Jasiukajtis */ 6825c28e83SPiotr Jasiukajtis /* INDENT ON */ 6925c28e83SPiotr Jasiukajtis ny = iy >> 23; 7025c28e83SPiotr Jasiukajtis k = ix >> 23; 7125c28e83SPiotr Jasiukajtis 7225c28e83SPiotr Jasiukajtis /* special case for subnormal y or x */ 7325c28e83SPiotr Jasiukajtis if (ny == 0) { 7425c28e83SPiotr Jasiukajtis ny = 1; 7525c28e83SPiotr Jasiukajtis while (iy < iu) { 7625c28e83SPiotr Jasiukajtis ny -= 1; 7725c28e83SPiotr Jasiukajtis iy += iy; 7825c28e83SPiotr Jasiukajtis } 7925c28e83SPiotr Jasiukajtis nd = k - ny; 8025c28e83SPiotr Jasiukajtis if (k == 0) { 8125c28e83SPiotr Jasiukajtis nd += 1; 8225c28e83SPiotr Jasiukajtis while (ix < iu) { 8325c28e83SPiotr Jasiukajtis nd -= 1; 8425c28e83SPiotr Jasiukajtis ix += ix; 8525c28e83SPiotr Jasiukajtis } 8625c28e83SPiotr Jasiukajtis } else { 8725c28e83SPiotr Jasiukajtis ix = iu | (ix & im); 8825c28e83SPiotr Jasiukajtis } 8925c28e83SPiotr Jasiukajtis } else { 9025c28e83SPiotr Jasiukajtis nd = k - ny; 9125c28e83SPiotr Jasiukajtis ix = iu | (ix & im); 9225c28e83SPiotr Jasiukajtis iy = iu | (iy & im); 9325c28e83SPiotr Jasiukajtis } 9425c28e83SPiotr Jasiukajtis 9525c28e83SPiotr Jasiukajtis /* fix point fmod for normalized ix and iy */ 9625c28e83SPiotr Jasiukajtis /* INDENT OFF */ 9725c28e83SPiotr Jasiukajtis /* 9825c28e83SPiotr Jasiukajtis * while (nd--) { 9925c28e83SPiotr Jasiukajtis * iz = ix - iy; 10025c28e83SPiotr Jasiukajtis * if (iz < 0) 10125c28e83SPiotr Jasiukajtis * ix = ix + ix; 10225c28e83SPiotr Jasiukajtis * else if (iz == 0) { 10325c28e83SPiotr Jasiukajtis * *(int *) &w = is & hx; 10425c28e83SPiotr Jasiukajtis * return w; 10525c28e83SPiotr Jasiukajtis * } 10625c28e83SPiotr Jasiukajtis * else 10725c28e83SPiotr Jasiukajtis * ix = iz + iz; 10825c28e83SPiotr Jasiukajtis * } 10925c28e83SPiotr Jasiukajtis */ 11025c28e83SPiotr Jasiukajtis /* INDENT ON */ 11125c28e83SPiotr Jasiukajtis /* unroll the above loop 4 times to gain performance */ 11225c28e83SPiotr Jasiukajtis k = nd >> 2; 11325c28e83SPiotr Jasiukajtis nd -= k << 2; 11425c28e83SPiotr Jasiukajtis while (k--) { 11525c28e83SPiotr Jasiukajtis iz = ix - iy; 11625c28e83SPiotr Jasiukajtis if (iz >= 0) 11725c28e83SPiotr Jasiukajtis ix = iz + iz; 11825c28e83SPiotr Jasiukajtis else 11925c28e83SPiotr Jasiukajtis ix += ix; 12025c28e83SPiotr Jasiukajtis iz = ix - iy; 12125c28e83SPiotr Jasiukajtis if (iz >= 0) 12225c28e83SPiotr Jasiukajtis ix = iz + iz; 12325c28e83SPiotr Jasiukajtis else 12425c28e83SPiotr Jasiukajtis ix += ix; 12525c28e83SPiotr Jasiukajtis iz = ix - iy; 12625c28e83SPiotr Jasiukajtis if (iz >= 0) 12725c28e83SPiotr Jasiukajtis ix = iz + iz; 12825c28e83SPiotr Jasiukajtis else 12925c28e83SPiotr Jasiukajtis ix += ix; 13025c28e83SPiotr Jasiukajtis iz = ix - iy; 13125c28e83SPiotr Jasiukajtis if (iz >= 0) 13225c28e83SPiotr Jasiukajtis ix = iz + iz; 13325c28e83SPiotr Jasiukajtis else 13425c28e83SPiotr Jasiukajtis ix += ix; 13525c28e83SPiotr Jasiukajtis if (iz == 0) { 13625c28e83SPiotr Jasiukajtis *(int *)&w = is & hx; 13725c28e83SPiotr Jasiukajtis return (w); 13825c28e83SPiotr Jasiukajtis } 13925c28e83SPiotr Jasiukajtis } 14025c28e83SPiotr Jasiukajtis while (nd--) { 14125c28e83SPiotr Jasiukajtis iz = ix - iy; 14225c28e83SPiotr Jasiukajtis if (iz >= 0) 14325c28e83SPiotr Jasiukajtis ix = iz + iz; 14425c28e83SPiotr Jasiukajtis else 14525c28e83SPiotr Jasiukajtis ix += ix; 14625c28e83SPiotr Jasiukajtis } 14725c28e83SPiotr Jasiukajtis /* end of unrolling */ 14825c28e83SPiotr Jasiukajtis 14925c28e83SPiotr Jasiukajtis iz = ix - iy; 15025c28e83SPiotr Jasiukajtis if (iz >= 0) 15125c28e83SPiotr Jasiukajtis ix = iz; 15225c28e83SPiotr Jasiukajtis 15325c28e83SPiotr Jasiukajtis /* convert back to floating value and restore the sign */ 15425c28e83SPiotr Jasiukajtis if (ix == 0) { 15525c28e83SPiotr Jasiukajtis *(int *)&w = is & hx; 15625c28e83SPiotr Jasiukajtis return (w); 15725c28e83SPiotr Jasiukajtis } 15825c28e83SPiotr Jasiukajtis while (ix < iu) { 15925c28e83SPiotr Jasiukajtis ix += ix; 16025c28e83SPiotr Jasiukajtis ny -= 1; 16125c28e83SPiotr Jasiukajtis } 16225c28e83SPiotr Jasiukajtis while (ix > (iu + iu)) { 16325c28e83SPiotr Jasiukajtis ny += 1; 16425c28e83SPiotr Jasiukajtis ix >>= 1; 16525c28e83SPiotr Jasiukajtis } 16625c28e83SPiotr Jasiukajtis if (ny > 0) { 16725c28e83SPiotr Jasiukajtis *(int *)&w = (is & hx) | (ix & im) | (ny << 23); 16825c28e83SPiotr Jasiukajtis } else { 16925c28e83SPiotr Jasiukajtis /* subnormal output */ 17025c28e83SPiotr Jasiukajtis k = -ny + 1; 17125c28e83SPiotr Jasiukajtis ix >>= k; 17225c28e83SPiotr Jasiukajtis *(int *)&w = (is & hx) | ix; 17325c28e83SPiotr Jasiukajtis } 17425c28e83SPiotr Jasiukajtis } 17525c28e83SPiotr Jasiukajtis return (w); 17625c28e83SPiotr Jasiukajtis } 177