1 /* ieee-utils/make_rep.c
3 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Brian Gough
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or (at
8 * your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include <gsl/gsl_ieee_utils.h>
24 #include "standardize.c"
26 static void sprint_nybble(int i, char *s) ;
27 static void sprint_byte(int i, char *s) ;
28 static int determine_ieee_type (int non_zero, int exponent, int max_exponent);
31 /* For the IEEE float format the bits are found from the following
36 mantisssa = 0x007fffff
38 For the IEEE double format the masks are,
40 sign = 0x8000000000000000
41 exponent = 0x7ff0000000000000
42 mantissa = 0x000fffffffffffff
47 gsl_ieee_float_to_rep (const float * x, gsl_ieee_float_rep * r)
54 unsigned char byte[4] ;
60 if (little_endian_p())
61 make_float_bigendian(&(u.f)) ;
63 /* note that r->sign is signed, u.ieee.byte is unsigned */
65 if (u.ieee.byte[3]>>7)
74 e = (u.ieee.byte[3] & 0x7f) << 1 | (u.ieee.byte[2] & 0x80)>>7 ;
76 r->exponent = e - 127 ;
78 sprint_byte((u.ieee.byte[2] & 0x7f) << 1,r->mantissa) ;
79 sprint_byte(u.ieee.byte[1],r->mantissa + 7) ;
80 sprint_byte(u.ieee.byte[0],r->mantissa + 15) ;
82 r->mantissa[23] = '\0' ;
84 non_zero = u.ieee.byte[0] || u.ieee.byte[1] || (u.ieee.byte[2] & 0x7f);
86 r->type = determine_ieee_type (non_zero, e, 255) ;
90 gsl_ieee_double_to_rep (const double * x, gsl_ieee_double_rep * r)
99 unsigned char byte[8];
105 if (little_endian_p())
106 make_double_bigendian(&(u.d)) ;
108 /* note that r->sign is signed, u.ieee.byte is unsigned */
110 if (u.ieee.byte[7]>>7)
120 e =(u.ieee.byte[7] & 0x7f)<<4 ^ (u.ieee.byte[6] & 0xf0)>>4 ;
122 r->exponent = e - 1023 ;
124 sprint_nybble(u.ieee.byte[6],r->mantissa) ;
125 sprint_byte(u.ieee.byte[5],r->mantissa + 4) ;
126 sprint_byte(u.ieee.byte[4],r->mantissa + 12) ;
127 sprint_byte(u.ieee.byte[3],r->mantissa + 20) ;
128 sprint_byte(u.ieee.byte[2],r->mantissa + 28) ;
129 sprint_byte(u.ieee.byte[1],r->mantissa + 36) ;
130 sprint_byte(u.ieee.byte[0],r->mantissa + 44) ;
132 r->mantissa[52] = '\0' ;
134 non_zero = (u.ieee.byte[0] || u.ieee.byte[1] || u.ieee.byte[2]
135 || u.ieee.byte[3] || u.ieee.byte[4] || u.ieee.byte[5]
136 || (u.ieee.byte[6] & 0x0f)) ;
138 r->type = determine_ieee_type (non_zero, e, 2047) ;
141 /* A table of character representations of nybbles */
143 static char nybble[16][5]={ /* include space for the \0 */
144 "0000", "0001", "0010", "0011",
145 "0100", "0101", "0110", "0111",
146 "1000", "1001", "1010", "1011",
147 "1100", "1101", "1110", "1111"
151 sprint_nybble(int i, char *s)
155 *s=c[0] ; *(s+1)=c[1] ; *(s+2)=c[2] ; *(s+3)=c[3] ;
159 sprint_byte(int i, char *s)
162 c=nybble[(i & 0xf0)>>4];
163 *s=c[0] ; *(s+1)=c[1] ; *(s+2)=c[2] ; *(s+3)=c[3] ;
165 *(s+4)=c[0] ; *(s+5)=c[1] ; *(s+6)=c[2] ; *(s+7)=c[3] ;
169 determine_ieee_type (int non_zero, int exponent, int max_exponent)
171 if (exponent == max_exponent)
175 return GSL_IEEE_TYPE_NAN ;
179 return GSL_IEEE_TYPE_INF ;
182 else if (exponent == 0)
186 return GSL_IEEE_TYPE_DENORMAL ;
190 return GSL_IEEE_TYPE_ZERO ;
195 return GSL_IEEE_TYPE_NORMAL ;