right shift is now by default sign preserving (#11322)
* right shift is now by default sign preserving * fix hashString and semfold * enable arithmetic shift right globally for CI * fix typo * remove xxx * use oldShiftRight as flag * apply feedback * add changelog entry
This commit is contained in:
parent
897e7c90ac
commit
88b5dd3362
18 changed files with 211 additions and 187 deletions
|
|
@ -38,7 +38,7 @@ type
|
|||
Trunk = object
|
||||
next: PTrunk # all nodes are connected with this pointer
|
||||
key: int # start address at bit 0
|
||||
bits: array[0..IntsPerTrunk-1, int] # a bit vector
|
||||
bits: array[0..IntsPerTrunk-1, uint] # a bit vector
|
||||
|
||||
TrunkBuckets = array[0..255, PTrunk]
|
||||
IntSet = object
|
||||
|
|
@ -332,21 +332,21 @@ proc contains(s: IntSet, key: int): bool =
|
|||
var t = intSetGet(s, key shr TrunkShift)
|
||||
if t != nil:
|
||||
var u = key and TrunkMask
|
||||
result = (t.bits[u shr IntShift] and (1 shl (u and IntMask))) != 0
|
||||
result = (t.bits[u shr IntShift] and (uint(1) shl (u and IntMask))) != 0
|
||||
else:
|
||||
result = false
|
||||
|
||||
proc incl(a: var MemRegion, s: var IntSet, key: int) =
|
||||
var t = intSetPut(a, s, key shr TrunkShift)
|
||||
var u = key and TrunkMask
|
||||
t.bits[u shr IntShift] = t.bits[u shr IntShift] or (1 shl (u and IntMask))
|
||||
t.bits[u shr IntShift] = t.bits[u shr IntShift] or (uint(1) shl (u and IntMask))
|
||||
|
||||
proc excl(s: var IntSet, key: int) =
|
||||
var t = intSetGet(s, key shr TrunkShift)
|
||||
if t != nil:
|
||||
var u = key and TrunkMask
|
||||
t.bits[u shr IntShift] = t.bits[u shr IntShift] and not
|
||||
(1 shl (u and IntMask))
|
||||
(uint(1) shl (u and IntMask))
|
||||
|
||||
iterator elements(t: IntSet): int {.inline.} =
|
||||
# while traversing it is forbidden to change the set!
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ type
|
|||
BitIndex = range[0..UnitsPerPage-1]
|
||||
PageDesc {.final, pure.} = object
|
||||
next: PPageDesc # all nodes are connected with this pointer
|
||||
key: ByteAddress # start address at bit 0
|
||||
key: uint # start address at bit 0
|
||||
bits: array[BitIndex, int] # a bit vector
|
||||
|
||||
PPageDescArray = ptr UncheckedArray[PPageDesc]
|
||||
|
|
@ -97,7 +97,7 @@ proc nextTry(h, maxHash: int): int {.inline.} =
|
|||
# generates each int in range(maxHash) exactly once (see any text on
|
||||
# random-number generation for proof).
|
||||
|
||||
proc cellSetGet(t: CellSet, key: ByteAddress): PPageDesc =
|
||||
proc cellSetGet(t: CellSet, key: uint): PPageDesc =
|
||||
var h = cast[int](key) and t.max
|
||||
while t.data[h] != nil:
|
||||
if t.data[h].key == key: return t.data[h]
|
||||
|
|
@ -122,7 +122,7 @@ proc cellSetEnlarge(t: var CellSet) =
|
|||
dealloc(t.data)
|
||||
t.data = n
|
||||
|
||||
proc cellSetPut(t: var CellSet, key: ByteAddress): PPageDesc =
|
||||
proc cellSetPut(t: var CellSet, key: uint): PPageDesc =
|
||||
var h = cast[int](key) and t.max
|
||||
while true:
|
||||
var x = t.data[h]
|
||||
|
|
@ -146,33 +146,33 @@ proc cellSetPut(t: var CellSet, key: ByteAddress): PPageDesc =
|
|||
# ---------- slightly higher level procs --------------------------------------
|
||||
|
||||
proc contains(s: CellSet, cell: PCell): bool =
|
||||
var u = cast[ByteAddress](cell)
|
||||
var u = cast[uint](cell)
|
||||
var t = cellSetGet(s, u shr PageShift)
|
||||
if t != nil:
|
||||
u = (u %% PageSize) /% MemAlign
|
||||
u = (u mod PageSize) div MemAlign
|
||||
result = (t.bits[u shr IntShift] and (1 shl (u and IntMask))) != 0
|
||||
else:
|
||||
result = false
|
||||
|
||||
proc incl(s: var CellSet, cell: PCell) {.noinline.} =
|
||||
var u = cast[ByteAddress](cell)
|
||||
var u = cast[uint](cell)
|
||||
var t = cellSetPut(s, u shr PageShift)
|
||||
u = (u %% PageSize) /% MemAlign
|
||||
u = (u mod PageSize) div MemAlign
|
||||
t.bits[u shr IntShift] = t.bits[u shr IntShift] or (1 shl (u and IntMask))
|
||||
|
||||
proc excl(s: var CellSet, cell: PCell) =
|
||||
var u = cast[ByteAddress](cell)
|
||||
var u = cast[uint](cell)
|
||||
var t = cellSetGet(s, u shr PageShift)
|
||||
if t != nil:
|
||||
u = (u %% PageSize) /% MemAlign
|
||||
u = (u mod PageSize) div MemAlign
|
||||
t.bits[u shr IntShift] = (t.bits[u shr IntShift] and
|
||||
not (1 shl (u and IntMask)))
|
||||
|
||||
proc containsOrIncl(s: var CellSet, cell: PCell): bool =
|
||||
var u = cast[ByteAddress](cell)
|
||||
var u = cast[uint](cell)
|
||||
var t = cellSetGet(s, u shr PageShift)
|
||||
if t != nil:
|
||||
u = (u %% PageSize) /% MemAlign
|
||||
u = (u mod PageSize) div MemAlign
|
||||
result = (t.bits[u shr IntShift] and (1 shl (u and IntMask))) != 0
|
||||
if not result:
|
||||
t.bits[u shr IntShift] = t.bits[u shr IntShift] or
|
||||
|
|
@ -185,15 +185,15 @@ iterator elements(t: CellSet): PCell {.inline.} =
|
|||
# while traversing it is forbidden to add pointers to the tree!
|
||||
var r = t.head
|
||||
while r != nil:
|
||||
var i = 0
|
||||
while i <= high(r.bits):
|
||||
var i: uint = 0
|
||||
while int(i) <= high(r.bits):
|
||||
var w = r.bits[i] # taking a copy of r.bits[i] here is correct, because
|
||||
# modifying operations are not allowed during traversation
|
||||
var j = 0
|
||||
var j: uint = 0
|
||||
while w != 0: # test all remaining bits for zero
|
||||
if (w and 1) != 0: # the bit is set!
|
||||
yield cast[PCell]((r.key shl PageShift) or
|
||||
(i shl IntShift +% j) *% MemAlign)
|
||||
(i shl IntShift + j) * MemAlign)
|
||||
inc(j)
|
||||
w = w shr 1
|
||||
inc(i)
|
||||
|
|
@ -238,16 +238,16 @@ iterator elementsExcept(t, s: CellSet): PCell {.inline.} =
|
|||
var r = t.head
|
||||
while r != nil:
|
||||
let ss = cellSetGet(s, r.key)
|
||||
var i = 0
|
||||
while i <= high(r.bits):
|
||||
var i:uint = 0
|
||||
while int(i) <= high(r.bits):
|
||||
var w = r.bits[i]
|
||||
if ss != nil:
|
||||
w = w and not ss.bits[i]
|
||||
var j = 0
|
||||
var j:uint = 0
|
||||
while w != 0:
|
||||
if (w and 1) != 0:
|
||||
yield cast[PCell]((r.key shl PageShift) or
|
||||
(i shl IntShift +% j) *% MemAlign)
|
||||
(i shl IntShift + j) * MemAlign)
|
||||
inc(j)
|
||||
w = w shr 1
|
||||
inc(i)
|
||||
|
|
|
|||
|
|
@ -133,7 +133,7 @@ proc extGetCellType(c: pointer): PNimType {.compilerproc.} =
|
|||
result = usrToCell(c).typ
|
||||
|
||||
proc internRefcount(p: pointer): int {.exportc: "getRefcount".} =
|
||||
result = int(usrToCell(p).refcount) shr rcShift
|
||||
result = usrToCell(p).refcount shr rcShift
|
||||
|
||||
# this that has to equals zero, otherwise we have to round up UnitsPerPage:
|
||||
when BitsPerPage mod (sizeof(int)*8) != 0:
|
||||
|
|
|
|||
|
|
@ -30,15 +30,15 @@ proc eqStrings(a, b: string): bool {.inline, compilerProc.} =
|
|||
proc hashString(s: string): int {.compilerproc.} =
|
||||
# the compiler needs exactly the same hash function!
|
||||
# this used to be used for efficient generation of string case statements
|
||||
var h = 0
|
||||
var h : uint = 0
|
||||
for i in 0..len(s)-1:
|
||||
h = h +% ord(s[i])
|
||||
h = h +% h shl 10
|
||||
h = h + uint(s[i])
|
||||
h = h + h shl 10
|
||||
h = h xor (h shr 6)
|
||||
h = h +% h shl 3
|
||||
h = h + h shl 3
|
||||
h = h xor (h shr 11)
|
||||
h = h +% h shl 15
|
||||
result = h
|
||||
h = h + h shl 15
|
||||
result = cast[int](h)
|
||||
|
||||
proc addInt*(result: var string; x: int64) =
|
||||
## Converts integer to its string representation and appends it to `result`.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue