Skip to content

Commit

Permalink
Fix semicolon detection, which cause invalid temperature parsing and …
Browse files Browse the repository at this point in the history
…subsequently segmentation faults due to counter addressing
  • Loading branch information
Smoofie committed Feb 1, 2024
1 parent fba93eb commit d4527f7
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions src/main/java/dev/morling/onebrc/CalculateAverage_Smoofie.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,25 @@ private CountResult(
}

private static int hash(long cityNameAddress, short cityLength) {
long[] city = new long[2];
unsafe.copyMemory(null, cityNameAddress, city, Unsafe.ARRAY_LONG_BASE_OFFSET, cityLength);
long hash = city[0];
int foldedHash = (int) (hash ^ (hash >>> 30));
return (foldedHash & foldedHash >>> 15) & 0xffff;
if (cityLength < 17) {
long[] city = new long[2];
unsafe.copyMemory(null, cityNameAddress, city, Unsafe.ARRAY_LONG_BASE_OFFSET, cityLength);
long hash = city[0] ^ (city[1] >> 1);
int foldedHash = (int) (hash ^ (hash >>> 31));
return (foldedHash & foldedHash >>> 15) & 0xffff;
}
else {
long[] city = new long[cityLength >> 3 + 1];
unsafe.copyMemory(null, cityNameAddress, city, Unsafe.ARRAY_LONG_BASE_OFFSET, cityLength);

long hash = city[0];
for (int i = 1; i < city.length; i++) {
hash ^= city[i];
}

int foldedHash = (int) (hash ^ (hash >>> 30));
return (foldedHash & foldedHash >>> 15) & 0xffff;
}
}

private static Unsafe getUnsafe() {
Expand All @@ -89,12 +103,7 @@ private static Unsafe getUnsafe() {

private static long locateSemicolon(long input) {
long semiXor = input ^ 0x3B3B3B3B3B3B3B3BL;
return (semiXor - 0x0101010101010101L) & ~semiXor & 0xf0f0f0f0f0f0f0f0L;
}

private static long locateDecimalPoint(long input) {
long decimalXor = input ^ 0x2E2E2E2E2E2E2E2EL;
return (decimalXor - 0x0101010101010101L) & ~decimalXor & 0xf0f0f0f0f0f0f0f0L;
return (semiXor - 0x0101010101010101L) & ~semiXor & 0x8080808080808080L;
}

public static void main(String[] args) throws IOException, InterruptedException {
Expand Down

0 comments on commit d4527f7

Please sign in to comment.