> -----Original Message----- > From: piclist-bounces@mit.edu [mailto:piclist-bounces@mit.edu] On Behalf > Of Walter Banks > Sent: 28 May 2008 19:53 > To: Microcontroller discussion list - Public. > Subject: Re: [PIC] Custom floating point calculation > > > > Michael Rigby-Jones wrote: > > > > -----Original Message----- > > > From: piclist-bounces@mit.edu [mailto:piclist-bounces@mit.edu] On > > Behalf > > > Of Timothy Weber > > > Sent: 28 May 2008 18:06 > > > To: Microcontroller discussion list - Public. > > > Subject: Re: [PIC] Custom floating point calculation > > > > > > Harold Hallikainen wrote: > > > > If you swap the nybbles so the > > > > exponent is in the high half, do you end up anywhere near a steadily > > > > increasing order? > > > > > > I think Harold is right (and Walter is almost right) - if you swap > > > nibbles first, you can simply compare x and y directly. > > > > Sadly my spreadsheet says no (unless it has a fatal error of course, > > quite possible). > > > > e.g. the following values are ordered by "swapped nibble"(SN) value: > > > > Hex Float SN > > 0x10 0.0625 0x01 > > 0xE0 0.9375 0x0E > > 0x01 0.0 0x10 > > 0xE1 0.09375 0x1E > > > > Note that the largest floating point value (0.9375) gives a SN result > > between the others, even though they have a lower floating point value. > > > > I have attached the spreadsheet if anyone is interested. > > > > Regards > > > > Mike > > Nybble swap xor with 0xF0 will put the numbers so they can be compared Thanks Walter, that almost works. The only snag it gives incorrect results on floating points that have differing exponents and mantissa but the same value e.g. 0x1E = 1/16 * 10E-14 = 6.25*10E-16 XOR = 0x11 0xAF = 15/16 * 10E-15 = 6.25*10E-16 XOR = 0x0A This is reasonably easily fixed by checking if the mantissa is == 1 and the exponent < 15, if so multiply the mantissa by 10 and increment the exponent. In reality no multiply is needed, just replace 1 with 10. A totally unoptimised function (just for clarity) that proves this technique is below. With a bit of optimisation this should compile down to a small handful of assembler operations. unsigned char ber_weight( unsigned char ber ) { unsigned char mantissa; unsigned char exponent; mantissa = ber >> 4; exponent = ber & 0x0F; /* If mantissa is zero result is always zero */ if( mantissa == 0 ) { return 0; } /* Eliminate duplicate value cases */ if( ( mantissa == 1 ) && ( exponent < 15 ) ) { mantissa *= 10; exponent += 1; } /* Invert the exponent and recombine nibbles in reverse order. */ exponent ^= 0x0F; return ( exponent << 4 ) | mantissa; } Thanks again, I knew the PIC list would yield results! Regards Mike ======================================================================= This e-mail is intended for the person it is addressed to only. The information contained in it may be confidential and/or protected by law. If you are not the intended recipient of this message, you must not make any use of this information, or copy or show it to any person. Please contact us immediately to tell us that you have received this e-mail, and return the original to us. Any use, forwarding, printing or copying of this message is strictly prohibited. No part of this message can be considered a request for goods or services. ======================================================================= -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist