Skip to content

Instantly share code, notes, and snippets.

@pmatos
Created April 4, 2024 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmatos/380e369e4af9e4f642b0361a5ecb4e4d to your computer and use it in GitHub Desktop.
Save pmatos/380e369e4af9e4f642b0361a5ecb4e4d to your computer and use it in GitHub Desktop.
f80 to decimal conversion
#lang racket
(define one #x3fff8000000000000000)
(define two #x40008000000000000000)
(define three #x4000c000000000000000)
(define (fits-in-80bit? x)
(zero? (arithmetic-shift x -80)))
(define (sign x)
(bitwise-and (arithmetic-shift x -79) #x1))
(define (exponent x)
(bitwise-and (arithmetic-shift x -64) #x7fff))
(define (significand x)
(bitwise-and x #xffffffffffffffff))
(define (f80->dec x)
(define sgn (sign x))
(define exp (exponent x))
(define sig (significand x))
(define unbiased-exp (- exp 16383))
(define f80
(* (expt -1 sgn)
(expt 2 unbiased-exp)
(for/sum ([i (range 64)]
#:when (bitwise-bit-set? sig (- 63 i)))
(printf "Bit ~a contributes ~a~n" (- 63 i) (expt 2 (- i)))
(expt 2 (- i)))))
(printf "Sign: 0x~x~n" sgn)
(printf "Unbiased Exponent: 0x~x~n" unbiased-exp)
(printf "Significand: 0x~x~n" sig)
(printf "decimal: ~a~n" f80))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment