Skip to content

Commit c73fd4b

Browse files
committed
Fix byte to long casts.
1 parent 5cbd788 commit c73fd4b

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

compiler/src/dotty/tools/dotc/util/CityHash.scala

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,15 @@ package dotty.tools.dotc.util
3434
private[util] class CityHash {
3535

3636
// Some primes between 2^63 and 2^64 for various uses.
37-
final val k0 = 0xc3a5c85c97cb3127L
38-
final val k1 = 0xb492b66fbe98f273L
39-
final val k2 = 0x9ae16a3b2f90404fL
37+
private val k0 = 0xc3a5c85c97cb3127L
38+
private val k1 = 0xb492b66fbe98f273L
39+
private val k2 = 0x9ae16a3b2f90404fL
40+
41+
protected final def cityHash64WithSeed(data: Array[Byte], seed: Long): Long =
42+
cityHash64WithSeeds(data, k2, seed)
43+
44+
protected final def cityHash64WithSeeds(data: Array[Byte], seed0: Long, seed1: Long): Long =
45+
hashLen16(cityHash64(data) - seed0, seed1)
4046

4147
protected final def cityHash64(data: Array[Byte]): Long = {
4248
implicit val implicitData: Array[Byte] = data
@@ -91,11 +97,11 @@ private[util] class CityHash {
9197
val a = fetch32(0)
9298
hashLen16(len + (a << 3), fetch32(len - 4), mul)
9399
} else if (len > 0) {
94-
val a: Byte = data(0)
95-
val b: Byte = data(len >> 1)
96-
val c: Byte = data(len - 1)
97-
val y: Int = a.toInt + (b.toInt << 8)
98-
val z: Int = len + (c.toInt << 2)
100+
val a: Int = data(0) & 0xFF
101+
val b: Int = data(len >> 1) & 0xFF
102+
val c: Int = data(len - 1) & 0xFF
103+
val y: Int = a + (b << 8)
104+
val z: Int = len + (c << 2)
99105
shiftMix(y * k2 ^ z * k0) * k2
100106
} else {
101107
k2
@@ -172,21 +178,21 @@ private[util] class CityHash {
172178
weakHashLen32WithSeeds(fetch64(s), fetch64(s + 8), fetch64(s + 16), fetch64(s + 24), a, b)
173179

174180
private final def fetch64(idx: Int)(implicit data: Array[Byte]): Long = {
175-
var x: Long = data(idx)
176-
x = data(idx + 1) | (x << 8)
177-
x = data(idx + 2) | (x << 8)
178-
x = data(idx + 3) | (x << 8)
179-
x = data(idx + 4) | (x << 8)
180-
x = data(idx + 5) | (x << 8)
181-
x = data(idx + 6) | (x << 8)
182-
data(idx + 7) | (x << 8)
181+
var x: Long = data(idx) & 0xFFL
182+
x = data(idx + 1) & 0xFFL | (x << 8)
183+
x = data(idx + 2) & 0xFFL | (x << 8)
184+
x = data(idx + 3) & 0xFFL | (x << 8)
185+
x = data(idx + 4) & 0xFFL | (x << 8)
186+
x = data(idx + 5) & 0xFFL | (x << 8)
187+
x = data(idx + 6) & 0xFFL | (x << 8)
188+
data(idx + 7) & 0xFFL | (x << 8)
183189
}
184190

185191
private final def fetch32(idx: Int)(implicit data: Array[Byte]): Long = {
186-
var x: Int = data(idx)
187-
x = data(idx + 1) | (x << 8)
188-
x = data(idx + 2) | (x << 8)
189-
data(idx + 3) | (x << 8)
192+
var x: Int = data(idx) & 0xFF
193+
x = data(idx + 1) & 0xFF | (x << 8)
194+
x = data(idx + 2) & 0xFF | (x << 8)
195+
data(idx + 3) & 0xFF | (x << 8)
190196
}
191197

192198
private final def bswap64(x: Long): Long = {

0 commit comments

Comments
 (0)