NCEPLIBS-g2 4.0.0
Loading...
Searching...
No Matches
intmath.F90
Go to the documentation of this file.
1
5
22module 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
41contains
42
48 function i1log2_8(ival)
49 implicit none
50 integer(kind=8), value :: ival
51 integer(kind=8)::i1log2_8
52 integer(kind=8), parameter :: one=1
53 if(ival+one<ival) then
54 i1log2_8=ilog2_8(ival)
55 else
56 i1log2_8=ilog2_8(ival+one)
57 endif
58 end function i1log2_8
59
65 function i1log2_4(ival)
66 implicit none
67 integer(kind=4), value :: ival
68 integer(kind=4)::i1log2_4
69 integer(kind=4), parameter :: one=1
70 if(ival+one<ival) then
71 i1log2_4=ilog2_4(ival)
72 else
73 i1log2_4=ilog2_4(ival+one)
74 endif
75 end function i1log2_4
76
82 function i1log2_2(ival)
83 implicit none
84 integer(kind=2), value :: ival
85 integer(kind=2)::i1log2_2
86 integer(kind=2), parameter :: one = 1_2
87 if(ival+one<ival) then
88 i1log2_2=ilog2_2(ival)
89 else
90 i1log2_2=ilog2_2(ival+one)
91 endif
92 end function i1log2_2
93
99 function i1log2_1(ival)
100 implicit none
101 integer(kind=1), value :: ival
102 integer(kind=1)::i1log2_1
103 integer(kind=1), parameter :: one = 1_1
104 if(ival+one<ival) then
105 i1log2_1=ilog2_1(ival)
106 else
107 i1log2_1=ilog2_1(ival+one)
108 endif
109 end function i1log2_1
110
115 function ilog2_8(i_in)
116 implicit none
117 integer(kind=8), value :: i_in
118 integer(kind=8)::ilog2_8,i
119 ilog2_8=0
120 i=i_in
121 if(i<=0) return
122 if(iand(i,i-1)/=0) then
123 !write(0,*) 'iand i-1'
124 ilog2_8=1
125 endif
126 if(iand(i,z'FFFFFFFF00000000')/=0) then
128 i=ishft(i,-32)
129 !write(0,*) 'iand ffffffff',i,ilog2_8
130 endif
131 if(iand(i,z'00000000FFFF0000')/=0) then
133 i=ishft(i,-16)
134 !write(0,*) 'iand ffff' ,i,ilog2_8
135 endif
136 if(iand(i,z'000000000000FF00')/=0) then
138 i=ishft(i,-8)
139 !write(0,*) 'iand ff',i,ilog2_8
140 endif
141 if(iand(i,z'00000000000000F0')/=0) then
143 i=ishft(i,-4)
144 !write(0,*) 'iand f',i,ilog2_8
145 endif
146 if(iand(i,z'000000000000000C')/=0) then
148 i=ishft(i,-2)
149 !write(0,*) 'iand c',i,ilog2_8
150 endif
151 if(iand(i,z'0000000000000002')/=0) then
153 i=ishft(i,-1)
154 !write(0,*) 'iand 2',i,ilog2_8
155 endif
156 end function ilog2_8
157
162 function ilog2_4(i_in)
163 implicit none
164 integer(kind=4), value :: i_in
165 integer(kind=4)::ilog2_4,i
166 ilog2_4=0
167 i=i_in
168 if(i<=0) return
169 if(iand(i,i-1)/=0) then
170 !write(0,*) 'iand i-1'
171 ilog2_4=1
172 endif
173 if(iand(i,z'FFFF0000')/=0) then
175 i=ishft(i,-16)
176 !write(0,*) 'iand ffff' ,i,ilog2_4
177 endif
178 if(iand(i,z'0000FF00')/=0) then
180 i=ishft(i,-8)
181 !write(0,*) 'iand ff',i,ilog2_4
182 endif
183 if(iand(i,z'000000F0')/=0) then
185 i=ishft(i,-4)
186 !write(0,*) 'iand f',i,ilog2_4
187 endif
188 if(iand(i,z'0000000C')/=0) then
190 i=ishft(i,-2)
191 !write(0,*) 'iand c',i,ilog2_4
192 endif
193 if(iand(i,z'00000002')/=0) then
195 i=ishft(i,-1)
196 !write(0,*) 'iand 2',i,ilog2_4
197 endif
198 end function ilog2_4
199
204 function ilog2_2(i_in)
205 implicit none
206 integer(kind=2), value :: i_in
207 integer(kind=2)::ilog2_2,i
208 ilog2_2 = 0_2
209 i=i_in
210 if(i<=0) return
211 if(iand(i,int(i-1,kind=2))/=0) then
212 !write(0,*) 'iand i-1'
213 ilog2_2 = 1_2
214 endif
215 if(iand(i,z'FF00')/=0) then
216 ilog2_2 = ilog2_2 + 8_2
217 i=ishft(i,-8)
218 !write(0,*) 'iand ff',i,ilog2_2
219 endif
220 if(iand(i,z'00F0')/=0) then
221 ilog2_2 = ilog2_2 + 4_2
222 i=ishft(i,-4)
223 !write(0,*) 'iand f',i,ilog2_2
224 endif
225 if(iand(i,z'000C')/=0) then
226 ilog2_2 = ilog2_2 + 2_2
227 i=ishft(i,-2)
228 !write(0,*) 'iand c',i,ilog2_2
229 endif
230 if(iand(i,z'0002')/=0) then
231 ilog2_2 = ilog2_2 + 1_2
232 i=ishft(i,-1)
233 !write(0,*) 'iand 2',i,ilog2_2
234 endif
235 end function ilog2_2
236
241 function ilog2_1(i_in)
242 implicit none
243 integer(kind=1), value :: i_in
244 integer(kind=1)::ilog2_1,i
245 ilog2_1 = 0_1
246 i=i_in
247 if(i<=0) return
248 if(iand(i,int(i-1,kind=1))/=0) then
249 !write(0,*) 'iand i-1'
250 ilog2_1 = 1_1
251 endif
252 if(iand(i,z'F0')/=0) then
253 ilog2_1 = ilog2_1 + 4_1
254 i=ishft(i,-4)
255 !write(0,*) 'iand f',i,ilog2_1
256 endif
257 if(iand(i,z'0C')/=0) then
258 ilog2_1 = ilog2_1 + 2_1
259 i=ishft(i,-2)
260 !write(0,*) 'iand c',i,ilog2_1
261 endif
262 if(iand(i,z'02')/=0) then
263 ilog2_1 = ilog2_1 + 1_1
264 i=ishft(i,-1)
265 !write(0,*) 'iand 2',i,ilog2_1
266 endif
267 end function ilog2_1
268end module intmath
Define math functions used by compack(), simpack(), and misspack().
Definition intmath.F90:22
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.F90:100
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.F90:66
integer(kind=4) function ilog2_4(i_in)
This function returns log(x)/log(2) for 4 bit integer numbers.
Definition intmath.F90:163
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.F90:83
integer(kind=2) function ilog2_2(i_in)
This function returns log(x)/log(2) for 2 bit integer numbers.
Definition intmath.F90:205
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.F90:49
integer(kind=8) function ilog2_8(i_in)
This function returns log(x)/log(2) for 8 bit integer numbers.
Definition intmath.F90:116
integer(kind=1) function ilog2_1(i_in)
This function returns log(x)/log(2) for 1 bit integer numbers.
Definition intmath.F90:242