bytevector-ieee.scm 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. ; Part of Scheme 48 1.9. See file COPYING for notices and license.
  2. ; Authors: Harald Glab-Phlak, Mike Sperber
  3. (define (bytevector:nan? x)
  4. (and (real? x)
  5. (not (= x x))))
  6. (define (bytevector:infinite? x)
  7. (and (real? x)
  8. (not (bytevector:nan? x))
  9. (bytevector:nan? (- x x))))
  10. ;exported stuff
  11. (define (bytevector-ieee-single-native-ref bytevector k)
  12. (r6rs-bytevect->float bytevector k))
  13. (define (bytevector-ieee-double-native-ref bytevector k)
  14. (r6rs-bytevect->double bytevector k))
  15. (define (bytevector-ieee-single-ref bytevector k endness)
  16. (if (eq? endness (native-endianness))
  17. (if (= 0 (remainder k 4))
  18. (bytevector-ieee-single-native-ref bytevector k)
  19. (let ((b (make-bytevector 4)))
  20. (bytevector-copy! bytevector k b 0 4)
  21. (bytevector-ieee-single-native-ref b 0)))
  22. (let ((b (make-bytevector 4)))
  23. (bytevector-u8-set! b 0 (bytevector-u8-ref bytevector (+ k 3)))
  24. (bytevector-u8-set! b 1 (bytevector-u8-ref bytevector (+ k 2)))
  25. (bytevector-u8-set! b 2 (bytevector-u8-ref bytevector (+ k 1)))
  26. (bytevector-u8-set! b 3 (bytevector-u8-ref bytevector k))
  27. (bytevector-ieee-single-native-ref b 0))))
  28. (define (bytevector-ieee-double-ref bytevector k endness)
  29. (if (eq? endness (native-endianness))
  30. (if (= 0 (remainder k 8))
  31. (bytevector-ieee-double-native-ref bytevector k)
  32. (let ((b (make-bytevector 8)))
  33. (bytevector-copy! bytevector k b 0 8)
  34. (bytevector-ieee-double-native-ref b 0)))
  35. (let ((b (make-bytevector 8)))
  36. (bytevector-u8-set! b 0 (bytevector-u8-ref bytevector (+ k 7)))
  37. (bytevector-u8-set! b 1 (bytevector-u8-ref bytevector (+ k 6)))
  38. (bytevector-u8-set! b 2 (bytevector-u8-ref bytevector (+ k 5)))
  39. (bytevector-u8-set! b 3 (bytevector-u8-ref bytevector (+ k 4)))
  40. (bytevector-u8-set! b 4 (bytevector-u8-ref bytevector (+ k 3)))
  41. (bytevector-u8-set! b 5 (bytevector-u8-ref bytevector (+ k 2)))
  42. (bytevector-u8-set! b 6 (bytevector-u8-ref bytevector (+ k 1)))
  43. (bytevector-u8-set! b 7 (bytevector-u8-ref bytevector k))
  44. (bytevector-ieee-double-native-ref b 0))))
  45. (define (bytevector-ieee-single-native-set! bytevector k x)
  46. (r6rs-float->bytevect! x bytevector k))
  47. (define (bytevector-ieee-double-native-set! bytevector k x)
  48. (r6rs-double->bytevect! x bytevector k))
  49. (define (bytevector-ieee-single-set! bytevector k x endness)
  50. (if (eq? endness (native-endianness))
  51. (if (= 0 (remainder k 4))
  52. (bytevector-ieee-single-native-set! bytevector k x)
  53. (let ((b (make-bytevector 4)))
  54. (bytevector-ieee-single-native-set! b 0 x)
  55. (bytevector-copy! b 0 bytevector k 4)))
  56. (let ((b (make-bytevector 4)))
  57. (bytevector-ieee-single-native-set! b 0 x)
  58. (bytevector-u8-set! bytevector k (bytevector-u8-ref b 3))
  59. (bytevector-u8-set! bytevector (+ k 1) (bytevector-u8-ref b 2))
  60. (bytevector-u8-set! bytevector (+ k 2) (bytevector-u8-ref b 1))
  61. (bytevector-u8-set! bytevector (+ k 3) (bytevector-u8-ref b 0)))))
  62. (define (bytevector-ieee-double-set! bytevector k x endness)
  63. (if (eq? endness (native-endianness))
  64. (if (= 0 (remainder k 8))
  65. (bytevector-ieee-double-native-set! bytevector k x)
  66. (let ((b (make-bytevector 8)))
  67. (bytevector-ieee-double-native-set! b 0 x)
  68. (bytevector-copy! b 0 bytevector k 8)))
  69. (let ((b (make-bytevector 8)))
  70. (bytevector-ieee-double-native-set! b 0 x)
  71. (bytevector-u8-set! bytevector k (bytevector-u8-ref b 7))
  72. (bytevector-u8-set! bytevector (+ k 1) (bytevector-u8-ref b 6))
  73. (bytevector-u8-set! bytevector (+ k 2) (bytevector-u8-ref b 5))
  74. (bytevector-u8-set! bytevector (+ k 3) (bytevector-u8-ref b 4))
  75. (bytevector-u8-set! bytevector (+ k 4) (bytevector-u8-ref b 3))
  76. (bytevector-u8-set! bytevector (+ k 5) (bytevector-u8-ref b 2))
  77. (bytevector-u8-set! bytevector (+ k 6) (bytevector-u8-ref b 1))
  78. (bytevector-u8-set! bytevector (+ k 7) (bytevector-u8-ref b 0)))))
  79. (define (r6rs-float->bytevect! float bytevect index)
  80. (external-r6rs-float->bytevect! float bytevect index))
  81. (define (r6rs-bytevect->float bytevect index)
  82. (external-r6rs-bytevect->float bytevect index))
  83. (define (r6rs-double->bytevect! double bytevect index)
  84. (external-r6rs-double->bytevect! double bytevect index))
  85. (define (r6rs-bytevect->double bytevect index)
  86. (external-r6rs-bytevect->double bytevect index))
  87. ;; external fun definition
  88. (import-lambda-definition-2 external-r6rs-float->bytevect!
  89. (double bytevect index)
  90. "r6rs_float_to_bytevect")
  91. (import-lambda-definition-2 external-r6rs-bytevect->float
  92. (bytevect index)
  93. "r6rs_bytevect_to_float")
  94. (import-lambda-definition-2 external-r6rs-double->bytevect!
  95. (double bytevect index)
  96. "r6rs_double_to_bytevect")
  97. (import-lambda-definition-2 external-r6rs-bytevect->double
  98. (bytevect index)
  99. "r6rs_bytevect_to_double")