| 1 |
!----------------------------------------------------------------- |
| 2 |
! |
| 3 |
! This file is (or was) part of SPLASH, a visualisation tool |
| 4 |
! for Smoothed Particle Hydrodynamics written by Daniel Price: |
| 5 |
! |
| 6 |
! http://users.monash.edu.au/~dprice/splash |
| 7 |
! |
| 8 |
! SPLASH comes with ABSOLUTELY NO WARRANTY. |
| 9 |
! This is free software; and you are welcome to redistribute |
| 10 |
! it under the terms of the GNU General Public License |
| 11 |
! (see LICENSE file for details) and the provision that |
| 12 |
! this notice remains intact. If you modify this file, please |
| 13 |
! note section 2a) of the GPLv2 states that: |
| 14 |
! |
| 15 |
! a) You must cause the modified files to carry prominent notices |
| 16 |
! stating that you changed the files and the date of any change. |
| 17 |
! |
| 18 |
! Copyright (C) 2005-2023 Daniel Price. All rights reserved. |
| 19 |
! Contact: [email protected] |
| 20 |
! |
| 21 |
!----------------------------------------------------------------- |
| 22 |
module byteswap |
| 23 |
implicit none |
| 24 |
integer, parameter :: little_endian_order8(8) = [8,7,6,5,4,3,2,1] |
| 25 |
integer, parameter :: little_endian_order4(4) = [4,3,2,1] |
| 26 |
|
| 27 |
public :: bs |
| 28 |
|
| 29 |
interface bs |
| 30 |
module procedure reverse_bytes_r4,reverse_bytes_r8,& |
| 31 |
reverse_bytes_int,reverse_bytes_int8 |
| 32 |
end interface bs |
| 33 |
|
| 34 |
private |
| 35 |
|
| 36 |
contains |
| 37 |
!-------------------------------------------------------- |
| 38 |
! function to swap byte order (real 4) |
| 39 |
!-------------------------------------------------------- |
| 40 |
real(kind=4) elemental function reverse_bytes_r4(x) result(y) |
| 41 |
real(kind=4), intent(in) :: x |
| 42 |
character(len=1) :: src_arr(4), dst_arr(4) |
| 43 |
|
| 44 |
! put whatever the variable was into an 8 x 1 character string |
| 45 |
src_arr = transfer(x,src_arr) |
| 46 |
! swap the bytes around into the desired order |
| 47 |
dst_arr = src_arr(little_endian_order4) |
| 48 |
! copy the 8 x 1 character string back to represent the original type |
| 49 |
y = transfer(dst_arr,y) |
| 50 |
|
| 51 |
end function reverse_bytes_r4 |
| 52 |
|
| 53 |
!-------------------------------------------------------- |
| 54 |
! function to swap byte order (real 8) |
| 55 |
!-------------------------------------------------------- |
| 56 |
real(kind=8) elemental function reverse_bytes_r8(x) result(y) |
| 57 |
real(kind=8), intent(in) :: x |
| 58 |
character(len=1) :: src_arr(8), dst_arr(8) |
| 59 |
|
| 60 |
! put whatever the variable was into an 8 x 1 character string |
| 61 |
src_arr = transfer(x,src_arr) |
| 62 |
! swap the bytes around into the desired order |
| 63 |
dst_arr = src_arr(little_endian_order8) |
| 64 |
! copy the 8 x 1 character string back to represent the original type |
| 65 |
y = transfer(dst_arr,y) |
| 66 |
|
| 67 |
end function reverse_bytes_r8 |
| 68 |
|
| 69 |
!-------------------------------------------------------- |
| 70 |
! function to swap byte order (int 4) |
| 71 |
!-------------------------------------------------------- |
| 72 |
integer(kind=4) elemental function reverse_bytes_int(x) result(y) |
| 73 |
integer(kind=4), intent(in) :: x |
| 74 |
character(len=1) :: src_arr(4), dst_arr(4) |
| 75 |
|
| 76 |
! put whatever the variable was into an 8 x 1 character string |
| 77 |
src_arr = transfer(x,src_arr) |
| 78 |
! swap the bytes around into the desired order |
| 79 |
dst_arr = src_arr(little_endian_order4) |
| 80 |
! copy the 8 x 1 character string back to represent the original type |
| 81 |
y = transfer(dst_arr,y) |
| 82 |
|
| 83 |
end function reverse_bytes_int |
| 84 |
|
| 85 |
!-------------------------------------------------------- |
| 86 |
! function to swap byte order (int 4) |
| 87 |
!-------------------------------------------------------- |
| 88 |
integer(kind=8) elemental function reverse_bytes_int8(x) result(y) |
| 89 |
integer(kind=8), intent(in) :: x |
| 90 |
character(len=1) :: src_arr(8), dst_arr(8) |
| 91 |
|
| 92 |
! put whatever the variable was into an 8 x 1 character string |
| 93 |
src_arr = transfer(x,src_arr) |
| 94 |
! swap the bytes around into the desired order |
| 95 |
dst_arr = src_arr(little_endian_order8) |
| 96 |
! copy the 8 x 1 character string back to represent the original type |
| 97 |
y = transfer(dst_arr,y) |
| 98 |
|
| 99 |
end function reverse_bytes_int8 |
| 100 |
|
| 101 |
end module byteswap |