From 78065174073f67b1133096e61bd7cb3ccae2df8d Mon Sep 17 00:00:00 2001 From: Philip Solobay Date: Fri, 13 Sep 2024 10:07:09 -0500 Subject: [PATCH] Fix Load Buffer --- compiler/test_load_buffer.scm | 39 ++++++++++++++++------------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/compiler/test_load_buffer.scm b/compiler/test_load_buffer.scm index 16b7224..771f73a 100644 --- a/compiler/test_load_buffer.scm +++ b/compiler/test_load_buffer.scm @@ -1,8 +1,9 @@ (use-modules (system foreign)) (define-module (buffer test) - #:use-module (system foreign) - #:export (get-buffer1 get-result)) + #:use-module (system foreign) + #:use-module (rnrs bytevectors) + #:export (get-buffer1 get-result)) ;; Load the shared library and initialize (load-extension "./liboutput.so" "perform_operations") @@ -27,33 +28,29 @@ (define perform-operations (pointer->procedure void - (dynamic-func "perform_operations" lib) - '())) + (dynamic-func "perform_operations" lib) + '())) - -;; Function to convert a bytevector into a list of floats (define (bytevector-to-floats bv) - (let ((floats '())) - (do ((i 0 (+ i 4))) - ((>= i (bytevector-length bv)) (reverse floats)) - ;; Read 4 bytes from the bytevector and convert to float - (let* ((b0 (bytevector-u8-ref bv i)) - (b1 (bytevector-u8-ref bv (+ i 1))) - (b2 (bytevector-u8-ref bv (+ i 2))) - (b3 (bytevector-u8-ref bv (+ i 3))) - ;; Combine bytes into a 32-bit float (little-endian) - (int32 (logior (ash b3 24) - (ash b2 16) - (ash b1 8) - b0)) - (float (bitwise-float32->number int32))) - (set! floats (cons float floats)))))) + (let loop ((i 0) (floats '())) + (if (>= i (bytevector-length bv)) + (reverse floats) + (let* ((float (bytevector-ieee-single-native-ref bv i))) + (loop (+ i 4) (cons float floats)))))) ;; Usage example (display "buffer1 as floats: ") (display (bytevector-to-floats (get-buffer1))) (newline) +(display "buffer2 as floats: ") +(display (bytevector-to-floats (get-buffer2))) +(newline) + +(display "multiplier as floats: ") +(display (bytevector-to-floats (get-multiplier))) +(newline) + (display "result as floats: ") (display (bytevector-to-floats (get-result))) (newline) -- 2.39.5