xref: /relibc/openlibm/src/s_rintl.c (revision 2729465a897156b9c745dc2fc00a0fdf56915dc6)
1 /*-
2  * Copyright (c) 2008 David Schultz <das@FreeBSD.ORG>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include "cdefs-compat.h"
28 //__FBSDID("$FreeBSD: src/lib/msun/src/s_rintl.c,v 1.5 2008/02/22 11:59:05 bde Exp $");
29 
30 #include <float.h>
31 #include <openlibm_fenv.h>
32 #include <openlibm_math.h>
33 
34 #include "fpmath.h"
35 
36 //VBS
37 #include "math_private.h"
38 
39 
40 #if LDBL_MAX_EXP != 0x4000
41 /* We also require the usual bias, min exp and expsign packing. */
42 #error "Unsupported long double format"
43 #endif
44 
45 #define	BIAS	(LDBL_MAX_EXP - 1)
46 
47 static const float
48 shift[2] = {
49 #if LDBL_MANT_DIG == 64
50 	0x1.0p63, -0x1.0p63
51 #elif LDBL_MANT_DIG == 113
52 	0x1.0p112, -0x1.0p112
53 #else
54 #error "Unsupported long double format"
55 #endif
56 };
57 static const float zero[2] = { 0.0, -0.0 };
58 
59 DLLEXPORT long double
60 rintl(long double x)
61 {
62 	union IEEEl2bits u;
63 	u_int32_t expsign;
64 	int ex, sign;
65 
66 	u.e = x;
67 	expsign = u.xbits.expsign;
68 	ex = expsign & 0x7fff;
69 
70 	if (ex >= BIAS + LDBL_MANT_DIG - 1) {
71 		if (ex == BIAS + LDBL_MAX_EXP)
72 			return (x + x);	/* Inf, NaN, or unsupported format */
73 		return (x);		/* finite and already an integer */
74 	}
75 	sign = expsign >> 15;
76 
77 	/*
78 	 * The following code assumes that intermediate results are
79 	 * evaluated in long double precision. If they are evaluated in
80 	 * greater precision, double rounding may occur, and if they are
81 	 * evaluated in less precision (as on i386), results will be
82 	 * wildly incorrect.
83 	 */
84 	x += shift[sign];
85 	x -= shift[sign];
86 
87 	/*
88 	 * If the result is +-0, then it must have the same sign as x, but
89 	 * the above calculation doesn't always give this.  Fix up the sign.
90 	 */
91 	if (ex < BIAS && x == 0.0L)
92 		return (zero[sign]);
93 
94 	return (x);
95 }
96 
97 /*
98  * We save and restore the floating-point environment to avoid raising
99  * an inexact exception.  We can get away with using fesetenv()
100  * instead of feclearexcept()/feupdateenv() to restore the environment
101  * because the only exception defined for rint() is overflow, and
102  * rounding can't overflow as long as emax >= p.
103  */
104 #define	DECL(type, fn, rint)	\
105 DLLEXPORT type				\
106 fn(type x)			\
107 {				\
108 	type ret;		\
109 	fenv_t env;		\
110 				\
111 	fegetenv(&env);		\
112 	ret = rint(x);		\
113 	fesetenv(&env);		\
114 	return (ret);		\
115 }
116 DECL(long double, nearbyintl, rintl)
117