xref: /freebsd/share/man/man3/fpgetround.3 (revision 1c05a6ea6b849ff95e539c31adea887c644a6a01)
1.\" Copyright (c) 1993 Andrew Moore, Talke Studio
2.\" All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. Neither the name of the University nor the names of its contributors
13.\"    may be used to endorse or promote products derived from this software
14.\"    without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.\"     @(#)fpgetround.3	1.0 (Berkeley) 9/23/93
29.\" $FreeBSD$
30.\"
31.Dd December 3, 2010
32.Dt FPGETROUND 3
33.Os
34.Sh NAME
35.Nm fpgetround ,
36.Nm fpsetround ,
37.Nm fpsetprec ,
38.Nm fpgetprec ,
39.Nm fpgetmask ,
40.Nm fpsetmask ,
41.Nm fpgetsticky ,
42.Nm fpresetsticky
43.Nd IEEE floating point interface
44.Sh SYNOPSIS
45.In ieeefp.h
46.Bd -literal
47typedef enum {
48	FP_RN,		/* round to nearest */
49	FP_RM,		/* round down to minus infinity */
50	FP_RP,		/* round up to plus infinity */
51	FP_RZ		/* truncate */
52} fp_rnd_t;
53.Ed
54.Ft fp_rnd_t
55.Fn fpgetround void
56.Ft fp_rnd_t
57.Fn fpsetround "fp_rnd_t direction"
58.Bd -literal
59typedef enum {
60	FP_PS,		/* 24 bit (single-precision) */
61	FP_PRS,		/* reserved */
62	FP_PD,		/* 53 bit (double-precision) */
63	FP_PE		/* 64 bit (extended-precision) */
64} fp_prec_t;
65.Ed
66.Ft fp_prec_t
67.Fn fpgetprec void
68.Ft fp_prec_t
69.Fn fpsetprec "fp_prec_t precision"
70.Bd -literal
71#define fp_except_t	int
72#define FP_X_INV	0x01	/* invalid operation */
73#define FP_X_DNML	0x02	/* denormal */
74#define FP_X_DZ		0x04	/* zero divide */
75#define FP_X_OFL	0x08	/* overflow */
76#define FP_X_UFL	0x10	/* underflow */
77#define FP_X_IMP	0x20	/* (im)precision */
78#define FP_X_STK	0x40	/* stack fault */
79.Ed
80.Ft fp_except_t
81.Fn fpgetmask void
82.Ft fp_except_t
83.Fn fpsetmask "fp_except_t mask"
84.Ft fp_except_t
85.Fn fpgetsticky void
86.Ft fp_except_t
87.Fn fpresetsticky "fp_except_t sticky"
88.Sh DESCRIPTION
89The routines described herein are deprecated.
90New code should use the functionality provided by
91.Xr fenv 3 .
92.Pp
93When a floating point exception is detected, the exception sticky flag is
94set and the exception mask is tested.
95If the mask is set, then a trap
96occurs.
97These routines allow both setting the floating point exception
98masks, and resetting the exception sticky flags after an exception is
99detected.
100In addition, they allow setting the floating point rounding mode
101and precision.
102.Pp
103The
104.Fn fpgetround
105function
106returns the current floating point rounding mode.
107.Pp
108The
109.Fn fpsetround
110function
111sets the floating point rounding mode and returns
112the previous mode.
113.Pp
114The
115.Fn fpgetprec
116function
117returns the current floating point precision.
118.Pp
119The
120.Fn fpsetprec
121function
122sets the floating point precision and returns
123the previous precision.
124.Pp
125The
126.Fn fpgetmask
127function
128returns the current floating point exception masks.
129.Pp
130The
131.Fn fpsetmask
132function
133sets the floating point exception masks and returns the
134previous masks.
135.Pp
136The
137.Fn fpgetsticky
138function
139returns the current floating point sticky flags.
140.Pp
141The
142.Fn fpresetsticky
143function
144clears the floating point sticky flags and returns
145the previous flags.
146.Pp
147Sample code which prevents a trap on divide-by-zero:
148.Bd -literal -offset indent
149fpsetmask(~FP_X_DZ);
150a = 1.0;
151b = 0;
152c = a / b;
153fpresetsticky(FP_X_DZ);
154fpsetmask(FP_X_DZ);
155.Ed
156.Sh IMPLEMENTATION NOTES
157The
158.Fn fpgetprec
159and
160.Fn fpsetprec
161functions provide functionality unavailable on many platforms.
162At present, they are implemented only on the i386 and amd64 platforms.
163Changing precision is not a supported feature:
164it may be ineffective when code is compiled to take advantage of SSE,
165and many library functions and compiler optimizations depend upon the
166default precision for correct behavior.
167.Sh SEE ALSO
168.Xr fenv 3 ,
169.Xr isnan 3
170.Sh HISTORY
171These routines are based on SysV/386 routines of the same name.
172.Sh CAVEATS
173After a floating point exception and before a mask is set, the sticky
174flags must be reset.
175If another exception occurs before the sticky
176flags are reset, then a wrong exception type may be signaled.
177