summaryrefslogtreecommitdiff
path: root/lib/jtty/jtty_tbcc_list_decoder.f90
blob: ca4a4bd123ca82bcbf25fe356f82bd458468f9cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
module jtty_tbcc_list_decoder
  use, intrinsic :: iso_fortran_env, only: int32, int64, real32, real64
  use, intrinsic :: ieee_arithmetic, only: ieee_is_finite
  use jtty_tbcc_code_profiles, only: jtty_tbcc_code_profile, &
       jtty_tbcc_code_profile_is_supported, JTTY_TBCC_INFORMATION_BITS
  implicit none
  private

  ! This module keeps decoder breadth and admission breadth separate.  The
  ! shorthand P1/P4 means retaining one/four distinct paths at every trellis
  ! merge.  H1/H4 means returning at most one/four unique, closed codewords to
  ! the CRC admission layer.  A wider path search therefore does not silently
  ! increase the number of CRC hypotheses tested.

  public :: JTTY_TBCC_INFORMATION_BITS
  integer(int32), parameter, public :: JTTY_TBCC_RESERVED_BIT = 33_int32
  integer(int32), parameter, public :: JTTY_TBCC_MAX_HYPOTHESES = 4_int32
  real(real64), parameter :: NEGATIVE_METRIC = -huge(0.0_real64)

  type :: survivor_t
    logical :: valid = .false.
    real(real64) :: metric = NEGATIVE_METRIC
    integer(int32) :: origin_state = -1_int32
    integer(int32) :: bits(JTTY_TBCC_INFORMATION_BITS) = 0_int32
  end type survivor_t

  type :: pool_candidate_t
    integer(int32) :: bits(JTTY_TBCC_INFORMATION_BITS) = 0_int32
    integer(int32) :: start_state = -1_int32
    integer(int64) :: identity = 0_int64
    real(real64) :: clean_metric = NEGATIVE_METRIC
    real(real64) :: wava_metric = NEGATIVE_METRIC
    logical :: crc_valid = .false.
  end type pool_candidate_t

  integer(int32), parameter :: PACKED_ORIGIN_BITS = 11_int32
  integer(int32), parameter :: PACKED_VALID_BIT = JTTY_TBCC_INFORMATION_BITS + PACKED_ORIGIN_BITS

  type :: packed_survivor_t
    real(real64) :: metric = NEGATIVE_METRIC
    ! Low bits hold the input word in lexical order, then origin and validity.
    ! Unwritten input bits stay zero, so integer order also orders every prefix.
    integer(int64) :: key = 0_int64
  end type packed_survivor_t

  type, public :: jtty_tbcc_decoder_plan
    private
    ! Immutable trellis geometry and coherent branch tables shared by decodes.
    logical :: initialized = .false.
    type(jtty_tbcc_code_profile) :: code
    integer(int32) :: coherent_length = 0_int32
    integer(int32) :: per_state_width = 0_int32
    integer(int32) :: wraps = 0_int32
    integer(int32) :: hypothesis_count = 0_int32
    integer(int32) :: state_count = 0_int32
    integer(int32) :: block_count = 0_int32
    integer(int32) :: max_branch_count = 0_int32
    integer(int32) :: pool_capacity = 0_int32
    integer(int32) :: hash_capacity = 0_int32
    integer(int32) :: energy_count = 0_int32
    integer(int32), allocatable :: next_state(:,:), tone(:,:)
    integer(int32), allocatable :: block_starts(:), block_lengths(:)
    integer(int32), allocatable :: branch_counts(:), energy_offsets(:)
    integer(int32), allocatable :: branch_end_states(:,:,:)
    integer(int32), allocatable :: branch_sequence_indices(:,:,:)
    integer(int64), allocatable :: branch_identities(:,:)
  end type jtty_tbcc_decoder_plan

  type, public :: jtty_tbcc_decoder_workspace
    private
    ! Reusable scratch storage keeps allocation outside the receiver hot path.
    logical :: initialized = .false.
    type(packed_survivor_t), allocatable :: previous(:,:), current(:,:)
    type(pool_candidate_t), allocatable :: pool(:)
    integer(int32), allocatable :: order(:), sort_scratch(:), hash_indices(:)
    integer(int64), allocatable :: hash_keys(:)
    real(real64), allocatable :: sequence_energies(:)
    logical, allocatable :: hash_used(:)
  end type jtty_tbcc_decoder_workspace

  public :: jtty_tbcc_transition
  public :: jtty_tbcc_supertransition
  public :: jtty_tbcc_coherent_metric
  public :: jtty_tbcc_score_candidate
  public :: jtty_tbcc_tailbiting_state
  public :: jtty_tbcc_candidate_identity
  public :: jtty_tbcc_crc_valid
  public :: jtty_tbcc_list_wava
  public :: jtty_tbcc_list_wava_reference
  public :: jtty_tbcc_init_decoder_plan
  public :: jtty_tbcc_init_decoder_workspace
  public :: jtty_tbcc_list_wava_optimized

