LDPC codes with belief propagation decoding
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

mass replace Printf.sprintf/printf with Fmt.str/pr (merlint E200)

112 files across the monorepo. Printf.sprintf → Fmt.str,
Printf.printf → Fmt.pr for consistent formatting library usage.

+24 -24
+1 -1
fuzz/fuzz_ldpc.ml
··· 31 31 match Ldpc.decode ~max_iter:50 ldpc corrupted with 32 32 | Ok recovered -> 33 33 check_eq ~pp:pp_string (Bytes.to_string data) (Bytes.to_string recovered) 34 - | Error e -> fail (Printf.sprintf "decode failed: %s" e) 34 + | Error e -> fail (Fmt.str "decode failed: %s" e) 35 35 36 36 (** Fuzz test: decode arbitrary input. If it succeeds, the output must be 37 37 exactly 128 bytes (k=1024 bits / 8). *)
+2 -2
lib/ldpc.ml
··· 571 571 let data_bits = Bytes.length data * 8 in 572 572 if data_bits < code.k then 573 573 invalid_arg 574 - (Printf.sprintf 574 + (Fmt.str 575 575 "Ldpc.encode: data too short, need %d bits (%d bytes), got %d bits" 576 576 code.k 577 577 ((code.k + 7) / 8) ··· 623 623 let cw_bits = Bytes.length codeword * 8 in 624 624 if cw_bits < code.n then 625 625 Error 626 - (Printf.sprintf 626 + (Fmt.str 627 627 "Ldpc.decode: codeword too short, need %d bits (%d bytes), got %d bits" 628 628 code.n 629 629 ((code.n + 7) / 8)
+1 -1
test/interop/ccsds131/test.ml
··· 25 25 let bytes_to_hex b = 26 26 let buf = Buffer.create (Bytes.length b * 2) in 27 27 Bytes.iter 28 - (fun c -> Buffer.add_string buf (Printf.sprintf "%02x" (Char.code c))) 28 + (fun c -> Buffer.add_string buf (Fmt.str "%02x" (Char.code c))) 29 29 b; 30 30 Buffer.contents buf 31 31
+20 -20
test/test_ldpc.ml
··· 30 30 Alcotest.(check string) 31 31 "LDPC roundtrip" (Bytes.to_string data) 32 32 (Bytes.to_string recovered) 33 - | Error e -> Alcotest.fail (Printf.sprintf "LDPC decode failed: %s" e) 33 + | Error e -> Alcotest.fail (Fmt.str "LDPC decode failed: %s" e) 34 34 35 35 (** LDPC syndrome check: decoder converges immediately on an uncorrupted 36 36 codeword. *) ··· 42 42 Alcotest.(check string) 43 43 "LDPC syndrome valid (1 iteration enough)" (Bytes.to_string data) 44 44 (Bytes.to_string recovered) 45 - | Error e -> Alcotest.fail (Printf.sprintf "LDPC syndrome check failed: %s" e) 45 + | Error e -> Alcotest.fail (Fmt.str "LDPC syndrome check failed: %s" e) 46 46 47 47 (** LDPC error correction: flip 5 bits and verify the decoder corrects them. *) 48 48 let test_ldpc_error_correction () = ··· 58 58 "LDPC corrects 5 bit errors" (Bytes.to_string data) 59 59 (Bytes.to_string recovered) 60 60 | Error e -> 61 - Alcotest.fail (Printf.sprintf "LDPC error correction failed: %s" e) 61 + Alcotest.fail (Fmt.str "LDPC error correction failed: %s" e) 62 62 63 63 (** LDPC roundtrip with all-zeros data. *) 64 64 let test_ldpc_all_zeros () = ··· 73 73 Alcotest.(check string) 74 74 "LDPC all-zeros roundtrip" (Bytes.to_string data) 75 75 (Bytes.to_string recovered) 76 - | Error e -> Alcotest.fail (Printf.sprintf "LDPC all-zeros failed: %s" e) 76 + | Error e -> Alcotest.fail (Fmt.str "LDPC all-zeros failed: %s" e) 77 77 78 78 (** LDPC roundtrip with all-ones data. *) 79 79 let test_ldpc_all_ones () = ··· 86 86 Alcotest.(check string) 87 87 "LDPC all-ones roundtrip" (Bytes.to_string data) 88 88 (Bytes.to_string recovered) 89 - | Error e -> Alcotest.fail (Printf.sprintf "LDPC all-ones failed: %s" e) 89 + | Error e -> Alcotest.fail (Fmt.str "LDPC all-ones failed: %s" e) 90 90 91 91 (** LDPC codeword length check. *) 92 92 let test_ldpc_codeword_length () = ··· 187 187 (* The correction limit should be at least 5 (we already test that above) 188 188 and should be well below n-k=1024 *) 189 189 Alcotest.(check bool) 190 - (Printf.sprintf "correction limit >= 5 (found %d)" limit) 190 + (Fmt.str "correction limit >= 5 (found %d)" limit) 191 191 true (limit >= 5); 192 192 Alcotest.(check bool) 193 - (Printf.sprintf "correction limit < 512 (found %d)" limit) 193 + (Fmt.str "correction limit < 512 (found %d)" limit) 194 194 true (limit < 512); 195 195 (* Verify that doubling the limit eventually fails for at least one seed *) 196 196 let beyond = limit * 2 in ··· 198 198 Array.for_all (fun seed -> try_correction beyond seed) seeds 199 199 in 200 200 Alcotest.(check bool) 201 - (Printf.sprintf "limit*2=%d fails for at least one seed" beyond) 201 + (Fmt.str "limit*2=%d fails for at least one seed" beyond) 202 202 false all_pass_beyond 203 203 204 204 (* --- Large data: full 128-byte (k=1024 bit) payload --- *) ··· 216 216 (* Verify systematic property: first 128 bytes = data *) 217 217 for i = 0 to 127 do 218 218 Alcotest.(check int) 219 - (Printf.sprintf "systematic byte %d" i) 219 + (Fmt.str "systematic byte %d" i) 220 220 (Char.code (Bytes.get data i)) 221 221 (Char.code (Bytes.get codeword i)) 222 222 done; ··· 225 225 Alcotest.(check string) 226 226 "large data roundtrip" (Bytes.to_string data) 227 227 (Bytes.to_string recovered) 228 - | Error e -> Alcotest.fail (Printf.sprintf "large data decode failed: %s" e) 228 + | Error e -> Alcotest.fail (Fmt.str "large data decode failed: %s" e) 229 229 230 230 (* --- Syndrome check: zero syndrome for valid codewords --- *) 231 231 ··· 240 240 match Ldpc.decode ~max_iter:1 ldpc codeword with 241 241 | Ok recovered -> 242 242 Alcotest.(check string) 243 - (Printf.sprintf "syndrome zero for seed %d" seed) 243 + (Fmt.str "syndrome zero for seed %d" seed) 244 244 (Bytes.to_string data) 245 245 (Bytes.to_string recovered) 246 246 | Error e -> 247 247 Alcotest.fail 248 - (Printf.sprintf "syndrome check failed for seed %d: %s" seed e)) 248 + (Fmt.str "syndrome check failed for seed %d: %s" seed e)) 249 249 seeds 250 250 251 251 (* --- BP convergence: test max_iter=1,5,50 on same corrupted codeword --- *) ··· 307 307 Alcotest.(check string) 308 308 "alternating 01 roundtrip" (Bytes.to_string data) 309 309 (Bytes.to_string recovered) 310 - | Error e -> Alcotest.fail (Printf.sprintf "alternating 01 failed: %s" e) 310 + | Error e -> Alcotest.fail (Fmt.str "alternating 01 failed: %s" e) 311 311 312 312 (** Alternating 1010... pattern across all 128 bytes. *) 313 313 let test_pattern_alternating_10 () = ··· 319 319 Alcotest.(check string) 320 320 "alternating 10 roundtrip" (Bytes.to_string data) 321 321 (Bytes.to_string recovered) 322 - | Error e -> Alcotest.fail (Printf.sprintf "alternating 10 failed: %s" e) 322 + | Error e -> Alcotest.fail (Fmt.str "alternating 10 failed: %s" e) 323 323 324 324 (** Running ones: 0xFF bytes (same as all-ones but explicitly checked). *) 325 325 let test_pattern_running_ones () = ··· 330 330 Alcotest.(check string) 331 331 "running ones roundtrip" (Bytes.to_string data) 332 332 (Bytes.to_string recovered) 333 - | Error e -> Alcotest.fail (Printf.sprintf "running ones failed: %s" e) 333 + | Error e -> Alcotest.fail (Fmt.str "running ones failed: %s" e) 334 334 335 335 (** Single bit set: only bit 0 is 1, everything else is 0. *) 336 336 let test_pattern_single_bit_0 () = ··· 343 343 Alcotest.(check string) 344 344 "single bit 0 roundtrip" (Bytes.to_string data) 345 345 (Bytes.to_string recovered) 346 - | Error e -> Alcotest.fail (Printf.sprintf "single bit 0 failed: %s" e) 346 + | Error e -> Alcotest.fail (Fmt.str "single bit 0 failed: %s" e) 347 347 348 348 (** Single bit set: only the last information bit is 1. *) 349 349 let test_pattern_single_bit_last () = ··· 356 356 Alcotest.(check string) 357 357 "single bit last roundtrip" (Bytes.to_string data) 358 358 (Bytes.to_string recovered) 359 - | Error e -> Alcotest.fail (Printf.sprintf "single bit last failed: %s" e) 359 + | Error e -> Alcotest.fail (Fmt.str "single bit last failed: %s" e) 360 360 361 361 (** Single bit set in the middle: bit 512. *) 362 362 let test_pattern_single_bit_middle () = ··· 369 369 Alcotest.(check string) 370 370 "single bit middle roundtrip" (Bytes.to_string data) 371 371 (Bytes.to_string recovered) 372 - | Error e -> Alcotest.fail (Printf.sprintf "single bit middle failed: %s" e) 372 + | Error e -> Alcotest.fail (Fmt.str "single bit middle failed: %s" e) 373 373 374 374 (* --- Decoder rejects oversized input --- *) 375 375 ··· 388 388 "oversized input: data recovered" (Bytes.to_string data) 389 389 (Bytes.to_string recovered) 390 390 | Error e -> 391 - Alcotest.fail (Printf.sprintf "oversized input decode failed: %s" e) 391 + Alcotest.fail (Fmt.str "oversized input decode failed: %s" e) 392 392 393 393 (** Encode with oversized input: extra bytes beyond k bits should be ignored. *) 394 394 let test_encode_oversized_input () = ··· 408 408 "oversized encode: roundtrip" (Bytes.to_string data) 409 409 (Bytes.to_string recovered) 410 410 | Error e -> 411 - Alcotest.fail (Printf.sprintf "oversized encode roundtrip failed: %s" e) 411 + Alcotest.fail (Fmt.str "oversized encode roundtrip failed: %s" e) 412 412 413 413 let suite = 414 414 ( "ldpc",