@@ -23,6 +23,18 @@ external unsafe_get_uint8 : bigstring -> int -> int = "%caml_ba_unsafe_ref_1"
23
23
external unsafe_get_uint16 : bigstring -> int -> int = " %caml_bigstring_get16u"
24
24
external swap16 : int -> int = " %bswap16"
25
25
external swap32 : int32 -> int32 = " %bswap_int32"
26
+ external int32_to_int : int32 -> int = " %int32_to_int"
27
+
28
+ let mask = 0xffff lsl 16 lor 0xffff
29
+ let int32_to_int n = int32_to_int n land mask
30
+
31
+ (* NOTE(dinosaure): Users may wonder why we use access with a bound-check (such
32
+ as [bufX.{...}]). The reason is simple: OCaml, with regard to bigarrays, can
33
+ unbox [int16]/[int32] if such access is desired. Furthermore, using a
34
+ function such as [unsafe_get_int{16,32}] would not allow OCaml to correctly
35
+ infer this access and to "prepare the ground" for unboxing. It should be
36
+ noted that bound-check is not the most expensive (and quite predictable in
37
+ reality), but unbox is. *)
26
38
27
39
let unsafe_digest_16_le ?(off = 0 ) ~len :top buf =
28
40
let buf16 = to_int16 ~off ~len: top buf in
@@ -40,13 +52,14 @@ let unsafe_digest_16_le ?(off = 0) ~len:top buf =
40
52
if ! sum > 0xffff then incr sum;
41
53
swap16 (lnot ! sum land 0xffff )
42
54
55
+ (* NOTE(dinosaure): only work on 64-bit architecture. *)
43
56
let unsafe_digest_32_le ?(off = 0 ) ~len :top buf =
44
57
let buf32 = to_int32 ~off ~len: top buf in
45
58
let len = ref top in
46
59
let sum = ref 0 in
47
60
let i = ref 0 in
48
61
while ! len > = 4 do
49
- let [ @ warning " -8 " ] ( Some v) = Int32. unsigned_to_int buf32.{! i} in
62
+ let v = int32_to_int buf32.{! i} in
50
63
sum := ! sum + v;
51
64
incr i;
52
65
len := ! len - 4
@@ -60,13 +73,14 @@ let unsafe_digest_32_le ?(off = 0) ~len:top buf =
60
73
done ;
61
74
swap16 (lnot ! sum land 0xffff )
62
75
76
+ (* NOTE(dinosaure): only work on 64-bit architecture. *)
63
77
let unsafe_digest_32_be ?(off = 0 ) ~len :top buf =
64
78
let buf32 = to_int32 ~off ~len: top buf in
65
79
let len = ref top in
66
80
let sum = ref 0 in
67
81
let i = ref 0 in
68
82
while ! len > = 4 do
69
- let [ @ warning " -8 " ] ( Some v) = Int32. unsigned_to_int (swap32 buf32.{! i}) in
83
+ let v = int32_to_int (swap32 buf32.{! i}) in
70
84
sum := ! sum + v;
71
85
incr i;
72
86
len := ! len - 4
0 commit comments