Skip to content

Commit

Permalink
integer decode refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
esaulpaugh committed Aug 30, 2024
1 parent aa30b90 commit c385c21
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/esaulpaugh/headlong/abi/BooleanType.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ void encodePackedUnchecked(Boolean value, ByteBuffer dest) {
public Boolean decode(ByteBuffer bb, byte[] unitBuffer) {
final long abc = bb.getLong() | bb.getLong() | bb.getLong();
final long abcd = bb.getLong() | abc;
if (abcd == 1L && abc == 0L) return true;
if (abcd == 0L) return false;
if (abcd == 1L && abc == 0L) return true;
throw err(bb);
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/esaulpaugh/headlong/abi/UnitType.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private static long toLong(Object value) {

final long decodeUnsignedLong(ByteBuffer bb) {
final long a = bb.getLong(), b = bb.getLong(), c = bb.getLong(), d = bb.getLong();
if ((a | b | c) == 0L && d >= 0L && d <= maxLong) {
if ((a | b | c | (d & (-1L << bitLength))) == 0L) {
return d;
}
throw err(bb);
Expand All @@ -110,10 +110,10 @@ final long decodeUnsignedLong(ByteBuffer bb) {
final long decodeSignedLong(ByteBuffer bb) {
final long a = bb.getLong(), b = bb.getLong(), c = bb.getLong(), d = bb.getLong();
if ((a | b | c) == 0L) {
if (Long.numberOfLeadingZeros(d) > Long.SIZE - bitLength) {
if (Long.numberOfLeadingZeros(d) - (Long.SIZE - bitLength) > 0) {
return d;
}
} else if ((a & b & c) == -1L && Long.numberOfLeadingZeros(~d) > Long.SIZE - bitLength) {
} else if ((a & b & c) == -1L && Long.numberOfLeadingZeros(~d) - (Long.SIZE - bitLength) > 0) {
return d;
}
throw err(bb);
Expand Down

0 comments on commit c385c21

Please sign in to comment.