Home | History | Annotate | Line # | Download | only in soft-fp
soft-fp.h revision 1.3
      1  1.1  mrg /* Software floating-point emulation.
      2  1.3  mrg    Copyright (C) 1997-2014 Free Software Foundation, Inc.
      3  1.1  mrg    This file is part of the GNU C Library.
      4  1.1  mrg    Contributed by Richard Henderson (rth (at) cygnus.com),
      5  1.1  mrg 		  Jakub Jelinek (jj (at) ultra.linux.cz),
      6  1.1  mrg 		  David S. Miller (davem (at) redhat.com) and
      7  1.1  mrg 		  Peter Maydell (pmaydell (at) chiark.greenend.org.uk).
      8  1.1  mrg 
      9  1.1  mrg    The GNU C Library is free software; you can redistribute it and/or
     10  1.1  mrg    modify it under the terms of the GNU Lesser General Public
     11  1.1  mrg    License as published by the Free Software Foundation; either
     12  1.1  mrg    version 2.1 of the License, or (at your option) any later version.
     13  1.1  mrg 
     14  1.1  mrg    In addition to the permissions in the GNU Lesser General Public
     15  1.1  mrg    License, the Free Software Foundation gives you unlimited
     16  1.1  mrg    permission to link the compiled version of this file into
     17  1.1  mrg    combinations with other programs, and to distribute those
     18  1.1  mrg    combinations without any restriction coming from the use of this
     19  1.1  mrg    file.  (The Lesser General Public License restrictions do apply in
     20  1.1  mrg    other respects; for example, they cover modification of the file,
     21  1.1  mrg    and distribution when not linked into a combine executable.)
     22  1.1  mrg 
     23  1.1  mrg    The GNU C Library is distributed in the hope that it will be useful,
     24  1.1  mrg    but WITHOUT ANY WARRANTY; without even the implied warranty of
     25  1.1  mrg    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     26  1.1  mrg    Lesser General Public License for more details.
     27  1.1  mrg 
     28  1.1  mrg    You should have received a copy of the GNU Lesser General Public
     29  1.1  mrg    License along with the GNU C Library; if not, see
     30  1.1  mrg    <http://www.gnu.org/licenses/>.  */
     31  1.1  mrg 
     32  1.1  mrg #ifndef SOFT_FP_H
     33  1.1  mrg #define SOFT_FP_H
     34  1.1  mrg 
     35  1.1  mrg #ifdef _LIBC
     36  1.3  mrg # include <sfp-machine.h>
     37  1.1  mrg #else
     38  1.3  mrg # include "sfp-machine.h"
     39  1.1  mrg #endif
     40  1.1  mrg 
     41  1.3  mrg /* Allow sfp-machine to have its own byte order definitions.  */
     42  1.1  mrg #ifndef __BYTE_ORDER
     43  1.3  mrg # ifdef _LIBC
     44  1.3  mrg #  include <endian.h>
     45  1.3  mrg # else
     46  1.3  mrg #  error "endianness not defined by sfp-machine.h"
     47  1.3  mrg # endif
     48  1.1  mrg #endif
     49  1.1  mrg 
     50  1.1  mrg #define _FP_WORKBITS		3
     51  1.3  mrg #define _FP_WORK_LSB		((_FP_W_TYPE) 1 << 3)
     52  1.3  mrg #define _FP_WORK_ROUND		((_FP_W_TYPE) 1 << 2)
     53  1.3  mrg #define _FP_WORK_GUARD		((_FP_W_TYPE) 1 << 1)
     54  1.3  mrg #define _FP_WORK_STICKY		((_FP_W_TYPE) 1 << 0)
     55  1.1  mrg 
     56  1.1  mrg #ifndef FP_RND_NEAREST
     57  1.1  mrg # define FP_RND_NEAREST		0
     58  1.1  mrg # define FP_RND_ZERO		1
     59  1.1  mrg # define FP_RND_PINF		2
     60  1.1  mrg # define FP_RND_MINF		3
     61  1.1  mrg #endif
     62  1.1  mrg #ifndef FP_ROUNDMODE
     63  1.1  mrg # define FP_ROUNDMODE		FP_RND_NEAREST
     64  1.1  mrg #endif
     65  1.1  mrg 
     66  1.3  mrg /* By default don't care about exceptions.  */
     67  1.1  mrg #ifndef FP_EX_INVALID
     68  1.3  mrg # define FP_EX_INVALID		0
     69  1.1  mrg #endif
     70  1.1  mrg #ifndef FP_EX_OVERFLOW
     71  1.3  mrg # define FP_EX_OVERFLOW		0
     72  1.1  mrg #endif
     73  1.1  mrg #ifndef FP_EX_UNDERFLOW
     74  1.3  mrg # define FP_EX_UNDERFLOW	0
     75  1.1  mrg #endif
     76  1.1  mrg #ifndef FP_EX_DIVZERO
     77  1.3  mrg # define FP_EX_DIVZERO		0
     78  1.1  mrg #endif
     79  1.1  mrg #ifndef FP_EX_INEXACT
     80  1.3  mrg # define FP_EX_INEXACT		0
     81  1.1  mrg #endif
     82  1.1  mrg #ifndef FP_EX_DENORM
     83  1.3  mrg # define FP_EX_DENORM		0
     84  1.3  mrg #endif
     85  1.3  mrg 
     86  1.3  mrg /* Sub-exceptions of "invalid".  */
     87  1.3  mrg /* Signaling NaN operand.  */
     88  1.3  mrg #ifndef FP_EX_INVALID_SNAN
     89  1.3  mrg # define FP_EX_INVALID_SNAN	0
     90  1.3  mrg #endif
     91  1.3  mrg /* Inf * 0.  */
     92  1.3  mrg #ifndef FP_EX_INVALID_IMZ
     93  1.3  mrg # define FP_EX_INVALID_IMZ	0
     94  1.3  mrg #endif
     95  1.3  mrg /* fma (Inf, 0, c).  */
     96  1.3  mrg #ifndef FP_EX_INVALID_IMZ_FMA
     97  1.3  mrg # define FP_EX_INVALID_IMZ_FMA	0
     98  1.3  mrg #endif
     99  1.3  mrg /* Inf - Inf.  */
    100  1.3  mrg #ifndef FP_EX_INVALID_ISI
    101  1.3  mrg # define FP_EX_INVALID_ISI	0
    102  1.3  mrg #endif
    103  1.3  mrg /* 0 / 0.  */
    104  1.3  mrg #ifndef FP_EX_INVALID_ZDZ
    105  1.3  mrg # define FP_EX_INVALID_ZDZ	0
    106  1.3  mrg #endif
    107  1.3  mrg /* Inf / Inf.  */
    108  1.3  mrg #ifndef FP_EX_INVALID_IDI
    109  1.3  mrg # define FP_EX_INVALID_IDI	0
    110  1.3  mrg #endif
    111  1.3  mrg /* sqrt (negative).  */
    112  1.3  mrg #ifndef FP_EX_INVALID_SQRT
    113  1.3  mrg # define FP_EX_INVALID_SQRT	0
    114  1.3  mrg #endif
    115  1.3  mrg /* Invalid conversion to integer.  */
    116  1.3  mrg #ifndef FP_EX_INVALID_CVI
    117  1.3  mrg # define FP_EX_INVALID_CVI	0
    118  1.3  mrg #endif
    119  1.3  mrg /* Invalid comparison.  */
    120  1.3  mrg #ifndef FP_EX_INVALID_VC
    121  1.3  mrg # define FP_EX_INVALID_VC	0
    122  1.1  mrg #endif
    123  1.1  mrg 
    124  1.1  mrg /* _FP_STRUCT_LAYOUT may be defined as an attribute to determine the
    125  1.1  mrg    struct layout variant used for structures where bit-fields are used
    126  1.1  mrg    to access specific parts of binary floating-point numbers.  This is
    127  1.1  mrg    required for systems where the default ABI uses struct layout with
    128  1.1  mrg    differences in how consecutive bit-fields are laid out from the
    129  1.1  mrg    default expected by soft-fp.  */
    130  1.1  mrg #ifndef _FP_STRUCT_LAYOUT
    131  1.3  mrg # define _FP_STRUCT_LAYOUT
    132  1.1  mrg #endif
    133  1.1  mrg 
    134  1.1  mrg #ifdef _FP_DECL_EX
    135  1.3  mrg # define FP_DECL_EX					\
    136  1.1  mrg   int _fex = 0;						\
    137  1.1  mrg   _FP_DECL_EX
    138  1.1  mrg #else
    139  1.3  mrg # define FP_DECL_EX int _fex = 0
    140  1.1  mrg #endif
    141  1.1  mrg 
    142  1.3  mrg /* Initialize any machine-specific state used in FP_ROUNDMODE,
    143  1.3  mrg    FP_TRAPPING_EXCEPTIONS or FP_HANDLE_EXCEPTIONS.  */
    144  1.1  mrg #ifndef FP_INIT_ROUNDMODE
    145  1.3  mrg # define FP_INIT_ROUNDMODE do {} while (0)
    146  1.3  mrg #endif
    147  1.3  mrg 
    148  1.3  mrg /* Initialize any machine-specific state used in
    149  1.3  mrg    FP_TRAPPING_EXCEPTIONS or FP_HANDLE_EXCEPTIONS.  */
    150  1.3  mrg #ifndef FP_INIT_TRAPPING_EXCEPTIONS
    151  1.3  mrg # define FP_INIT_TRAPPING_EXCEPTIONS FP_INIT_ROUNDMODE
    152  1.3  mrg #endif
    153  1.3  mrg 
    154  1.3  mrg /* Initialize any machine-specific state used in
    155  1.3  mrg    FP_HANDLE_EXCEPTIONS.  */
    156  1.3  mrg #ifndef FP_INIT_EXCEPTIONS
    157  1.3  mrg # define FP_INIT_EXCEPTIONS FP_INIT_TRAPPING_EXCEPTIONS
    158  1.1  mrg #endif
    159  1.1  mrg 
    160  1.1  mrg #ifndef FP_HANDLE_EXCEPTIONS
    161  1.3  mrg # define FP_HANDLE_EXCEPTIONS do {} while (0)
    162  1.3  mrg #endif
    163  1.3  mrg 
    164  1.3  mrg /* Whether to flush subnormal inputs to zero with the same sign.  */
    165  1.3  mrg #ifndef FP_DENORM_ZERO
    166  1.3  mrg # define FP_DENORM_ZERO 0
    167  1.1  mrg #endif
    168  1.1  mrg 
    169  1.1  mrg #ifndef FP_INHIBIT_RESULTS
    170  1.1  mrg /* By default we write the results always.
    171  1.3  mrg    sfp-machine may override this and e.g.
    172  1.3  mrg    check if some exceptions are unmasked
    173  1.3  mrg    and inhibit it in such a case.  */
    174  1.3  mrg # define FP_INHIBIT_RESULTS 0
    175  1.1  mrg #endif
    176  1.1  mrg 
    177  1.1  mrg #define FP_SET_EXCEPTION(ex)				\
    178  1.1  mrg   _fex |= (ex)
    179  1.1  mrg 
    180  1.1  mrg #define FP_CUR_EXCEPTIONS				\
    181  1.1  mrg   (_fex)
    182  1.1  mrg 
    183  1.1  mrg #ifndef FP_TRAPPING_EXCEPTIONS
    184  1.3  mrg # define FP_TRAPPING_EXCEPTIONS 0
    185  1.1  mrg #endif
    186  1.1  mrg 
    187  1.3  mrg /* A file using soft-fp may define FP_NO_EXCEPTIONS before including
    188  1.3  mrg    soft-fp.h to indicate that, although a macro used there could raise
    189  1.3  mrg    exceptions, or do rounding and potentially thereby raise
    190  1.3  mrg    exceptions, for some arguments, for the particular arguments used
    191  1.3  mrg    in that file no exceptions or rounding can occur.  Such a file
    192  1.3  mrg    should not itself use macros relating to handling exceptions and
    193  1.3  mrg    rounding modes; this is only for indirect uses (in particular, in
    194  1.3  mrg    _FP_FROM_INT and the macros it calls).  */
    195  1.3  mrg #ifdef FP_NO_EXCEPTIONS
    196  1.3  mrg 
    197  1.3  mrg # undef FP_SET_EXCEPTION
    198  1.3  mrg # define FP_SET_EXCEPTION(ex) do {} while (0)
    199  1.1  mrg 
    200  1.3  mrg # undef FP_CUR_EXCEPTIONS
    201  1.3  mrg # define FP_CUR_EXCEPTIONS 0
    202  1.3  mrg 
    203  1.3  mrg # undef FP_TRAPPING_EXCEPTIONS
    204  1.3  mrg # define FP_TRAPPING_EXCEPTIONS 0
    205  1.3  mrg 
    206  1.3  mrg # undef FP_ROUNDMODE
    207  1.3  mrg # define FP_ROUNDMODE FP_RND_ZERO
    208  1.3  mrg 
    209  1.3  mrg # undef _FP_TININESS_AFTER_ROUNDING
    210  1.3  mrg # define _FP_TININESS_AFTER_ROUNDING 0
    211  1.3  mrg 
    212  1.3  mrg #endif
    213  1.3  mrg 
    214  1.3  mrg /* A file using soft-fp may define FP_NO_EXACT_UNDERFLOW before
    215  1.3  mrg    including soft-fp.h to indicate that, although a macro used there
    216  1.3  mrg    could allow for the case of exact underflow requiring the underflow
    217  1.3  mrg    exception to be raised if traps are enabled, for the particular
    218  1.3  mrg    arguments used in that file no exact underflow can occur.  */
    219  1.3  mrg #ifdef FP_NO_EXACT_UNDERFLOW
    220  1.3  mrg # undef FP_TRAPPING_EXCEPTIONS
    221  1.3  mrg # define FP_TRAPPING_EXCEPTIONS 0
    222  1.3  mrg #endif
    223  1.3  mrg 
    224  1.3  mrg #define _FP_ROUND_NEAREST(wc, X)				\
    225  1.3  mrg   do								\
    226  1.3  mrg     {								\
    227  1.3  mrg       if ((_FP_FRAC_LOW_##wc (X) & 15) != _FP_WORK_ROUND)	\
    228  1.3  mrg 	_FP_FRAC_ADDI_##wc (X, _FP_WORK_ROUND);			\
    229  1.3  mrg     }								\
    230  1.3  mrg   while (0)
    231  1.3  mrg 
    232  1.3  mrg #define _FP_ROUND_ZERO(wc, X)		(void) 0
    233  1.1  mrg 
    234  1.1  mrg #define _FP_ROUND_PINF(wc, X)				\
    235  1.3  mrg   do							\
    236  1.3  mrg     {							\
    237  1.3  mrg       if (!X##_s && (_FP_FRAC_LOW_##wc (X) & 7))	\
    238  1.3  mrg 	_FP_FRAC_ADDI_##wc (X, _FP_WORK_LSB);		\
    239  1.3  mrg     }							\
    240  1.3  mrg   while (0)
    241  1.3  mrg 
    242  1.3  mrg #define _FP_ROUND_MINF(wc, X)			\
    243  1.3  mrg   do						\
    244  1.3  mrg     {						\
    245  1.3  mrg       if (X##_s && (_FP_FRAC_LOW_##wc (X) & 7))	\
    246  1.3  mrg 	_FP_FRAC_ADDI_##wc (X, _FP_WORK_LSB);	\
    247  1.3  mrg     }						\
    248  1.3  mrg   while (0)
    249  1.1  mrg 
    250  1.1  mrg #define _FP_ROUND(wc, X)			\
    251  1.3  mrg   do						\
    252  1.3  mrg     {						\
    253  1.3  mrg       if (_FP_FRAC_LOW_##wc (X) & 7)		\
    254  1.3  mrg 	{					\
    255  1.3  mrg 	  FP_SET_EXCEPTION (FP_EX_INEXACT);	\
    256  1.3  mrg 	  switch (FP_ROUNDMODE)			\
    257  1.3  mrg 	    {					\
    258  1.3  mrg 	    case FP_RND_NEAREST:		\
    259  1.3  mrg 	      _FP_ROUND_NEAREST (wc, X);	\
    260  1.3  mrg 	      break;				\
    261  1.3  mrg 	    case FP_RND_ZERO:			\
    262  1.3  mrg 	      _FP_ROUND_ZERO (wc, X);		\
    263  1.3  mrg 	      break;				\
    264  1.3  mrg 	    case FP_RND_PINF:			\
    265  1.3  mrg 	      _FP_ROUND_PINF (wc, X);		\
    266  1.3  mrg 	      break;				\
    267  1.3  mrg 	    case FP_RND_MINF:			\
    268  1.3  mrg 	      _FP_ROUND_MINF (wc, X);		\
    269  1.3  mrg 	      break;				\
    270  1.3  mrg 	    }					\
    271  1.3  mrg 	}					\
    272  1.3  mrg     }						\
    273  1.3  mrg   while (0)
    274  1.1  mrg 
    275  1.1  mrg #define FP_CLS_NORMAL		0
    276  1.1  mrg #define FP_CLS_ZERO		1
    277  1.1  mrg #define FP_CLS_INF		2
    278  1.1  mrg #define FP_CLS_NAN		3
    279  1.1  mrg 
    280  1.3  mrg #define _FP_CLS_COMBINE(x, y)	(((x) << 2) | (y))
    281  1.1  mrg 
    282  1.1  mrg #include "op-1.h"
    283  1.1  mrg #include "op-2.h"
    284  1.1  mrg #include "op-4.h"
    285  1.1  mrg #include "op-8.h"
    286  1.1  mrg #include "op-common.h"
    287  1.1  mrg 
    288  1.1  mrg /* Sigh.  Silly things longlong.h needs.  */
    289  1.1  mrg #define UWtype		_FP_W_TYPE
    290  1.1  mrg #define W_TYPE_SIZE	_FP_W_TYPE_SIZE
    291  1.1  mrg 
    292  1.3  mrg typedef int QItype __attribute__ ((mode (QI)));
    293  1.3  mrg typedef int SItype __attribute__ ((mode (SI)));
    294  1.3  mrg typedef int DItype __attribute__ ((mode (DI)));
    295  1.3  mrg typedef unsigned int UQItype __attribute__ ((mode (QI)));
    296  1.3  mrg typedef unsigned int USItype __attribute__ ((mode (SI)));
    297  1.3  mrg typedef unsigned int UDItype __attribute__ ((mode (DI)));
    298  1.1  mrg #if _FP_W_TYPE_SIZE == 32
    299  1.3  mrg typedef unsigned int UHWtype __attribute__ ((mode (HI)));
    300  1.1  mrg #elif _FP_W_TYPE_SIZE == 64
    301  1.1  mrg typedef USItype UHWtype;
    302  1.1  mrg #endif
    303  1.1  mrg 
    304  1.1  mrg #ifndef CMPtype
    305  1.3  mrg # define CMPtype	int
    306  1.1  mrg #endif
    307  1.1  mrg 
    308  1.3  mrg #define SI_BITS		(__CHAR_BIT__ * (int) sizeof (SItype))
    309  1.3  mrg #define DI_BITS		(__CHAR_BIT__ * (int) sizeof (DItype))
    310  1.1  mrg 
    311  1.1  mrg #ifndef umul_ppmm
    312  1.3  mrg # ifdef _LIBC
    313  1.3  mrg #  include <stdlib/longlong.h>
    314  1.3  mrg # else
    315  1.3  mrg #  include "longlong.h"
    316  1.3  mrg # endif
    317  1.1  mrg #endif
    318  1.1  mrg 
    319  1.1  mrg #ifdef _LIBC
    320  1.3  mrg # include <stdlib.h>
    321  1.1  mrg #else
    322  1.1  mrg extern void abort (void);
    323  1.1  mrg #endif
    324  1.1  mrg 
    325  1.1  mrg #endif
    326