Skip to content

Commit

Permalink
renamed mova2i to g2_mova2i (#623)
Browse files Browse the repository at this point in the history
* renamed mova2i to g2_mova2i

* fixed documentation

* renamed mova2i to g2_mova2i
  • Loading branch information
edwardhartnett authored Feb 23, 2024
1 parent fbdf813 commit 4c58a32
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 51 deletions.
36 changes: 18 additions & 18 deletions src/g2_gbytesc.F90
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ subroutine g2_gbytesc(in, iout, iskip, nbits, nskip, n)
integer, parameter :: ones(8) = (/ 1, 3, 7, 15, 31, 63, 127, 255 /)

integer :: nbit, i, index, ibit, itmp
integer, external :: mova2i
integer, external :: g2_mova2i

! nbit is the start position of the field in bits
nbit = iskip
Expand All @@ -59,21 +59,21 @@ subroutine g2_gbytesc(in, iout, iskip, nbits, nskip, n)

! first byte
tbit = min(bitcnt, 8 - ibit)
itmp = iand(mova2i(in(index)), ones(8 - ibit))
itmp = iand(g2_mova2i(in(index)), ones(8 - ibit))
if (tbit .ne. 8 - ibit) itmp = ishft(itmp, tbit - 8 + ibit)
index = index + 1
bitcnt = bitcnt - tbit

! now transfer whole bytes
do while (bitcnt .ge. 8)
itmp = ior(ishft(itmp,8), mova2i(in(index)))
itmp = ior(ishft(itmp,8), g2_mova2i(in(index)))
bitcnt = bitcnt - 8
index = index + 1
enddo

! get data from last byte
if (bitcnt .gt. 0) then
itmp = ior(ishft(itmp, bitcnt), iand(ishft(mova2i(in(index)), &
itmp = ior(ishft(itmp, bitcnt), iand(ishft(g2_mova2i(in(index)), &
- (8 - bitcnt)), ones(bitcnt)))
endif

Expand Down Expand Up @@ -127,8 +127,8 @@ subroutine g2_gbytesc8(in, iout, iskip, nbits, nskip, n)

integer :: nbit, i, index, ibit, itmp
integer (kind = 8) :: itmp8, itmp8_2, itmp8_3
integer, external :: mova2i
integer (kind = 8), external :: mova2i8
integer, external :: g2_mova2i
integer (kind = 8), external :: g2_mova2i8

! nbit is the start position of the field in bits
nbit = iskip
Expand All @@ -140,25 +140,25 @@ subroutine g2_gbytesc8(in, iout, iskip, nbits, nskip, n)

! first byte
tbit = min(bitcnt, 8 - ibit)
itmp8 = iand(mova2i8(in(index)), int(ones(8 - ibit), kind = 8))
itmp = iand(mova2i(in(index)), ones(8 - ibit))
itmp8 = iand(g2_mova2i8(in(index)), int(ones(8 - ibit), kind = 8))
itmp = iand(g2_mova2i(in(index)), ones(8 - ibit))
if (tbit .ne. 8 - ibit) itmp = ishft(itmp, tbit - 8 + ibit)
if (tbit .ne. 8 - ibit) itmp8 = ishft(itmp8, tbit - 8 + ibit)
index = index + 1
bitcnt = bitcnt - tbit

! now transfer whole bytes
do while (bitcnt .ge. 8)
itmp = ior(ishft(itmp,8), mova2i(in(index)))
itmp8 = ior(ishft(itmp8,8), mova2i8(in(index)))
itmp = ior(ishft(itmp,8), g2_mova2i(in(index)))
itmp8 = ior(ishft(itmp8,8), g2_mova2i8(in(index)))
bitcnt = bitcnt - 8
index = index + 1
enddo

! get data from last byte
if (bitcnt .gt. 0) then
itmp = ior(ishft(itmp, bitcnt), iand(ishft(mova2i(in(index)), - (8 - bitcnt)), ones(bitcnt)))
itmp8_2 = ishft(mova2i8(in(index)), int(-(8 - bitcnt), kind(8)))
itmp = ior(ishft(itmp, bitcnt), iand(ishft(g2_mova2i(in(index)), - (8 - bitcnt)), ones(bitcnt)))
itmp8_2 = ishft(g2_mova2i8(in(index)), int(-(8 - bitcnt), kind(8)))
itmp8_3 = int(ones(bitcnt), kind(8))
itmp8 = ior(ishft(itmp8, bitcnt), iand(itmp8_2, itmp8_3))
endif
Expand Down Expand Up @@ -213,7 +213,7 @@ subroutine g2_sbytesc(out, in, iskip, nbits, nskip, n)
integer :: bitcnt, tbit
integer, parameter :: ones(8)=(/ 1, 3, 7, 15, 31, 63, 127, 255/)
integer :: nbit, i, itmp, index, ibit, imask, itmp2, itmp3
integer, external :: mova2i
integer, external :: g2_mova2i

! number bits from zero to ...
! nbit is the last bit of the field to be filled
Expand All @@ -232,7 +232,7 @@ subroutine g2_sbytesc(out, in, iskip, nbits, nskip, n)
tbit = min(bitcnt, ibit + 1)
imask = ishft(ones(tbit), 7 - ibit)
itmp2 = iand(ishft(itmp, 7 - ibit),imask)
itmp3 = iand(mova2i(out(index)), 255 - imask)
itmp3 = iand(g2_mova2i(out(index)), 255 - imask)
out(index) = char(ior(itmp2, itmp3))
bitcnt = bitcnt - tbit
itmp = ishft(itmp, -tbit)
Expand All @@ -254,7 +254,7 @@ subroutine g2_sbytesc(out, in, iskip, nbits, nskip, n)
if (bitcnt .gt. 0) then
itmp2 = iand(itmp, ones(bitcnt))
!print '(z2.2, x, z2.2)', ones(bitcnt), itmp2
itmp3 = iand(mova2i(out(index)), 255 - ones(bitcnt))
itmp3 = iand(g2_mova2i(out(index)), 255 - ones(bitcnt))
out(index) = char(ior(itmp2, itmp3))
endif
enddo
Expand Down Expand Up @@ -308,7 +308,7 @@ subroutine g2_sbytesc8(out, in, iskip, nbits, nskip, n)
integer, parameter :: ones(8)=(/ 1, 3, 7, 15, 31, 63, 127, 255/)
integer :: nbit, i, index, ibit, imask, itmp1, itmp2, itmp3
integer (kind = 8) :: itmp8, itmp8_2
integer, external :: mova2i
integer, external :: g2_mova2i

! number bits from zero to ...
! nbit is the last bit of the field to be filled
Expand All @@ -328,7 +328,7 @@ subroutine g2_sbytesc8(out, in, iskip, nbits, nskip, n)
imask = ishft(ones(tbit), 7 - ibit)
itmp1 = int(ishft(itmp8, int(7 - ibit, kind(8))), kind(4))
itmp2 = iand(itmp1, imask)
itmp3 = iand(mova2i(out(index)), 255 - imask)
itmp3 = iand(g2_mova2i(out(index)), 255 - imask)
out(index) = char(ior(itmp2, itmp3))
bitcnt = bitcnt - tbit
itmp8 = ishft(itmp8, -tbit)
Expand All @@ -350,7 +350,7 @@ subroutine g2_sbytesc8(out, in, iskip, nbits, nskip, n)
if (bitcnt .gt. 0) then
itmp8_2 = int(ones(bitcnt), kind(8))
itmp2 = int(iand(itmp8, itmp8_2), kind(4))
itmp3 = iand(mova2i(out(index)), 255 - ones(bitcnt))
itmp3 = iand(g2_mova2i(out(index)), 255 - ones(bitcnt))
out(index) = char(ior(itmp2, itmp3))
endif
enddo
Expand Down
8 changes: 5 additions & 3 deletions src/g2index.F90
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
!> @param[in] lugb Logical unit of opened GRIB2 file.
!> @param[in] lugi Logical unit file opened to write index to.
!> @param[in] idxver Index version.
!> @param[in] filename Name of GRIB2 file.
!> @param[in] filename Name of GRIB2 file. This file must already be
!> open, with logical unit passed to the lugb parameter. The filename
!> is also encoded in one of the index header records.
!> @param[out] iret Return code:
!> - 0 success
!> - 90 problem opening GRIB2 file.
Expand Down Expand Up @@ -1214,7 +1216,7 @@ subroutine ix2gb2(lugb, lskip8, idxver, lgrib, cbuf, numfld, mlen, iret)
character cver, cdisc
character(len = 4) :: ctemp
integer loclus, locgds, locbms
integer :: indbmp, numsec, next, newsize, mova2i, mbuf, lindex
integer :: indbmp, numsec, next, newsize, g2_mova2i, mbuf, lindex
integer :: linmax, ixskp
integer :: mxspd, mxskp, mxsgd, mxsdr, mxsbm, mxlus
integer :: mxlen, mxds, mxfld, mxbms
Expand Down Expand Up @@ -1346,7 +1348,7 @@ subroutine ix2gb2(lugb, lskip8, idxver, lgrib, cbuf, numfld, mlen, iret)
endif
lindex = lindex + ilndrs
elseif (numsec .eq. 6) then ! found bms
indbmp = mova2i(cbread(6))
indbmp = g2_mova2i(cbread(6))
mypos = (ixsbm + inc) * INT1_BITS
if (indbmp.lt.254) then
locbms = int(ibskip8 - lskip8, kind(4))
Expand Down
4 changes: 2 additions & 2 deletions src/mova2i.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*
* @author Stephen Gilbert @date 2002-08-15
* */
int mova2i_(unsigned char *a)
int g2_mova2i_(unsigned char *a)
{
return (int)(*a);
}
Expand All @@ -35,7 +35,7 @@ int mova2i_(unsigned char *a)
*
* @author Ed Hartnett @date Feb 7, 2024
* */
int mova2i8_(unsigned char *a)
int g2_mova2i8_(unsigned char *a)
{
return (long long int)(*a);
}
56 changes: 28 additions & 28 deletions tests/test_gbytec.F90
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ program test_gbytec
integer :: num
integer(kind = 4) :: in44(4), in44_1(4)
integer(kind = 8) :: in8(1), in8_1(1), in84(4), in84_1(4)
integer, external :: mova2i
integer, external :: g2_mova2i

print *, 'Testing g2_gbytesc.F90 subroutines.'

print *, ' testing g2_sbytec(), packing one 32-bit int into 8 bits of a char array in big-endian...'
in(1) = 3
out(1) = char(0)
call g2_sbytec(out, in, iskip, nbits)
if (mova2i(out(1)) .ne. in(1)) stop 10
if (g2_mova2i(out(1)) .ne. in(1)) stop 10
!print '(z2.2)', out(1)

print *, ' packing one 32-bit int into 32 bits of a char array in big-endian...'
Expand All @@ -52,13 +52,13 @@ program test_gbytec

print *, ' testing g2_gbytec()...'
call g2_gbytec(out, in, iskip, nbits)
if (mova2i(out(1)) .ne. in(1)) stop 11
if (g2_mova2i(out(1)) .ne. in(1)) stop 11

print *, ' testing g2_sbytesc()...'
in(1) = 3
out(1) = char(0)
call g2_sbytesc(out, in, iskip, nbits, nskip, n)
if (mova2i(out(1)) .ne. in(1)) stop 20
if (g2_mova2i(out(1)) .ne. in(1)) stop 20

! This will pack the numbers 1 and 2 into the first two chars of the
! buffer. The rest of the output buffer will remain zeros.
Expand All @@ -72,9 +72,9 @@ program test_gbytec
call g2_sbytesc(out8, in2, iskip, nbits, nskip, n2)
do i = 1, 8
if (i .le. 2) then
if (mova2i(out8(i)) .ne. in2(i)) stop 30;
if (g2_mova2i(out8(i)) .ne. in2(i)) stop 30;
else
if (mova2i(out8(i)) .ne. 0) stop 31;
if (g2_mova2i(out8(i)) .ne. 0) stop 31;
endif
end do

Expand All @@ -92,7 +92,7 @@ program test_gbytec
end do
call g2_sbytesc(out5, in5, iskip, nbits, nskip, n5)
do i = 1, 5
if (mova2i(out5(i)) .ne. in5(i)) stop 40;
if (g2_mova2i(out5(i)) .ne. in5(i)) stop 40;
end do

! Now pack 5 values into the 10 character array out10. Skip every
Expand All @@ -107,17 +107,17 @@ program test_gbytec
do i = 1, 10
! print '(z2.2)', out10(i)
if (mod(i, 2) .gt. 0) then
if (mova2i(out10(i)) .ne. in5(int(i/2) + 1)) stop 51;
if (g2_mova2i(out10(i)) .ne. in5(int(i/2) + 1)) stop 51;
else
if (mova2i(out10(i)) .ne. 0) stop 52;
if (g2_mova2i(out10(i)) .ne. 0) stop 52;
endif
end do

print *, ' testing g2_sbytec() with iskip of 1...'
in(1) = 1
out(1) = char(0)
call g2_sbytec(out, in, 1, 6)
if (mova2i(out(1)) .ne. 2) stop 53
if (g2_mova2i(out(1)) .ne. 2) stop 53

print *, ' testing g2_sbytesc() with a size 4 output array...'
iskip = 0
Expand All @@ -126,8 +126,8 @@ program test_gbytec
num = 1
in(1) = 1
call g2_sbytesc(out4, in, iskip, nbits, nskip, num)
if (mova2i(out4(1)) .ne. 0 .and. mova2i(out4(2)) .ne. 0 .and. &
mova2i(out4(3)) .ne. 0 .and. mova2i(out4(4)) .ne. 1) stop 60
if (g2_mova2i(out4(1)) .ne. 0 .and. g2_mova2i(out4(2)) .ne. 0 .and. &
g2_mova2i(out4(3)) .ne. 0 .and. g2_mova2i(out4(4)) .ne. 1) stop 60

print *, ' now unpack into 4 ints with g2_gbytesc()...'
call g2_gbytesc(out4, in4, iskip, 8, 0, 4)
Expand Down Expand Up @@ -156,8 +156,8 @@ program test_gbytec
! call g2_sbytesc(out4, r_in, iskip, nbits, nskip, num)
! ! Note that the 32-bit IEEE representation of 1.0 is 3f800000. The
! ! decimal for 3f is 63, the decimal for 80 is 128.
! if (mova2i(out4(1)) .ne. 63 .and. mova2i(out4(2)) .ne. 128 .and. &
! mova2i(out4(3)) .ne. 0 .and. mova2i(out4(4)) .ne. 0) stop 80
! if (g2_mova2i(out4(1)) .ne. 63 .and. g2_mova2i(out4(2)) .ne. 128 .and. &
! g2_mova2i(out4(3)) .ne. 0 .and. g2_mova2i(out4(4)) .ne. 0) stop 80
! print '(z2.2)', out4(1)

! This test is the same as above, but does not require the
Expand All @@ -172,8 +172,8 @@ program test_gbytec
call g2_sbytesc(out4, in, iskip, nbits, nskip, num)
! Note that the 32-bit IEEE representation of 1.0 is 3f800000. The
! decimal for 3f is 63, the decimal for 80 is 128.
if (mova2i(out4(1)) .ne. 63 .and. mova2i(out4(2)) .ne. 128 .and. &
mova2i(out4(3)) .ne. 0 .and. mova2i(out4(4)) .ne. 0) stop 90
if (g2_mova2i(out4(1)) .ne. 63 .and. g2_mova2i(out4(2)) .ne. 128 .and. &
g2_mova2i(out4(3)) .ne. 0 .and. g2_mova2i(out4(4)) .ne. 0) stop 90

! For this test to pass the -fallow-argument-mismatch flag must be
! used, because I am passing in a real array instead of an int array
Expand All @@ -189,10 +189,10 @@ program test_gbytec
! call g2_sbytesc(out8, r_in2, iskip, nbits, nskip, num)
! ! Note that the 32-bit IEEE representation of 1.0 is 3f800000. The
! ! decimal for 3f is 63, the decimal for 80 is 128.
! if (mova2i(out8(1)) .ne. 63 .and. mova2i(out8(2)) .ne. 128 .and. &
! mova2i(out8(3)) .ne. 0 .and. mova2i(out8(4)) .ne. 0) stop 100
! if (mova2i(out8(5)) .ne. 63 .and. mova2i(out8(6)) .ne. 128 .and. &
! mova2i(out8(7)) .ne. 0 .and. mova2i(out8(8)) .ne. 0) stop 110
! if (g2_mova2i(out8(1)) .ne. 63 .and. g2_mova2i(out8(2)) .ne. 128 .and. &
! g2_mova2i(out8(3)) .ne. 0 .and. g2_mova2i(out8(4)) .ne. 0) stop 100
! if (g2_mova2i(out8(5)) .ne. 63 .and. g2_mova2i(out8(6)) .ne. 128 .and. &
! g2_mova2i(out8(7)) .ne. 0 .and. g2_mova2i(out8(8)) .ne. 0) stop 110
! print '(z2.2)', out8(1)

! This test is the same as above, but does not require the -fallow-argument-mismatch flag.
Expand All @@ -207,10 +207,10 @@ program test_gbytec
call g2_sbytesc(out8, in2, iskip, nbits, nskip, num)
! Note that the 32-bit IEEE representation of 1.0 is 3f800000. The
! decimal for 3f is 63, the decimal for 80 is 128.
if (mova2i(out4(1)) .ne. 63 .and. mova2i(out4(2)) .ne. 128 .and. &
mova2i(out4(3)) .ne. 0 .and. mova2i(out4(4)) .ne. 0) stop 120
if (mova2i(out8(5)) .ne. 63 .and. mova2i(out8(6)) .ne. 128 .and. &
mova2i(out8(7)) .ne. 0 .and. mova2i(out8(8)) .ne. 0) stop 130
if (g2_mova2i(out4(1)) .ne. 63 .and. g2_mova2i(out4(2)) .ne. 128 .and. &
g2_mova2i(out4(3)) .ne. 0 .and. g2_mova2i(out4(4)) .ne. 0) stop 120
if (g2_mova2i(out8(5)) .ne. 63 .and. g2_mova2i(out8(6)) .ne. 128 .and. &
g2_mova2i(out8(7)) .ne. 0 .and. g2_mova2i(out8(8)) .ne. 0) stop 130
!print '(z2.2)', out4(1)

print *, ' testing g2_sbytec8() with 64-bit int...'
Expand All @@ -222,9 +222,9 @@ program test_gbytec
do i = 1, 8
!print '(z2.2)', out8(i)
if (i .lt. 8) then
if (mova2i(out8(i)) .ne. 0) stop 140
if (g2_mova2i(out8(i)) .ne. 0) stop 140
else
if (mova2i(out8(i)) .ne. 1) stop 140
if (g2_mova2i(out8(i)) .ne. 1) stop 140
endif
end do

Expand Down Expand Up @@ -288,9 +288,9 @@ program test_gbytec
do i = 1, 8
print '(z2.2)', out8(i)
if (i .eq. 5) then
if (mova2i(out8(i)) .ne. 128) stop 210
if (g2_mova2i(out8(i)) .ne. 128) stop 210
else
if (mova2i(out8(i)) .ne. 0) stop 210
if (g2_mova2i(out8(i)) .ne. 0) stop 210
endif
end do

Expand Down

0 comments on commit 4c58a32

Please sign in to comment.