├── float.h ├── README.md ├── LICENSE └── float.c /float.h: -------------------------------------------------------------------------------- 1 | #ifndef IEE754_FLOAT_H 2 | #define IEE754_FLOAT_H 3 | 4 | void IEE754_binary64_encode( double x, char out[8] ); 5 | double IEE754_binary64_decode( char out[8] ); 6 | void IEE754_binary32_encode( float x, char out[4] ); 7 | float IEE754_binary32_decode( char out[4] ); 8 | 9 | #endif -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # IEEE754_binary_encoder 2 | A C library for converting float and double values to binary 3 | 4 | Functions 5 | --------- 6 | 7 | It contains the following function prototypes: 8 | 9 | ```c 10 | void IEE754_binary64_encode( double, char[8] ); 11 | double IEE754_binary64_decode( char[8] ); 12 | void IEE754_binary32_encode( float, char[4] ); 13 | float IEE754_binary32_decode( char[4] ); 14 | ``` 15 | 16 | Limitations 17 | ----------- 18 | 19 | Subnormal floating point values (values really close to zero) are truncated to zero for simplicity reasons. 20 | I may change this someday. 21 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Daniel Abrecht 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /float.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "float.h" 5 | 6 | void IEE754_binary64_encode( double x, char out[8] ){ 7 | bool sign = x < 0; 8 | uint16_t exponent; 9 | uint64_t fraction; 10 | if( isinf( x ) ){ 11 | exponent = 0x7FF; 12 | fraction = 0; 13 | }else if( isnan( x ) ){ // nan check 14 | exponent = 0x7FF; 15 | fraction = 0xFFFFFFFFFFFFF; 16 | }else{ 17 | if(sign) 18 | x = -x; 19 | int e = 0; 20 | fraction = frexp( x, &e ) * ((uint64_t)2<<52); 21 | exponent = e + 1022; 22 | if( exponent > 0x7FF ){ 23 | exponent = 0x7FF; 24 | fraction = 0; 25 | } 26 | } 27 | out[0] = ( ( sign << 7 ) & 0x80 ) 28 | | ( ( exponent >> 4 ) & 0x7F ); 29 | out[1] = ( ( exponent << 4 ) & 0xF0 ) 30 | | ( ( fraction >> 48 ) & 0x0F ); 31 | out[2] = ( fraction >> 40 ) & 0xFF; 32 | out[3] = ( fraction >> 32 ) & 0xFF; 33 | out[4] = ( fraction >> 24 ) & 0xFF; 34 | out[5] = ( fraction >> 16 ) & 0xFF; 35 | out[6] = ( fraction >> 8 ) & 0xFF; 36 | out[7] = fraction & 0xFF; 37 | } 38 | 39 | double IEE754_binary64_decode( char out[8] ){ 40 | bool sign = out[0] & 0x80; 41 | uint16_t exponent = ( ( out[0] << 4 ) & 0x7F0 ) 42 | | ( ( out[1] >> 4 ) & 0x0F ); 43 | uint64_t fraction = ( (uint64_t)( out[1] & 0x0F ) << 48 ) 44 | | ( (uint64_t)( out[2] & 0xFF ) << 40 ) 45 | | ( (uint64_t)( out[3] & 0xFF ) << 32 ) 46 | | ( (uint64_t)( out[4] & 0xFF ) << 24 ) 47 | | ( (uint64_t)( out[5] & 0xFF ) << 16 ) 48 | | ( (uint64_t)( out[6] & 0xFF ) << 8 ) 49 | | (uint64_t)( out[7] & 0xFF ) 50 | | ( (uint64_t)1<<52 ); 51 | double frac = (double)fraction / ( (uint64_t)2<<52 ); 52 | if( exponent == 0x7FF ){ 53 | if( fraction == (uint64_t)1<<52 ){ // Infinity 54 | return sign ? -1.0/0.0 : 1.0/0.0; 55 | }else{ // NaN 56 | return sign ? 0.0/0.0 : -(0.0/0.0); 57 | } 58 | } 59 | return ldexp( frac, exponent-1022 ) * ( sign ? -1 : 1 ); 60 | } 61 | 62 | void IEE754_binary32_encode( float x, char out[4] ){ 63 | bool sign = x < 0; 64 | uint8_t exponent; 65 | uint32_t fraction; 66 | if( isinf( x ) ){ 67 | exponent = 0xFF; 68 | fraction = 0; 69 | }else if( isnan( x ) ){ // nan check 70 | exponent = 0xFF; 71 | fraction = 0x7FFFFF; 72 | }else{ 73 | if(sign) 74 | x = -x; 75 | int e = 0; 76 | fraction = frexp( x, &e ) * ((uint32_t)2<<23); 77 | exponent = e + 126; 78 | if( e + 126 > 0xFF ){ 79 | exponent = 0xFF; 80 | fraction = 0; 81 | } 82 | } 83 | out[0] = ( ( sign << 7 ) & 0x80 ) 84 | | ( ( exponent >> 1 ) & 0x7F ); 85 | out[1] = ( ( exponent << 7 ) & 0x80 ) 86 | | ( ( fraction >> 16 ) & 0x7F ); 87 | out[2] = ( fraction >> 8 ) & 0xFF; 88 | out[3] = fraction & 0xFF; 89 | } 90 | 91 | float IEE754_binary32_decode( char out[4] ){ 92 | bool sign = out[0] & 0x80; 93 | uint8_t exponent = ( ( out[0] << 1 ) & 0xFE ) 94 | | ( ( out[1] >> 7 ) & 0x01 ); 95 | uint32_t fraction = ( (uint32_t)( out[1] & 0x7F ) << 16 ) 96 | | ( (uint32_t)( out[2] & 0xFF ) << 8 ) 97 | | (uint32_t)( out[3] & 0xFF ) 98 | | ( (uint32_t)1<<23 ); 99 | float frac = (float)fraction / ( (uint32_t)2<<23 ); 100 | if( exponent == 0xFF ){ 101 | if( fraction == (uint32_t)1<<23 ){ // Infinity 102 | return sign ? -1.0/0.0 : 1.0/0.0; 103 | }else{ // NaN 104 | return sign ? 0.0/0.0 : -(0.0/0.0); 105 | } 106 | } 107 | return ldexp( frac, exponent-126 ) * ( sign ? -1 : 1 ); 108 | } 109 | 110 | --------------------------------------------------------------------------------