contains

  pure subroutine jtty_tbcc_transition(code, state, input_bit, next_state, tone)
    type(jtty_tbcc_code_profile), intent(in) :: code
    integer(int32), intent(in) :: state, input_bit
    integer(int32), intent(out) :: next_state, tone
    integer(int32) :: output_register, out_b0, out_b1, state_mask

    state_mask = code%state_mask
    output_register = iand(ior(shiftl(state, 1), input_bit), &
         code%register_mask)
    out_b0 = modulo(popcnt(iand(output_register, code%generator_0)), 2)
    out_b1 = modulo(popcnt(iand(output_register, code%generator_1)), 2)
    tone = 2_int32*out_b0 + ieor(out_b0, out_b1)
    next_state = iand(output_register, state_mask)
  end subroutine jtty_tbcc_transition

  pure subroutine jtty_tbcc_supertransition(code, start_state, input_bits, &
       end_state, tones)
    type(jtty_tbcc_code_profile), intent(in) :: code
    integer(int32), intent(in) :: start_state
    integer(int32), intent(in) :: input_bits(:)
    integer(int32), intent(out) :: end_state
    integer(int32), intent(out) :: tones(size(input_bits))
    integer(int32) :: offset, state

    state = start_state
    do offset = 1, size(input_bits)
      call jtty_tbcc_transition(code, state, input_bits(offset), &
           end_state, tones(offset))
      state = end_state
    end do
    end_state = state
  end subroutine jtty_tbcc_supertransition

  pure real(real64) function jtty_tbcc_coherent_metric(correlations, &
       first_symbol, tones) result(metric)
    complex(real32), intent(in) :: correlations(0:3, JTTY_TBCC_INFORMATION_BITS)
    integer(int32), intent(in) :: first_symbol
    integer(int32), intent(in) :: tones(:)
    complex(real32) :: coherent_sum
    integer(int32) :: offset

    coherent_sum = cmplx(0.0_real32, 0.0_real32, real32)
    do offset = 1, size(tones)
      coherent_sum = coherent_sum + &
           correlations(tones(offset), first_symbol + offset - 1)
    end do
    metric = real(coherent_sum*conjg(coherent_sum), real64) / &
         real(size(tones), real64)
  end function jtty_tbcc_coherent_metric

  pure integer(int32) function jtty_tbcc_tailbiting_state(code, bits) &
       result(state)
    type(jtty_tbcc_code_profile), intent(in) :: code
    integer(int32), intent(in) :: bits(JTTY_TBCC_INFORMATION_BITS)
    integer(int32) :: bit_index, state_mask

    state = 0_int32
    state_mask = code%state_mask
    do bit_index = JTTY_TBCC_INFORMATION_BITS - code%memory_nu + 1, &
         JTTY_TBCC_INFORMATION_BITS
      state = iand(ior(shiftl(state, 1), bits(bit_index)), state_mask)
    end do
  end function jtty_tbcc_tailbiting_state

  pure subroutine jtty_tbcc_score_candidate(code, correlations, &
       coherent_length, bits, metric, closed)
    type(jtty_tbcc_code_profile), intent(in) :: code
    complex(real32), intent(in) :: correlations(0:3, JTTY_TBCC_INFORMATION_BITS)
    integer(int32), intent(in) :: coherent_length
    integer(int32), intent(in) :: bits(JTTY_TBCC_INFORMATION_BITS)
    real(real64), intent(out) :: metric
    logical, intent(out) :: closed
    integer(int32) :: block_length, block_start, end_state, next_state, start_state
    integer(int32) :: tones(4)

    start_state = jtty_tbcc_tailbiting_state(code, bits)
    end_state = start_state
    metric = 0.0_real64
    do block_start = 1, JTTY_TBCC_INFORMATION_BITS, coherent_length
      block_length = min(coherent_length, &
           JTTY_TBCC_INFORMATION_BITS - block_start + 1_int32)
      call jtty_tbcc_supertransition(code, end_state, &
           bits(block_start:block_start + block_length - 1), next_state, &
           tones(1:block_length))
      metric = metric + jtty_tbcc_coherent_metric(correlations, &
           block_start, tones(1:block_length))
      end_state = next_state
    end do
    closed = end_state == start_state
  end subroutine jtty_tbcc_score_candidate

  pure integer(int64) function jtty_tbcc_candidate_identity(bits) &
       result(identity)
    integer(int32), intent(in) :: bits(JTTY_TBCC_INFORMATION_BITS)
    integer(int32) :: bit_index

    identity = 0_int64
    do bit_index = 1, JTTY_TBCC_INFORMATION_BITS
      if (bits(bit_index) /= 0_int32) identity = ibset(identity, bit_index - 1)
    end do
  end function jtty_tbcc_candidate_identity

  pure logical function jtty_tbcc_crc_valid(code, bits) result(valid)
    type(jtty_tbcc_code_profile), intent(in) :: code
    integer(int32), intent(in) :: bits(JTTY_TBCC_INFORMATION_BITS)
    integer(int32) :: bit_index, crc_register

    crc_register = 0_int32
    do bit_index = 1, JTTY_TBCC_INFORMATION_BITS
      crc_register = ieor(crc_register, &
           shiftl(bits(bit_index), code%outer_check_bits - 1_int32))
      if (iand(crc_register, code%outer_top_bit_mask) /= 0_int32) then
        crc_register = ieor(shiftl(crc_register, 1), code%outer_polynomial)
      else
        crc_register = shiftl(crc_register, 1)
      end if
      crc_register = iand(crc_register, code%outer_register_mask)
    end do
    valid = crc_register == 0_int32
  end function jtty_tbcc_crc_valid

  subroutine jtty_tbcc_init_decoder_plan(plan, code, coherent_length, &
       per_state_width, wraps, hypothesis_count)
    type(jtty_tbcc_decoder_plan), intent(out) :: plan
    type(jtty_tbcc_code_profile), intent(in) :: code
    integer(int32), intent(in) :: coherent_length, per_state_width, wraps
    integer(int32), intent(in) :: hypothesis_count
    integer(int32) :: block_index, block_length, block_start, branch_word
    integer(int32) :: end_state, input_bit, offset, state, tone_word
    integer(int32) :: input_bits(4), tones(4)
    integer(int64) :: branch_identity

    call validate_decoder_arguments(code, coherent_length, per_state_width, &
         wraps, hypothesis_count)
    if (code%memory_nu > PACKED_ORIGIN_BITS) error stop 'TBCC state does not fit packed survivor'
    plan%code = code
    plan%coherent_length = coherent_length
    plan%per_state_width = per_state_width
    plan%wraps = wraps
    plan%hypothesis_count = hypothesis_count
    plan%state_count = code%state_count
    plan%block_count = (JTTY_TBCC_INFORMATION_BITS + coherent_length - 1_int32) / &
         coherent_length
    plan%max_branch_count = shiftl(1_int32, coherent_length)
    plan%pool_capacity = plan%state_count*per_state_width
    plan%hash_capacity = 1_int32
    do while (plan%hash_capacity < 2_int32*plan%pool_capacity)
      plan%hash_capacity = 2_int32*plan%hash_capacity
    end do

    allocate(plan%next_state(0:plan%state_count - 1, 0:1))
    allocate(plan%tone(0:plan%state_count - 1, 0:1))
    do input_bit = 0, 1
      do state = 0, plan%state_count - 1
        call jtty_tbcc_transition(code, state, input_bit, &
             plan%next_state(state, input_bit), plan%tone(state, input_bit))
      end do
    end do

    allocate(plan%block_starts(plan%block_count), plan%block_lengths(plan%block_count))
    allocate(plan%branch_counts(plan%block_count), plan%energy_offsets(plan%block_count))
    allocate(plan%branch_identities(0:plan%max_branch_count - 1, plan%block_count))
    allocate(plan%branch_end_states(0:plan%state_count - 1, &
         0:plan%max_branch_count - 1, plan%block_count))
    allocate(plan%branch_sequence_indices(0:plan%state_count - 1, &
         0:plan%max_branch_count - 1, plan%block_count))
    plan%branch_identities = 0_int64
    plan%branch_end_states = 0_int32
    plan%branch_sequence_indices = 0_int32
    plan%energy_count = 0_int32
    do block_index = 1, plan%block_count
      block_start = 1_int32 + (block_index - 1_int32)*coherent_length
      block_length = min(coherent_length, &
           JTTY_TBCC_INFORMATION_BITS - block_start + 1_int32)
      plan%block_starts(block_index) = block_start
      plan%block_lengths(block_index) = block_length
      plan%branch_counts(block_index) = shiftl(1_int32, block_length)
      plan%energy_offsets(block_index) = plan%energy_count + 1_int32
      plan%energy_count = plan%energy_count + shiftl(1_int32, 2_int32*block_length)
      do branch_word = 0, plan%branch_counts(block_index) - 1
        call unpack_branch_word(branch_word, block_length, input_bits)
        branch_identity = 0_int64
        do offset = 1, block_length
          if (input_bits(offset) /= 0_int32) &
               branch_identity = ibset(branch_identity, JTTY_TBCC_INFORMATION_BITS - block_start - offset + 1_int32)
        end do
        plan%branch_identities(branch_word, block_index) = branch_identity
        do state = 0, plan%state_count - 1
          call planned_supertransition(plan, state, input_bits, block_length, &
               end_state, tones, tone_word)
          plan%branch_end_states(state, branch_word, block_index) = end_state
          plan%branch_sequence_indices(state, branch_word, block_index) = &
               plan%energy_offsets(block_index) + tone_word
        end do
      end do
    end do
    plan%initialized = .true.
  end subroutine jtty_tbcc_init_decoder_plan

  subroutine jtty_tbcc_init_decoder_workspace(workspace, plan)
    type(jtty_tbcc_decoder_workspace), intent(out) :: workspace
    type(jtty_tbcc_decoder_plan), intent(in) :: plan

    if (.not.plan%initialized) error stop 'TBCC list decoder plan is not initialized'
    allocate(workspace%previous(0:plan%state_count - 1, plan%per_state_width))
    allocate(workspace%current(0:plan%state_count - 1, plan%per_state_width))
    allocate(workspace%pool(max(1_int32, plan%pool_capacity)))
    allocate(workspace%order(max(1_int32, plan%pool_capacity)))
    allocate(workspace%sort_scratch(max(1_int32, plan%pool_capacity)))
    allocate(workspace%hash_keys(plan%hash_capacity))
    allocate(workspace%hash_indices(plan%hash_capacity))
    allocate(workspace%hash_used(plan%hash_capacity))
    allocate(workspace%sequence_energies(plan%energy_count))
    workspace%initialized = .true.
  end subroutine jtty_tbcc_init_decoder_workspace

  subroutine jtty_tbcc_list_wava(correlations, code, coherent_length, &
       per_state_width, wraps, hypothesis_count, candidate_bits, &
       candidate_identities, candidate_clean_metrics, candidate_wava_metrics, &
       candidate_start_states, candidate_crc_valid, candidate_count, pool_count, &
       prune_reserved_zero, reference_bits, reference_present, &
       reference_pool_rank, reference_h_rank)
    complex(real32), intent(in) :: correlations(0:3, JTTY_TBCC_INFORMATION_BITS)
    type(jtty_tbcc_code_profile), intent(in) :: code
    integer(int32), intent(in) :: coherent_length, per_state_width, wraps
    integer(int32), intent(in) :: hypothesis_count
    integer(int32), intent(out) :: candidate_bits(JTTY_TBCC_INFORMATION_BITS, hypothesis_count)
    integer(int64), intent(out) :: candidate_identities(hypothesis_count)
    real(real64), intent(out) :: candidate_clean_metrics(hypothesis_count)
    real(real64), intent(out) :: candidate_wava_metrics(hypothesis_count)
    integer(int32), intent(out) :: candidate_start_states(hypothesis_count)
    logical, intent(out) :: candidate_crc_valid(hypothesis_count)
    integer(int32), intent(out) :: candidate_count, pool_count
    logical, intent(in), optional :: prune_reserved_zero
    integer(int32), intent(in), optional :: reference_bits(JTTY_TBCC_INFORMATION_BITS)
    logical, intent(out), optional :: reference_present
    integer(int32), intent(out), optional :: reference_pool_rank, reference_h_rank
    type(jtty_tbcc_decoder_plan) :: plan
    type(jtty_tbcc_decoder_workspace) :: workspace

    call jtty_tbcc_init_decoder_plan(plan, code, coherent_length, &
         per_state_width, wraps, hypothesis_count)
    call jtty_tbcc_init_decoder_workspace(workspace, plan)
    call jtty_tbcc_list_wava_optimized(plan, workspace, correlations, &
         candidate_bits, candidate_identities, candidate_clean_metrics, &
         candidate_wava_metrics, candidate_start_states, candidate_crc_valid, &
         candidate_count, pool_count, prune_reserved_zero, reference_bits, &
         reference_present, reference_pool_rank, reference_h_rank)
  end subroutine jtty_tbcc_list_wava

  subroutine jtty_tbcc_list_wava_optimized(plan, workspace, correlations, &
       candidate_bits, candidate_identities, candidate_clean_metrics, &
       candidate_wava_metrics, candidate_start_states, candidate_crc_valid, &
       candidate_count, pool_count, prune_reserved_zero, reference_bits, &
       reference_present, reference_pool_rank, reference_h_rank)
    type(jtty_tbcc_decoder_plan), intent(in) :: plan
    type(jtty_tbcc_decoder_workspace), intent(inout) :: workspace
    complex(real32), intent(in) :: correlations(0:3, JTTY_TBCC_INFORMATION_BITS)
    integer(int32), intent(out) :: candidate_bits(:, :)
    integer(int64), intent(out) :: candidate_identities(:)
    real(real64), intent(out) :: candidate_clean_metrics(:), candidate_wava_metrics(:)
    integer(int32), intent(out) :: candidate_start_states(:)
    logical, intent(out) :: candidate_crc_valid(:)
    integer(int32), intent(out) :: candidate_count, pool_count
    logical, intent(in), optional :: prune_reserved_zero
    integer(int32), intent(in), optional :: reference_bits(JTTY_TBCC_INFORMATION_BITS)
    logical, intent(out), optional :: reference_present
    integer(int32), intent(out), optional :: reference_pool_rank, reference_h_rank
    type(packed_survivor_t), allocatable :: swap_survivors(:,:)
    integer(int32) :: block_index, hash_slot
    integer(int32) :: output_rank, pool_index, rank, state, wrap
    integer(int64) :: identity, reference_identity
    real(real64) :: clean_metric
    logical :: closed, found, prune, ordered_metrics

    call validate_optimized_storage(plan, workspace, candidate_bits, &
         candidate_identities, candidate_clean_metrics, candidate_wava_metrics, &
         candidate_start_states, candidate_crc_valid)
    call precompute_sequence_energies(plan, correlations, workspace%sequence_energies)
    ! Nonfinite metrics do not provide the monotone rank order needed by the cutoff.
    ordered_metrics = all(ieee_is_finite(workspace%sequence_energies))
    call clear_packed_survivors(workspace%previous)
    do state = 0, plan%state_count - 1
      workspace%previous(state, 1)%key = ibset(shiftl(int(state, int64), JTTY_TBCC_INFORMATION_BITS), PACKED_VALID_BIT)
      workspace%previous(state, 1)%metric = 0.0_real64
    end do

    prune = .false.
    if (present(prune_reserved_zero)) prune = prune_reserved_zero
    do wrap = 1, plan%wraps
      call reset_packed_frame_identity(workspace%previous)
      do block_index = 1, plan%block_count
        call advance_packed_survivors(plan, block_index, prune, workspace%sequence_energies, &
             workspace%previous, workspace%current, ordered_metrics)
        call move_alloc(workspace%previous, swap_survivors)
        call move_alloc(workspace%current, workspace%previous)
        call move_alloc(swap_survivors, workspace%current)
      end do
    end do

    pool_count = 0_int32
    workspace%hash_used = .false.
    do state = 0, plan%state_count - 1
      do rank = 1, plan%per_state_width
        if (.not.btest(workspace%previous(state, rank)%key, PACKED_VALID_BIT)) cycle
        ! Tail-biting paths are eligible only when their final and remembered
        ! initial states agree after the last wrap.
        if (ibits(workspace%previous(state, rank)%key, &
             JTTY_TBCC_INFORMATION_BITS, PACKED_ORIGIN_BITS) /= state) cycle
        identity = reverse_identity(workspace%previous(state, rank)%key)
        ! The same complete word can close through more than one retained path;
        ! hash by all 46 bits before applying the global hypothesis limit.
        call locate_identity(identity, workspace%hash_keys, workspace%hash_indices, &
             workspace%hash_used, hash_slot, pool_index, found)
        if (found) then
          if (workspace%previous(state, rank)%metric > &
               workspace%pool(pool_index)%wava_metric .or. &
               (.not.(workspace%previous(state, rank)%metric < &
               workspace%pool(pool_index)%wava_metric) .and. &
               state < workspace%pool(pool_index)%start_state)) then
            workspace%pool(pool_index)%wava_metric = &
                 workspace%previous(state, rank)%metric
            workspace%pool(pool_index)%start_state = state
          end if
          cycle
        end if
        pool_count = pool_count + 1_int32
        pool_index = pool_count
        workspace%hash_used(hash_slot) = .true.
        workspace%hash_keys(hash_slot) = identity
        workspace%hash_indices(hash_slot) = pool_index
        workspace%pool(pool_index)%identity = identity
        workspace%pool(pool_index)%start_state = state
        workspace%pool(pool_index)%wava_metric = workspace%previous(state, rank)%metric
        call unpack_identity(identity, workspace%pool(pool_index)%bits)
        ! WAVA survivor metrics include history from earlier wraps.  Admission
        ! order is based on a clean, single-frame score of each complete word.
        call score_planned_identity(plan, workspace%sequence_energies, identity, &
             clean_metric, closed)
        if (.not.closed) error stop 'closed TBCC survivor failed clean scoring closure'
        workspace%pool(pool_index)%clean_metric = clean_metric
        ! This flag reports the polynomial remainder only.  Message admission,
        ! including the protocol's all-zero rejection, remains with the caller.
        workspace%pool(pool_index)%crc_valid = &
             jtty_tbcc_crc_valid(plan%code, workspace%pool(pool_index)%bits)
      end do
    end do

    do pool_index = 1, pool_count
      workspace%order(pool_index) = pool_index
    end do
    call sort_candidate_order_with_scratch(workspace%pool, workspace%order, &
         workspace%sort_scratch, pool_count)
    candidate_bits = 0_int32
    candidate_identities = 0_int64
    candidate_clean_metrics = NEGATIVE_METRIC
    candidate_wava_metrics = NEGATIVE_METRIC
    candidate_start_states = -1_int32
    candidate_crc_valid = .false.
    candidate_count = min(plan%hypothesis_count, pool_count)
    do output_rank = 1, candidate_count
      pool_index = workspace%order(output_rank)
      candidate_bits(:, output_rank) = workspace%pool(pool_index)%bits
      candidate_identities(output_rank) = workspace%pool(pool_index)%identity
      candidate_clean_metrics(output_rank) = workspace%pool(pool_index)%clean_metric
      candidate_wava_metrics(output_rank) = workspace%pool(pool_index)%wava_metric
      candidate_start_states(output_rank) = workspace%pool(pool_index)%start_state
      candidate_crc_valid(output_rank) = workspace%pool(pool_index)%crc_valid
    end do

    if (present(reference_present)) reference_present = .false.
    if (present(reference_pool_rank)) reference_pool_rank = 0_int32
    if (present(reference_h_rank)) reference_h_rank = 0_int32
    if (present(reference_bits)) then
      reference_identity = jtty_tbcc_candidate_identity(reference_bits)
      do output_rank = 1, pool_count
        pool_index = workspace%order(output_rank)
        if (workspace%pool(pool_index)%identity /= reference_identity) cycle
        if (present(reference_present)) reference_present = .true.
        if (present(reference_pool_rank)) reference_pool_rank = output_rank
        if (present(reference_h_rank)) then
          if (output_rank <= candidate_count) reference_h_rank = output_rank
        end if
        exit
      end do
    end if
  end subroutine jtty_tbcc_list_wava_optimized

  subroutine jtty_tbcc_list_wava_reference(correlations, code, coherent_length, &
       per_state_width, wraps, hypothesis_count, candidate_bits, &
       candidate_identities, candidate_clean_metrics, candidate_wava_metrics, &
       candidate_start_states, candidate_crc_valid, candidate_count, pool_count, &
       prune_reserved_zero, reference_bits, reference_present, &
       reference_pool_rank, reference_h_rank)
    complex(real32), intent(in) :: correlations(0:3, JTTY_TBCC_INFORMATION_BITS)
    type(jtty_tbcc_code_profile), intent(in) :: code
    integer(int32), intent(in) :: coherent_length, per_state_width, wraps
    integer(int32), intent(in) :: hypothesis_count
    integer(int32), intent(out) :: candidate_bits(JTTY_TBCC_INFORMATION_BITS, hypothesis_count)
    integer(int64), intent(out) :: candidate_identities(hypothesis_count)
    real(real64), intent(out) :: candidate_clean_metrics(hypothesis_count)
    real(real64), intent(out) :: candidate_wava_metrics(hypothesis_count)
    integer(int32), intent(out) :: candidate_start_states(hypothesis_count)
    logical, intent(out) :: candidate_crc_valid(hypothesis_count)
    integer(int32), intent(out) :: candidate_count, pool_count
    logical, intent(in), optional :: prune_reserved_zero
    integer(int32), intent(in), optional :: reference_bits(JTTY_TBCC_INFORMATION_BITS)
    logical, intent(out), optional :: reference_present
    integer(int32), intent(out), optional :: reference_pool_rank
    integer(int32), intent(out), optional :: reference_h_rank
    type(survivor_t), allocatable :: previous(:,:), current(:,:)
    type(pool_candidate_t), allocatable :: pool(:)
    integer(int32), allocatable :: order(:), hash_indices(:)
    integer(int32), allocatable :: block_starts(:), block_lengths(:)
    integer(int32), allocatable :: branch_bits(:,:,:), branch_end_states(:,:,:)
    integer(int64), allocatable :: hash_keys(:)
    real(real64), allocatable :: branch_metrics(:,:,:)
    logical, allocatable :: hash_used(:)
    integer(int32) :: block_length, block_start, branch_count, branch_word
    integer(int32) :: block_count, block_index, max_branch_count
    integer(int32) :: state_count, state, rank, wrap, end_state, output_rank
    integer(int32) :: input_bits(4), tones(4), pool_capacity, hash_capacity
    integer(int32) :: pool_index, hash_slot
    integer(int64) :: identity, reference_identity
    real(real64) :: branch_metric, clean_metric
    logical :: prune, closed, found
    type(survivor_t) :: extension

    call validate_decoder_arguments(code, coherent_length, per_state_width, &
         wraps, hypothesis_count)
    state_count = code%state_count
    block_count = (JTTY_TBCC_INFORMATION_BITS + coherent_length - 1_int32) / coherent_length
    max_branch_count = shiftl(1_int32, coherent_length)
    pool_capacity = state_count*per_state_width
    hash_capacity = 1_int32
    do while (hash_capacity < 2_int32*pool_capacity)
      hash_capacity = 2_int32*hash_capacity
    end do

    allocate(previous(0:state_count - 1, per_state_width))
    allocate(current(0:state_count - 1, per_state_width))
    allocate(pool(max(1_int32, pool_capacity)))
    allocate(order(max(1_int32, pool_capacity)))
    allocate(hash_keys(hash_capacity), hash_indices(hash_capacity))
    allocate(hash_used(hash_capacity))
    allocate(block_starts(block_count), block_lengths(block_count))
    allocate(branch_bits(4, 0:max_branch_count - 1, block_count))
    allocate(branch_end_states(0:state_count - 1, 0:max_branch_count - 1, block_count))
    allocate(branch_metrics(0:state_count - 1, 0:max_branch_count - 1, block_count))
    branch_bits = 0_int32
    branch_end_states = 0_int32
    branch_metrics = 0.0_real64
    do block_index = 1, block_count
      block_start = 1_int32 + (block_index - 1_int32)*coherent_length
      block_length = min(coherent_length, &
           JTTY_TBCC_INFORMATION_BITS - block_start + 1_int32)
      block_starts(block_index) = block_start
      block_lengths(block_index) = block_length
      branch_count = shiftl(1_int32, block_length)
      do branch_word = 0, branch_count - 1
        call unpack_branch_word(branch_word, block_length, input_bits)
        branch_bits(:, branch_word, block_index) = input_bits
        do state = 0, state_count - 1
          call jtty_tbcc_supertransition(code, state, &
               input_bits(1:block_length), end_state, tones(1:block_length))
          branch_end_states(state, branch_word, block_index) = end_state
          branch_metrics(state, branch_word, block_index) = &
               jtty_tbcc_coherent_metric(correlations, block_start, &
               tones(1:block_length))
        end do
      end do
    end do
    call clear_survivors(previous)
    do state = 0, state_count - 1
      previous(state, 1)%valid = .true.
      previous(state, 1)%metric = 0.0_real64
      previous(state, 1)%origin_state = state
    end do

    prune = .false.
    if (present(prune_reserved_zero)) prune = prune_reserved_zero
    do wrap = 1, wraps
      call reset_frame_identity(previous)
      do block_index = 1, block_count
        block_start = block_starts(block_index)
        block_length = block_lengths(block_index)
        branch_count = shiftl(1_int32, block_length)
        call clear_survivors(current)
        do state = 0, state_count - 1
          do rank = 1, per_state_width
            if (.not.previous(state, rank)%valid) cycle
            do branch_word = 0, branch_count - 1
              if (prune .and. block_contains_reserved(block_start, block_length)) then
                if (branch_bits(JTTY_TBCC_RESERVED_BIT - block_start + 1, &
                     branch_word, block_index) /= &
                     0_int32) cycle
              end if
              end_state = branch_end_states(state, branch_word, block_index)
              branch_metric = branch_metrics(state, branch_word, block_index)
              extension = previous(state, rank)
              extension%metric = extension%metric + branch_metric
              extension%bits(block_start:block_start + block_length - 1) = &
                   branch_bits(1:block_length, branch_word, block_index)
              call insert_survivor(current(end_state, :), extension, &
                   block_start + block_length - 1_int32)
            end do
          end do
        end do
        previous = current
      end do
    end do

    pool_count = 0_int32
    hash_used = .false.
    hash_keys = 0_int64
    hash_indices = 0_int32
    do state = 0, state_count - 1
      do rank = 1, per_state_width
        if (.not.previous(state, rank)%valid) cycle
        if (previous(state, rank)%origin_state /= state) cycle
        identity = jtty_tbcc_candidate_identity(previous(state, rank)%bits)
        call locate_identity(identity, hash_keys, hash_indices, hash_used, &
             hash_slot, pool_index, found)
        if (found) then
          if (previous(state, rank)%metric > pool(pool_index)%wava_metric .or. &
               (.not.(previous(state, rank)%metric < pool(pool_index)%wava_metric) .and. &
                state < pool(pool_index)%start_state)) then
            pool(pool_index)%wava_metric = previous(state, rank)%metric
            pool(pool_index)%start_state = state
          end if
          cycle
        end if
        pool_count = pool_count + 1_int32
        pool_index = pool_count
        hash_used(hash_slot) = .true.
        hash_keys(hash_slot) = identity
        hash_indices(hash_slot) = pool_index
        pool(pool_index)%bits = previous(state, rank)%bits
        pool(pool_index)%identity = identity
        pool(pool_index)%start_state = state
        pool(pool_index)%wava_metric = previous(state, rank)%metric
        call jtty_tbcc_score_candidate(code, correlations, coherent_length, &
             pool(pool_index)%bits, clean_metric, closed)
        if (.not.closed) error stop 'closed TBCC survivor failed clean scoring closure'
        pool(pool_index)%clean_metric = clean_metric
        pool(pool_index)%crc_valid = jtty_tbcc_crc_valid(code, pool(pool_index)%bits)
      end do
    end do

    do pool_index = 1, pool_count
      order(pool_index) = pool_index
    end do
    call sort_candidate_order(pool, order, pool_count)
    candidate_bits = 0_int32
    candidate_identities = 0_int64
    candidate_clean_metrics = NEGATIVE_METRIC
    candidate_wava_metrics = NEGATIVE_METRIC
    candidate_start_states = -1_int32
    candidate_crc_valid = .false.
    candidate_count = min(hypothesis_count, pool_count)
    do output_rank = 1, candidate_count
      pool_index = order(output_rank)
      candidate_bits(:, output_rank) = pool(pool_index)%bits
      candidate_identities(output_rank) = pool(pool_index)%identity
      candidate_clean_metrics(output_rank) = pool(pool_index)%clean_metric
      candidate_wava_metrics(output_rank) = pool(pool_index)%wava_metric
      candidate_start_states(output_rank) = pool(pool_index)%start_state
      candidate_crc_valid(output_rank) = pool(pool_index)%crc_valid
    end do

    if (present(reference_present)) reference_present = .false.
    if (present(reference_pool_rank)) reference_pool_rank = 0_int32
    if (present(reference_h_rank)) reference_h_rank = 0_int32
    if (present(reference_bits)) then
      reference_identity = jtty_tbcc_candidate_identity(reference_bits)
      do output_rank = 1, pool_count
        pool_index = order(output_rank)
        if (pool(pool_index)%identity /= reference_identity) cycle
        if (.not.all(pool(pool_index)%bits == reference_bits)) cycle
        if (present(reference_present)) reference_present = .true.
        if (present(reference_pool_rank)) reference_pool_rank = output_rank
        if (present(reference_h_rank)) then
          if (output_rank <= candidate_count) reference_h_rank = output_rank
        end if
        exit
      end do
    end if

    deallocate(previous, current, pool, order, hash_keys, hash_indices, hash_used)
    deallocate(block_starts, block_lengths, branch_bits, branch_end_states, branch_metrics)
  end subroutine jtty_tbcc_list_wava_reference


  pure subroutine planned_supertransition(plan, start_state, input_bits, &
       block_length, end_state, tones, tone_word)
    type(jtty_tbcc_decoder_plan), intent(in) :: plan
    integer(int32), intent(in) :: start_state, input_bits(4), block_length
    integer(int32), intent(out) :: end_state, tones(4), tone_word
    integer(int32) :: offset, state

    state = start_state
    tones = 0_int32
    tone_word = 0_int32
    do offset = 1, block_length
      tones(offset) = plan%tone(state, input_bits(offset))
      tone_word = 4_int32*tone_word + tones(offset)
      state = plan%next_state(state, input_bits(offset))
    end do
    end_state = state
  end subroutine planned_supertransition

  subroutine precompute_sequence_energies(plan, correlations, energies)
    type(jtty_tbcc_decoder_plan), intent(in) :: plan
    complex(real32), intent(in) :: correlations(0:3, JTTY_TBCC_INFORMATION_BITS)
    real(real64), intent(out) :: energies(:)
    complex(real32) :: coherent_sum
    integer(int32) :: block_index, block_length, block_start
    integer(int32) :: offset, sequence_count, sequence_word, tone

    do block_index = 1, plan%block_count
      block_start = plan%block_starts(block_index)
      block_length = plan%block_lengths(block_index)
      sequence_count = shiftl(1_int32, 2_int32*block_length)
      do sequence_word = 0, sequence_count - 1
        coherent_sum = cmplx(0.0_real32, 0.0_real32, real32)
        do offset = 1, block_length
          tone = ibits(sequence_word, 2_int32*(block_length - offset), 2)
          coherent_sum = coherent_sum + correlations(tone, block_start + offset - 1_int32)
        end do
        energies(plan%energy_offsets(block_index) + sequence_word) = &
             real(coherent_sum*conjg(coherent_sum), real64) / real(block_length, real64)
      end do
    end do
  end subroutine precompute_sequence_energies

  subroutine score_planned_identity(plan, energies, identity, metric, closed)
    type(jtty_tbcc_decoder_plan), intent(in) :: plan
    real(real64), intent(in) :: energies(:)
    integer(int64), intent(in) :: identity
    real(real64), intent(out) :: metric
    logical, intent(out) :: closed
    integer(int32) :: bit_index, block_index, branch_word, offset, state, start_state

    start_state = 0_int32
    do bit_index = JTTY_TBCC_INFORMATION_BITS - plan%code%memory_nu + 1, &
         JTTY_TBCC_INFORMATION_BITS
      start_state = iand(ior(shiftl(start_state, 1), &
           merge(1_int32, 0_int32, btest(identity, bit_index - 1))), &
           plan%state_count - 1_int32)
    end do
    state = start_state
    metric = 0.0_real64
    do block_index = 1, plan%block_count
      branch_word = 0_int32
      do offset = 0, plan%block_lengths(block_index) - 1
        branch_word = 2_int32*branch_word + merge(1_int32, 0_int32, &
             btest(identity, plan%block_starts(block_index) + offset - 1_int32))
      end do
      metric = metric + energies( &
           plan%branch_sequence_indices(state, branch_word, block_index))
      state = plan%branch_end_states(state, branch_word, block_index)
    end do
    closed = state == start_state
  end subroutine score_planned_identity

  pure integer(int64) function reverse_identity(key) result(identity)
    integer(int64), intent(in) :: key
    integer(int32) :: bit_index

    identity = 0_int64
    do bit_index = 0, JTTY_TBCC_INFORMATION_BITS - 1
      if (btest(key, bit_index)) identity = ibset(identity, JTTY_TBCC_INFORMATION_BITS - bit_index - 1)
    end do
  end function reverse_identity

  pure subroutine unpack_identity(identity, bits)
    integer(int64), intent(in) :: identity
    integer(int32), intent(out) :: bits(JTTY_TBCC_INFORMATION_BITS)
    integer(int32) :: bit_index

    do bit_index = 1, JTTY_TBCC_INFORMATION_BITS
      bits(bit_index) = merge(1_int32, 0_int32, btest(identity, bit_index - 1))
    end do
  end subroutine unpack_identity

  subroutine clear_packed_survivors(survivors)
    type(packed_survivor_t), intent(inout) :: survivors(0:, :)
    integer(int32) :: rank, state

    do rank = 1, size(survivors, 2)
      do state = 0, size(survivors, 1) - 1
        survivors(state, rank)%key = 0_int64
        survivors(state, rank)%metric = NEGATIVE_METRIC
      end do
    end do
  end subroutine clear_packed_survivors

  subroutine reset_packed_frame_identity(survivors)
    type(packed_survivor_t), intent(inout) :: survivors(0:, :)
    integer(int32) :: rank, state

    do rank = 1, size(survivors, 2)
      do state = 0, size(survivors, 1) - 1
        if (.not.btest(survivors(state, rank)%key, PACKED_VALID_BIT)) cycle
        survivors(state, rank)%key = ibset(shiftl(int(state, int64), JTTY_TBCC_INFORMATION_BITS), PACKED_VALID_BIT)
      end do
    end do
  end subroutine reset_packed_frame_identity

  subroutine advance_packed_survivors(plan, block_index, prune, energies, previous, current, ordered_metrics)
    type(jtty_tbcc_decoder_plan), intent(in) :: plan
    integer(int32), intent(in) :: block_index
    logical, intent(in) :: prune, ordered_metrics
    real(real64), intent(in) :: energies(:)
    type(packed_survivor_t), intent(in) :: previous(0:, :)
    type(packed_survivor_t), intent(inout) :: current(0:, :)
    type(packed_survivor_t) :: selected(4), extension
    integer(int32) :: branch_count, branch_word, end_state, state, state_stride
    integer(int32) :: rank, width
    integer(int64) :: branch_identity
    real(real64) :: branch_metric, metric
    logical :: prune_block

    width = plan%per_state_width
    branch_count = plan%branch_counts(block_index)
    state_stride = plan%state_count / branch_count
    prune_block = prune .and. block_contains_reserved( &
         plan%block_starts(block_index), plan%block_lengths(block_index))
    do end_state = 0, plan%state_count - 1
      selected%key = 0_int64
      branch_word = iand(end_state, branch_count - 1_int32)
      branch_identity = plan%branch_identities(branch_word, block_index)
      if (.not.(prune_block .and. btest(branch_identity, JTTY_TBCC_INFORMATION_BITS - JTTY_TBCC_RESERVED_BIT))) then
        ! The low L destination bits are the input word; the discarded high
        ! L source bits enumerate every predecessor in the original order.
        do state = end_state / branch_count, plan%state_count - 1, state_stride
          branch_metric = energies(plan%branch_sequence_indices(state, branch_word, block_index))
          do rank = 1, width
            if (.not.btest(previous(state, rank)%key, PACKED_VALID_BIT)) cycle
            metric = previous(state, rank)%metric + branch_metric
            if (btest(selected(width)%key, PACKED_VALID_BIT)) then
              if (metric < selected(width)%metric) then
                ! Lower ranks receive the same branch metric and cannot recover.
                if (ordered_metrics) exit
                cycle
              end if
            end if
            extension = previous(state, rank)
            extension%metric = metric
            extension%key = ior(extension%key, branch_identity)
            call insert_packed_survivor(selected, width, extension, block_index == 1)
          end do
        end do
      end if
      do rank = 1, width
        current(end_state, rank) = selected(rank)
      end do
    end do
  end subroutine advance_packed_survivors

  subroutine insert_packed_survivor(survivors, width, candidate, deduplicate)
    type(packed_survivor_t), intent(inout) :: survivors(4)
    type(packed_survivor_t), intent(in) :: candidate
    integer(int32), intent(in) :: width
    logical, intent(in) :: deduplicate
    integer(int32) :: insertion_slot, slot

    ! Resetting a wrap's origins and identities can duplicate a state's ranks.
    ! After the first block, an origin and prefix determine exactly one path,
    ! so extending distinct retained paths cannot create another duplicate.
    if (deduplicate) then
      do slot = 1, width
        if (.not.btest(survivors(slot)%key, PACKED_VALID_BIT)) cycle
        if (survivors(slot)%key == candidate%key) then
          if (candidate%metric <= survivors(slot)%metric) return
          do insertion_slot = 1, 3
            if (insertion_slot >= slot .and. insertion_slot < width) &
                 survivors(insertion_slot) = survivors(insertion_slot + 1)
          end do
          survivors(width)%key = 0_int64
          exit
        end if
      end do
    end if

    insertion_slot = width + 1_int32
    do slot = 1, width
      if (.not.btest(survivors(slot)%key, PACKED_VALID_BIT) .or. &
           packed_survivor_precedes(candidate, survivors(slot))) then
        insertion_slot = slot
        exit
      end if
    end do
    if (insertion_slot > width) return
    do slot = 4, 2, -1
      if (slot <= width .and. slot > insertion_slot) survivors(slot) = survivors(slot - 1)
    end do
    survivors(insertion_slot) = candidate
  end subroutine insert_packed_survivor

  pure logical function packed_survivor_precedes(left, right) &
       result(precedes)
    type(packed_survivor_t), intent(in) :: left, right

    if (left%metric > right%metric) then
      precedes = .true.
      return
    else if (left%metric < right%metric) then
      precedes = .false.
      return
    end if
    precedes = left%key < right%key
  end function packed_survivor_precedes

  subroutine validate_optimized_storage(plan, workspace, candidate_bits, &
       candidate_identities, candidate_clean_metrics, candidate_wava_metrics, &
       candidate_start_states, candidate_crc_valid)
    type(jtty_tbcc_decoder_plan), intent(in) :: plan
    type(jtty_tbcc_decoder_workspace), intent(in) :: workspace
    integer(int32), intent(in) :: candidate_bits(:, :)
    integer(int64), intent(in) :: candidate_identities(:)
    real(real64), intent(in) :: candidate_clean_metrics(:), candidate_wava_metrics(:)
    integer(int32), intent(in) :: candidate_start_states(:)
    logical, intent(in) :: candidate_crc_valid(:)

    if (.not.plan%initialized) error stop 'TBCC list decoder plan is not initialized'
    if (.not.workspace%initialized) &
         error stop 'TBCC list decoder workspace is not initialized'
    if (size(workspace%previous, 1) /= plan%state_count .or. &
         size(workspace%previous, 2) /= plan%per_state_width .or. &
         size(workspace%sequence_energies) /= plan%energy_count) &
         error stop 'TBCC list decoder workspace does not match plan'
    if (size(candidate_bits, 1) /= JTTY_TBCC_INFORMATION_BITS .or. &
         size(candidate_bits, 2) < plan%hypothesis_count .or. &
         size(candidate_identities) < plan%hypothesis_count .or. &
         size(candidate_clean_metrics) < plan%hypothesis_count .or. &
         size(candidate_wava_metrics) < plan%hypothesis_count .or. &
         size(candidate_start_states) < plan%hypothesis_count .or. &
         size(candidate_crc_valid) < plan%hypothesis_count) &
         error stop 'TBCC list decoder output storage is too small'
  end subroutine validate_optimized_storage


  subroutine validate_decoder_arguments(code, coherent_length, per_state_width, &
       wraps, hypothesis_count)
    type(jtty_tbcc_code_profile), intent(in) :: code
    integer(int32), intent(in) :: coherent_length, per_state_width, wraps
    integer(int32), intent(in) :: hypothesis_count
    if (.not.jtty_tbcc_code_profile_is_supported(code)) &
         error stop 'TBCC list decoder requires a supported code profile'
    if (coherent_length /= 1_int32 .and. coherent_length /= 2_int32 .and. &
         coherent_length /= 4_int32) &
         error stop 'TBCC list decoder supports coherent lengths 1, 2, and 4'
    if (per_state_width < 1_int32 .or. per_state_width > 4_int32) &
         error stop 'TBCC list decoder per-state width must be between 1 and 4'
    if (wraps < 1_int32 .or. wraps > 2_int32) &
         error stop 'TBCC list decoder supports one or two wraps'
    if (hypothesis_count < 1_int32 .or. &
         hypothesis_count > JTTY_TBCC_MAX_HYPOTHESES) &
         error stop 'TBCC list decoder hypothesis count must be between 1 and 4'
  end subroutine validate_decoder_arguments

  subroutine clear_survivors(survivors)
    type(survivor_t), intent(inout) :: survivors(0:, :)
    integer(int32) :: rank, state

    do rank = 1, size(survivors, 2)
      do state = 0, size(survivors, 1) - 1
        survivors(state, rank)%valid = .false.
        survivors(state, rank)%metric = NEGATIVE_METRIC
        survivors(state, rank)%origin_state = -1_int32
        survivors(state, rank)%bits = 0_int32
      end do
    end do
  end subroutine clear_survivors

  subroutine reset_frame_identity(survivors)
    type(survivor_t), intent(inout) :: survivors(0:, :)
    integer(int32) :: rank, state

    do rank = 1, size(survivors, 2)
      do state = 0, size(survivors, 1) - 1
        if (.not.survivors(state, rank)%valid) cycle
        survivors(state, rank)%origin_state = state
        survivors(state, rank)%bits = 0_int32
      end do
    end do
  end subroutine reset_frame_identity

  pure subroutine unpack_branch_word(branch_word, block_length, input_bits)
    integer(int32), intent(in) :: branch_word, block_length
    integer(int32), intent(out) :: input_bits(4)
    integer(int32) :: offset

    input_bits = 0_int32
    do offset = 1, block_length
      input_bits(offset) = ibits(branch_word, block_length - offset, 1)
    end do
  end subroutine unpack_branch_word

  pure logical function block_contains_reserved(block_start, block_length) &
       result(contains_reserved)
    integer(int32), intent(in) :: block_start, block_length

    contains_reserved = block_start <= JTTY_TBCC_RESERVED_BIT .and. &
         block_start + block_length > JTTY_TBCC_RESERVED_BIT
  end function block_contains_reserved

  subroutine insert_survivor(survivors, candidate, prefix_length)
    type(survivor_t), intent(inout) :: survivors(:)
    type(survivor_t), intent(in) :: candidate
    integer(int32), intent(in) :: prefix_length
    integer(int32) :: slot, insertion_slot

    do slot = 1, size(survivors)
      if (.not.survivors(slot)%valid) cycle
      if (survivors(slot)%origin_state /= candidate%origin_state) cycle
      if (.not.all(survivors(slot)%bits(1:prefix_length) == &
           candidate%bits(1:prefix_length))) cycle
      if (candidate%metric <= survivors(slot)%metric) return
      do insertion_slot = slot, size(survivors) - 1
        survivors(insertion_slot) = survivors(insertion_slot + 1)
      end do
      survivors(size(survivors))%valid = .false.
      survivors(size(survivors))%metric = NEGATIVE_METRIC
      survivors(size(survivors))%origin_state = -1_int32
      survivors(size(survivors))%bits = 0_int32
      exit
    end do

    insertion_slot = size(survivors) + 1
    do slot = 1, size(survivors)
      if (.not.survivors(slot)%valid .or. &
           survivor_precedes(candidate, survivors(slot), prefix_length)) then
        insertion_slot = slot
        exit
      end if
    end do
    if (insertion_slot > size(survivors)) return
    do slot = size(survivors), insertion_slot + 1, -1
      survivors(slot) = survivors(slot - 1)
    end do
    survivors(insertion_slot) = candidate
  end subroutine insert_survivor

  pure logical function survivor_precedes(left, right, prefix_length) result(precedes)
    type(survivor_t), intent(in) :: left, right
    integer(int32), intent(in) :: prefix_length
    integer(int32) :: bit_index

    if (left%metric > right%metric) then
      precedes = .true.
      return
    else if (left%metric < right%metric) then
      precedes = .false.
      return
    else
      precedes = left%metric > right%metric
    end if
    if (left%origin_state /= right%origin_state) then
      precedes = left%origin_state < right%origin_state
      return
    end if
    do bit_index = 1, prefix_length
      if (left%bits(bit_index) == right%bits(bit_index)) cycle
      precedes = left%bits(bit_index) < right%bits(bit_index)
      return
    end do
    precedes = .false.
  end function survivor_precedes

  subroutine locate_identity(identity, hash_keys, hash_indices, hash_used, &
       hash_slot, pool_index, found)
    integer(int64), intent(in) :: identity
    integer(int64), intent(in) :: hash_keys(:)
    integer(int32), intent(in) :: hash_indices(:)
    logical, intent(in) :: hash_used(:)
    integer(int32), intent(out) :: hash_slot, pool_index
    logical, intent(out) :: found

    hash_slot = 1_int32 + int(modulo(identity, int(size(hash_keys), int64)), int32)
    do
      if (.not.hash_used(hash_slot)) then
        pool_index = 0_int32
        found = .false.
        return
      end if
      if (hash_keys(hash_slot) == identity) then
        pool_index = hash_indices(hash_slot)
        found = .true.
        return
      end if
      hash_slot = hash_slot + 1_int32
      if (hash_slot > size(hash_keys)) hash_slot = 1_int32
    end do
  end subroutine locate_identity

  subroutine sort_candidate_order(pool, order, count)
    type(pool_candidate_t), intent(in) :: pool(:)
    integer(int32), intent(inout) :: order(:)
    integer(int32), intent(in) :: count
    integer(int32), allocatable :: scratch(:)

    if (count <= 1_int32) return
    allocate(scratch(count))
    call sort_candidate_order_with_scratch(pool, order, scratch, count)
    deallocate(scratch)
  end subroutine sort_candidate_order

  subroutine sort_candidate_order_with_scratch(pool, order, scratch, count)
    type(pool_candidate_t), intent(in) :: pool(:)
    integer(int32), intent(inout) :: order(:), scratch(:)
    integer(int32), intent(in) :: count
    integer(int32) :: left, middle, right, width

    if (count <= 1_int32) return
    width = 1_int32
    do while (width < count)
      left = 1_int32
      do while (left <= count)
        middle = min(left + width, count + 1_int32)
        right = min(left + 2_int32*width - 1_int32, count)
        call merge_candidate_order(pool, order, scratch, left, middle, right)
        left = left + 2_int32*width
      end do
      order(1:count) = scratch(1:count)
      width = 2_int32*width
    end do
  end subroutine sort_candidate_order_with_scratch

  subroutine merge_candidate_order(pool, order, scratch, left, middle, right)
    type(pool_candidate_t), intent(in) :: pool(:)
    integer(int32), intent(in) :: order(:), left, middle, right
    integer(int32), intent(inout) :: scratch(:)
    integer(int32) :: left_cursor, right_cursor, output

    left_cursor = left
    right_cursor = middle
    do output = left, right
      if (left_cursor >= middle) then
        scratch(output) = order(right_cursor)
        right_cursor = right_cursor + 1_int32
      else if (right_cursor > right) then
        scratch(output) = order(left_cursor)
        left_cursor = left_cursor + 1_int32
      else if (candidate_precedes(pool(order(left_cursor)), &
           pool(order(right_cursor)))) then
        scratch(output) = order(left_cursor)
        left_cursor = left_cursor + 1_int32
      else
        scratch(output) = order(right_cursor)
        right_cursor = right_cursor + 1_int32
      end if
    end do
  end subroutine merge_candidate_order

  pure logical function candidate_precedes(left, right) result(precedes)
    type(pool_candidate_t), intent(in) :: left, right

    if (left%clean_metric > right%clean_metric) then
      precedes = .true.
    else if (left%clean_metric < right%clean_metric) then
      precedes = .false.
    else if (left%identity /= right%identity) then
      precedes = left%identity < right%identity
    else if (left%start_state /= right%start_state) then
      precedes = left%start_state < right%start_state
    else
      precedes = left%wava_metric > right%wava_metric
    end if
  end function candidate_precedes

end module jtty_tbcc_list_decoder