Home | History | Annotate | Line # | Download | only in soft-fp
soft-fp.h revision 1.3.4.2
      1      1.1       mrg /* Software floating-point emulation.
      2  1.3.4.2    martin    Copyright (C) 1997-2016 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.3.4.1  christos #define SOFT_FP_H	1
     34      1.1       mrg 
     35      1.1       mrg #ifdef _LIBC
     36      1.3       mrg # include <sfp-machine.h>
     37  1.3.4.1  christos #elif defined __KERNEL__
     38  1.3.4.1  christos /* The Linux kernel uses asm/ names for architecture-specific
     39  1.3.4.1  christos    files.  */
     40  1.3.4.1  christos # include <asm/sfp-machine.h>
     41      1.1       mrg #else
     42      1.3       mrg # include "sfp-machine.h"
     43      1.1       mrg #endif
     44      1.1       mrg 
     45      1.3       mrg /* Allow sfp-machine to have its own byte order definitions.  */
     46      1.1       mrg #ifndef __BYTE_ORDER
     47      1.3       mrg # ifdef _LIBC
     48      1.3       mrg #  include <endian.h>
     49      1.3       mrg # else
     50      1.3       mrg #  error "endianness not defined by sfp-machine.h"
     51      1.3       mrg # endif
     52      1.1       mrg #endif
     53      1.1       mrg 
     54  1.3.4.1  christos /* For unreachable default cases in switch statements over bitwise OR
     55  1.3.4.1  christos    of FP_CLS_* values.  */
     56  1.3.4.1  christos #if (defined __GNUC__							\
     57  1.3.4.1  christos      && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)))
     58  1.3.4.1  christos # define _FP_UNREACHABLE	__builtin_unreachable ()
     59  1.3.4.1  christos #else
     60  1.3.4.1  christos # define _FP_UNREACHABLE	abort ()
     61  1.3.4.1  christos #endif
     62  1.3.4.1  christos 
     63  1.3.4.1  christos #if ((defined __GNUC__							\
     64  1.3.4.1  christos       && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)))	\
     65  1.3.4.1  christos      || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 201112L))
     66  1.3.4.1  christos # define _FP_STATIC_ASSERT(expr, msg)		\
     67  1.3.4.1  christos   _Static_assert ((expr), msg)
     68  1.3.4.1  christos #else
     69  1.3.4.1  christos # define _FP_STATIC_ASSERT(expr, msg)					\
     70  1.3.4.1  christos   extern int (*__Static_assert_function (void))				\
     71  1.3.4.1  christos     [!!sizeof (struct { int __error_if_negative: (expr) ? 2 : -1; })]
     72  1.3.4.1  christos #endif
     73  1.3.4.1  christos 
     74  1.3.4.1  christos /* In the Linux kernel, some architectures have a single function that
     75  1.3.4.1  christos    uses different kinds of unpacking and packing depending on the
     76  1.3.4.1  christos    instruction being emulated, meaning it is not readily visible to
     77  1.3.4.1  christos    the compiler that variables from _FP_DECL and _FP_FRAC_DECL_*
     78  1.3.4.1  christos    macros are only used in cases where they were initialized.  */
     79  1.3.4.1  christos #ifdef __KERNEL__
     80  1.3.4.1  christos # define _FP_ZERO_INIT		= 0
     81  1.3.4.1  christos #else
     82  1.3.4.1  christos # define _FP_ZERO_INIT
     83  1.3.4.1  christos #endif
     84  1.3.4.1  christos 
     85      1.1       mrg #define _FP_WORKBITS		3
     86      1.3       mrg #define _FP_WORK_LSB		((_FP_W_TYPE) 1 << 3)
     87      1.3       mrg #define _FP_WORK_ROUND		((_FP_W_TYPE) 1 << 2)
     88      1.3       mrg #define _FP_WORK_GUARD		((_FP_W_TYPE) 1 << 1)
     89      1.3       mrg #define _FP_WORK_STICKY		((_FP_W_TYPE) 1 << 0)
     90      1.1       mrg 
     91      1.1       mrg #ifndef FP_RND_NEAREST
     92      1.1       mrg # define FP_RND_NEAREST		0
     93      1.1       mrg # define FP_RND_ZERO		1
     94      1.1       mrg # define FP_RND_PINF		2
     95      1.1       mrg # define FP_RND_MINF		3
     96      1.1       mrg #endif
     97      1.1       mrg #ifndef FP_ROUNDMODE
     98      1.1       mrg # define FP_ROUNDMODE		FP_RND_NEAREST
     99      1.1       mrg #endif
    100      1.1       mrg 
    101      1.3       mrg /* By default don't care about exceptions.  */
    102      1.1       mrg #ifndef FP_EX_INVALID
    103      1.3       mrg # define FP_EX_INVALID		0
    104      1.1       mrg #endif
    105      1.1       mrg #ifndef FP_EX_OVERFLOW
    106      1.3       mrg # define FP_EX_OVERFLOW		0
    107      1.1       mrg #endif
    108      1.1       mrg #ifndef FP_EX_UNDERFLOW
    109      1.3       mrg # define FP_EX_UNDERFLOW	0
    110      1.1       mrg #endif
    111      1.1       mrg #ifndef FP_EX_DIVZERO
    112      1.3       mrg # define FP_EX_DIVZERO		0
    113      1.1       mrg #endif
    114      1.1       mrg #ifndef FP_EX_INEXACT
    115      1.3       mrg # define FP_EX_INEXACT		0
    116      1.1       mrg #endif
    117      1.1       mrg #ifndef FP_EX_DENORM
    118      1.3       mrg # define FP_EX_DENORM		0
    119      1.3       mrg #endif
    120      1.3       mrg 
    121      1.3       mrg /* Sub-exceptions of "invalid".  */
    122      1.3       mrg /* Signaling NaN operand.  */
    123      1.3       mrg #ifndef FP_EX_INVALID_SNAN
    124      1.3       mrg # define FP_EX_INVALID_SNAN	0
    125      1.3       mrg #endif
    126      1.3       mrg /* Inf * 0.  */
    127      1.3       mrg #ifndef FP_EX_INVALID_IMZ
    128      1.3       mrg # define FP_EX_INVALID_IMZ	0
    129      1.3       mrg #endif
    130      1.3       mrg /* fma (Inf, 0, c).  */
    131      1.3       mrg #ifndef FP_EX_INVALID_IMZ_FMA
    132      1.3       mrg # define FP_EX_INVALID_IMZ_FMA	0
    133      1.3       mrg #endif
    134      1.3       mrg /* Inf - Inf.  */
    135      1.3       mrg #ifndef FP_EX_INVALID_ISI
    136      1.3       mrg # define FP_EX_INVALID_ISI	0
    137      1.3       mrg #endif
    138      1.3       mrg /* 0 / 0.  */
    139      1.3       mrg #ifndef FP_EX_INVALID_ZDZ
    140      1.3       mrg # define FP_EX_INVALID_ZDZ	0
    141      1.3       mrg #endif
    142      1.3       mrg /* Inf / Inf.  */
    143      1.3       mrg #ifndef FP_EX_INVALID_IDI
    144      1.3       mrg # define FP_EX_INVALID_IDI	0
    145      1.3       mrg #endif
    146      1.3       mrg /* sqrt (negative).  */
    147      1.3       mrg #ifndef FP_EX_INVALID_SQRT
    148      1.3       mrg # define FP_EX_INVALID_SQRT	0
    149      1.3       mrg #endif
    150      1.3       mrg /* Invalid conversion to integer.  */
    151      1.3       mrg #ifndef FP_EX_INVALID_CVI
    152      1.3       mrg # define FP_EX_INVALID_CVI	0
    153      1.3       mrg #endif
    154      1.3       mrg /* Invalid comparison.  */
    155      1.3       mrg #ifndef FP_EX_INVALID_VC
    156      1.3       mrg # define FP_EX_INVALID_VC	0
    157      1.1       mrg #endif
    158      1.1       mrg 
    159      1.1       mrg /* _FP_STRUCT_LAYOUT may be defined as an attribute to determine the
    160      1.1       mrg    struct layout variant used for structures where bit-fields are used
    161      1.1       mrg    to access specific parts of binary floating-point numbers.  This is
    162      1.1       mrg    required for systems where the default ABI uses struct layout with
    163      1.1       mrg    differences in how consecutive bit-fields are laid out from the
    164      1.1       mrg    default expected by soft-fp.  */
    165      1.1       mrg #ifndef _FP_STRUCT_LAYOUT
    166      1.3       mrg # define _FP_STRUCT_LAYOUT
    167      1.1       mrg #endif
    168      1.1       mrg 
    169      1.1       mrg #ifdef _FP_DECL_EX
    170      1.3       mrg # define FP_DECL_EX					\
    171      1.1       mrg   int _fex = 0;						\
    172      1.1       mrg   _FP_DECL_EX
    173      1.1       mrg #else
    174      1.3       mrg # define FP_DECL_EX int _fex = 0
    175      1.1       mrg #endif
    176      1.1       mrg 
    177      1.3       mrg /* Initialize any machine-specific state used in FP_ROUNDMODE,
    178      1.3       mrg    FP_TRAPPING_EXCEPTIONS or FP_HANDLE_EXCEPTIONS.  */
    179      1.1       mrg #ifndef FP_INIT_ROUNDMODE
    180      1.3       mrg # define FP_INIT_ROUNDMODE do {} while (0)
    181      1.3       mrg #endif
    182      1.3       mrg 
    183      1.3       mrg /* Initialize any machine-specific state used in
    184      1.3       mrg    FP_TRAPPING_EXCEPTIONS or FP_HANDLE_EXCEPTIONS.  */
    185      1.3       mrg #ifndef FP_INIT_TRAPPING_EXCEPTIONS
    186      1.3       mrg # define FP_INIT_TRAPPING_EXCEPTIONS FP_INIT_ROUNDMODE
    187      1.3       mrg #endif
    188      1.3       mrg 
    189      1.3       mrg /* Initialize any machine-specific state used in
    190      1.3       mrg    FP_HANDLE_EXCEPTIONS.  */
    191      1.3       mrg #ifndef FP_INIT_EXCEPTIONS
    192      1.3       mrg # define FP_INIT_EXCEPTIONS FP_INIT_TRAPPING_EXCEPTIONS
    193      1.1       mrg #endif
    194      1.1       mrg 
    195      1.1       mrg #ifndef FP_HANDLE_EXCEPTIONS
    196      1.3       mrg # define FP_HANDLE_EXCEPTIONS do {} while (0)
    197      1.3       mrg #endif
    198      1.3       mrg 
    199      1.3       mrg /* Whether to flush subnormal inputs to zero with the same sign.  */
    200      1.3       mrg #ifndef FP_DENORM_ZERO
    201      1.3       mrg # define FP_DENORM_ZERO 0
    202      1.1       mrg #endif
    203      1.1       mrg 
    204      1.1       mrg #ifndef FP_INHIBIT_RESULTS
    205      1.1       mrg /* By default we write the results always.
    206      1.3       mrg    sfp-machine may override this and e.g.
    207      1.3       mrg    check if some exceptions are unmasked
    208      1.3       mrg    and inhibit it in such a case.  */
    209      1.3       mrg # define FP_INHIBIT_RESULTS 0
    210      1.1       mrg #endif
    211      1.1       mrg 
    212      1.1       mrg #define FP_SET_EXCEPTION(ex)				\
    213      1.1       mrg   _fex |= (ex)
    214      1.1       mrg 
    215      1.1       mrg #define FP_CUR_EXCEPTIONS				\
    216      1.1       mrg   (_fex)
    217      1.1       mrg 
    218      1.1       mrg #ifndef FP_TRAPPING_EXCEPTIONS
    219      1.3       mrg # define FP_TRAPPING_EXCEPTIONS 0
    220      1.1       mrg #endif
    221      1.1       mrg 
    222      1.3       mrg /* A file using soft-fp may define FP_NO_EXCEPTIONS before including
    223      1.3       mrg    soft-fp.h to indicate that, although a macro used there could raise
    224      1.3       mrg    exceptions, or do rounding and potentially thereby raise
    225      1.3       mrg    exceptions, for some arguments, for the particular arguments used
    226      1.3       mrg    in that file no exceptions or rounding can occur.  Such a file
    227      1.3       mrg    should not itself use macros relating to handling exceptions and
    228      1.3       mrg    rounding modes; this is only for indirect uses (in particular, in
    229      1.3       mrg    _FP_FROM_INT and the macros it calls).  */
    230      1.3       mrg #ifdef FP_NO_EXCEPTIONS
    231      1.3       mrg 
    232      1.3       mrg # undef FP_SET_EXCEPTION
    233      1.3       mrg # define FP_SET_EXCEPTION(ex) do {} while (0)
    234      1.1       mrg 
    235      1.3       mrg # undef FP_CUR_EXCEPTIONS
    236      1.3       mrg # define FP_CUR_EXCEPTIONS 0
    237      1.3       mrg 
    238      1.3       mrg # undef FP_TRAPPING_EXCEPTIONS
    239      1.3       mrg # define FP_TRAPPING_EXCEPTIONS 0
    240      1.3       mrg 
    241      1.3       mrg # undef FP_ROUNDMODE
    242      1.3       mrg # define FP_ROUNDMODE FP_RND_ZERO
    243      1.3       mrg 
    244      1.3       mrg # undef _FP_TININESS_AFTER_ROUNDING
    245      1.3       mrg # define _FP_TININESS_AFTER_ROUNDING 0
    246      1.3       mrg 
    247      1.3       mrg #endif
    248      1.3       mrg 
    249      1.3       mrg /* A file using soft-fp may define FP_NO_EXACT_UNDERFLOW before
    250      1.3       mrg    including soft-fp.h to indicate that, although a macro used there
    251      1.3       mrg    could allow for the case of exact underflow requiring the underflow
    252      1.3       mrg    exception to be raised if traps are enabled, for the particular
    253      1.3       mrg    arguments used in that file no exact underflow can occur.  */
    254      1.3       mrg #ifdef FP_NO_EXACT_UNDERFLOW
    255      1.3       mrg # undef FP_TRAPPING_EXCEPTIONS
    256      1.3       mrg # define FP_TRAPPING_EXCEPTIONS 0
    257      1.3       mrg #endif
    258      1.3       mrg 
    259      1.3       mrg #define _FP_ROUND_NEAREST(wc, X)				\
    260      1.3       mrg   do								\
    261      1.3       mrg     {								\
    262      1.3       mrg       if ((_FP_FRAC_LOW_##wc (X) & 15) != _FP_WORK_ROUND)	\
    263      1.3       mrg 	_FP_FRAC_ADDI_##wc (X, _FP_WORK_ROUND);			\
    264      1.3       mrg     }								\
    265      1.3       mrg   while (0)
    266      1.3       mrg 
    267      1.3       mrg #define _FP_ROUND_ZERO(wc, X)		(void) 0
    268      1.1       mrg 
    269      1.1       mrg #define _FP_ROUND_PINF(wc, X)				\
    270      1.3       mrg   do							\
    271      1.3       mrg     {							\
    272      1.3       mrg       if (!X##_s && (_FP_FRAC_LOW_##wc (X) & 7))	\
    273      1.3       mrg 	_FP_FRAC_ADDI_##wc (X, _FP_WORK_LSB);		\
    274      1.3       mrg     }							\
    275      1.3       mrg   while (0)
    276      1.3       mrg 
    277      1.3       mrg #define _FP_ROUND_MINF(wc, X)			\
    278      1.3       mrg   do						\
    279      1.3       mrg     {						\
    280      1.3       mrg       if (X##_s && (_FP_FRAC_LOW_##wc (X) & 7))	\
    281      1.3       mrg 	_FP_FRAC_ADDI_##wc (X, _FP_WORK_LSB);	\
    282      1.3       mrg     }						\
    283      1.3       mrg   while (0)
    284      1.1       mrg 
    285      1.1       mrg #define _FP_ROUND(wc, X)			\
    286      1.3       mrg   do						\
    287      1.3       mrg     {						\
    288      1.3       mrg       if (_FP_FRAC_LOW_##wc (X) & 7)		\
    289      1.3       mrg 	{					\
    290      1.3       mrg 	  FP_SET_EXCEPTION (FP_EX_INEXACT);	\
    291      1.3       mrg 	  switch (FP_ROUNDMODE)			\
    292      1.3       mrg 	    {					\
    293      1.3       mrg 	    case FP_RND_NEAREST:		\
    294      1.3       mrg 	      _FP_ROUND_NEAREST (wc, X);	\
    295      1.3       mrg 	      break;				\
    296      1.3       mrg 	    case FP_RND_ZERO:			\
    297      1.3       mrg 	      _FP_ROUND_ZERO (wc, X);		\
    298      1.3       mrg 	      break;				\
    299      1.3       mrg 	    case FP_RND_PINF:			\
    300      1.3       mrg 	      _FP_ROUND_PINF (wc, X);		\
    301      1.3       mrg 	      break;				\
    302      1.3       mrg 	    case FP_RND_MINF:			\
    303      1.3       mrg 	      _FP_ROUND_MINF (wc, X);		\
    304      1.3       mrg 	      break;				\
    305      1.3       mrg 	    }					\
    306      1.3       mrg 	}					\
    307      1.3       mrg     }						\
    308      1.3       mrg   while (0)
    309      1.1       mrg 
    310      1.1       mrg #define FP_CLS_NORMAL		0
    311      1.1       mrg #define FP_CLS_ZERO		1
    312      1.1       mrg #define FP_CLS_INF		2
    313      1.1       mrg #define FP_CLS_NAN		3
    314      1.1       mrg 
    315      1.3       mrg #define _FP_CLS_COMBINE(x, y)	(((x) << 2) | (y))
    316      1.1       mrg 
    317      1.1       mrg #include "op-1.h"
    318      1.1       mrg #include "op-2.h"
    319      1.1       mrg #include "op-4.h"
    320      1.1       mrg #include "op-8.h"
    321      1.1       mrg #include "op-common.h"
    322      1.1       mrg 
    323      1.1       mrg /* Sigh.  Silly things longlong.h needs.  */
    324      1.1       mrg #define UWtype		_FP_W_TYPE
    325      1.1       mrg #define W_TYPE_SIZE	_FP_W_TYPE_SIZE
    326      1.1       mrg 
    327      1.3       mrg typedef int QItype __attribute__ ((mode (QI)));
    328      1.3       mrg typedef int SItype __attribute__ ((mode (SI)));
    329      1.3       mrg typedef int DItype __attribute__ ((mode (DI)));
    330      1.3       mrg typedef unsigned int UQItype __attribute__ ((mode (QI)));
    331      1.3       mrg typedef unsigned int USItype __attribute__ ((mode (SI)));
    332      1.3       mrg typedef unsigned int UDItype __attribute__ ((mode (DI)));
    333      1.1       mrg #if _FP_W_TYPE_SIZE == 32
    334      1.3       mrg typedef unsigned int UHWtype __attribute__ ((mode (HI)));
    335      1.1       mrg #elif _FP_W_TYPE_SIZE == 64
    336      1.1       mrg typedef USItype UHWtype;
    337      1.1       mrg #endif
    338      1.1       mrg 
    339      1.1       mrg #ifndef CMPtype
    340      1.3       mrg # define CMPtype	int
    341      1.1       mrg #endif
    342      1.1       mrg 
    343      1.3       mrg #define SI_BITS		(__CHAR_BIT__ * (int) sizeof (SItype))
    344      1.3       mrg #define DI_BITS		(__CHAR_BIT__ * (int) sizeof (DItype))
    345      1.1       mrg 
    346      1.1       mrg #ifndef umul_ppmm
    347      1.3       mrg # ifdef _LIBC
    348      1.3       mrg #  include <stdlib/longlong.h>
    349      1.3       mrg # else
    350      1.3       mrg #  include "longlong.h"
    351      1.3       mrg # endif
    352      1.1       mrg #endif
    353      1.1       mrg 
    354  1.3.4.1  christos #endif /* !SOFT_FP_H */
    355