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/types.h>
14#include <machine/ieee.h>
15
16#include <float.h>
17#include <math.h>
18#include <stdint.h>
19
20#include "math_private.h"
21
22#define	BIAS (LDBL_MAX_EXP - 1)
23
24/*
25 * These macros add and remove an explicit integer bit in front of the
26 * fractional mantissa, if the architecture doesn't have such a bit by
27 * default already.
28 */
29#ifdef LDBL_IMPLICIT_NBIT
30#define	LDBL_NBIT	0
31#define	SET_NBIT(hx)	((hx) | (1ULL << LDBL_MANH_SIZE))
32#define	HFRAC_BITS	EXT_FRACHBITS
33#else
34#define	LDBL_NBIT	0x80000000
35#define	SET_NBIT(hx)	(hx)
36#define	HFRAC_BITS	(EXT_FRACHBITS - 1)
37#endif
38
39#define	MANL_SHIFT	(EXT_FRACLBITS - 1)
40
41static const long double one = 1.0, Zero[] = {0.0, -0.0,};
42
43/*
44 * fmodl(x,y)
45 * Return x mod y in exact arithmetic
46 * Method: shift and subtract
47 *
48 * Assumptions:
49 * - The low part of the mantissa fits in a manl_t exactly.
50 * - The high part of the mantissa fits in an int64_t with enough room
51 *   for an explicit integer bit in front of the fractional bits.
52 */
53long double
54fmodl(long double x, long double y)
55{
56	union {
57		long double e;
58		struct ieee_ext bits;
59	} ux, uy;
60	int64_t hx,hz;	/* We need a carry bit even if LDBL_MANH_SIZE is 32. */
61	uint32_t hy;
62	uint32_t lx,ly,lz;
63	int ix,iy,n,sx;
64
65	ux.e = x;
66	uy.e = y;
67	sx = ux.bits.ext_sign;
68
69    /* purge off exception values */
70	if((uy.bits.ext_exp|uy.bits.ext_frach|uy.bits.ext_fracl)==0 || /* y=0 */
71	   (ux.bits.ext_exp == BIAS + LDBL_MAX_EXP) ||	 /* or x not finite */
72	   (uy.bits.ext_exp == BIAS + LDBL_MAX_EXP &&
73	    ((uy.bits.ext_frach&~LDBL_NBIT)|uy.bits.ext_fracl)!=0)) /* or y is NaN */
74	    return (x*y)/(x*y);
75	if(ux.bits.ext_exp<=uy.bits.ext_exp) {
76	    if((ux.bits.ext_exp<uy.bits.ext_exp) ||
77	       (ux.bits.ext_frach<=uy.bits.ext_frach &&
78		(ux.bits.ext_frach<uy.bits.ext_frach ||
79		 ux.bits.ext_fracl<uy.bits.ext_fracl))) {
80		return x;		/* |x|<|y| return x or x-y */
81	    }
82	    if(ux.bits.ext_frach==uy.bits.ext_frach &&
83		ux.bits.ext_fracl==uy.bits.ext_fracl) {
84		return Zero[sx];	/* |x|=|y| return x*0*/
85	    }
86	}
87
88    /* determine ix = ilogb(x) */
89	if(ux.bits.ext_exp == 0) {	/* subnormal x */
90	    ux.e *= 0x1.0p512;
91	    ix = ux.bits.ext_exp - (BIAS + 512);
92	} else {
93	    ix = ux.bits.ext_exp - BIAS;
94	}
95
96    /* determine iy = ilogb(y) */
97	if(uy.bits.ext_exp == 0) {	/* subnormal y */
98	    uy.e *= 0x1.0p512;
99	    iy = uy.bits.ext_exp - (BIAS + 512);
100	} else {
101	    iy = uy.bits.ext_exp - BIAS;
102	}
103
104    /* set up {hx,lx}, {hy,ly} and align y to x */
105	hx = SET_NBIT(ux.bits.ext_frach);
106	hy = SET_NBIT(uy.bits.ext_frach);
107	lx = ux.bits.ext_fracl;
108	ly = uy.bits.ext_fracl;
109
110    /* fix point fmod */
111	n = ix - iy;
112
113	while(n--) {
114	    hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
115	    if(hz<0){hx = hx+hx+(lx>>MANL_SHIFT); lx = lx+lx;}
116	    else {
117		if ((hz|lz)==0)		/* return sign(x)*0 */
118		    return Zero[sx];
119		hx = hz+hz+(lz>>MANL_SHIFT); lx = lz+lz;
120	    }
121	}
122	hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
123	if(hz>=0) {hx=hz;lx=lz;}
124
125    /* convert back to floating value and restore the sign */
126	if((hx|lx)==0)			/* return sign(x)*0 */
127	    return Zero[sx];
128	while(hx<(1ULL<<HFRAC_BITS)) {	/* normalize x */
129	    hx = hx+hx+(lx>>MANL_SHIFT); lx = lx+lx;
130	    iy -= 1;
131	}
132	ux.bits.ext_frach = hx; /* The mantissa is truncated here if needed. */
133	ux.bits.ext_fracl = lx;
134	if (iy < LDBL_MIN_EXP) {
135	    ux.bits.ext_exp = iy + (BIAS + 512);
136	    ux.e *= 0x1p-512;
137	} else {
138	    ux.bits.ext_exp = iy + BIAS;
139	}
140	x = ux.e * one;		/* create necessary signal */
141	return x;		/* exact output */
142}
143