Difference between revisions of "User:Chaser Zaks/IEEE-754"
Jump to navigation
Jump to search
Chaser Zaks (talk | contribs) (Created page with "Licensed under public domain ==Encoder== <syntaxhighlight lang="lsl2"> //Todo </syntaxhighlight> ==Decoder== <syntaxhighlight lang="lsl2"> float integerToFloat(integer input...") |
Chaser Zaks (talk | contribs) |
||
(2 intermediate revisions by the same user not shown) | |||
Line 3: | Line 3: | ||
==Encoder== | ==Encoder== | ||
<syntaxhighlight lang="lsl2"> | <syntaxhighlight lang="lsl2"> | ||
// | integer floatToInteger(float input){ | ||
if(input == 0.0) return 0; | |||
integer sign; | |||
float fnorm; | |||
if(input < 0){ | |||
sign = 1; | |||
fnorm = -input; | |||
}else{ | |||
sign = 0; | |||
fnorm = input; | |||
} | |||
integer shift = 0; | |||
while(fnorm++ >= 2.0) fnorm /= 2.0; | |||
while(fnorm-- < 1.0) fnorm *= 2.0; | |||
return (sign<<31) //Sign | |||
| ((shift + ((1<<7) - 1))<<23) //exponent | |||
| (integer)((fnorm - 1.0) * ((float)(1<<23) + 0.5)); //fraction | |||
} | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 19: | Line 39: | ||
return sign * (float)llPow( 2.0, ((input & 0x7f800000) >> 23) - 127) * (val + 1.0); | return sign * (float)llPow( 2.0, ((input & 0x7f800000) >> 23) - 127) * (val + 1.0); | ||
} | |||
</syntaxhighlight> | |||
==Endianness== | |||
<syntaxhighlight lang="lsl2"> | |||
integer swapEndianness(integer input){ | |||
return ((input & 0xFF) << 24) | |||
| ((input & 0xFF00) << 8) | |||
| ((input >> 8) & 0xFF00) | |||
| ((input >> 24) & 0xFF); | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
==Also see== | ==Also see== | ||
{{ | {{{!}} | ||
{{Issue|BUG-139326|llFloatToBase64 and llBase64ToFloat|type=nf|resolution=clo}} | |||
{{!}}} |
Latest revision as of 05:19, 9 November 2017
Licensed under public domain
Encoder
integer floatToInteger(float input){
if(input == 0.0) return 0;
integer sign;
float fnorm;
if(input < 0){
sign = 1;
fnorm = -input;
}else{
sign = 0;
fnorm = input;
}
integer shift = 0;
while(fnorm++ >= 2.0) fnorm /= 2.0;
while(fnorm-- < 1.0) fnorm *= 2.0;
return (sign<<31) //Sign
| ((shift + ((1<<7) - 1))<<23) //exponent
| (integer)((fnorm - 1.0) * ((float)(1<<23) + 0.5)); //fraction
}
Decoder
float integerToFloat(integer input){
integer sign = 1;
if(input & 0x80000000) sign = -1;
integer pow = -1;
float val = 0.0;
integer i = 9;
for(; i < 32; i++)
val += (float)(((input >> 31 - i) & 1)) * (float)llPow(2.0, pow--);
return sign * (float)llPow( 2.0, ((input & 0x7f800000) >> 23) - 127) * (val + 1.0);
}
Endianness
integer swapEndianness(integer input){
return ((input & 0xFF) << 24)
| ((input & 0xFF00) << 8)
| ((input >> 8) & 0xFF00)
| ((input >> 24) & 0xFF);
}
Also see
BUG-139326 | llFloatToBase64 and llBase64ToFloat |