Leopold Toetsch (via RT) wrote:

# New Ticket Created by Leopold Toetsch # Please include the string: [perl #20597]
# in the subject line of all future correspondence about this issue. # <URL: http://rt.perl.org/rt2/Ticket/Display.html?id=20597 >
Here is a corrected version, which handles obscure things like nan, +/-inf and 0 ;-)

static void cvt_num12_num8(unsigned char *dest, unsigned char *src)
{
int expo, i, s;

memset (dest, 0, 8);
/* exponents 15 -> 11 bits */
s = src[9] & 0x80; /* sign */
expo = ((src[9] & 0x7f)<< 8 | src[8]);
if (expo == 0) {
nul:
if (s)
dest[7] |= 0x80;
return;
}
expo -= 16383; /* - bias */
expo += 1023; /* + bias 8byte */
if (expo <= 0) /* underflow */
goto nul;
if (expo > 0x7ff) { /* inf/nan */
dest[7] = 0x7f;
dest[6] = src[7] == 0xc0 ? 0xf8 : 0xf0 ;
goto nul;
}
expo <<= 4;
dest[6] = (expo & 0xff);
dest[7] = (expo & 0x7f00) >> 8;
if (s)
dest[7] |= 0x80;
/* long double frac 63 bits => 52 bits
src[7] &= 0x7f; reset integer bit */
for (i = 0; i < 6; i++) {
dest[i+1] |= (i==5 ? src[7]&0x7f : src[i+2]) >> 3;
dest[i] |= (src[i+2] & 0x1f) << 5;
}
dest[0] |= src[1] >> 3;
}

leo


Reply via email to