fenv.c revision 2d367905a2e1b950f79b408141eea07c222b590b
12d367905a2e1b950f79b408141eea07c222b590bCalin Juravle/*  $OpenBSD: fenv.c,v 1.3 2012/12/05 23:20:02 deraadt Exp $  */
22d367905a2e1b950f79b408141eea07c222b590bCalin Juravle/*  $NetBSD: fenv.c,v 1.1 2010/07/31 21:47:53 joerg Exp $ */
3770a3495607497071693147f162ac75f39423973Elliott Hughes
4ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin/*-
5770a3495607497071693147f162ac75f39423973Elliott Hughes * Copyright (c) 2004-2005 David Schultz <das (at) FreeBSD.ORG>
6ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin * All rights reserved.
7ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin *
8ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin * Redistribution and use in source and binary forms, with or without
9ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin * modification, are permitted provided that the following conditions
10ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin * are met:
11ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin * 1. Redistributions of source code must retain the above copyright
12ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin *    notice, this list of conditions and the following disclaimer.
13ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin * 2. Redistributions in binary form must reproduce the above copyright
14ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin *    notice, this list of conditions and the following disclaimer in the
15ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin *    documentation and/or other materials provided with the distribution.
16ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin *
17ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin * SUCH DAMAGE.
28ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin */
29ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin
30770a3495607497071693147f162ac75f39423973Elliott Hughes#include <fenv.h>
31ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin#include <machine/fpu.h>
32ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin
33770a3495607497071693147f162ac75f39423973Elliott Hughes/*
34770a3495607497071693147f162ac75f39423973Elliott Hughes * The following constant represents the default floating-point environment
35770a3495607497071693147f162ac75f39423973Elliott Hughes * (that is, the one installed at program startup) and has type pointer to
36770a3495607497071693147f162ac75f39423973Elliott Hughes * const-qualified fenv_t.
37770a3495607497071693147f162ac75f39423973Elliott Hughes *
38770a3495607497071693147f162ac75f39423973Elliott Hughes * It can be used as an argument to the functions within the <fenv.h> header
39770a3495607497071693147f162ac75f39423973Elliott Hughes * that manage the floating-point environment, namely fesetenv() and
40770a3495607497071693147f162ac75f39423973Elliott Hughes * feupdateenv().
41770a3495607497071693147f162ac75f39423973Elliott Hughes *
42770a3495607497071693147f162ac75f39423973Elliott Hughes * x87 fpu registers are 16bit wide. The upper bits, 31-16, are marked as
43770a3495607497071693147f162ac75f39423973Elliott Hughes * RESERVED.
44770a3495607497071693147f162ac75f39423973Elliott Hughes */
45770a3495607497071693147f162ac75f39423973Elliott Hughesfenv_t __fe_dfl_env = {
462d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  {
472d367905a2e1b950f79b408141eea07c222b590bCalin Juravle    0xffff0000 | __INITIAL_NPXCW__, /* Control word register */
482d367905a2e1b950f79b408141eea07c222b590bCalin Juravle    0xffff0000,                     /* Status word register */
492d367905a2e1b950f79b408141eea07c222b590bCalin Juravle    0xffffffff,                     /* Tag word register */
502d367905a2e1b950f79b408141eea07c222b590bCalin Juravle    {
512d367905a2e1b950f79b408141eea07c222b590bCalin Juravle      0x00000000,
522d367905a2e1b950f79b408141eea07c222b590bCalin Juravle      0x00000000,
532d367905a2e1b950f79b408141eea07c222b590bCalin Juravle      0x00000000,
542d367905a2e1b950f79b408141eea07c222b590bCalin Juravle      0xffff0000
552d367905a2e1b950f79b408141eea07c222b590bCalin Juravle    }
562d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  },
572d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __INITIAL_MXCSR__                 /* MXCSR register */
58ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin};
59ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin
60ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin
61770a3495607497071693147f162ac75f39423973Elliott Hughes/*
62770a3495607497071693147f162ac75f39423973Elliott Hughes * The feclearexcept() function clears the supported floating-point exceptions
63770a3495607497071693147f162ac75f39423973Elliott Hughes * represented by `excepts'.
64770a3495607497071693147f162ac75f39423973Elliott Hughes */
65ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupinint
66770a3495607497071693147f162ac75f39423973Elliott Hughesfeclearexcept(int excepts)
67ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin{
682d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  fenv_t fenv;
692d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  unsigned int mxcsr;
70770a3495607497071693147f162ac75f39423973Elliott Hughes
712d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  excepts &= FE_ALL_EXCEPT;
72770a3495607497071693147f162ac75f39423973Elliott Hughes
732d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Store the current x87 floating-point environment */
742d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("fnstenv %0" : "=m" (fenv));
75ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin
762d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Clear the requested floating-point exceptions */
772d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  fenv.__x87.__status &= ~excepts;
78ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin
792d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Load the x87 floating-point environent */
802d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("fldenv %0" : : "m" (fenv));
81770a3495607497071693147f162ac75f39423973Elliott Hughes
822d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Same for SSE environment */
832d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr));
842d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  mxcsr &= ~excepts;
852d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("ldmxcsr %0" : : "m" (mxcsr));
86ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin
872d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  return (0);
88ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin}
89ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin
90770a3495607497071693147f162ac75f39423973Elliott Hughes/*
91770a3495607497071693147f162ac75f39423973Elliott Hughes * The fegetexceptflag() function stores an implementation-defined
92770a3495607497071693147f162ac75f39423973Elliott Hughes * representation of the states of the floating-point status flags indicated by
93770a3495607497071693147f162ac75f39423973Elliott Hughes * the argument excepts in the object pointed to by the argument flagp.
94770a3495607497071693147f162ac75f39423973Elliott Hughes */
95770a3495607497071693147f162ac75f39423973Elliott Hughesint
96770a3495607497071693147f162ac75f39423973Elliott Hughesfegetexceptflag(fexcept_t *flagp, int excepts)
97770a3495607497071693147f162ac75f39423973Elliott Hughes{
982d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  unsigned short status;
992d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  unsigned int mxcsr;
100770a3495607497071693147f162ac75f39423973Elliott Hughes
1012d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  excepts &= FE_ALL_EXCEPT;
102770a3495607497071693147f162ac75f39423973Elliott Hughes
1032d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Store the current x87 status register */
1042d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("fnstsw %0" : "=am" (status));
105770a3495607497071693147f162ac75f39423973Elliott Hughes
1062d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Store the MXCSR register */
1072d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr));
108770a3495607497071693147f162ac75f39423973Elliott Hughes
1092d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Store the results in flagp */
1102d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  *flagp = (status | mxcsr) & excepts;
111770a3495607497071693147f162ac75f39423973Elliott Hughes
1122d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  return (0);
113770a3495607497071693147f162ac75f39423973Elliott Hughes}
114770a3495607497071693147f162ac75f39423973Elliott Hughes
115770a3495607497071693147f162ac75f39423973Elliott Hughes/*
116770a3495607497071693147f162ac75f39423973Elliott Hughes * The feraiseexcept() function raises the supported floating-point exceptions
117770a3495607497071693147f162ac75f39423973Elliott Hughes * represented by the argument `excepts'.
118770a3495607497071693147f162ac75f39423973Elliott Hughes *
119770a3495607497071693147f162ac75f39423973Elliott Hughes * The standard explicitly allows us to execute an instruction that has the
120770a3495607497071693147f162ac75f39423973Elliott Hughes * exception as a side effect, but we choose to manipulate the status register
121770a3495607497071693147f162ac75f39423973Elliott Hughes * directly.
122770a3495607497071693147f162ac75f39423973Elliott Hughes *
123770a3495607497071693147f162ac75f39423973Elliott Hughes * The validation of input is being deferred to fesetexceptflag().
124770a3495607497071693147f162ac75f39423973Elliott Hughes */
125ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupinint
126ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupinferaiseexcept(int excepts)
127ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin{
1282d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  excepts &= FE_ALL_EXCEPT;
129770a3495607497071693147f162ac75f39423973Elliott Hughes
1302d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  fesetexceptflag((fexcept_t *)&excepts, excepts);
1312d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("fwait");
132770a3495607497071693147f162ac75f39423973Elliott Hughes
1332d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  return (0);
134770a3495607497071693147f162ac75f39423973Elliott Hughes}
135770a3495607497071693147f162ac75f39423973Elliott Hughes
136770a3495607497071693147f162ac75f39423973Elliott Hughes/*
137770a3495607497071693147f162ac75f39423973Elliott Hughes * This function sets the floating-point status flags indicated by the argument
138770a3495607497071693147f162ac75f39423973Elliott Hughes * `excepts' to the states stored in the object pointed to by `flagp'. It does
139770a3495607497071693147f162ac75f39423973Elliott Hughes * NOT raise any floating-point exceptions, but only sets the state of the flags.
140770a3495607497071693147f162ac75f39423973Elliott Hughes */
141770a3495607497071693147f162ac75f39423973Elliott Hughesint
142770a3495607497071693147f162ac75f39423973Elliott Hughesfesetexceptflag(const fexcept_t *flagp, int excepts)
143770a3495607497071693147f162ac75f39423973Elliott Hughes{
1442d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  fenv_t fenv;
1452d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  unsigned int mxcsr;
146770a3495607497071693147f162ac75f39423973Elliott Hughes
1472d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  excepts &= FE_ALL_EXCEPT;
148770a3495607497071693147f162ac75f39423973Elliott Hughes
1492d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Store the current x87 floating-point environment */
1502d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("fnstenv %0" : "=m" (fenv));
151770a3495607497071693147f162ac75f39423973Elliott Hughes
1522d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Set the requested status flags */
1532d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  fenv.__x87.__status &= ~excepts;
1542d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  fenv.__x87.__status |= *flagp & excepts;
155770a3495607497071693147f162ac75f39423973Elliott Hughes
1562d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Load the x87 floating-point environent */
1572d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("fldenv %0" : : "m" (fenv));
158770a3495607497071693147f162ac75f39423973Elliott Hughes
1592d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Same for SSE environment */
1602d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr));
1612d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  mxcsr &= ~excepts;
1622d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  mxcsr |= *flagp & excepts;
1632d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("ldmxcsr %0" : : "m" (mxcsr));
164ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin
1652d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  return (0);
166ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin}
167ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin
168770a3495607497071693147f162ac75f39423973Elliott Hughes/*
169770a3495607497071693147f162ac75f39423973Elliott Hughes * The fetestexcept() function determines which of a specified subset of the
170770a3495607497071693147f162ac75f39423973Elliott Hughes * floating-point exception flags are currently set. The `excepts' argument
171770a3495607497071693147f162ac75f39423973Elliott Hughes * specifies the floating-point status flags to be queried.
172770a3495607497071693147f162ac75f39423973Elliott Hughes */
173770a3495607497071693147f162ac75f39423973Elliott Hughesint
174770a3495607497071693147f162ac75f39423973Elliott Hughesfetestexcept(int excepts)
175770a3495607497071693147f162ac75f39423973Elliott Hughes{
1762d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  unsigned short status;
1772d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  unsigned int mxcsr;
178770a3495607497071693147f162ac75f39423973Elliott Hughes
1792d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  excepts &= FE_ALL_EXCEPT;
180770a3495607497071693147f162ac75f39423973Elliott Hughes
1812d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Store the current x87 status register */
1822d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("fnstsw %0" : "=am" (status));
183770a3495607497071693147f162ac75f39423973Elliott Hughes
1842d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Store the MXCSR register state */
1852d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr));
186770a3495607497071693147f162ac75f39423973Elliott Hughes
1872d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  return ((status | mxcsr) & excepts);
188770a3495607497071693147f162ac75f39423973Elliott Hughes}
189ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin
190770a3495607497071693147f162ac75f39423973Elliott Hughes/*
191770a3495607497071693147f162ac75f39423973Elliott Hughes * The fegetround() function gets the current rounding direction.
192770a3495607497071693147f162ac75f39423973Elliott Hughes */
193770a3495607497071693147f162ac75f39423973Elliott Hughesint
194770a3495607497071693147f162ac75f39423973Elliott Hughesfegetround(void)
195770a3495607497071693147f162ac75f39423973Elliott Hughes{
1962d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  unsigned short control;
197770a3495607497071693147f162ac75f39423973Elliott Hughes
1982d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /*
1992d367905a2e1b950f79b408141eea07c222b590bCalin Juravle   * We assume that the x87 and the SSE unit agree on the
2002d367905a2e1b950f79b408141eea07c222b590bCalin Juravle   * rounding mode.  Reading the control word on the x87 turns
2012d367905a2e1b950f79b408141eea07c222b590bCalin Juravle   * out to be about 5 times faster than reading it on the SSE
2022d367905a2e1b950f79b408141eea07c222b590bCalin Juravle   * unit on an Opteron 244.
2032d367905a2e1b950f79b408141eea07c222b590bCalin Juravle   */
2042d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("fnstcw %0" : "=m" (control));
205770a3495607497071693147f162ac75f39423973Elliott Hughes
2062d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  return (control & _X87_ROUND_MASK);
207770a3495607497071693147f162ac75f39423973Elliott Hughes}
208770a3495607497071693147f162ac75f39423973Elliott Hughes
209770a3495607497071693147f162ac75f39423973Elliott Hughes/*
210770a3495607497071693147f162ac75f39423973Elliott Hughes * The fesetround() function establishes the rounding direction represented by
211770a3495607497071693147f162ac75f39423973Elliott Hughes * its argument `round'. If the argument is not equal to the value of a rounding
212770a3495607497071693147f162ac75f39423973Elliott Hughes * direction macro, the rounding direction is not changed.
213770a3495607497071693147f162ac75f39423973Elliott Hughes */
214770a3495607497071693147f162ac75f39423973Elliott Hughesint
215770a3495607497071693147f162ac75f39423973Elliott Hughesfesetround(int round)
216770a3495607497071693147f162ac75f39423973Elliott Hughes{
2172d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  unsigned short control;
2182d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  unsigned int mxcsr;
219770a3495607497071693147f162ac75f39423973Elliott Hughes
2202d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Check whether requested rounding direction is supported */
2212d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  if (round & ~_X87_ROUND_MASK)
2222d367905a2e1b950f79b408141eea07c222b590bCalin Juravle    return (-1);
223770a3495607497071693147f162ac75f39423973Elliott Hughes
2242d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Store the current x87 control word register */
2252d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("fnstcw %0" : "=m" (control));
226770a3495607497071693147f162ac75f39423973Elliott Hughes
2272d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Set the rounding direction */
2282d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  control &= ~_X87_ROUND_MASK;
2292d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  control |= round;
230770a3495607497071693147f162ac75f39423973Elliott Hughes
2312d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Load the x87 control word register */
2322d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("fldcw %0" : : "m" (control));
233770a3495607497071693147f162ac75f39423973Elliott Hughes
2342d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Same for the SSE environment */
2352d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr));
2362d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  mxcsr &= ~(_X87_ROUND_MASK << _SSE_ROUND_SHIFT);
2372d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  mxcsr |= round << _SSE_ROUND_SHIFT;
2382d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("ldmxcsr %0" : : "m" (mxcsr));
239770a3495607497071693147f162ac75f39423973Elliott Hughes
2402d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  return (0);
241770a3495607497071693147f162ac75f39423973Elliott Hughes}
242770a3495607497071693147f162ac75f39423973Elliott Hughes
243770a3495607497071693147f162ac75f39423973Elliott Hughes/*
244770a3495607497071693147f162ac75f39423973Elliott Hughes * The fegetenv() function attempts to store the current floating-point
245770a3495607497071693147f162ac75f39423973Elliott Hughes * environment in the object pointed to by envp.
246770a3495607497071693147f162ac75f39423973Elliott Hughes */
247ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupinint
248ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupinfegetenv(fenv_t *envp)
249ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin{
2502d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Store the current x87 floating-point environment */
2512d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("fnstenv %0" : "=m" (*envp));
2522d367905a2e1b950f79b408141eea07c222b590bCalin Juravle
2532d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Store the MXCSR register state */
2542d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("stmxcsr %0" : "=m" (envp->__mxcsr));
2552d367905a2e1b950f79b408141eea07c222b590bCalin Juravle
2562d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /*
2572d367905a2e1b950f79b408141eea07c222b590bCalin Juravle   * When an FNSTENV instruction is executed, all pending exceptions are
2582d367905a2e1b950f79b408141eea07c222b590bCalin Juravle   * essentially lost (either the x87 FPU status register is cleared or
2592d367905a2e1b950f79b408141eea07c222b590bCalin Juravle   * all exceptions are masked).
2602d367905a2e1b950f79b408141eea07c222b590bCalin Juravle   *
2612d367905a2e1b950f79b408141eea07c222b590bCalin Juravle   * 8.6 X87 FPU EXCEPTION SYNCHRONIZATION -
2622d367905a2e1b950f79b408141eea07c222b590bCalin Juravle   * Intel(R) 64 and IA-32 Architectures Softare Developer's Manual - Vol1
2632d367905a2e1b950f79b408141eea07c222b590bCalin Juravle   */
2642d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("fldcw %0" : : "m" (envp->__x87.__control));
2652d367905a2e1b950f79b408141eea07c222b590bCalin Juravle
2662d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  return (0);
267ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin}
268ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin
269770a3495607497071693147f162ac75f39423973Elliott Hughes/*
270770a3495607497071693147f162ac75f39423973Elliott Hughes * The feholdexcept() function saves the current floating-point environment
271770a3495607497071693147f162ac75f39423973Elliott Hughes * in the object pointed to by envp, clears the floating-point status flags, and
272770a3495607497071693147f162ac75f39423973Elliott Hughes * then installs a non-stop (continue on floating-point exceptions) mode, if
273770a3495607497071693147f162ac75f39423973Elliott Hughes * available, for all floating-point exceptions.
274770a3495607497071693147f162ac75f39423973Elliott Hughes */
275ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupinint
276ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupinfeholdexcept(fenv_t *envp)
277ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin{
2782d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  unsigned int mxcsr;
279770a3495607497071693147f162ac75f39423973Elliott Hughes
2802d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Store the current x87 floating-point environment */
2812d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("fnstenv %0" : "=m" (*envp));
282770a3495607497071693147f162ac75f39423973Elliott Hughes
2832d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Clear all exception flags in FPU */
2842d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("fnclex");
285ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin
2862d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Store the MXCSR register state */
2872d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("stmxcsr %0" : "=m" (envp->__mxcsr));
288770a3495607497071693147f162ac75f39423973Elliott Hughes
2892d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Clear exception flags in MXCSR */
2902d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  mxcsr = envp->__mxcsr;
2912d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  mxcsr &= ~FE_ALL_EXCEPT;
292770a3495607497071693147f162ac75f39423973Elliott Hughes
2932d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Mask all exceptions */
2942d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  mxcsr |= FE_ALL_EXCEPT << _SSE_MASK_SHIFT;
295770a3495607497071693147f162ac75f39423973Elliott Hughes
2962d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Store the MXCSR register */
2972d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("ldmxcsr %0" : : "m" (mxcsr));
298770a3495607497071693147f162ac75f39423973Elliott Hughes
2992d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  return (0);
300ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin}
301ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin
302770a3495607497071693147f162ac75f39423973Elliott Hughes/*
303770a3495607497071693147f162ac75f39423973Elliott Hughes * The fesetenv() function attempts to establish the floating-point environment
304770a3495607497071693147f162ac75f39423973Elliott Hughes * represented by the object pointed to by envp. The argument `envp' points
305770a3495607497071693147f162ac75f39423973Elliott Hughes * to an object set by a call to fegetenv() or feholdexcept(), or equal a
306770a3495607497071693147f162ac75f39423973Elliott Hughes * floating-point environment macro. The fesetenv() function does not raise
307770a3495607497071693147f162ac75f39423973Elliott Hughes * floating-point exceptions, but only installs the state of the floating-point
308770a3495607497071693147f162ac75f39423973Elliott Hughes * status flags represented through its argument.
309770a3495607497071693147f162ac75f39423973Elliott Hughes */
310770a3495607497071693147f162ac75f39423973Elliott Hughesint
311770a3495607497071693147f162ac75f39423973Elliott Hughesfesetenv(const fenv_t *envp)
312770a3495607497071693147f162ac75f39423973Elliott Hughes{
3132d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Load the x87 floating-point environent */
3142d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("fldenv %0" : : "m" (*envp));
315770a3495607497071693147f162ac75f39423973Elliott Hughes
3162d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Store the MXCSR register */
3172d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("ldmxcsr %0" : : "m" (envp->__mxcsr));
318ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin
3192d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  return (0);
320770a3495607497071693147f162ac75f39423973Elliott Hughes}
321770a3495607497071693147f162ac75f39423973Elliott Hughes
322770a3495607497071693147f162ac75f39423973Elliott Hughes/*
323770a3495607497071693147f162ac75f39423973Elliott Hughes * The feupdateenv() function saves the currently raised floating-point
324770a3495607497071693147f162ac75f39423973Elliott Hughes * exceptions in its automatic storage, installs the floating-point environment
325770a3495607497071693147f162ac75f39423973Elliott Hughes * represented by the object pointed to by `envp', and then raises the saved
326770a3495607497071693147f162ac75f39423973Elliott Hughes * floating-point exceptions. The argument `envp' shall point to an object set
327770a3495607497071693147f162ac75f39423973Elliott Hughes * by a call to feholdexcept() or fegetenv(), or equal a floating-point
328770a3495607497071693147f162ac75f39423973Elliott Hughes * environment macro.
329770a3495607497071693147f162ac75f39423973Elliott Hughes */
330ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupinint
331ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupinfeupdateenv(const fenv_t *envp)
332ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin{
3332d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  unsigned short status;
3342d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  unsigned int mxcsr;
335770a3495607497071693147f162ac75f39423973Elliott Hughes
3362d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Store the x87 status register */
3372d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("fnstsw %0" : "=am" (status));
338ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin
3392d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Store the MXCSR register */
3402d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr));
341770a3495607497071693147f162ac75f39423973Elliott Hughes
3422d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Install new floating-point environment */
3432d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  fesetenv(envp);
344770a3495607497071693147f162ac75f39423973Elliott Hughes
3452d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Raise any previously accumulated exceptions */
3462d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  feraiseexcept(status | mxcsr);
347770a3495607497071693147f162ac75f39423973Elliott Hughes
3482d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  return (0);
349ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin}
350ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin
351770a3495607497071693147f162ac75f39423973Elliott Hughes/*
352770a3495607497071693147f162ac75f39423973Elliott Hughes * The following functions are extentions to the standard
353770a3495607497071693147f162ac75f39423973Elliott Hughes */
354ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupinint
355770a3495607497071693147f162ac75f39423973Elliott Hughesfeenableexcept(int mask)
356ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin{
3572d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  unsigned int mxcsr, omask;
3582d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  unsigned short control;
359ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin
3602d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  mask &= FE_ALL_EXCEPT;
361770a3495607497071693147f162ac75f39423973Elliott Hughes
3622d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("fnstcw %0" : "=m" (control));
3632d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr));
364770a3495607497071693147f162ac75f39423973Elliott Hughes
3652d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  omask = ~(control | (mxcsr >> _SSE_MASK_SHIFT)) & FE_ALL_EXCEPT;
3662d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  control &= ~mask;
3672d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("fldcw %0" : : "m" (control));
368770a3495607497071693147f162ac75f39423973Elliott Hughes
3692d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  mxcsr &= ~(mask << _SSE_MASK_SHIFT);
3702d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("ldmxcsr %0" : : "m" (mxcsr));
371770a3495607497071693147f162ac75f39423973Elliott Hughes
3722d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  return (omask);
373ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin}
374ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin
375ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupinint
376770a3495607497071693147f162ac75f39423973Elliott Hughesfedisableexcept(int mask)
377ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin{
3782d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  unsigned int mxcsr, omask;
3792d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  unsigned short control;
380ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin
3812d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  mask &= FE_ALL_EXCEPT;
382770a3495607497071693147f162ac75f39423973Elliott Hughes
3832d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("fnstcw %0" : "=m" (control));
3842d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr));
385770a3495607497071693147f162ac75f39423973Elliott Hughes
3862d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  omask = ~(control | (mxcsr >> _SSE_MASK_SHIFT)) & FE_ALL_EXCEPT;
3872d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  control |= mask;
3882d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("fldcw %0" : : "m" (control));
389770a3495607497071693147f162ac75f39423973Elliott Hughes
3902d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  mxcsr |= mask << _SSE_MASK_SHIFT;
3912d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("ldmxcsr %0" : : "m" (mxcsr));
392770a3495607497071693147f162ac75f39423973Elliott Hughes
3932d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  return (omask);
394ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin}
395ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin
396770a3495607497071693147f162ac75f39423973Elliott Hughesint
397770a3495607497071693147f162ac75f39423973Elliott Hughesfegetexcept(void)
398770a3495607497071693147f162ac75f39423973Elliott Hughes{
3992d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  unsigned short control;
400770a3495607497071693147f162ac75f39423973Elliott Hughes
4012d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /*
4022d367905a2e1b950f79b408141eea07c222b590bCalin Juravle   * We assume that the masks for the x87 and the SSE unit are
4032d367905a2e1b950f79b408141eea07c222b590bCalin Juravle   * the same.
4042d367905a2e1b950f79b408141eea07c222b590bCalin Juravle   */
4052d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("fnstcw %0" : "=m" (control));
406770a3495607497071693147f162ac75f39423973Elliott Hughes
4072d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  return (~control & FE_ALL_EXCEPT);
408770a3495607497071693147f162ac75f39423973Elliott Hughes}
409