Home | History | Annotate | Line # | Download | only in src
      1 /* @(#)e_fmod.c 1.3 95/01/18 */
      2 /*-
      3  * ====================================================
      4  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
      5  *
      6  * Developed at SunSoft, a Sun Microsystems, Inc. business.
      7  * Permission to use, copy, modify, and distribute this
      8  * software is freely granted, provided that this notice
      9  * is preserved.
     10  * ====================================================
     11  */
     12 
     13 #include <sys/cdefs.h>
     14 __RCSID("$NetBSD: e_fmodl.c,v 1.3 2024/07/17 12:00:48 riastradh Exp $");
     15 #if 0
     16 __FBSDID("$FreeBSD: head/lib/msun/src/e_fmodl.c 181063 2008-07-31 20:09:47Z das $");
     17 #endif
     18 
     19 #include "namespace.h"
     20 
     21 #include <float.h>
     22 #include <stdint.h>
     23 
     24 #include "math.h"
     25 #include "math_private.h"
     26 #include <machine/ieee.h>
     27 
     28 #ifdef __HAVE_LONG_DOUBLE
     29 
     30 #define	BIAS (LDBL_MAX_EXP - 1)
     31 
     32 #if EXT_FRACLBITS > 32
     33 typedef	uint64_t manl_t;
     34 #else
     35 typedef	uint32_t manl_t;
     36 #endif
     37 
     38 #if EXT_FRACHBITS > 32
     39 typedef	uint64_t manh_t;
     40 #else
     41 typedef	uint32_t manh_t;
     42 #endif
     43 
     44 /*
     45  * These macros add and remove an explicit integer bit in front of the
     46  * fractional significand, if the architecture doesn't have such a bit
     47  * by default already.
     48  */
     49 #ifdef LDBL_IMPLICIT_NBIT
     50 #define	SET_NBIT(hx)	((hx) | (1ULL << EXT_FRACHBITS))
     51 #define	HFRAC_BITS	EXT_FRACHBITS
     52 #define	LDBL_NBIT	0
     53 #else
     54 #define	SET_NBIT(hx)	(hx)
     55 #define	HFRAC_BITS	(EXT_FRACHBITS - 1)
     56 #endif
     57 
     58 #define	MANL_SHIFT	(EXT_FRACLBITS - 1)
     59 
     60 static const long double one = 1.0, Zero[] = {0.0, -0.0,};
     61 
     62 /*
     63  * fmodl(x,y)
     64  * Return x mod y in exact arithmetic
     65  * Method: shift and subtract
     66  *
     67  * Assumptions:
     68  * - The low part of the significand fits in a manl_t exactly.
     69  * - The high part of the significand fits in an int64_t with enough
     70  *   room for an explicit integer bit in front of the fractional bits.
     71  */
     72 long double
     73 __ieee754_fmodl(long double x, long double y)
     74 {
     75 	union ieee_ext_u ux = { .extu_ld = x, };
     76 	union ieee_ext_u uy = { .extu_ld = y, };
     77 	int64_t hx,hz;	/* We need a carry bit even if EXT_FRACHBITS is 32. */
     78 	manh_t hy;
     79 	manl_t lx,ly,lz;
     80 	int ix,iy,n,sx;
     81 
     82 	sx = ux.extu_sign;
     83 
     84     /* purge off exception values */
     85 	if((uy.extu_exp|uy.extu_frach|uy.extu_fracl)==0 || /* y=0 */
     86 	   (ux.extu_exp == BIAS + LDBL_MAX_EXP) ||	 /* or x not finite */
     87 	   (uy.extu_exp == BIAS + LDBL_MAX_EXP &&
     88 	    ((uy.extu_frach&~LDBL_NBIT)|uy.extu_fracl)!=0)) /* or y is NaN */
     89 	    return (x*y)/(x*y);
     90 	if(ux.extu_exp<=uy.extu_exp) {
     91 	    if((ux.extu_exp<uy.extu_exp) ||
     92 	       (ux.extu_frach<=uy.extu_frach &&
     93 		(ux.extu_frach<uy.extu_frach ||
     94 		 ux.extu_fracl<uy.extu_fracl))) {
     95 		return x;		/* |x|<|y| return x or x-y */
     96 	    }
     97 	    if(ux.extu_frach==uy.extu_frach && ux.extu_fracl==uy.extu_fracl) {
     98 		return Zero[sx];	/* |x|=|y| return x*0*/
     99 	    }
    100 	}
    101 
    102     /* determine ix = ilogb(x) */
    103 	if(ux.extu_exp == 0) {	/* subnormal x */
    104 	    ux.extu_ld *= 0x1.0p512;
    105 	    ix = ux.extu_exp - (BIAS + 512);
    106 	} else {
    107 	    ix = ux.extu_exp - BIAS;
    108 	}
    109 
    110     /* determine iy = ilogb(y) */
    111 	if(uy.extu_exp == 0) {	/* subnormal y */
    112 	    uy.extu_ld *= 0x1.0p512;
    113 	    iy = uy.extu_exp - (BIAS + 512);
    114 	} else {
    115 	    iy = uy.extu_exp - BIAS;
    116 	}
    117 
    118     /* set up {hx,lx}, {hy,ly} and align y to x */
    119 	hx = SET_NBIT(ux.extu_frach);
    120 	hy = SET_NBIT(uy.extu_frach);
    121 	lx = ux.extu_fracl;
    122 	ly = uy.extu_fracl;
    123 
    124     /* fix point fmod */
    125 	n = ix - iy;
    126 
    127 	while(n--) {
    128 	    hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
    129 	    if(hz<0){hx = hx+hx+(lx>>MANL_SHIFT); lx = lx+lx;}
    130 	    else {
    131 		if ((hz|lz)==0)		/* return sign(x)*0 */
    132 		    return Zero[sx];
    133 		hx = hz+hz+(lz>>MANL_SHIFT); lx = lz+lz;
    134 	    }
    135 	}
    136 	hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
    137 	if(hz>=0) {hx=hz;lx=lz;}
    138 
    139     /* convert back to floating value and restore the sign */
    140 	if((hx|lx)==0)			/* return sign(x)*0 */
    141 	    return Zero[sx];
    142 	while(hx<(1LL<<HFRAC_BITS)) {	/* normalize x */
    143 	    hx = hx+hx+(lx>>MANL_SHIFT); lx = lx+lx;
    144 	    iy -= 1;
    145 	}
    146 	ux.extu_frach = hx; /* The significand is truncated here if needed. */
    147 	ux.extu_fracl = lx;
    148 	if (iy < LDBL_MIN_EXP) {
    149 	    ux.extu_exp = iy + (BIAS + 512);
    150 	    ux.extu_ld *= 0x1p-512;
    151 	} else {
    152 	    ux.extu_exp = iy + BIAS;
    153 	}
    154 	x = ux.extu_ld * one;	/* create necessary signal */
    155 	return x;		/* exact output */
    156 }
    157 
    158 #endif /* __HAVE_LONG_DOUBLE */
    159