endian_swap.f90 5.22 KB
Newer Older
Matthias Redies's avatar
Matthias Redies committed
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 180 181 182 183
module endian_swap
    implicit none

    PRIVATE
    PUBLIC :: Big_Endian
    PUBLIC :: Swap_Endian

    INTERFACE Swap_Endian
        module procedure SWAP_I1
        module procedure SWAP_I2 
        module procedure SWAP_I4
        module procedure SWAP_I8
        module procedure SWAP_F4
        module procedure SWAP_F8 
        module procedure SWAP_F16
        module procedure SWAP_C4
        module procedure SWAP_C8
    END INTERFACE Swap_Endian


    CONTAINS

        FUNCTION Big_Endian()

            LOGICAL :: Big_Endian

            Big_Endian = ichar(transfer(1,'a')) == 0

        END FUNCTION Big_Endian

        function SWAP_I4(input) result(output)
            implicit none
            integer(4), parameter  :: b_sz = 4
            integer(b_sz), intent(in) :: input 
            integer(b_sz)             :: output 

            integer(1) :: byte_arr(b_sz), byte_arr_tmp(b_sz)
            integer(1) :: i

            byte_arr_tmp =  transfer(input, byte_arr_tmp)

            do i = 1,b_sz
                byte_arr(i) =  byte_arr_tmp(1 + b_sz - i)
            enddo

            output =  transfer(byte_arr, output)
        end function SWAP_I4 

        function SWAP_I2(input) result(output)
            implicit none
            integer(4), parameter  :: b_sz =  2
            integer(b_sz), intent(in) :: input 
            integer(b_sz)             :: output 

            integer(1) :: byte_arr(b_sz), byte_arr_tmp(b_sz)
            integer(1) :: i

            byte_arr_tmp =  transfer(input, byte_arr_tmp)

            do i = 1,b_sz
                byte_arr(i) =  byte_arr_tmp(1 + b_sz - i)
            enddo

            output =  transfer(byte_arr, output)
        end function SWAP_I2
        
        function SWAP_I1(input) result(output)
            implicit none
            integer(4), parameter  :: b_sz =  1
            integer(b_sz), intent(in) :: input 
            integer(b_sz)             :: output 

            integer(1) :: byte_arr(b_sz), byte_arr_tmp(b_sz)
            integer(1) :: i

            byte_arr_tmp =  transfer(input, byte_arr_tmp)

            do i = 1,b_sz
                byte_arr(i) =  byte_arr_tmp(1 + b_sz - i)
            enddo

            output =  transfer(byte_arr, output)
        end function SWAP_I1
        
        function SWAP_I8(input) result(output)
            implicit none
            integer(4), parameter  :: b_sz =  8
            integer(b_sz), intent(in) :: input 
            integer(b_sz)             :: output 

            integer(1) :: byte_arr(b_sz), byte_arr_tmp(b_sz)
            integer(1) :: i

            byte_arr_tmp =  transfer(input, byte_arr_tmp)

            do i = 1,b_sz
                byte_arr(i) =  byte_arr_tmp(1 + b_sz - i)
            enddo

            output =  transfer(byte_arr, output)
        end function SWAP_I8


        function SWAP_F4(input) result(output)
            implicit none
            integer(4), parameter  :: b_sz =  4
            real(b_sz), intent(in) :: input 
            real(b_sz)             :: output 

            integer(1) :: byte_arr(b_sz), byte_arr_tmp(b_sz)
            integer(1) :: i

            byte_arr_tmp =  transfer(input, byte_arr_tmp)

            do i = 1,b_sz
                byte_arr(i) =  byte_arr_tmp(1 + b_sz - i)
            enddo

            output =  transfer(byte_arr, output)
        end function SWAP_F4
        
        function SWAP_F8(input) result(output)
            implicit none
            integer(4), parameter  :: b_sz =  8
            real(b_sz), intent(in) :: input 
            real(b_sz)             :: output 

            integer(1) :: byte_arr(b_sz), byte_arr_tmp(b_sz)
            integer(1) :: i

            byte_arr_tmp =  transfer(input, byte_arr_tmp)

            do i = 1,b_sz
                byte_arr(i) =  byte_arr_tmp(1 + b_sz - i)
            enddo

            output =  transfer(byte_arr, output)
        end function SWAP_F8 

        function SWAP_F16(input) result(output)
            implicit none
            integer(4), parameter  :: b_sz =  16
            real(b_sz), intent(in) :: input 
            real(b_sz)             :: output 

            integer(1) :: byte_arr(b_sz), byte_arr_tmp(b_sz)
            integer(1) :: i

            byte_arr_tmp =  transfer(input, byte_arr_tmp)

            do i = 1,b_sz
                byte_arr(i) =  byte_arr_tmp(1 + b_sz - i)
            enddo

            output =  transfer(byte_arr, output)
        end function SWAP_F16
        
        function SWAP_C8(input) result(output)
            implicit none
            integer(4), parameter  :: b_sz = 8
            complex(b_sz), intent(in) :: input
            complex(b_sz)             :: output
            real(b_sz)                :: re, im

            re = Swap_Endian(real(input))
            im = Swap_Endian(aimag(input))

            output = cmplx(re, im)
        end function swap_c8

        function SWAP_C4(input) result(output)
            implicit none
            integer(4), parameter  :: b_sz = 4
            complex(b_sz), intent(in) :: input
            complex(b_sz)             :: output
            real(b_sz)                :: re, im

            re = Swap_Endian(real(input))
            im = Swap_Endian(aimag(input))

            output = cmplx(re, im)
        end function swap_c4
END module endian_swap