Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/libraries/AP_Common/missing/fenv.h
Views: 1799
#pragma once12#include_next <fenv.h>34#ifndef HAVE_FEENABLEEXCEPT5#if defined(__APPLE__) && defined(__MACH__)6#if defined __i386__ || defined __x86_64__78// Public domain polyfill for feenableexcept on OS X9// http://www-personal.umich.edu/~williams/archive/computation/fe-handling-example.c1011inline int feenableexcept(unsigned int excepts)12{13static fenv_t fenv;14unsigned int new_excepts = excepts & FE_ALL_EXCEPT;15// previous masks16unsigned int old_excepts;1718if (fegetenv(&fenv)) {19return -1;20}21old_excepts = fenv.__control & FE_ALL_EXCEPT;2223// unmask24fenv.__control &= ~new_excepts;25fenv.__mxcsr &= ~(new_excepts << 7);2627return fesetenv(&fenv) ? -1 : old_excepts;28}2930inline int fedisableexcept(unsigned int excepts)31{32static fenv_t fenv;33unsigned int new_excepts = excepts & FE_ALL_EXCEPT;34// all previous masks35unsigned int old_excepts;3637if (fegetenv(&fenv)) {38return -1;39}40old_excepts = fenv.__control & FE_ALL_EXCEPT;4142// mask43fenv.__control |= new_excepts;44fenv.__mxcsr |= new_excepts << 7;4546return fesetenv(&fenv) ? -1 : old_excepts;47}4849#elif defined __arm64__5051/*52Yoinked and adapted this ARM64 implementation of floating point exceptions from53https://android.googlesource.com/platform/bionic/+/a147a1d/libm/arm64/fenv.c54*/5556/*-57* Copyright (c) 2004 David Schultz <[email protected]>58* All rights reserved.59*60* Redistribution and use in source and binary forms, with or without61* modification, are permitted provided that the following conditions62* are met:63* 1. Redistributions of source code must retain the above copyright64* notice, this list of conditions and the following disclaimer.65* 2. Redistributions in binary form must reproduce the above copyright66* notice, this list of conditions and the following disclaimer in the67* documentation and/or other materials provided with the distribution.68*69* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND70* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE71* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE72* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE73* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL74* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS75* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)76* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT77* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY78* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF79* SUCH DAMAGE.80*81* $FreeBSD: libm/aarch64/fenv.c $82*/838485#define FPCR_EXCEPT_SHIFT 88687// FPCR, Floating-point Control Register.88#define __get_fpcr(__fpcr) __asm__ __volatile__("mrs %0,fpcr" : "=r" (__fpcr))89#define __set_fpcr(__fpcr) __asm__ __volatile__("msr fpcr,%0" : :"ri" (__fpcr))9091inline int feenableexcept(int mask) {92unsigned long long old_fpcr, new_fpcr;93__get_fpcr(old_fpcr);94new_fpcr = old_fpcr | ((mask & FE_ALL_EXCEPT) << FPCR_EXCEPT_SHIFT);95if (new_fpcr != old_fpcr) {96__set_fpcr(new_fpcr);97}98return ((old_fpcr >> FPCR_EXCEPT_SHIFT) & FE_ALL_EXCEPT);99}100101inline int fedisableexcept(int mask) {102unsigned long long old_fpcr, new_fpcr;103__get_fpcr(old_fpcr);104new_fpcr = old_fpcr & ~((mask & FE_ALL_EXCEPT) << FPCR_EXCEPT_SHIFT);105if (new_fpcr != old_fpcr) {106__set_fpcr(new_fpcr);107}108return ((old_fpcr >> FPCR_EXCEPT_SHIFT) & FE_ALL_EXCEPT);109}110111#endif112113#else114inline int feenableexcept(unsigned int excepts)115{116#pragma STDC FENV_ACCESS ON117fexcept_t flags;118/* Save current exception flags. */119fegetexceptflag(&flags, FE_ALL_EXCEPT);120121feclearexcept(FE_ALL_EXCEPT); /* clear all fp exception conditions */122return fesetexceptflag(&flags, excepts) != 0 ? -1 : flags; /* set new flags */123124}125126inline int fedisableexcept(unsigned int excepts)127{128#pragma STDC FENV_ACCESS ON129fexcept_t flags;130/* Save current exception flags. */131fegetexceptflag(&flags, FE_ALL_EXCEPT);132133feclearexcept(FE_ALL_EXCEPT); /* clear all fp exception conditions */134return fesetexceptflag(&flags, ~excepts) != 0 ? -1 : flags; /* set new flags */135}136137#endif138#endif139140141