| 1 |
/* PERMUTE_ARGS: -O |
| 2 |
* https://issues.dlang.org/show_bug.cgi?id=19813 |
| 3 |
*/ |
| 4 |
|
| 5 |
struct BitArray |
| 6 |
{ |
| 7 |
import core.bitop : btc, bts, btr, bsf, bt; |
| 8 |
|
| 9 |
size_t _len; |
| 10 |
size_t* _ptr; |
| 11 |
enum bitsPerSizeT = size_t.sizeof * 8; |
| 12 |
|
| 13 |
static size_t lenToDim(size_t len) @nogc pure nothrow @safe |
| 14 |
{ |
| 15 |
return (len + (bitsPerSizeT-1)) / bitsPerSizeT; |
| 16 |
} |
| 17 |
|
| 18 |
this(in bool[] ba) nothrow pure |
| 19 |
{ |
| 20 |
length = ba.length; |
| 21 |
foreach (i, b; ba) |
| 22 |
{ |
| 23 |
if (b) |
| 24 |
bts(_ptr, i); |
| 25 |
else |
| 26 |
btr(_ptr, i); |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
@property size_t length(size_t newlen) pure nothrow @system |
| 31 |
{ |
| 32 |
if (newlen != _len) |
| 33 |
{ |
| 34 |
size_t olddim = lenToDim(_len); |
| 35 |
immutable newdim = lenToDim(newlen); |
| 36 |
|
| 37 |
if (newdim != olddim) |
| 38 |
{ |
| 39 |
// Create a fake array so we can use D's realloc machinery |
| 40 |
auto b = _ptr[0 .. olddim]; |
| 41 |
b.length = newdim; // realloc |
| 42 |
_ptr = b.ptr; |
| 43 |
} |
| 44 |
|
| 45 |
_len = newlen; |
| 46 |
} |
| 47 |
return _len; |
| 48 |
} |
| 49 |
|
| 50 |
int opCmp(ref BitArray a2) const @nogc pure nothrow |
| 51 |
{ |
| 52 |
const lesser = this._len < a2._len ? &this : &a2; |
| 53 |
immutable fullWords = lesser._len / lesser.bitsPerSizeT; |
| 54 |
immutable endBits = lesser._len % lesser.bitsPerSizeT; |
| 55 |
auto p1 = this._ptr; |
| 56 |
auto p2 = a2._ptr; |
| 57 |
|
| 58 |
foreach (i; 0 .. fullWords) |
| 59 |
{ |
| 60 |
if (p1[i] != p2[i]) |
| 61 |
{ |
| 62 |
return p1[i] & (size_t(1) << bsf(p1[i] ^ p2[i])) ? 1 : -1; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
if (endBits) |
| 67 |
{ |
| 68 |
immutable i = fullWords; |
| 69 |
immutable diff = p1[i] ^ p2[i]; |
| 70 |
if (diff) |
| 71 |
{ |
| 72 |
immutable index = bsf(diff); |
| 73 |
if (index < endBits) |
| 74 |
{ |
| 75 |
// This gets optimized into OPbtst, and was doing it incorrectly |
| 76 |
return p1[i] & (size_t(1) << index) ? 1 : -1; |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
return -1; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
void test1() |
| 86 |
{ |
| 87 |
bool[] ba = [1,0,1,0,1]; |
| 88 |
bool[] bd = [1,0,1,1,1]; |
| 89 |
|
| 90 |
auto a = BitArray(ba); |
| 91 |
auto d = BitArray(bd); |
| 92 |
|
| 93 |
assert(a < d); |
| 94 |
} |
| 95 |
|
| 96 |
/***************************************/ |
| 97 |
|
| 98 |
// https://issues.dlang.org/show_bug.cgi?id=18748 |
| 99 |
|
| 100 |
int bt_32_imm(in uint* p) |
| 101 |
{ |
| 102 |
enum bitnum = 1; |
| 103 |
return ((p[bitnum >> 5] & (1 << (bitnum & 31)))) != 0; |
| 104 |
} |
| 105 |
|
| 106 |
void test18748() |
| 107 |
{ |
| 108 |
version (linux) |
| 109 |
{ |
| 110 |
import core.sys.posix.sys.mman; |
| 111 |
import core.sys.posix.unistd; |
| 112 |
// Allocate two pages. |
| 113 |
immutable sz = 2 * sysconf(_SC_PAGESIZE); |
| 114 |
auto m = mmap(null, sz, PROT_READ, MAP_PRIVATE | MAP_ANON, -1, 0); |
| 115 |
// Discard the higher page. It becomes unreadable. |
| 116 |
munmap(m + sz / 2, sz / 2); |
| 117 |
// Try looking at the last 4 bytes of the readable page. |
| 118 |
uint* p = cast(uint*) (m + sz / 2 - uint.sizeof); |
| 119 |
bt_32_imm(p); |
| 120 |
munmap(m, sz / 2); // Free the readable page. |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
/***************************************/ |
| 125 |
|
| 126 |
// https://issues.dlang.org/show_bug.cgi?id=18749 |
| 127 |
|
| 128 |
ulong f(ulong* p, uint shift) |
| 129 |
{ |
| 130 |
return (*p >> shift) & 1; |
| 131 |
} |
| 132 |
|
| 133 |
ulong g(ulong* p, ulong shift) |
| 134 |
{ |
| 135 |
return f(p, cast(uint) shift); |
| 136 |
} |
| 137 |
|
| 138 |
void test18749() |
| 139 |
{ |
| 140 |
enum shift = uint.max + 1L; |
| 141 |
assert(cast(uint) shift == 0); |
| 142 |
ulong s = 1; |
| 143 |
assert(g(&s, shift)); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
/***************************************/ |
| 148 |
|
| 149 |
int main() |
| 150 |
{ |
| 151 |
test1(); |
| 152 |
test18748(); |
| 153 |
test18749(); |
| 154 |
|
| 155 |
return 0; |
| 156 |
} |