-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathm_endian.F90
306 lines (235 loc) · 8.94 KB
/
m_endian.F90
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
!! ----------------------------------------------------------------------------------------------------------------------------- !!
!>
!! Detection and conversion of machine endian of binary data
!<
!! ----------------------------------------------------------------------------------------------------------------------------- !!
module m_endian
!! -- Dependency
use m_std
!! -- Declarations
implicit none
private
!! -- Public Procedures
public :: endian__check
public :: endian__change
public :: endian__byteswap
!! -- Internal Constants
integer, parameter, public :: BIG_ENDIAN = 0
integer, parameter, public :: LITTLE_ENDIAN = 1
integer, parameter, public :: UNKNOWN_ENDIAN = -1
!! --------------------------------------------------------------------------------------------------------------------------- !!
!>
!! Check machine endian
!!
!! call endian__check( endian )
!! endian: integer or string
!!
!! return integer value has:
!! 1: little_endian , 0: big_endian , -1: unknown_endian as defined parameters in this module
!<
!! --
interface endian__check
module procedure checkendian_i, checkendian_a
end interface
!! --------------------------------------------------------------------------------------------------------------------------- !!
!! --------------------------------------------------------------------------------------------------------------------------- !!
!!>
!! Change between big and little endians
!!
!! Example call chgEndian( var )
!<
!! --
interface endian__change
module procedure chgEndian_r, &
chgEndian_i, &
chgEndian_l, &
chgEndian_a4, &
chgEndian_d, &
chgEndian_c, &
chgEndian_cd
end interface
!! --------------------------------------------------------------------------------------------------------------------------- !!
contains
!! --------------------------------------------------------------------------------------------------------------------------- !!
!>
!! Check machine endian and returns integer
!<
subroutine checkendian_i( endian )
!! -- Arguments
integer, intent(out) :: endian
!! -- Local parameters
integer, parameter :: ascii_0 = 48 ! ascii codes of '0'
integer, parameter :: ascii_1 = 49 ! ascii codes of '1'
integer, parameter :: ascii_2 = 50 ! ascii codes of '2'
integer, parameter :: ascii_3 = 51 ! ascii codes of '3'
integer :: i, io
character(4) :: wk
!! ----
! test variable
i = ascii_0 + ascii_1*(256**1) + ascii_2*(256**2) + ascii_3*(256**3)
! create temporary file
call std__getio( io )
! old direct access mode
!open( io, status='scratch', access='direct', recl = 4) ! for old machine, need to care on recl dependency
!write( io, rec = 1 ) i
!read ( io, rec = 1 ) wk
! stream i/o version
open( io, status='scratch', access='stream' )
write( io ) i
rewind( io )
read ( io ) wk
select case ( wk )
case( '0123' )
endian = little_endian
case( '3210' )
endian = big_endian
case default
endian = unknown_endian
end select
close( io ) ! automatically delete file in scratch mode
end subroutine checkendian_i
!! --------------------------------------------------------------------------------------------------------------------------- !!
!! --------------------------------------------------------------------------------------------------------------------------- !!
!>
!! Check machine endian and returns characters "Little" "BIG" or "Unknown"
!<
!! --
subroutine checkendian_a( endian )
!! -- Arguments
character(*), intent(out) :: endian
integer :: iendian
!! ----
call checkendian_i( iendian )
if( iendian == LITTLE_ENDIAN ) then
endian = 'Little'
else if ( iendian == BIG_ENDIAN ) then
endian = 'Big'
else
endian = 'Unknown'
end if
end subroutine checkendian_a
!! --------------------------------------------------------------------------------------------------------------------------- !!
!! --------------------------------------------------------------------------------------------------------------------------- !!
!>
!! Swap byte order of character foo. Used by endian change routines
!<
!! --
subroutine endian__byteSwap( foo, nbyte )
!! -- Arguments
integer, intent(in) :: nbyte ! must be even
character(nbyte), intent(inout) :: foo
integer :: i, j
character(1) :: wk
!! ----
do i=1, nbyte/2
j = nbyte - i + 1
wk = foo(i:i)
foo(i:i) = foo(j:j)
foo(j:j) = wk
end do
end subroutine endian__byteSwap
!! --------------------------------------------------------------------------------------------------------------------------- !!
!! --------------------------------------------------------------------------------------------------------------------------- !!
!>
!! Change endian of real-value
!<
!! --
subroutine chgEndian_r( var )
!! -- Arguments
real, intent(inout) :: var
character(4) :: c
!! ----
c = transfer( var, c )
call endian__byteswap( c, 4 )
var = transfer( c, var )
end subroutine chgEndian_r
!! --------------------------------------------------------------------------------------------------------------------------- !!
!! --------------------------------------------------------------------------------------------------------------------------- !!
!>
!! Change endian of integer value
!<
!! --
subroutine chgEndian_i( var )
!! -- Arguments
integer, intent(inout) :: var
character(4) :: c
!! ----
c = transfer( var, c )
call endian__byteswap( c, 4 )
var = transfer( c, var )
end subroutine chgEndian_i
!! --------------------------------------------------------------------------------------------------------------------------- !!
!! --------------------------------------------------------------------------------------------------------------------------- !!
!>
!! Change endian of logical value
!<
!! --
subroutine chgEndian_l( var )
!! -- Arguments
logical, intent(inout) :: var
character(4) :: c
!! ----
c = transfer( var, c )
call endian__byteswap( c, 4 )
var = transfer( c, var )
end subroutine chgEndian_l
!! --------------------------------------------------------------------------------------------------------------------------- !!
!! --------------------------------------------------------------------------------------------------------------------------- !!
!>
!! Change endian of 4-character
!<
!! --
subroutine chgEndian_a4( var )
!! -- Arguments
character(4), intent(inout) :: var
!! ----
call endian__byteswap( var, 4 )
end subroutine chgEndian_a4
!! --------------------------------------------------------------------------------------------------------------------------- !!
!! --------------------------------------------------------------------------------------------------------------------------- !!
!>
!! Change endian of double precision value
!<
!! --
subroutine chgEndian_d( var )
!! -- Arguments
real(DP), intent(inout) :: var
character(8) :: c
!! ----
c = transfer( var, c )
call endian__byteswap( c, 8 )
var = transfer( c, var )
end subroutine chgEndian_d
!! --------------------------------------------------------------------------------------------------------------------------- !!
!! --------------------------------------------------------------------------------------------------------------------------- !!
!>
!! Change endian of complex value
!<
!! --
subroutine chgEndian_c( var )
!! -- Arguments
complex, intent(inout) :: var
character(8) :: c
!! ----
c = transfer( var, c )
call endian__byteswap( c, 8 )
var = transfer( c, var )
end subroutine chgEndian_c
!! --------------------------------------------------------------------------------------------------------------------------- !!
!! --------------------------------------------------------------------------------------------------------------------------- !!
!>
!! Change endian of double precision complex value
!<
!! --
subroutine chgEndian_cd( var )
!! -- Arguments
complex(DP), intent(inout) :: var
character(16) :: c
!! ----
c = transfer( var, c )
call endian__byteswap( c, 16 )
var = transfer( c, var )
end subroutine chgEndian_cd
!! --------------------------------------------------------------------------------------------------------------------------- !!
end module m_endian
!! ----------------------------------------------------------------------------------------------------------------------------- !!