Skip to content

Commit 4a0c89f

Browse files
authored
flambda-backend: Revert "Revert bswap PRs (480 and 482)" (#506)
* Revert "Revert bswap PRs (480 and 482) (#501)" This reverts commit 52cd50e. * Test for bswap32 * Add more test inputs * Refactor test * Add more tests
1 parent 7803705 commit 4a0c89f

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
(* TEST
2+
*)
3+
type buf = bytes
4+
5+
external create_buf : int -> buf = "caml_create_bytes"
6+
external unsafe_get32 : buf -> int -> int32 = "%caml_bytes_get32u"
7+
external unsafe_set32 : buf -> int -> int32 -> unit = "%caml_bytes_set32u"
8+
external bswap32 : int32 -> int32 = "%bswap_int32"
9+
10+
let buf = create_buf 32
11+
let read buf ~pos = unsafe_get32 buf pos |> bswap32 |> Stdlib.Int32.to_int
12+
let write buf ~pos n = unsafe_set32 buf pos (n |> Stdlib.Int32.of_int |> bswap32)
13+
14+
let numbers =
15+
[ 0x11223344l
16+
; 0xf0f0f0f0l
17+
; 0l
18+
; -1l
19+
; 0xf0fedcbal
20+
; 0x0fedfcbal
21+
; 0x7fedfcbal
22+
; Stdlib.Int32.max_int
23+
; Stdlib.Int32.min_int
24+
]
25+
;;
26+
27+
let test n =
28+
let n = Stdlib.Int32.to_int n in
29+
write buf ~pos:0 n;
30+
let n' = read buf ~pos:0 in
31+
assert (Int.equal n n')
32+
33+
let () =
34+
List.iter test numbers

0 commit comments

Comments
 (0)