summaryrefslogtreecommitdiff
path: root/lib/fst4/decode240_74.f90
blob: a4bf3ea7bbda784906861458804a26a93b54c802 (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
module fst4_ldpc_74
   use fst4_osd_workspace, only: fst4_osd_workspace_type
   use fastosd240_74_module, only: fastosd240_74_owned
   private
   public :: decode240_74_owned
contains
subroutine decode240_74_owned(work,llr,Keff,maxosd,norder,apmask,message74,cw,ntype,nharderror,dmin)
!
! A hybrid bp/osd decoder for the (240,74) code.
!
! maxosd<0: do bp only
! maxosd=0: do bp and then call osd once with channel llrs
! maxosd>1: do bp and then call osd maxosd times with saved bp outputs
! norder  : osd decoding depth
!
   type(fst4_osd_workspace_type), intent(inout), target :: work
   integer, parameter:: N=240, K=74, M=N-K
   integer*1 cw(N),apmask(N)
   integer*1 nxor(N),hdec(N)
   integer*1 message74(74),m74(74)
   integer nrw(M),ncw
   integer Nm(5,M)
   integer Mn(3,N)  ! 3 checks per bit
   integer synd(M)
   real tov(3,N)
   real toc(5,M)
   real tanhtoc(5,M)
   real zn(N),zsum(N),zsave(N,3)
   real llr(N)
   real Tmn

   include "ldpc_240_74_parity.f90"

   maxiterations=30
   if(Keff.eq.50) maxiterations=1

   nosd=0
   if(maxosd.gt.3) maxosd=3
   if(maxosd.eq.0) then ! osd with channel llrs
      nosd=1
      zsave(:,1)=llr
   elseif(maxosd.gt.0) then !
      nosd=maxosd
   elseif(maxosd.lt.0) then ! just bp
      nosd=0
   endif

   if(maxosd.eq.0) goto 73

   toc=0
   tov=0
   tanhtoc=0
! initialize messages to checks
   do j=1,M
      do i=1,nrw(j)
         toc(i,j)=llr((Nm(i,j)))
      enddo
   enddo

   ncnt=0
   nclast=0
   zsum=0.0
   do iter=0,maxiterations
! Update bit log likelihood ratios (tov=0 in iteration 0).
      do i=1,N
         if( apmask(i) .ne. 1 ) then
            zn(i)=llr(i)+sum(tov(1:ncw,i))
         else
            zn(i)=llr(i)
         endif
      enddo
      zsum=zsum+zn
      if(iter.gt.0 .and. iter.le.maxosd) then
         zsave(:,iter)=zsum
      endif

! Check to see if we have a codeword (check before we do any iteration).
      cw=0
      where( zn .gt. 0. ) cw=1
      ncheck=0
      do i=1,M
         synd(i)=sum(cw(Nm(1:nrw(i),i)))
         if( mod(synd(i),2) .ne. 0 ) ncheck=ncheck+1
      enddo
      if( ncheck .eq. 0 ) then ! we have a codeword - if crc is good, return it
         m74=0
         m74(1:74)=cw(1:74)
         call get_crc24(m74,74,nbadcrc)
         if(nbadcrc.eq.0) then
            message74=cw(1:74)
            hdec=0
            where(llr .ge. 0) hdec=1
            nxor=ieor(hdec,cw)
            nharderror=sum(nxor)
            dmin=sum(nxor*abs(llr))
            ntype=1
            return
         endif
      endif

  if( iter.gt.0 ) then  ! this code block implements an early stopping criterion
!      if( iter.gt.10000 ) then  ! this code block implements an early stopping criterion
         nd=ncheck-nclast
         if( nd .lt. 0 ) then ! # of unsatisfied parity checks decreased
            ncnt=0  ! reset counter
         else
            ncnt=ncnt+1
         endif
!    write(*,*) iter,ncheck,nd,ncnt
         if( ncnt .ge. 5 .and. iter .ge. 10 .and. ncheck .gt. 15) then
            nharderror=-1
            exit
         endif
      endif
      nclast=ncheck

! Send messages from bits to check nodes
      do j=1,M
         do i=1,nrw(j)
            ibj=Nm(i,j)
            toc(i,j)=zn(ibj)
            do kk=1,ncw ! subtract off what the bit had received from the check
               if( Mn(kk,ibj) .eq. j ) then
                  toc(i,j)=toc(i,j)-tov(kk,ibj)
               endif
            enddo
         enddo
      enddo

! send messages from check nodes to variable nodes
      do i=1,M
         tanhtoc(1:5,i)=tanh(-toc(1:5,i)/2)
      enddo

      do j=1,N
         do i=1,ncw
            ichk=Mn(i,j)  ! Mn(:,j) are the checks that include bit j
            Tmn=product(tanhtoc(1:nrw(ichk),ichk),mask=Nm(1:nrw(ichk),ichk).ne.j)
            call platanh(-Tmn,y)
!      y=atanh(-Tmn)
            tov(i,j)=2*y
         enddo
      enddo

   enddo   ! bp iterations

73 continue
   do i=1,nosd
      zn=zsave(:,i)
!      call osd240_74(zn,Keff,apmask,norder,message74,cw,nharderror,dminosd)
      call fastosd240_74_owned(work,zn,Keff,apmask,norder,message74,cw,nharderror,dminosd)
      if(nharderror.gt.0) then
         hdec=0
         where(llr .ge. 0) hdec=1
         nxor=ieor(hdec,cw)
         nharderror=sum(nxor) ! nharderror based on input llrs
         dmin=sum(nxor*abs(llr))
         ntype=1+i
         return
      endif
   enddo

   ntype=0
   nharderror=-1
   dminosd=0.0

   return
end subroutine decode240_74_owned

end module fst4_ldpc_74

subroutine decode240_74(llr,Keff,maxosd,norder,apmask,message74,cw,ntype,nharderror,dmin)
   use fst4_ldpc_74, only: decode240_74_owned
   use fst4_osd_workspace, only: fst4_osd_workspace_type
   type(fst4_osd_workspace_type), save :: legacy
   real llr(240),dmin
   integer*1 apmask(240),message74(74),cw(240)
   call decode240_74_owned(legacy,llr,Keff,maxosd,norder,apmask,message74,cw,ntype,nharderror,dmin)
end subroutine