Advertisement
Guest User

Untitled

a guest
Jun 21st, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.         subroutine checksum(buffer,length,sum32)
  2.  
  3. C       Calculate a 32-bit 1's complement checksum of the input buffer, adding
  4. C       it to the value of sum32.  This algorithm assumes that the buffer
  5. C       length is a multiple of 4 bytes.
  6.  
  7. C       a double precision value (which has at least 48 bits of precision)
  8. C       is used to accumulate the checksum because standard Fortran does not
  9. C       support an unsigned integer datatype.
  10.  
  11. C       buffer  - integer buffer to be summed
  12. C       length  - number of bytes in the buffer (must be multiple of 4)
  13. C       sum32   - double precision checksum value (The calculated checksum
  14. C                 is added to the input value of sum32 to produce the
  15. C                 output value of sum32)
  16.  
  17.         integer buffer(*),length,i,hibits
  18.         double precision sum32,word32
  19.         parameter (word32=4.294967296D+09)
  20. C                 (word32 is equal to 2**32)
  21.  
  22. C       LENGTH must be less than 2**15, otherwise precision may be lost
  23. C       in the sum
  24.         if (length .gt. 32768)then
  25.             print *, 'Error: size of block to sum is too large'
  26.             return
  27.         end if
  28.  
  29.         do i=1,length/4
  30.             if (buffer(i) .ge. 0)then
  31.                 sum32=sum32+buffer(i)
  32.             else
  33. C               sign bit is set, so add the equivalent unsigned value
  34.                 sum32=sum32+(word32+buffer(i))
  35.             end if
  36.         end do
  37.  
  38. C       fold any overflow bits beyond 32 back into the word
  39. 10      hibits=sum32/word32
  40.         if (hibits .gt. 0)then
  41.             sum32=sum32-(hibits*word32)+hibits
  42.             go to 10
  43.         end if
  44.  
  45.         end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement