| 1 | /* This file is part of SIS (SPARC instruction simulator) |
| 2 | |
| 3 | Copyright (C) 1995-2020 Free Software Foundation, Inc. |
| 4 | Contributed by Jiri Gaisler, European Space Agency |
| 5 | |
| 6 | This program is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 3 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | /* This file implements the interface between the host and the simulated |
| 20 | FPU. IEEE trap handling is done as follows: |
| 21 | 1. In the host, all IEEE traps are masked |
| 22 | 2. After each simulated FPU instruction, check if any exception |
| 23 | occured by reading the exception bits from the host FPU status |
| 24 | register (get_accex()). |
| 25 | 3. Propagate any exceptions to the simulated FSR. |
| 26 | 4. Clear host exception bits. |
| 27 | */ |
| 28 | |
| 29 | #include "config.h" |
| 30 | #include "sis.h" |
| 31 | #include <fenv.h> |
| 32 | |
| 33 | /* This routine should return the accrued exceptions */ |
| 34 | int |
| 35 | get_accex() |
| 36 | { |
| 37 | int fexc, accx; |
| 38 | |
| 39 | fexc = fetestexcept (FE_ALL_EXCEPT); |
| 40 | accx = 0; |
| 41 | if (fexc & FE_INEXACT) |
| 42 | accx |= 1; |
| 43 | if (fexc & FE_DIVBYZERO) |
| 44 | accx |= 2; |
| 45 | if (fexc & FE_UNDERFLOW) |
| 46 | accx |= 4; |
| 47 | if (fexc & FE_OVERFLOW) |
| 48 | accx |= 8; |
| 49 | if (fexc & FE_INVALID) |
| 50 | accx |= 0x10; |
| 51 | return accx; |
| 52 | } |
| 53 | |
| 54 | /* How to clear the accrued exceptions */ |
| 55 | void |
| 56 | clear_accex() |
| 57 | { |
| 58 | feclearexcept (FE_ALL_EXCEPT); |
| 59 | } |
| 60 | |
| 61 | /* How to map SPARC FSR onto the host */ |
| 62 | void |
| 63 | set_fsr(fsr) |
| 64 | uint32 fsr; |
| 65 | { |
| 66 | int fround; |
| 67 | |
| 68 | fsr >>= 30; |
| 69 | switch (fsr) { |
| 70 | case 0: |
| 71 | fround = FE_TONEAREST; |
| 72 | break; |
| 73 | case 1: |
| 74 | fround = FE_TOWARDZERO; |
| 75 | break; |
| 76 | case 2: |
| 77 | fround = FE_UPWARD; |
| 78 | break; |
| 79 | case 3: |
| 80 | fround = FE_DOWNWARD; |
| 81 | break; |
| 82 | } |
| 83 | fesetround (fround); |
| 84 | } |