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
3343bf81e54427ac7ae55dc79c057cca62f94c5f77Elliott Hughes#define SSE_MASK_SHIFT 7
3443bf81e54427ac7ae55dc79c057cca62f94c5f77Elliott Hughes
3543bf81e54427ac7ae55dc79c057cca62f94c5f77Elliott Hughes/*
3643bf81e54427ac7ae55dc79c057cca62f94c5f77Elliott Hughes * The following symbol is simply the bitwise-inclusive OR of all floating-point
3743bf81e54427ac7ae55dc79c057cca62f94c5f77Elliott Hughes * rounding direction constants defined above.
3843bf81e54427ac7ae55dc79c057cca62f94c5f77Elliott Hughes */
3943bf81e54427ac7ae55dc79c057cca62f94c5f77Elliott Hughes#define X87_ROUND_MASK  (FE_TONEAREST | FE_DOWNWARD | FE_UPWARD | FE_TOWARDZERO)
4043bf81e54427ac7ae55dc79c057cca62f94c5f77Elliott Hughes#define SSE_ROUND_SHIFT 3
4143bf81e54427ac7ae55dc79c057cca62f94c5f77Elliott Hughes
42770a3495607497071693147f162ac75f39423973Elliott Hughes/*
43770a3495607497071693147f162ac75f39423973Elliott Hughes * The following constant represents the default floating-point environment
44770a3495607497071693147f162ac75f39423973Elliott Hughes * (that is, the one installed at program startup) and has type pointer to
45770a3495607497071693147f162ac75f39423973Elliott Hughes * const-qualified fenv_t.
46770a3495607497071693147f162ac75f39423973Elliott Hughes *
47770a3495607497071693147f162ac75f39423973Elliott Hughes * It can be used as an argument to the functions within the <fenv.h> header
48770a3495607497071693147f162ac75f39423973Elliott Hughes * that manage the floating-point environment, namely fesetenv() and
49770a3495607497071693147f162ac75f39423973Elliott Hughes * feupdateenv().
50770a3495607497071693147f162ac75f39423973Elliott Hughes *
51770a3495607497071693147f162ac75f39423973Elliott Hughes * x87 fpu registers are 16bit wide. The upper bits, 31-16, are marked as
52770a3495607497071693147f162ac75f39423973Elliott Hughes * RESERVED.
53770a3495607497071693147f162ac75f39423973Elliott Hughes */
547ba84d3108a65bc69e121f82b4ff747fb203d049Pavel Chupinconst fenv_t __fe_dfl_env = {
552d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  {
562d367905a2e1b950f79b408141eea07c222b590bCalin Juravle    0xffff0000 | __INITIAL_NPXCW__, /* Control word register */
572d367905a2e1b950f79b408141eea07c222b590bCalin Juravle    0xffff0000,                     /* Status word register */
582d367905a2e1b950f79b408141eea07c222b590bCalin Juravle    0xffffffff,                     /* Tag word register */
592d367905a2e1b950f79b408141eea07c222b590bCalin Juravle    {
602d367905a2e1b950f79b408141eea07c222b590bCalin Juravle      0x00000000,
612d367905a2e1b950f79b408141eea07c222b590bCalin Juravle      0x00000000,
622d367905a2e1b950f79b408141eea07c222b590bCalin Juravle      0x00000000,
632d367905a2e1b950f79b408141eea07c222b590bCalin Juravle      0xffff0000
642d367905a2e1b950f79b408141eea07c222b590bCalin Juravle    }
652d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  },
662d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __INITIAL_MXCSR__                 /* MXCSR register */
67ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin};
68ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin
69ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin
70770a3495607497071693147f162ac75f39423973Elliott Hughes/*
71770a3495607497071693147f162ac75f39423973Elliott Hughes * The feclearexcept() function clears the supported floating-point exceptions
72770a3495607497071693147f162ac75f39423973Elliott Hughes * represented by `excepts'.
73770a3495607497071693147f162ac75f39423973Elliott Hughes */
74ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupinint
75770a3495607497071693147f162ac75f39423973Elliott Hughesfeclearexcept(int excepts)
76ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin{
772d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  fenv_t fenv;
782d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  unsigned int mxcsr;
79770a3495607497071693147f162ac75f39423973Elliott Hughes
802d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  excepts &= FE_ALL_EXCEPT;
81770a3495607497071693147f162ac75f39423973Elliott Hughes
822d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Store the current x87 floating-point environment */
832d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("fnstenv %0" : "=m" (fenv));
84ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin
852d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Clear the requested floating-point exceptions */
862d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  fenv.__x87.__status &= ~excepts;
87ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin
882d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Load the x87 floating-point environent */
892d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("fldenv %0" : : "m" (fenv));
90770a3495607497071693147f162ac75f39423973Elliott Hughes
912d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Same for SSE environment */
922d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr));
932d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  mxcsr &= ~excepts;
942d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("ldmxcsr %0" : : "m" (mxcsr));
95ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin
962d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  return (0);
97ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin}
98ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin
99770a3495607497071693147f162ac75f39423973Elliott Hughes/*
100770a3495607497071693147f162ac75f39423973Elliott Hughes * The fegetexceptflag() function stores an implementation-defined
101770a3495607497071693147f162ac75f39423973Elliott Hughes * representation of the states of the floating-point status flags indicated by
102770a3495607497071693147f162ac75f39423973Elliott Hughes * the argument excepts in the object pointed to by the argument flagp.
103770a3495607497071693147f162ac75f39423973Elliott Hughes */
104770a3495607497071693147f162ac75f39423973Elliott Hughesint
105770a3495607497071693147f162ac75f39423973Elliott Hughesfegetexceptflag(fexcept_t *flagp, int excepts)
106770a3495607497071693147f162ac75f39423973Elliott Hughes{
1072d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  unsigned short status;
1082d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  unsigned int mxcsr;
109770a3495607497071693147f162ac75f39423973Elliott Hughes
1102d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  excepts &= FE_ALL_EXCEPT;
111770a3495607497071693147f162ac75f39423973Elliott Hughes
1122d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Store the current x87 status register */
1132d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("fnstsw %0" : "=am" (status));
114770a3495607497071693147f162ac75f39423973Elliott Hughes
1152d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Store the MXCSR register */
1162d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr));
117770a3495607497071693147f162ac75f39423973Elliott Hughes
1182d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Store the results in flagp */
1192d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  *flagp = (status | mxcsr) & excepts;
120770a3495607497071693147f162ac75f39423973Elliott Hughes
1212d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  return (0);
122770a3495607497071693147f162ac75f39423973Elliott Hughes}
123770a3495607497071693147f162ac75f39423973Elliott Hughes
124770a3495607497071693147f162ac75f39423973Elliott Hughes/*
125770a3495607497071693147f162ac75f39423973Elliott Hughes * The feraiseexcept() function raises the supported floating-point exceptions
126770a3495607497071693147f162ac75f39423973Elliott Hughes * represented by the argument `excepts'.
127770a3495607497071693147f162ac75f39423973Elliott Hughes *
128770a3495607497071693147f162ac75f39423973Elliott Hughes * The standard explicitly allows us to execute an instruction that has the
129770a3495607497071693147f162ac75f39423973Elliott Hughes * exception as a side effect, but we choose to manipulate the status register
130770a3495607497071693147f162ac75f39423973Elliott Hughes * directly.
131770a3495607497071693147f162ac75f39423973Elliott Hughes *
132770a3495607497071693147f162ac75f39423973Elliott Hughes * The validation of input is being deferred to fesetexceptflag().
133770a3495607497071693147f162ac75f39423973Elliott Hughes */
134ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupinint
135ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupinferaiseexcept(int excepts)
136ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin{
1372d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  excepts &= FE_ALL_EXCEPT;
138770a3495607497071693147f162ac75f39423973Elliott Hughes
1392d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  fesetexceptflag((fexcept_t *)&excepts, excepts);
1402d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("fwait");
141770a3495607497071693147f162ac75f39423973Elliott Hughes
1422d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  return (0);
143770a3495607497071693147f162ac75f39423973Elliott Hughes}
144770a3495607497071693147f162ac75f39423973Elliott Hughes
145770a3495607497071693147f162ac75f39423973Elliott Hughes/*
146770a3495607497071693147f162ac75f39423973Elliott Hughes * This function sets the floating-point status flags indicated by the argument
147770a3495607497071693147f162ac75f39423973Elliott Hughes * `excepts' to the states stored in the object pointed to by `flagp'. It does
148770a3495607497071693147f162ac75f39423973Elliott Hughes * NOT raise any floating-point exceptions, but only sets the state of the flags.
149770a3495607497071693147f162ac75f39423973Elliott Hughes */
150770a3495607497071693147f162ac75f39423973Elliott Hughesint
151770a3495607497071693147f162ac75f39423973Elliott Hughesfesetexceptflag(const fexcept_t *flagp, int excepts)
152770a3495607497071693147f162ac75f39423973Elliott Hughes{
1532d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  fenv_t fenv;
1542d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  unsigned int mxcsr;
155770a3495607497071693147f162ac75f39423973Elliott Hughes
1562d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  excepts &= FE_ALL_EXCEPT;
157770a3495607497071693147f162ac75f39423973Elliott Hughes
1582d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Store the current x87 floating-point environment */
1592d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("fnstenv %0" : "=m" (fenv));
160770a3495607497071693147f162ac75f39423973Elliott Hughes
1612d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Set the requested status flags */
1622d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  fenv.__x87.__status &= ~excepts;
1632d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  fenv.__x87.__status |= *flagp & excepts;
164770a3495607497071693147f162ac75f39423973Elliott Hughes
1652d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Load the x87 floating-point environent */
1662d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("fldenv %0" : : "m" (fenv));
167770a3495607497071693147f162ac75f39423973Elliott Hughes
1682d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Same for SSE environment */
1692d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr));
1702d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  mxcsr &= ~excepts;
1712d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  mxcsr |= *flagp & excepts;
1722d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("ldmxcsr %0" : : "m" (mxcsr));
173ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin
1742d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  return (0);
175ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin}
176ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin
177770a3495607497071693147f162ac75f39423973Elliott Hughes/*
178770a3495607497071693147f162ac75f39423973Elliott Hughes * The fetestexcept() function determines which of a specified subset of the
179770a3495607497071693147f162ac75f39423973Elliott Hughes * floating-point exception flags are currently set. The `excepts' argument
180770a3495607497071693147f162ac75f39423973Elliott Hughes * specifies the floating-point status flags to be queried.
181770a3495607497071693147f162ac75f39423973Elliott Hughes */
182770a3495607497071693147f162ac75f39423973Elliott Hughesint
183770a3495607497071693147f162ac75f39423973Elliott Hughesfetestexcept(int excepts)
184770a3495607497071693147f162ac75f39423973Elliott Hughes{
1852d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  unsigned short status;
1862d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  unsigned int mxcsr;
187770a3495607497071693147f162ac75f39423973Elliott Hughes
1882d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  excepts &= FE_ALL_EXCEPT;
189770a3495607497071693147f162ac75f39423973Elliott Hughes
1902d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Store the current x87 status register */
1912d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("fnstsw %0" : "=am" (status));
192770a3495607497071693147f162ac75f39423973Elliott Hughes
1932d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Store the MXCSR register state */
1942d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr));
195770a3495607497071693147f162ac75f39423973Elliott Hughes
1962d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  return ((status | mxcsr) & excepts);
197770a3495607497071693147f162ac75f39423973Elliott Hughes}
198ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin
199770a3495607497071693147f162ac75f39423973Elliott Hughes/*
200770a3495607497071693147f162ac75f39423973Elliott Hughes * The fegetround() function gets the current rounding direction.
201770a3495607497071693147f162ac75f39423973Elliott Hughes */
202770a3495607497071693147f162ac75f39423973Elliott Hughesint
203770a3495607497071693147f162ac75f39423973Elliott Hughesfegetround(void)
204770a3495607497071693147f162ac75f39423973Elliott Hughes{
2052d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  unsigned short control;
206770a3495607497071693147f162ac75f39423973Elliott Hughes
2072d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /*
2082d367905a2e1b950f79b408141eea07c222b590bCalin Juravle   * We assume that the x87 and the SSE unit agree on the
2092d367905a2e1b950f79b408141eea07c222b590bCalin Juravle   * rounding mode.  Reading the control word on the x87 turns
2102d367905a2e1b950f79b408141eea07c222b590bCalin Juravle   * out to be about 5 times faster than reading it on the SSE
2112d367905a2e1b950f79b408141eea07c222b590bCalin Juravle   * unit on an Opteron 244.
2122d367905a2e1b950f79b408141eea07c222b590bCalin Juravle   */
2132d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("fnstcw %0" : "=m" (control));
214770a3495607497071693147f162ac75f39423973Elliott Hughes
21543bf81e54427ac7ae55dc79c057cca62f94c5f77Elliott Hughes  return (control & X87_ROUND_MASK);
216770a3495607497071693147f162ac75f39423973Elliott Hughes}
217770a3495607497071693147f162ac75f39423973Elliott Hughes
218770a3495607497071693147f162ac75f39423973Elliott Hughes/*
219770a3495607497071693147f162ac75f39423973Elliott Hughes * The fesetround() function establishes the rounding direction represented by
220770a3495607497071693147f162ac75f39423973Elliott Hughes * its argument `round'. If the argument is not equal to the value of a rounding
221770a3495607497071693147f162ac75f39423973Elliott Hughes * direction macro, the rounding direction is not changed.
222770a3495607497071693147f162ac75f39423973Elliott Hughes */
223770a3495607497071693147f162ac75f39423973Elliott Hughesint
224770a3495607497071693147f162ac75f39423973Elliott Hughesfesetround(int round)
225770a3495607497071693147f162ac75f39423973Elliott Hughes{
2262d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  unsigned short control;
2272d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  unsigned int mxcsr;
228770a3495607497071693147f162ac75f39423973Elliott Hughes
2292d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Check whether requested rounding direction is supported */
23043bf81e54427ac7ae55dc79c057cca62f94c5f77Elliott Hughes  if (round & ~X87_ROUND_MASK)
2312d367905a2e1b950f79b408141eea07c222b590bCalin Juravle    return (-1);
232770a3495607497071693147f162ac75f39423973Elliott Hughes
2332d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Store the current x87 control word register */
2342d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("fnstcw %0" : "=m" (control));
235770a3495607497071693147f162ac75f39423973Elliott Hughes
2362d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Set the rounding direction */
23743bf81e54427ac7ae55dc79c057cca62f94c5f77Elliott Hughes  control &= ~X87_ROUND_MASK;
2382d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  control |= round;
239770a3495607497071693147f162ac75f39423973Elliott Hughes
2402d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Load the x87 control word register */
2412d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("fldcw %0" : : "m" (control));
242770a3495607497071693147f162ac75f39423973Elliott Hughes
2432d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Same for the SSE environment */
2442d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr));
24543bf81e54427ac7ae55dc79c057cca62f94c5f77Elliott Hughes  mxcsr &= ~(X87_ROUND_MASK << SSE_ROUND_SHIFT);
24643bf81e54427ac7ae55dc79c057cca62f94c5f77Elliott Hughes  mxcsr |= round << SSE_ROUND_SHIFT;
2472d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("ldmxcsr %0" : : "m" (mxcsr));
248770a3495607497071693147f162ac75f39423973Elliott Hughes
2492d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  return (0);
250770a3495607497071693147f162ac75f39423973Elliott Hughes}
251770a3495607497071693147f162ac75f39423973Elliott Hughes
252770a3495607497071693147f162ac75f39423973Elliott Hughes/*
253770a3495607497071693147f162ac75f39423973Elliott Hughes * The fegetenv() function attempts to store the current floating-point
254770a3495607497071693147f162ac75f39423973Elliott Hughes * environment in the object pointed to by envp.
255770a3495607497071693147f162ac75f39423973Elliott Hughes */
256ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupinint
257ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupinfegetenv(fenv_t *envp)
258ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin{
2592d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Store the current x87 floating-point environment */
2602d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("fnstenv %0" : "=m" (*envp));
2612d367905a2e1b950f79b408141eea07c222b590bCalin Juravle
2622d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Store the MXCSR register state */
2632d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("stmxcsr %0" : "=m" (envp->__mxcsr));
2642d367905a2e1b950f79b408141eea07c222b590bCalin Juravle
2652d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /*
2662d367905a2e1b950f79b408141eea07c222b590bCalin Juravle   * When an FNSTENV instruction is executed, all pending exceptions are
2672d367905a2e1b950f79b408141eea07c222b590bCalin Juravle   * essentially lost (either the x87 FPU status register is cleared or
2682d367905a2e1b950f79b408141eea07c222b590bCalin Juravle   * all exceptions are masked).
2692d367905a2e1b950f79b408141eea07c222b590bCalin Juravle   *
2702d367905a2e1b950f79b408141eea07c222b590bCalin Juravle   * 8.6 X87 FPU EXCEPTION SYNCHRONIZATION -
2712d367905a2e1b950f79b408141eea07c222b590bCalin Juravle   * Intel(R) 64 and IA-32 Architectures Softare Developer's Manual - Vol1
2722d367905a2e1b950f79b408141eea07c222b590bCalin Juravle   */
2732d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("fldcw %0" : : "m" (envp->__x87.__control));
2742d367905a2e1b950f79b408141eea07c222b590bCalin Juravle
2752d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  return (0);
276ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin}
277ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin
278770a3495607497071693147f162ac75f39423973Elliott Hughes/*
279770a3495607497071693147f162ac75f39423973Elliott Hughes * The feholdexcept() function saves the current floating-point environment
280770a3495607497071693147f162ac75f39423973Elliott Hughes * in the object pointed to by envp, clears the floating-point status flags, and
281770a3495607497071693147f162ac75f39423973Elliott Hughes * then installs a non-stop (continue on floating-point exceptions) mode, if
282770a3495607497071693147f162ac75f39423973Elliott Hughes * available, for all floating-point exceptions.
283770a3495607497071693147f162ac75f39423973Elliott Hughes */
284ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupinint
285ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupinfeholdexcept(fenv_t *envp)
286ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin{
2872d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  unsigned int mxcsr;
288770a3495607497071693147f162ac75f39423973Elliott Hughes
2892d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Store the current x87 floating-point environment */
2902d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("fnstenv %0" : "=m" (*envp));
291770a3495607497071693147f162ac75f39423973Elliott Hughes
2922d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Clear all exception flags in FPU */
2932d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("fnclex");
294ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin
2952d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Store the MXCSR register state */
2962d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("stmxcsr %0" : "=m" (envp->__mxcsr));
297770a3495607497071693147f162ac75f39423973Elliott Hughes
2982d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Clear exception flags in MXCSR */
2992d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  mxcsr = envp->__mxcsr;
3002d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  mxcsr &= ~FE_ALL_EXCEPT;
301770a3495607497071693147f162ac75f39423973Elliott Hughes
3022d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Mask all exceptions */
30343bf81e54427ac7ae55dc79c057cca62f94c5f77Elliott Hughes  mxcsr |= FE_ALL_EXCEPT << SSE_MASK_SHIFT;
304770a3495607497071693147f162ac75f39423973Elliott Hughes
3052d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Store the MXCSR register */
3062d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("ldmxcsr %0" : : "m" (mxcsr));
307770a3495607497071693147f162ac75f39423973Elliott Hughes
3082d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  return (0);
309ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin}
310ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin
311770a3495607497071693147f162ac75f39423973Elliott Hughes/*
312770a3495607497071693147f162ac75f39423973Elliott Hughes * The fesetenv() function attempts to establish the floating-point environment
313770a3495607497071693147f162ac75f39423973Elliott Hughes * represented by the object pointed to by envp. The argument `envp' points
314770a3495607497071693147f162ac75f39423973Elliott Hughes * to an object set by a call to fegetenv() or feholdexcept(), or equal a
315770a3495607497071693147f162ac75f39423973Elliott Hughes * floating-point environment macro. The fesetenv() function does not raise
316770a3495607497071693147f162ac75f39423973Elliott Hughes * floating-point exceptions, but only installs the state of the floating-point
317770a3495607497071693147f162ac75f39423973Elliott Hughes * status flags represented through its argument.
318770a3495607497071693147f162ac75f39423973Elliott Hughes */
319770a3495607497071693147f162ac75f39423973Elliott Hughesint
320770a3495607497071693147f162ac75f39423973Elliott Hughesfesetenv(const fenv_t *envp)
321770a3495607497071693147f162ac75f39423973Elliott Hughes{
3222d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Load the x87 floating-point environent */
3232d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("fldenv %0" : : "m" (*envp));
324770a3495607497071693147f162ac75f39423973Elliott Hughes
3252d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Store the MXCSR register */
3262d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("ldmxcsr %0" : : "m" (envp->__mxcsr));
327ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin
3282d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  return (0);
329770a3495607497071693147f162ac75f39423973Elliott Hughes}
330770a3495607497071693147f162ac75f39423973Elliott Hughes
331770a3495607497071693147f162ac75f39423973Elliott Hughes/*
332770a3495607497071693147f162ac75f39423973Elliott Hughes * The feupdateenv() function saves the currently raised floating-point
333770a3495607497071693147f162ac75f39423973Elliott Hughes * exceptions in its automatic storage, installs the floating-point environment
334770a3495607497071693147f162ac75f39423973Elliott Hughes * represented by the object pointed to by `envp', and then raises the saved
335770a3495607497071693147f162ac75f39423973Elliott Hughes * floating-point exceptions. The argument `envp' shall point to an object set
336770a3495607497071693147f162ac75f39423973Elliott Hughes * by a call to feholdexcept() or fegetenv(), or equal a floating-point
337770a3495607497071693147f162ac75f39423973Elliott Hughes * environment macro.
338770a3495607497071693147f162ac75f39423973Elliott Hughes */
339ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupinint
340ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupinfeupdateenv(const fenv_t *envp)
341ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin{
3422d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  unsigned short status;
3432d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  unsigned int mxcsr;
344770a3495607497071693147f162ac75f39423973Elliott Hughes
3452d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Store the x87 status register */
3462d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("fnstsw %0" : "=am" (status));
347ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin
3482d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Store the MXCSR register */
3492d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr));
350770a3495607497071693147f162ac75f39423973Elliott Hughes
3512d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Install new floating-point environment */
3522d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  fesetenv(envp);
353770a3495607497071693147f162ac75f39423973Elliott Hughes
3542d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /* Raise any previously accumulated exceptions */
3552d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  feraiseexcept(status | mxcsr);
356770a3495607497071693147f162ac75f39423973Elliott Hughes
3572d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  return (0);
358ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin}
359ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin
360770a3495607497071693147f162ac75f39423973Elliott Hughes/*
361770a3495607497071693147f162ac75f39423973Elliott Hughes * The following functions are extentions to the standard
362770a3495607497071693147f162ac75f39423973Elliott Hughes */
363ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupinint
364770a3495607497071693147f162ac75f39423973Elliott Hughesfeenableexcept(int mask)
365ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin{
3662d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  unsigned int mxcsr, omask;
3672d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  unsigned short control;
368ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin
3692d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  mask &= FE_ALL_EXCEPT;
370770a3495607497071693147f162ac75f39423973Elliott Hughes
3712d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("fnstcw %0" : "=m" (control));
3722d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr));
373770a3495607497071693147f162ac75f39423973Elliott Hughes
37443bf81e54427ac7ae55dc79c057cca62f94c5f77Elliott Hughes  omask = ~(control | (mxcsr >> SSE_MASK_SHIFT)) & FE_ALL_EXCEPT;
3752d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  control &= ~mask;
3762d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("fldcw %0" : : "m" (control));
377770a3495607497071693147f162ac75f39423973Elliott Hughes
37843bf81e54427ac7ae55dc79c057cca62f94c5f77Elliott Hughes  mxcsr &= ~(mask << SSE_MASK_SHIFT);
3792d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("ldmxcsr %0" : : "m" (mxcsr));
380770a3495607497071693147f162ac75f39423973Elliott Hughes
3812d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  return (omask);
382ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin}
383ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin
384ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupinint
385770a3495607497071693147f162ac75f39423973Elliott Hughesfedisableexcept(int mask)
386ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin{
3872d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  unsigned int mxcsr, omask;
3882d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  unsigned short control;
389ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin
3902d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  mask &= FE_ALL_EXCEPT;
391770a3495607497071693147f162ac75f39423973Elliott Hughes
3922d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("fnstcw %0" : "=m" (control));
3932d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr));
394770a3495607497071693147f162ac75f39423973Elliott Hughes
39543bf81e54427ac7ae55dc79c057cca62f94c5f77Elliott Hughes  omask = ~(control | (mxcsr >> SSE_MASK_SHIFT)) & FE_ALL_EXCEPT;
3962d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  control |= mask;
3972d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("fldcw %0" : : "m" (control));
398770a3495607497071693147f162ac75f39423973Elliott Hughes
39943bf81e54427ac7ae55dc79c057cca62f94c5f77Elliott Hughes  mxcsr |= mask << SSE_MASK_SHIFT;
4002d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("ldmxcsr %0" : : "m" (mxcsr));
401770a3495607497071693147f162ac75f39423973Elliott Hughes
4022d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  return (omask);
403ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin}
404ce7add19d8100cb4935b193d77ad6cb14ed3d3bcPavel Chupin
405770a3495607497071693147f162ac75f39423973Elliott Hughesint
406770a3495607497071693147f162ac75f39423973Elliott Hughesfegetexcept(void)
407770a3495607497071693147f162ac75f39423973Elliott Hughes{
4082d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  unsigned short control;
409770a3495607497071693147f162ac75f39423973Elliott Hughes
4102d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  /*
4112d367905a2e1b950f79b408141eea07c222b590bCalin Juravle   * We assume that the masks for the x87 and the SSE unit are
4122d367905a2e1b950f79b408141eea07c222b590bCalin Juravle   * the same.
4132d367905a2e1b950f79b408141eea07c222b590bCalin Juravle   */
4142d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  __asm__ __volatile__ ("fnstcw %0" : "=m" (control));
415770a3495607497071693147f162ac75f39423973Elliott Hughes
4162d367905a2e1b950f79b408141eea07c222b590bCalin Juravle  return (~control & FE_ALL_EXCEPT);
417770a3495607497071693147f162ac75f39423973Elliott Hughes}
418