From: Klaus Stehle <klaus.stehle@uni-tuebingen.de>
To: 11310@debbugs.gnu.org
Subject: bug#11310: wrong conversions of ieee754 from/to foreign endianness
Date: Sun, 22 Apr 2012 18:44:44 +0200 (CEST) [thread overview]
Message-ID: <Pine.LNX.4.61.1204221838310.16082@commlink.zdv.uni-tuebingen.de> (raw)
Hi,
There is a problem in bytevectors.c - conversions of float and double
to the foreign endianess don't work.
Here is the scheme example:
---------------------------
(use-modules (rnrs bytevectors))
(define bv (make-bytevector 4))
(bytevector-ieee-single-set! bv 0 1.0 (endianness little))
(display bv) (newline)
=> #vu8(0 0 128 63) correct!
(bytevector-ieee-single-set! bv 0 1.0 (endianness big))
(display bv) (newline)
=> #vu8(254 0 0 0) oops!
The results come from a little-endian machine and therefore the
big-endian result is wrong. We expect #vu8(63 128 0 0).
In bytevectors.c there is the following code:
#ifdef WORDS_BIGENDIAN
/* Assuming little endian for both byte and word order. */
target->little_endian.negative = src.big_endian.negative;
target->little_endian.exponent = src.big_endian.exponent;
target->little_endian.mantissa = src.big_endian.mantissa;
#else
target->big_endian.negative = src.little_endian.negative;
target->big_endian.exponent = src.little_endian.exponent;
target->big_endian.mantissa = src.little_endian.mantissa;
#endif
Using the 'FOREIGN' structure of the scm_ieee754_float union
does NOT work, because in this case the structure assumes the
wrong *BIT* order.
example in C shows better (to run on a little endian machine):
--------------------------------------------------------------
union scm_ieee754_float target;
target.f = 0.0;
target.litte_endian.negative = 1;
=> 00 00 00 80 correct!
target.f = 0.0;
target.big_endian.negative = 1;
=> 01 00 00 00 oops!
Maybe this effect depends on the machine and the compiler.
I am using an amd64 with gcc.
The bug should be easy to fix, instead of using the ieee754 unions
and their weird structures, exchanging whole bytes should suffice
and this probably may be faster.
Thanks
Klaus
next reply other threads:[~2012-04-22 16:44 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-04-22 16:44 Klaus Stehle [this message]
2012-04-22 18:44 ` bug#11310: wrong conversions of ieee754 from/to foreign endianness Ludovic Courtès
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/guile/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Pine.LNX.4.61.1204221838310.16082@commlink.zdv.uni-tuebingen.de \
--to=klaus.stehle@uni-tuebingen.de \
--cc=11310@debbugs.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).