186d131a0SDavid Schultz /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3d915a14eSPedro F. Giffuni *
486d131a0SDavid Schultz * Copyright (c) 2012 Ian Lepore <freebsd@damnhippie.dyndns.org>
586d131a0SDavid Schultz * All rights reserved.
686d131a0SDavid Schultz *
786d131a0SDavid Schultz * Redistribution and use in source and binary forms, with or without
886d131a0SDavid Schultz * modification, are permitted provided that the following conditions
986d131a0SDavid Schultz * are met:
1086d131a0SDavid Schultz * 1. Redistributions of source code must retain the above copyright
1186d131a0SDavid Schultz * notice, this list of conditions and the following disclaimer.
1286d131a0SDavid Schultz * 2. Redistributions in binary form must reproduce the above copyright
1386d131a0SDavid Schultz * notice, this list of conditions and the following disclaimer in the
1486d131a0SDavid Schultz * documentation and/or other materials provided with the distribution.
1586d131a0SDavid Schultz *
1686d131a0SDavid Schultz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1786d131a0SDavid Schultz * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1886d131a0SDavid Schultz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1986d131a0SDavid Schultz * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2086d131a0SDavid Schultz * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2186d131a0SDavid Schultz * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2286d131a0SDavid Schultz * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2386d131a0SDavid Schultz * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2486d131a0SDavid Schultz * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2586d131a0SDavid Schultz * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2686d131a0SDavid Schultz * SUCH DAMAGE.
2786d131a0SDavid Schultz */
2886d131a0SDavid Schultz
2986d131a0SDavid Schultz #include <fenv.h>
3086d131a0SDavid Schultz #include <float.h>
3186d131a0SDavid Schultz
32cc0c9bbaSAndrew Turner #ifndef __ARM_PCS_VFP
337082a2cfSDavid Schultz #include "softfloat-for-gcc.h"
347082a2cfSDavid Schultz #include "milieu.h"
3586d131a0SDavid Schultz #include "softfloat.h"
36cc0c9bbaSAndrew Turner #endif
3786d131a0SDavid Schultz
3886d131a0SDavid Schultz int
__flt_rounds(void)3986d131a0SDavid Schultz __flt_rounds(void)
4086d131a0SDavid Schultz {
41cc0c9bbaSAndrew Turner int mode;
4286d131a0SDavid Schultz
43cc0c9bbaSAndrew Turner #ifndef __ARM_PCS_VFP
4486d131a0SDavid Schultz /*
4586d131a0SDavid Schultz * Translate our rounding modes to the unnamed
4686d131a0SDavid Schultz * manifest constants required by C99 et. al.
4786d131a0SDavid Schultz */
48cc0c9bbaSAndrew Turner mode = __softfloat_float_rounding_mode;
49cc0c9bbaSAndrew Turner #else /* __ARM_PCS_VFP */
50cc0c9bbaSAndrew Turner /*
51cc0c9bbaSAndrew Turner * Read the floating-point status and control register
52cc0c9bbaSAndrew Turner */
53cc0c9bbaSAndrew Turner __asm __volatile("vmrs %0, fpscr" : "=&r"(mode));
54cc0c9bbaSAndrew Turner mode &= _ROUND_MASK;
55cc0c9bbaSAndrew Turner #endif /* __ARM_PCS_VFP */
56cc0c9bbaSAndrew Turner
57cc0c9bbaSAndrew Turner switch (mode) {
5886d131a0SDavid Schultz case FE_TOWARDZERO:
5986d131a0SDavid Schultz return (0);
6086d131a0SDavid Schultz case FE_TONEAREST:
6186d131a0SDavid Schultz return (1);
6286d131a0SDavid Schultz case FE_UPWARD:
6386d131a0SDavid Schultz return (2);
6486d131a0SDavid Schultz case FE_DOWNWARD:
6586d131a0SDavid Schultz return (3);
6686d131a0SDavid Schultz }
6786d131a0SDavid Schultz return (-1);
6886d131a0SDavid Schultz }
69