Created
May 11, 2014 02:23
-
-
Save ityonemo/88b110688078a17982c0 to your computer and use it in GitHub Desktop.
try 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
typedef struct { | |
signed int exp:4; | |
signed int val:4; | |
} minifloat; | |
float to_float(minifloat m){ | |
/* generate a product accumulator */ | |
float acc = 1.0; | |
acc *= m.val; | |
acc *= (m.exp >= 0) ? (1 << m.exp) : (1 / ((float) (1 << (-m.exp)))); | |
/* return the accumulator */ | |
return acc; | |
} | |
minifloat add_inv(minifloat m){ | |
minifloat t = m; | |
t.val *= -1; | |
return t; | |
} | |
minifloat add(minifloat m, n){ | |
/*first find the more significant floating point value*/ | |
signed int shift = m.exp - n.exp; | |
minifloat res = (shift >= 0) ? m : n; | |
if (shift >= 0) | |
res.val += n.val / (1 << shift); | |
else | |
res.val += m.val / (1 << shift); | |
return res; | |
} | |
main() | |
{ | |
minifloat m; | |
m.exp = -3; | |
m.val = 5; | |
minifloat n = {2, 3} | |
printf("%6.3f inverted is %6.3f\n", to_float(m), to_float(add_inv(m))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment