Skip to content

Commit 85039e0

Browse files
now can pack/unpack 64-bit ints
1 parent 04b84c5 commit 85039e0

File tree

3 files changed

+243
-219
lines changed

3 files changed

+243
-219
lines changed

src/g2_gbytesc.F90

+10-3
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ subroutine g2_gbytesc8(in, iout, iskip, nbits, nskip, n)
126126
integer, parameter :: ones(8) = (/ 1, 3, 7, 15, 31, 63, 127, 255 /)
127127

128128
integer :: nbit, i, index, ibit, itmp
129+
integer (kind = 8) :: itmp8, itmp8_2, itmp8_3
129130
integer, external :: mova2i
131+
integer (kind = 8), external :: mova2i8
130132

131133
! nbit is the start position of the field in bits
132134
nbit = iskip
@@ -138,25 +140,30 @@ subroutine g2_gbytesc8(in, iout, iskip, nbits, nskip, n)
138140

139141
! first byte
140142
tbit = min(bitcnt, 8 - ibit)
143+
itmp8 = iand(mova2i8(in(index)), int(ones(8 - ibit), kind = 8))
141144
itmp = iand(mova2i(in(index)), ones(8 - ibit))
142145
if (tbit .ne. 8 - ibit) itmp = ishft(itmp, tbit - 8 + ibit)
146+
if (tbit .ne. 8 - ibit) itmp8 = ishft(itmp8, tbit - 8 + ibit)
143147
index = index + 1
144148
bitcnt = bitcnt - tbit
145149

146150
! now transfer whole bytes
147151
do while (bitcnt .ge. 8)
148152
itmp = ior(ishft(itmp,8), mova2i(in(index)))
153+
itmp8 = ior(ishft(itmp8,8), mova2i8(in(index)))
149154
bitcnt = bitcnt - 8
150155
index = index + 1
151156
enddo
152157

153158
! get data from last byte
154159
if (bitcnt .gt. 0) then
155-
itmp = ior(ishft(itmp, bitcnt), iand(ishft(mova2i(in(index)), &
156-
- (8 - bitcnt)), ones(bitcnt)))
160+
itmp = ior(ishft(itmp, bitcnt), iand(ishft(mova2i(in(index)), - (8 - bitcnt)), ones(bitcnt)))
161+
itmp8_2 = ishft(mova2i8(in(index)), int(-(8 - bitcnt), kind(8)))
162+
itmp8_3 = int(ones(bitcnt), kind(8))
163+
itmp8 = ior(ishft(itmp8, bitcnt), iand(itmp8_2, itmp8_3))
157164
endif
158165

159-
iout(i) = itmp
166+
iout(i) = itmp8
160167
enddo
161168

162169
end subroutine g2_gbytesc8

src/mova2i.c

+27-9
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,41 @@
11
/**
22
* @file
3-
* @brief mova2i Moves a bit string from a char*1 to int
3+
* @brief Move bits from a char*1 to an int.
44
* @author Stephen Gilbert @date 2002-08-15
55
*/
66

77
/**
8-
* This Function copies a bit string from a Character*1 variable
9-
* to an integer variable. It is intended to replace the Fortran Intrinsic
10-
* Function ICHAR, which only supports 0 <= ICHAR(a) <= 127 on the
11-
* IBM SP. If "a" is greater than 127 in the collating sequence,
12-
* ICHAR(a) does not return the expected bit value.
13-
* This function can be used for all values 0 <= ICHAR(a) <= 255.
8+
* Derefrence char pointer and cast result as 32-bit int.
9+
*
10+
* This function is intended to replace the Fortran Intrinsic Function
11+
* ICHAR, which only supports 0 <= ICHAR(a) <= 127 on the IBM SP. If
12+
* "a" is greater than 127 in the collating sequence, ICHAR(a) does
13+
* not return the expected bit value. This function can be used for
14+
* all values 0 <= ICHAR(a) <= 255.
1415
*
15-
* @param[in] a - Character*1 variable that holds the bitstring to extract
16-
* @return - > mova2i - Integer value of the bitstring in character a
16+
* @param a Pointer to char.
17+
*
18+
* @return 32-bit integer containing the value of the bits.
1719
*
1820
* @author Stephen Gilbert @date 2002-08-15
1921
* */
2022
int mova2i_(unsigned char *a)
2123
{
2224
return (int)(*a);
2325
}
26+
27+
/**
28+
* Derefrence char pointer and cast result as 64-bit int.
29+
*
30+
* See mova2i_() for details.
31+
*
32+
* @param a Pointer to char.
33+
*
34+
* @return 64-bit integer containing value of the bits.
35+
*
36+
* @author Ed Hartnett @date Feb 7, 2024
37+
* */
38+
int mova2i8_(unsigned char *a)
39+
{
40+
return (long long int)(*a);
41+
}

0 commit comments

Comments
 (0)