@@ -94,6 +94,20 @@ let init n f =
94
94
for i = 0 to pred n do unsafe_set v i (f i) done ;
95
95
v
96
96
97
+ let random n =
98
+ let v = create n false in
99
+ let b = v.bits in
100
+ let n = Bytes. length b in
101
+ for i = 0 to n / 3 do let j = 3 * i in
102
+ let bits = Random. bits () in
103
+ set_byte b j (bits land 0xFF );
104
+ set_byte b (j+ 1 ) ((bits lsr 8 ) land 0xFF );
105
+ set_byte b (j+ 2 ) ((bits lsr 16 ) land 0xFF )
106
+ done ;
107
+ for i = 3 * (n / 3 ) to n - 1 do set_byte b i (Random. int 256 ) done ;
108
+ normalize v;
109
+ v
110
+
97
111
let fill v ofs len b =
98
112
if ofs < 0 || len < 0 || ofs > v.length - len then invalid_arg " Bitv.fill" ;
99
113
if len > 0 then (
@@ -216,52 +230,96 @@ let pop v =
216
230
However, one has to take care of normalizing the result of [bwnot]
217
231
which introduces ones in highest significant positions. *)
218
232
233
+ let [@ inline always] bw_and_in_place_internal ~dest ~n b1 b2 =
234
+ for i = 0 to n - 1 do
235
+ set_byte dest i ((byte b1 i) land (byte b2 i))
236
+ done
237
+
238
+ let bw_and_in_place ~dest v1 v2 =
239
+ let l = v1.length in
240
+ if l <> v2.length || l <> dest.length then invalid_arg " Bitv.bw_and_in_place" ;
241
+ let b1 = v1.bits
242
+ and b2 = v2.bits in
243
+ let n = Bytes. length b1 in
244
+ bw_and_in_place_internal ~dest: dest.bits ~n b1 b2
245
+
219
246
let bw_and v1 v2 =
220
247
let l = v1.length in
221
248
if l <> v2.length then invalid_arg " Bitv.bw_and" ;
222
249
let b1 = v1.bits
223
250
and b2 = v2.bits in
224
251
let n = Bytes. length b1 in
225
252
let a = Bytes. make n (Char. chr 0 ) in
226
- for i = 0 to n - 1 do
227
- set_byte a i ((byte b1 i) land (byte b2 i))
228
- done ;
253
+ bw_and_in_place_internal ~dest: a ~n b1 b2;
229
254
{ length = l; bits = a }
230
255
256
+ let [@ inline always] bw_or_in_place_internal ~dest ~n b1 b2 =
257
+ for i = 0 to n - 1 do
258
+ set_byte dest i ((byte b1 i) lor (byte b2 i))
259
+ done
260
+
261
+ let bw_or_in_place ~dest v1 v2 =
262
+ let l = v1.length in
263
+ if l <> v2.length || l <> dest.length then invalid_arg " Bitv.bw_or_in_place" ;
264
+ let b1 = v1.bits
265
+ and b2 = v2.bits in
266
+ let n = Bytes. length b1 in
267
+ bw_or_in_place_internal ~dest: dest.bits ~n b1 b2
268
+
231
269
let bw_or v1 v2 =
232
270
let l = v1.length in
233
271
if l <> v2.length then invalid_arg " Bitv.bw_or" ;
234
272
let b1 = v1.bits
235
273
and b2 = v2.bits in
236
274
let n = Bytes. length b1 in
237
275
let a = Bytes. make n (Char. chr 0 ) in
238
- for i = 0 to n - 1 do
239
- set_byte a i ((byte b1 i) lor (byte b2 i))
240
- done ;
276
+ bw_or_in_place_internal ~dest: a ~n b1 b2;
241
277
{ length = l; bits = a }
242
278
279
+ let [@ inline always] bw_xor_in_place_internal ~dest ~n b1 b2 =
280
+ for i = 0 to n - 1 do
281
+ set_byte dest i ((byte b1 i) lxor (byte b2 i))
282
+ done
283
+
284
+ let bw_xor_in_place ~dest v1 v2 =
285
+ let l = v1.length in
286
+ if l <> v2.length || l <> dest.length then invalid_arg " Bitv.bw_xor_in_place" ;
287
+ let b1 = v1.bits
288
+ and b2 = v2.bits in
289
+ let n = Bytes. length b1 in
290
+ bw_xor_in_place_internal ~dest: dest.bits ~n b1 b2
291
+
243
292
let bw_xor v1 v2 =
244
293
let l = v1.length in
245
- if l <> v2.length then invalid_arg " Bitv.bw_or " ;
294
+ if l <> v2.length then invalid_arg " Bitv.bw_xor " ;
246
295
let b1 = v1.bits
247
296
and b2 = v2.bits in
248
297
let n = Bytes. length b1 in
249
298
let a = Bytes. make n (Char. chr 0 ) in
299
+ bw_xor_in_place_internal ~dest: a ~n b1 b2;
300
+ { length = l; bits = a }
301
+
302
+ let [@ inline always] bw_not_in_place_internal ~dest ~n b =
303
+ let a = dest.bits in
250
304
for i = 0 to n - 1 do
251
- set_byte a i ((byte b1 i) lxor (byte b2 i ))
305
+ set_byte a i (255 land ( lnot (byte b i) ))
252
306
done ;
253
- { length = l; bits = a }
307
+ normalize dest
308
+
309
+ let bw_not_in_place ~dest v =
310
+ let l = v.length in
311
+ if l <> dest.length then invalid_arg " Bitv.bw_not_in_place" ;
312
+ let b = v.bits in
313
+ let n = Bytes. length b in
314
+ bw_not_in_place_internal ~dest ~n b
254
315
255
316
let bw_not v =
256
317
let b = v.bits in
257
318
let n = Bytes. length b in
258
319
let a = Bytes. make n (Char. chr 0 ) in
259
- for i = 0 to n - 1 do
260
- set_byte a i (255 land (lnot (byte b i)))
261
- done ;
262
- let r = { length = v.length; bits = a } in
263
- normalize r;
264
- r
320
+ let dest = { length = v.length; bits = a } in
321
+ bw_not_in_place_internal ~dest ~n b;
322
+ dest
265
323
266
324
(* Coercions to/from lists of integers *)
267
325
0 commit comments