big-endian double packing pasted by wasamasa on Thu Nov 19 13:36:44 2020
(import scheme) (import (chicken base)) (import (chicken bitwise)) (import (chicken foreign)) (import (srfi 4)) (define double->uint64 (foreign-lambda* unsigned-integer64 ((double d)) "uint64_t dest; memcpy(&dest, &d, sizeof(d)); C_return(dest);")) (define (uint64->u8vector-big i) (u8vector (bitwise-and (arithmetic-shift i -56) #xff) (bitwise-and (arithmetic-shift i -48) #xff) (bitwise-and (arithmetic-shift i -40) #xff) (bitwise-and (arithmetic-shift i -32) #xff) (bitwise-and (arithmetic-shift i -24) #xff) (bitwise-and (arithmetic-shift i -16) #xff) (bitwise-and (arithmetic-shift i -8) #xff) (bitwise-and i #xff))) (for-each (lambda (double) (let* ((integer64 (double->uint64 double)) (fraction (bitwise-and integer64 #xfffffffffffff)) (exponent (bitwise-and (arithmetic-shift integer64 -52) #x7ff)) (sign (if (negative? double) '- '+)) (binary (uint64->u8vector-big integer64))) (write (list double (u8vector->list binary) sign exponent fraction)) (newline))) (list (atan -1) (atan 1) (expt 2.0 64) (- (expt 2.0 64))))
big-endian double packing (python) pasted by wasamasa on Thu Nov 19 13:37:11 2020
from math import atan import struct for double in [atan(-1), atan(1), 2.0**64, -2.0**64]: binary = struct.pack('>d', double) integer64 = int.from_bytes(binary, 'big') fraction = integer64 & 0xfffffffffffff exponent = (integer64 >> 52) & 0x7ff sign = '-' if double < 0 else '+' print([double, list(binary), sign, exponent, fraction])
big-endian double packing (output) added by wasamasa on Thu Nov 19 13:37:39 2020
; ; csc tmp/test.scm && tmp/test (-0.785398163397448 (191 233 33 251 84 68 45 24) - 1022 2570638124657944) (0.785398163397448 (63 233 33 251 84 68 45 24) + 1022 2570638124657944) (1.84467440737096e+19 (67 240 0 0 0 0 0 0) + 1087 0) (-1.84467440737096e+19 (195 240 0 0 0 0 0 0) - 1087 0) ; ; python tmp/test.py [-0.7853981633974483, [191, 233, 33, 251, 84, 68, 45, 24], '-', 1022, 2570638124657944] [0.7853981633974483, [63, 233, 33, 251, 84, 68, 45, 24], '+', 1022, 2570638124657944] [1.8446744073709552e+19, [67, 240, 0, 0, 0, 0, 0, 0], '+', 1087, 0] [-1.8446744073709552e+19, [195, 240, 0, 0, 0, 0, 0, 0], '-', 1087, 0]