NCEPLIBS-g2  3.4.5
intmath.f
Go to the documentation of this file.
1 
5 
21 
22  module intmath
23  implicit none
24 
25  interface ilog2
26  ! log(x)/log(2)
27  module procedure ilog2_8
28  module procedure ilog2_4
29  module procedure ilog2_2
30  module procedure ilog2_1
31  end interface ilog2
32 
33  interface i1log2
34  ! log(x+1)/log(2) unless x=maxint, in which case log(x)/log(2)
35  module procedure i1log2_8
36  module procedure i1log2_4
37  module procedure i1log2_2
38  module procedure i1log2_1
39  end interface i1log2
40 
41  contains
42 
43  ! ----------------------------------------------------------------
44 
51 
52  function i1log2_8(ival)
53  implicit none
54  integer(kind=8), value :: ival
55  integer(kind=8)::i1log2_8
56  integer(kind=8), parameter :: one=1
57  if(ival+one<ival) then
58  i1log2_8=ilog2_8(ival)
59  else
60  i1log2_8=ilog2_8(ival+one)
61  endif
62  end function i1log2_8
63 
64  ! ----------------------------------------------------------------
65 
72 
73  function i1log2_4(ival)
74  implicit none
75  integer(kind=4), value :: ival
76  integer(kind=4)::i1log2_4
77  integer(kind=4), parameter :: one=1
78  if(ival+one<ival) then
79  i1log2_4=ilog2_4(ival)
80  else
81  i1log2_4=ilog2_4(ival+one)
82  endif
83  end function i1log2_4
84 
85  ! ----------------------------------------------------------------
86 
93 
94  function i1log2_2(ival)
95  implicit none
96  integer(kind=2), value :: ival
97  integer(kind=2)::i1log2_2
98  integer(kind=2), parameter :: one=1
99  if(ival+one<ival) then
100  i1log2_2=ilog2_2(ival)
101  else
102  i1log2_2=ilog2_2(ival+one)
103  endif
104  end function i1log2_2
105 
106  ! ----------------------------------------------------------------
107 
114 
115  function i1log2_1(ival)
116  implicit none
117  integer(kind=1), value :: ival
118  integer(kind=1)::i1log2_1
119  integer(kind=1), parameter :: one=1
120  if(ival+one<ival) then
121  i1log2_1=ilog2_1(ival)
122  else
123  i1log2_1=ilog2_1(ival+one)
124  endif
125  end function i1log2_1
126 
127  ! ----------------------------------------------------------------
128 
134 
135  function ilog2_8(i_in)
136  implicit none
137  integer(kind=8), value :: i_in
138  integer(kind=8)::ilog2_8,i
139  ilog2_8=0
140  i=i_in
141  if(i<=0) return
142  if(iand(i,i-1)/=0) then
143  !write(0,*) 'iand i-1'
144  ilog2_8=1
145  endif
146  if(iand(i,z'FFFFFFFF00000000')/=0) then
147  ilog2_8=ilog2_8+32
148  i=ishft(i,-32)
149  !write(0,*) 'iand ffffffff',i,ilog2_8
150  endif
151  if(iand(i,z'00000000FFFF0000')/=0) then
152  ilog2_8=ilog2_8+16
153  i=ishft(i,-16)
154  !write(0,*) 'iand ffff' ,i,ilog2_8
155  endif
156  if(iand(i,z'000000000000FF00')/=0) then
157  ilog2_8=ilog2_8+8
158  i=ishft(i,-8)
159  !write(0,*) 'iand ff',i,ilog2_8
160  endif
161  if(iand(i,z'00000000000000F0')/=0) then
162  ilog2_8=ilog2_8+4
163  i=ishft(i,-4)
164  !write(0,*) 'iand f',i,ilog2_8
165  endif
166  if(iand(i,z'000000000000000C')/=0) then
167  ilog2_8=ilog2_8+2
168  i=ishft(i,-2)
169  !write(0,*) 'iand c',i,ilog2_8
170  endif
171  if(iand(i,z'0000000000000002')/=0) then
172  ilog2_8=ilog2_8+1
173  i=ishft(i,-1)
174  !write(0,*) 'iand 2',i,ilog2_8
175  endif
176  end function ilog2_8
177 
178  ! ----------------------------------------------------------------
179 
185 
186  function ilog2_4(i_in)
187  implicit none
188  integer(kind=4), value :: i_in
189  integer(kind=4)::ilog2_4,i
190  ilog2_4=0
191  i=i_in
192  if(i<=0) return
193  if(iand(i,i-1)/=0) then
194  !write(0,*) 'iand i-1'
195  ilog2_4=1
196  endif
197  if(iand(i,z'FFFF0000')/=0) then
198  ilog2_4=ilog2_4+16
199  i=ishft(i,-16)
200  !write(0,*) 'iand ffff' ,i,ilog2_4
201  endif
202  if(iand(i,z'0000FF00')/=0) then
203  ilog2_4=ilog2_4+8
204  i=ishft(i,-8)
205  !write(0,*) 'iand ff',i,ilog2_4
206  endif
207  if(iand(i,z'000000F0')/=0) then
208  ilog2_4=ilog2_4+4
209  i=ishft(i,-4)
210  !write(0,*) 'iand f',i,ilog2_4
211  endif
212  if(iand(i,z'0000000C')/=0) then
213  ilog2_4=ilog2_4+2
214  i=ishft(i,-2)
215  !write(0,*) 'iand c',i,ilog2_4
216  endif
217  if(iand(i,z'00000002')/=0) then
218  ilog2_4=ilog2_4+1
219  i=ishft(i,-1)
220  !write(0,*) 'iand 2',i,ilog2_4
221  endif
222  end function ilog2_4
223 
224  ! ----------------------------------------------------------------
225 
231 
232  function ilog2_2(i_in)
233  implicit none
234  integer(kind=2), value :: i_in
235  integer(kind=2)::ilog2_2,i
236  ilog2_2=0
237  i=i_in
238  if(i<=0) return
239  if(iand(i,int(i-1,kind=2))/=0) then
240  !write(0,*) 'iand i-1'
241  ilog2_2=1
242  endif
243  if(iand(i,z'FF00')/=0) then
244  ilog2_2=ilog2_2+8
245  i=ishft(i,-8)
246  !write(0,*) 'iand ff',i,ilog2_2
247  endif
248  if(iand(i,z'00F0')/=0) then
249  ilog2_2=ilog2_2+4
250  i=ishft(i,-4)
251  !write(0,*) 'iand f',i,ilog2_2
252  endif
253  if(iand(i,z'000C')/=0) then
254  ilog2_2=ilog2_2+2
255  i=ishft(i,-2)
256  !write(0,*) 'iand c',i,ilog2_2
257  endif
258  if(iand(i,z'0002')/=0) then
259  ilog2_2=ilog2_2+1
260  i=ishft(i,-1)
261  !write(0,*) 'iand 2',i,ilog2_2
262  endif
263  end function ilog2_2
264 
265  ! ----------------------------------------------------------------
266 
272 
273  function ilog2_1(i_in)
274  implicit none
275  integer(kind=1), value :: i_in
276  integer(kind=1)::ilog2_1,i
277  ilog2_1=0
278  i=i_in
279  if(i<=0) return
280  if(iand(i,int(i-1,kind=1))/=0) then
281  !write(0,*) 'iand i-1'
282  ilog2_1=1
283  endif
284  if(iand(i,z'F0')/=0) then
285  ilog2_1=ilog2_1+4
286  i=ishft(i,-4)
287  !write(0,*) 'iand f',i,ilog2_1
288  endif
289  if(iand(i,z'0C')/=0) then
290  ilog2_1=ilog2_1+2
291  i=ishft(i,-2)
292  !write(0,*) 'iand c',i,ilog2_1
293  endif
294  if(iand(i,z'02')/=0) then
295  ilog2_1=ilog2_1+1
296  i=ishft(i,-1)
297  !write(0,*) 'iand 2',i,ilog2_1
298  endif
299  end function ilog2_1
300  end module intmath
301 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
302 c$$$ TEST PROGRAM FOR THIS MODULE
303 c$$$ program test_intmath
304 c$$$ use intmath
305 c$$$ implicit none
306 c$$$ real(kind=16) :: temp
307 c$$$ real(kind=16), parameter :: alog2=log(2.0_16)
308 c$$$ integer(kind=8), parameter :: &
309 c$$$ & one=1,big=Z'7FFFFFFFFFFFFFFF',small=-2000000_8, &
310 c$$$ & check=Z'1FFFFFFF'
311 c$$$ integer(kind=8) :: ival, iret
312 c$$$ !$OMP PARALLEL DO PRIVATE(ival,temp,iret)
313 c$$$ do ival=small,big
314 c$$$ 10 format(Z16,' -- MISMATCH: ',I0,'=>',I0,' (',I0,' = ',F0.10,')')
315 c$$$ 20 format(Z16,' -- OKAY: ',I0,'=>',I0,' (',I0,' = ',F0.10,')')
316 c$$$ if(ival+one<ival) then
317 c$$$ temp=log(real(max(ival,one),kind=16))/alog2
318 c$$$ else
319 c$$$ temp=log(real(max(ival+one,one),kind=16))/alog2
320 c$$$ endif
321 c$$$ iret=i1log2(ival)
322 c$$$ if(iret/=ceiling(temp) .or. ival==0 .or. ival==check) then
323 c$$$ !$OMP CRITICAL
324 c$$$ if(iret/=ceiling(temp)) then
325 c$$$ print 10, ival, ival, iret,ceiling(temp),temp
326 c$$$ else
327 c$$$ print 20, ival, ival, iret,ceiling(temp),temp
328 c$$$ endif
329 c$$$ !$OMP END CRITICAL
330 c$$$ endif
331 c$$$ enddo
332 c$$$ !$OMP END PARALLEL DO
333 c$$$ end program test_intmath
intmath::ilog2
Definition: intmath.f:25
intmath::i1log2_1
integer(kind=1) function i1log2_1(ival)
This function returns log(x+1)/log(2) unless x=maxint, in which case log(x)/log(2) for 1 bit integer ...
Definition: intmath.f:116
intmath::i1log2
Definition: intmath.f:33
intmath::ilog2_8
integer(kind=8) function ilog2_8(i_in)
This function returns log(x)/log(2) for 8 bit integer numbers.
Definition: intmath.f:136
intmath::i1log2_8
integer(kind=8) function i1log2_8(ival)
This function returns log(x+1)/log(2) unless x=maxint, in which case log(x)/log(2) for 8 bit integer ...
Definition: intmath.f:53
intmath::i1log2_2
integer(kind=2) function i1log2_2(ival)
This function returns log(x+1)/log(2) unless x=maxint, in which case log(x)/log(2) for 2 bit integer ...
Definition: intmath.f:95
intmath::ilog2_1
integer(kind=1) function ilog2_1(i_in)
This function returns log(x)/log(2) for 1 bit integer numbers.
Definition: intmath.f:274
intmath::ilog2_4
integer(kind=4) function ilog2_4(i_in)
This function returns log(x)/log(2) for 4 bit integer numbers.
Definition: intmath.f:187
intmath::ilog2_2
integer(kind=2) function ilog2_2(i_in)
This function returns log(x)/log(2) for 2 bit integer numbers.
Definition: intmath.f:233
intmath
This module defines integer math functions used by other programs.
Definition: intmath.f:22
intmath::i1log2_4
integer(kind=4) function i1log2_4(ival)
This function returns log(x+1)/log(2) unless x=maxint, in which case log(x)/log(2) for 4 bit integer ...
Definition: intmath.f:74