GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libm/src/ld80/s_remquol.c Lines: 0 70 0.0 %
Date: 2017-11-13 Branches: 0 54 0.0 %

Line Branch Exec Source
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
41
static const long double Zero[] = {0.0L, -0.0L};
42
43
/*
44
 * Return the IEEE remainder and set *quo to the last n bits of the
45
 * quotient, rounded to the nearest integer.  We choose n=31 because
46
 * we wind up computing all the integer bits of the quotient anyway as
47
 * a side-effect of computing the remainder by the shift and subtract
48
 * method.  In practice, this is far more bits than are needed to use
49
 * remquo in reduction algorithms.
50
 *
51
 * Assumptions:
52
 * - The low part of the mantissa fits in a manl_t exactly.
53
 * - The high part of the mantissa fits in an int64_t with enough room
54
 *   for an explicit integer bit in front of the fractional bits.
55
 */
56
long double
57
remquol(long double x, long double y, int *quo)
58
{
59
	int64_t hx,hz;	/* We need a carry bit even if LDBL_MANH_SIZE is 32. */
60
	uint32_t hy;
61
	uint32_t lx,ly,lz;
62
	uint32_t esx, esy;
63
	int ix,iy,n,q,sx,sxy;
64
65
	GET_LDOUBLE_WORDS(esx,hx,lx,x);
66
	GET_LDOUBLE_WORDS(esy,hy,ly,y);
67
	sx = esx & 0x8000;
68
	sxy = sx ^ (esy & 0x8000);
69
	esx &= 0x7fff;				/* |x| */
70
	esy &= 0x7fff;				/* |y| */
71
	SET_LDOUBLE_EXP(x,esx);
72
	SET_LDOUBLE_EXP(y,esy);
73
74
    /* purge off exception values */
75
	if((esy|hy|ly)==0 ||			/* y=0 */
76
	   (esx == BIAS + LDBL_MAX_EXP) ||	/* or x not finite */
77
	   (esy == BIAS + LDBL_MAX_EXP &&
78
	    ((hy&~LDBL_NBIT)|ly)!=0))		/* or y is NaN */
79
	    return (x*y)/(x*y);
80
	if(esx<=esy) {
81
	    if((esx<esy) ||
82
	       (hx<=hy &&
83
		(hx<hy ||
84
		 lx<ly))) {
85
		q = 0;
86
		goto fixup;			/* |x|<|y| return x or x-y */
87
	    }
88
	    if(hx==hy && lx==ly) {
89
		*quo = 1;
90
		return Zero[sx!=0];		/* |x|=|y| return x*0*/
91
	    }
92
	}
93
94
    /* determine ix = ilogb(x) */
95
	if(esx == 0) {				/* subnormal x */
96
	    x *= 0x1.0p512;
97
	    GET_LDOUBLE_WORDS(esx,hx,lx,x);
98
	    ix = esx - (BIAS + 512);
99
	} else {
100
	    ix = esx - BIAS;
101
	}
102
103
    /* determine iy = ilogb(y) */
104
	if(esy == 0) {				/* subnormal y */
105
	    y *= 0x1.0p512;
106
	    GET_LDOUBLE_WORDS(esy,hy,ly,y);
107
	    iy = esy - (BIAS + 512);
108
	} else {
109
	    iy = esy - BIAS;
110
	}
111
112
    /* set up {hx,lx}, {hy,ly} and align y to x */
113
	hx = SET_NBIT(hx);
114
	lx = SET_NBIT(lx);
115
116
    /* fix point fmod */
117
	n = ix - iy;
118
	q = 0;
119
120
	while(n--) {
121
	    hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
122
	    if(hz<0){hx = hx+hx+(lx>>MANL_SHIFT); lx = lx+lx;}
123
	    else {hx = hz+hz+(lz>>MANL_SHIFT); lx = lz+lz; q++;}
124
	    q <<= 1;
125
	}
126
	hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
127
	if(hz>=0) {hx=hz;lx=lz;q++;}
128
129
    /* convert back to floating value and restore the sign */
130
	if((hx|lx)==0) {			/* return sign(x)*0 */
131
	    *quo = (sxy ? -q : q);
132
	    return Zero[sx!=0];
133
	}
134
	while(hx<(1ULL<<HFRAC_BITS)) {	/* normalize x */
135
	    hx = hx+hx+(lx>>MANL_SHIFT); lx = lx+lx;
136
	    iy -= 1;
137
	}
138
	if (iy < LDBL_MIN_EXP) {
139
	    esx = (iy + BIAS + 512) & 0x7fff;
140
	    SET_LDOUBLE_WORDS(x,esx,hx,lx);
141
	    x *= 0x1p-512;
142
	    GET_LDOUBLE_WORDS(esx,hx,lx,x);
143
	} else {
144
	    esx = (iy + BIAS) & 0x7fff;
145
	}
146
	SET_LDOUBLE_WORDS(x,esx,hx,lx);
147
fixup:
148
	y = fabsl(y);
149
	if (y < LDBL_MIN * 2) {
150
	    if (x+x>y || (x+x==y && (q & 1))) {
151
		q++;
152
		x-=y;
153
	    }
154
	} else if (x>0.5*y || (x==0.5*y && (q & 1))) {
155
	    q++;
156
	    x-=y;
157
	}
158
159
	GET_LDOUBLE_EXP(esx,x);
160
	esx ^= sx;
161
	SET_LDOUBLE_EXP(x,esx);
162
163
	q &= 0x7fffffff;
164
	*quo = (sxy ? -q : q);
165
	return x;
166
}
167
DEF_STD(remquol);