| 
									
										
										
										
											2018-06-16 16:42:02 -05:00
										 |  |  | subroutine bpdecode128_90(llr,apmask,maxiterations,message77,cw,nharderror,iter)
 | 
					
						
							| 
									
										
										
										
											2018-06-13 09:40:04 -05:00
										 |  |  | !
 | 
					
						
							|  |  |  | ! A log-domain belief propagation decoder for the (128,90) code.
 | 
					
						
							|  |  |  | !
 | 
					
						
							| 
									
										
										
										
											2018-06-16 16:42:02 -05:00
										 |  |  |   use iso_c_binding, only: c_loc,c_size_t
 | 
					
						
							|  |  |  |   use crc
 | 
					
						
							|  |  |  |   integer, parameter:: N=128, K=90, M=N-K
 | 
					
						
							|  |  |  |   integer*1 cw(N),apmask(N)
 | 
					
						
							|  |  |  |   integer*1 decoded(K)
 | 
					
						
							|  |  |  |   integer*1 message77(77)
 | 
					
						
							| 
									
										
										
										
											2018-07-07 09:44:52 -05:00
										 |  |  |   integer Nm(11,M)   
 | 
					
						
							|  |  |  |   integer Mn(3,N) 
 | 
					
						
							|  |  |  |   integer nrw(M)
 | 
					
						
							| 
									
										
										
										
											2018-06-16 16:42:02 -05:00
										 |  |  |   integer synd(M)
 | 
					
						
							|  |  |  |   real tov(4,N)
 | 
					
						
							| 
									
										
										
										
											2018-07-07 09:44:52 -05:00
										 |  |  |   real toc(11,M)
 | 
					
						
							|  |  |  |   real tanhtoc(11,M)
 | 
					
						
							| 
									
										
										
										
											2018-06-16 16:42:02 -05:00
										 |  |  |   real zn(N)
 | 
					
						
							|  |  |  |   real llr(N)
 | 
					
						
							|  |  |  |   real Tmn
 | 
					
						
							| 
									
										
										
										
											2018-06-13 09:40:04 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-07 09:44:52 -05:00
										 |  |  |   include "ldpc_128_90_reordered_parity.f90"
 | 
					
						
							| 
									
										
										
										
											2018-06-13 09:40:04 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-16 16:42:02 -05:00
										 |  |  |   decoded=0
 | 
					
						
							|  |  |  |   toc=0
 | 
					
						
							|  |  |  |   tov=0
 | 
					
						
							|  |  |  |   tanhtoc=0
 | 
					
						
							| 
									
										
										
										
											2018-06-13 09:40:04 -05:00
										 |  |  | ! initialize messages to checks
 | 
					
						
							| 
									
										
										
										
											2018-06-16 16:42:02 -05:00
										 |  |  |   do j=1,M
 | 
					
						
							|  |  |  |     do i=1,nrw(j)
 | 
					
						
							|  |  |  |       toc(i,j)=llr((Nm(i,j)))
 | 
					
						
							|  |  |  |     enddo
 | 
					
						
							| 
									
										
										
										
											2018-06-13 09:40:04 -05:00
										 |  |  |   enddo
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-16 16:42:02 -05:00
										 |  |  |   ncnt=0
 | 
					
						
							| 
									
										
										
										
											2020-03-19 11:00:21 -04:00
										 |  |  |   nclast=0
 | 
					
						
							| 
									
										
										
										
											2018-06-13 09:40:04 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-16 16:42:02 -05:00
										 |  |  |   do iter=0,maxiterations
 | 
					
						
							| 
									
										
										
										
											2018-06-13 09:40:04 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | ! Update bit log likelihood ratios (tov=0 in iteration 0).
 | 
					
						
							|  |  |  |     do i=1,N
 | 
					
						
							| 
									
										
										
										
											2018-06-16 16:42:02 -05:00
										 |  |  |       if( apmask(i) .ne. 1 ) then
 | 
					
						
							| 
									
										
										
										
											2018-07-07 09:44:52 -05:00
										 |  |  |         zn(i)=llr(i)+sum(tov(1:ncw,i))
 | 
					
						
							| 
									
										
										
										
											2018-06-16 16:42:02 -05:00
										 |  |  |       else
 | 
					
						
							|  |  |  |         zn(i)=llr(i)
 | 
					
						
							|  |  |  |       endif
 | 
					
						
							| 
									
										
										
										
											2018-06-13 09:40:04 -05:00
										 |  |  |     enddo
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-16 16:42:02 -05:00
										 |  |  | ! 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
 | 
					
						
							|  |  |  | !     if( mod(synd(i),2) .ne. 0 ) write(*,*) 'check ',i,' unsatisfied'
 | 
					
						
							|  |  |  |     enddo
 | 
					
						
							|  |  |  | !   write(*,*) 'number of unsatisfied parity checks ',ncheck
 | 
					
						
							|  |  |  |     if( ncheck .eq. 0 ) then ! we have a codeword - reorder the columns and return it
 | 
					
						
							|  |  |  |       decoded=cw(1:K)
 | 
					
						
							|  |  |  |       call chkcrc13a(decoded,nbadcrc)
 | 
					
						
							|  |  |  |       if(nbadcrc.eq.0) then
 | 
					
						
							|  |  |  |         message77=decoded(1:77)
 | 
					
						
							|  |  |  |         nharderror=count( (2*cw-1)*llr .lt. 0.0 )
 | 
					
						
							|  |  |  |         return
 | 
					
						
							|  |  |  |       endif
 | 
					
						
							| 
									
										
										
										
											2018-06-13 09:40:04 -05:00
										 |  |  |     endif
 | 
					
						
							| 
									
										
										
										
											2018-06-16 16:42:02 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     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
 | 
					
						
							| 
									
										
										
										
											2018-07-07 09:44:52 -05:00
										 |  |  |       if( ncnt .ge. 3 .and. iter .ge. 5 .and. ncheck .gt. 10) then
 | 
					
						
							| 
									
										
										
										
											2018-06-16 16:42:02 -05:00
										 |  |  |         nharderror=-1
 | 
					
						
							|  |  |  |         return
 | 
					
						
							|  |  |  |       endif
 | 
					
						
							| 
									
										
										
										
											2018-06-13 09:40:04 -05:00
										 |  |  |     endif
 | 
					
						
							| 
									
										
										
										
											2018-06-16 16:42:02 -05:00
										 |  |  |     nclast=ncheck
 | 
					
						
							| 
									
										
										
										
											2018-06-13 09:40:04 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | ! Send messages from bits to check nodes 
 | 
					
						
							| 
									
										
										
										
											2018-06-16 16:42:02 -05:00
										 |  |  |     do j=1,M
 | 
					
						
							|  |  |  |       do i=1,nrw(j)
 | 
					
						
							|  |  |  |         ibj=Nm(i,j)
 | 
					
						
							|  |  |  |         toc(i,j)=zn(ibj)  
 | 
					
						
							| 
									
										
										
										
											2018-07-07 09:44:52 -05:00
										 |  |  |         do kk=1,ncw ! subtract off what the bit had received from the check
 | 
					
						
							| 
									
										
										
										
											2018-06-16 16:42:02 -05:00
										 |  |  |           if( Mn(kk,ibj) .eq. j ) then  
 | 
					
						
							|  |  |  |             toc(i,j)=toc(i,j)-tov(kk,ibj)
 | 
					
						
							|  |  |  |           endif
 | 
					
						
							|  |  |  |         enddo
 | 
					
						
							| 
									
										
										
										
											2018-06-13 09:40:04 -05:00
										 |  |  |       enddo
 | 
					
						
							|  |  |  |     enddo
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! send messages from check nodes to variable nodes
 | 
					
						
							| 
									
										
										
										
											2018-06-16 16:42:02 -05:00
										 |  |  |     do i=1,M
 | 
					
						
							| 
									
										
										
										
											2018-07-07 09:44:52 -05:00
										 |  |  |       tanhtoc(1:11,i)=tanh(-toc(1:11,i)/2)
 | 
					
						
							| 
									
										
										
										
											2018-06-16 16:42:02 -05:00
										 |  |  |     enddo
 | 
					
						
							| 
									
										
										
										
											2018-06-13 09:40:04 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-16 16:42:02 -05:00
										 |  |  |     do j=1,N
 | 
					
						
							| 
									
										
										
										
											2018-07-07 09:44:52 -05:00
										 |  |  |       do i=1,ncw
 | 
					
						
							| 
									
										
										
										
											2018-06-16 16:42:02 -05:00
										 |  |  |         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
 | 
					
						
							| 
									
										
										
										
											2018-06-13 09:40:04 -05:00
										 |  |  |     enddo
 | 
					
						
							| 
									
										
										
										
											2018-06-16 16:42:02 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-13 09:40:04 -05:00
										 |  |  |   enddo
 | 
					
						
							| 
									
										
										
										
											2018-06-16 16:42:02 -05:00
										 |  |  |   nharderror=-1
 | 
					
						
							|  |  |  |   return
 | 
					
						
							| 
									
										
										
										
											2018-06-13 09:40:04 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | end subroutine bpdecode128_90
 |