byteswapper.F90 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. subroutine swapendian(a)
  2. implicit none
  3. integer(kind=8), intent(inout) :: a ! 4-bytes
  4. integer(kind=8) ii4, io4 ! 4-bytes
  5. common/czioxe/ ii4, io4 ! helps prevent unwanted optimization
  6. save /czioxe/
  7. integer(kind=1) ii1(8),io1(8) ! 1-byte
  8. equivalence (ii4,ii1(1)), (io4,io1(1)) ! non-standard f90
  9. ii4 = a
  10. io1(1) = ii1(8)
  11. io1(2) = ii1(7)
  12. io1(3) = ii1(6)
  13. io1(4) = ii1(5)
  14. io1(5) = ii1(4)
  15. io1(6) = ii1(3)
  16. io1(7) = ii1(2)
  17. io1(8) = ii1(1)
  18. a = io4
  19. return
  20. end subroutine swapendian
  21. subroutine swapendian2(a,n)
  22. implicit none
  23. integer , intent(in) :: n ! Size of input type to convert
  24. ! NB - input can be anything - can not be compiled with input argument checking
  25. integer(kind=1), intent(inout) :: a(n)
  26. integer k
  27. integer(kind=1) ii4(16), io4(16) ! 16 bytes should beenough for everyone
  28. !common/czioxe/ ii4, io4 ! helps prevent unwanted optimization
  29. !save /czioxe/
  30. !integer(kind=1) ii1(16),io1(16) ! 1-byte
  31. !equivalence (ii4(1),ii1(1)), (io4(1),io1(1)) ! non-standard f90
  32. ii4(1:n) = a
  33. do k=1,n
  34. !io1(k) = ii1(n-k+1)
  35. io4(k) = ii4(n-k+1)
  36. end do
  37. a = io4(1:n)
  38. return
  39. end subroutine swapendian2