1  /* Copyright (C) 1992-2023 Free Software Foundation, Inc.
       2     This file is part of the GNU C Library.
       3  
       4     The GNU C Library is free software; you can redistribute it and/or
       5     modify it under the terms of the GNU Lesser General Public
       6     License as published by the Free Software Foundation; either
       7     version 2.1 of the License, or (at your option) any later version.
       8  
       9     The GNU C Library is distributed in the hope that it will be useful,
      10     but WITHOUT ANY WARRANTY; without even the implied warranty of
      11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12     Lesser General Public License for more details.
      13  
      14     You should have received a copy of the GNU Lesser General Public
      15     License along with the GNU C Library.  If not, see
      16     <https://www.gnu.org/licenses/>.  */
      17  
      18  #ifndef _IEEE754_H
      19  #define _IEEE754_H 1
      20  
      21  #include <features.h>
      22  
      23  #include <bits/endian.h>
      24  
      25  #ifndef __LDBL_MANT_DIG__
      26  # include <float.h>
      27  # define __LDBL_MANT_DIG__ LDBL_MANT_DIG
      28  #endif
      29  
      30  __BEGIN_DECLS
      31  
      32  union ieee754_float
      33    {
      34      float f;
      35  
      36      /* This is the IEEE 754 single-precision format.  */
      37      struct
      38        {
      39  #if	__BYTE_ORDER == __BIG_ENDIAN
      40  	unsigned int negative:1;
      41  	unsigned int exponent:8;
      42  	unsigned int mantissa:23;
      43  #endif				/* Big endian.  */
      44  #if	__BYTE_ORDER == __LITTLE_ENDIAN
      45  	unsigned int mantissa:23;
      46  	unsigned int exponent:8;
      47  	unsigned int negative:1;
      48  #endif				/* Little endian.  */
      49        } ieee;
      50  
      51      /* This format makes it easier to see if a NaN is a signalling NaN.  */
      52      struct
      53        {
      54  #if	__BYTE_ORDER == __BIG_ENDIAN
      55  	unsigned int negative:1;
      56  	unsigned int exponent:8;
      57  	unsigned int quiet_nan:1;
      58  	unsigned int mantissa:22;
      59  #endif				/* Big endian.  */
      60  #if	__BYTE_ORDER == __LITTLE_ENDIAN
      61  	unsigned int mantissa:22;
      62  	unsigned int quiet_nan:1;
      63  	unsigned int exponent:8;
      64  	unsigned int negative:1;
      65  #endif				/* Little endian.  */
      66        } ieee_nan;
      67    };
      68  
      69  #define IEEE754_FLOAT_BIAS	0x7f /* Added to exponent.  */
      70  
      71  
      72  union ieee754_double
      73    {
      74      double d;
      75  
      76      /* This is the IEEE 754 double-precision format.  */
      77      struct
      78        {
      79  #if	__BYTE_ORDER == __BIG_ENDIAN
      80  	unsigned int negative:1;
      81  	unsigned int exponent:11;
      82  	/* Together these comprise the mantissa.  */
      83  	unsigned int mantissa0:20;
      84  	unsigned int mantissa1:32;
      85  #endif				/* Big endian.  */
      86  #if	__BYTE_ORDER == __LITTLE_ENDIAN
      87  # if	__FLOAT_WORD_ORDER == __BIG_ENDIAN
      88  	unsigned int mantissa0:20;
      89  	unsigned int exponent:11;
      90  	unsigned int negative:1;
      91  	unsigned int mantissa1:32;
      92  # else
      93  	/* Together these comprise the mantissa.  */
      94  	unsigned int mantissa1:32;
      95  	unsigned int mantissa0:20;
      96  	unsigned int exponent:11;
      97  	unsigned int negative:1;
      98  # endif
      99  #endif				/* Little endian.  */
     100        } ieee;
     101  
     102      /* This format makes it easier to see if a NaN is a signalling NaN.  */
     103      struct
     104        {
     105  #if	__BYTE_ORDER == __BIG_ENDIAN
     106  	unsigned int negative:1;
     107  	unsigned int exponent:11;
     108  	unsigned int quiet_nan:1;
     109  	/* Together these comprise the mantissa.  */
     110  	unsigned int mantissa0:19;
     111  	unsigned int mantissa1:32;
     112  #else
     113  # if	__FLOAT_WORD_ORDER == __BIG_ENDIAN
     114  	unsigned int mantissa0:19;
     115  	unsigned int quiet_nan:1;
     116  	unsigned int exponent:11;
     117  	unsigned int negative:1;
     118  	unsigned int mantissa1:32;
     119  # else
     120  	/* Together these comprise the mantissa.  */
     121  	unsigned int mantissa1:32;
     122  	unsigned int mantissa0:19;
     123  	unsigned int quiet_nan:1;
     124  	unsigned int exponent:11;
     125  	unsigned int negative:1;
     126  # endif
     127  #endif
     128        } ieee_nan;
     129    };
     130  
     131  #define IEEE754_DOUBLE_BIAS	0x3ff /* Added to exponent.  */
     132  
     133  #if __LDBL_MANT_DIG__ == 113
     134  
     135  union ieee854_long_double
     136    {
     137      long double d;
     138  
     139      /* This is the IEEE 854 quad-precision format.  */
     140      struct
     141        {
     142  #if	__BYTE_ORDER == __BIG_ENDIAN
     143  	unsigned int negative:1;
     144  	unsigned int exponent:15;
     145  	/* Together these comprise the mantissa.  */
     146  	unsigned int mantissa0:16;
     147  	unsigned int mantissa1:32;
     148  	unsigned int mantissa2:32;
     149  	unsigned int mantissa3:32;
     150  #endif				/* Big endian.  */
     151  #if	__BYTE_ORDER == __LITTLE_ENDIAN
     152  	/* Together these comprise the mantissa.  */
     153  	unsigned int mantissa3:32;
     154  	unsigned int mantissa2:32;
     155  	unsigned int mantissa1:32;
     156  	unsigned int mantissa0:16;
     157  	unsigned int exponent:15;
     158  	unsigned int negative:1;
     159  #endif				/* Little endian.  */
     160        } ieee;
     161  
     162      /* This format makes it easier to see if a NaN is a signalling NaN.  */
     163      struct
     164        {
     165  #if	__BYTE_ORDER == __BIG_ENDIAN
     166  	unsigned int negative:1;
     167  	unsigned int exponent:15;
     168  	unsigned int quiet_nan:1;
     169  	/* Together these comprise the mantissa.  */
     170  	unsigned int mantissa0:15;
     171  	unsigned int mantissa1:32;
     172  	unsigned int mantissa2:32;
     173  	unsigned int mantissa3:32;
     174  #endif				/* Big endian.  */
     175  #if	__BYTE_ORDER == __LITTLE_ENDIAN
     176  	/* Together these comprise the mantissa.  */
     177  	unsigned int mantissa3:32;
     178  	unsigned int mantissa2:32;
     179  	unsigned int mantissa1:32;
     180  	unsigned int mantissa0:15;
     181  	unsigned int quiet_nan:1;
     182  	unsigned int exponent:15;
     183  	unsigned int negative:1;
     184  #endif				/* Little endian.  */
     185        } ieee_nan;
     186    };
     187  
     188  #define IEEE854_LONG_DOUBLE_BIAS 0x3fff /* Added to exponent.  */
     189  
     190  #elif __LDBL_MANT_DIG__ == 64
     191  
     192  union ieee854_long_double
     193    {
     194      long double d;
     195  
     196      /* This is the IEEE 854 double-extended-precision format.  */
     197      struct
     198        {
     199  #if	__BYTE_ORDER == __BIG_ENDIAN
     200  	unsigned int negative:1;
     201  	unsigned int exponent:15;
     202  	unsigned int empty:16;
     203  	unsigned int mantissa0:32;
     204  	unsigned int mantissa1:32;
     205  #endif
     206  #if	__BYTE_ORDER == __LITTLE_ENDIAN
     207  # if	__FLOAT_WORD_ORDER == __BIG_ENDIAN
     208  	unsigned int exponent:15;
     209  	unsigned int negative:1;
     210  	unsigned int empty:16;
     211  	unsigned int mantissa0:32;
     212  	unsigned int mantissa1:32;
     213  # else
     214  	unsigned int mantissa1:32;
     215  	unsigned int mantissa0:32;
     216  	unsigned int exponent:15;
     217  	unsigned int negative:1;
     218  	unsigned int empty:16;
     219  # endif
     220  #endif
     221        } ieee;
     222  
     223      /* This is for NaNs in the IEEE 854 double-extended-precision format.  */
     224      struct
     225        {
     226  #if	__BYTE_ORDER == __BIG_ENDIAN
     227  	unsigned int negative:1;
     228  	unsigned int exponent:15;
     229  	unsigned int empty:16;
     230  	unsigned int one:1;
     231  	unsigned int quiet_nan:1;
     232  	unsigned int mantissa0:30;
     233  	unsigned int mantissa1:32;
     234  #endif
     235  #if	__BYTE_ORDER == __LITTLE_ENDIAN
     236  # if	__FLOAT_WORD_ORDER == __BIG_ENDIAN
     237  	unsigned int exponent:15;
     238  	unsigned int negative:1;
     239  	unsigned int empty:16;
     240  	unsigned int mantissa0:30;
     241  	unsigned int quiet_nan:1;
     242  	unsigned int one:1;
     243  	unsigned int mantissa1:32;
     244  # else
     245  	unsigned int mantissa1:32;
     246  	unsigned int mantissa0:30;
     247  	unsigned int quiet_nan:1;
     248  	unsigned int one:1;
     249  	unsigned int exponent:15;
     250  	unsigned int negative:1;
     251  	unsigned int empty:16;
     252  # endif
     253  #endif
     254        } ieee_nan;
     255    };
     256  
     257  #define IEEE854_LONG_DOUBLE_BIAS 0x3fff
     258  
     259  #elif __LDBL_MANT_DIG__ == 53
     260  
     261  union ieee854_long_double
     262    {
     263      long double d;
     264  
     265      /* This is the IEEE 754 double-precision format.  */
     266      struct
     267        {
     268  #if	__BYTE_ORDER == __BIG_ENDIAN
     269  	unsigned int negative:1;
     270  	unsigned int exponent:11;
     271  	/* Together these comprise the mantissa.  */
     272  	unsigned int mantissa0:20;
     273  	unsigned int mantissa1:32;
     274  #endif				/* Big endian.  */
     275  #if	__BYTE_ORDER == __LITTLE_ENDIAN
     276  # if	__FLOAT_WORD_ORDER == __BIG_ENDIAN
     277  	unsigned int mantissa0:20;
     278  	unsigned int exponent:11;
     279  	unsigned int negative:1;
     280  	unsigned int mantissa1:32;
     281  # else
     282  	/* Together these comprise the mantissa.  */
     283  	unsigned int mantissa1:32;
     284  	unsigned int mantissa0:20;
     285  	unsigned int exponent:11;
     286  	unsigned int negative:1;
     287  # endif
     288  #endif				/* Little endian.  */
     289        } ieee;
     290  
     291      /* This format makes it easier to see if a NaN is a signalling NaN.  */
     292      struct
     293        {
     294  #if	__BYTE_ORDER == __BIG_ENDIAN
     295  	unsigned int negative:1;
     296  	unsigned int exponent:11;
     297  	unsigned int quiet_nan:1;
     298  	/* Together these comprise the mantissa.  */
     299  	unsigned int mantissa0:19;
     300  	unsigned int mantissa1:32;
     301  #else
     302  # if	__FLOAT_WORD_ORDER == __BIG_ENDIAN
     303  	unsigned int mantissa0:19;
     304  	unsigned int quiet_nan:1;
     305  	unsigned int exponent:11;
     306  	unsigned int negative:1;
     307  	unsigned int mantissa1:32;
     308  # else
     309  	/* Together these comprise the mantissa.  */
     310  	unsigned int mantissa1:32;
     311  	unsigned int mantissa0:19;
     312  	unsigned int quiet_nan:1;
     313  	unsigned int exponent:11;
     314  	unsigned int negative:1;
     315  # endif
     316  #endif
     317        } ieee_nan;
     318    };
     319  
     320  #define IEEE854_LONG_DOUBLE_BIAS	0x3ff /* Added to exponent.  */
     321  
     322  #endif /* __LDBL_MANT_DIG__ == 53 */
     323  
     324  __END_DECLS
     325  
     326  #endif /* ieee754.h */