Skip to content

Commit 11f2ce4

Browse files
committed
fix bitmanip for betterC
1 parent 28ae2f4 commit 11f2ce4

File tree

1 file changed

+24
-37
lines changed

1 file changed

+24
-37
lines changed

source/mir/bitmanip.d

Lines changed: 24 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,11 @@ module mir.bitmanip;
2323

2424
import std.traits;
2525

26-
private string myToString()(ulong n)
26+
private string normString()(string str)
2727
{
28-
UnsignedStringBuf buf;
29-
auto s = unsignedToTempString(n, buf);
30-
return s ~ (n > uint.max ? "UL" : "U");
31-
}
32-
33-
private alias UnsignedStringBuf = char[20];
34-
35-
private string unsignedToTempString()(ulong value, char[] buf, uint radix = 10) @safe
36-
{
37-
size_t i = buf.length;
38-
do
39-
{
40-
ubyte x = cast(ubyte)(value % radix);
41-
value = value / radix;
42-
buf[--i] = cast(char)((x < 10) ? x + '0' : x - 10 + 'a');
43-
} while (value);
44-
return buf[i .. $].idup;
28+
// if (str.length && (str[$-1] == 'U' || str[$-1] == 'u')) str = str[0 .. $-1];
29+
// if (str.length && (str[$-1] == 'L' || str[$-1] == 'l')) str = str[0 .. $-1];
30+
return str;
4531
}
4632

4733
private template createAccessors(
@@ -66,7 +52,7 @@ private template createAccessors(
6652
static if (T.min < 0)
6753
{
6854
enum long minVal = -(1uL << (len - 1));
69-
enum ulong maxVal = (1uL << (len - 1)) - 1;
55+
enum long maxVal = (1uL << (len - 1)) - 1;
7056
alias UT = Unsigned!(T);
7157
enum UT extendSign = cast(UT)~((~0uL) >> (64 - len));
7258
}
@@ -83,37 +69,37 @@ private template createAccessors(
8369
enum result =
8470
// getter
8571
"@property bool " ~ name ~ "()() @safe pure nothrow @nogc const { return "
86-
~"("~store~" & "~myToString(maskAllElse)~") != 0;}\n"
72+
~"("~store~" & "~ maskAllElse.stringof ~") != 0;}\n"
8773
// setter
8874
~"@property void " ~ name ~ "()(bool v) @safe pure nothrow @nogc { "
89-
~"if (v) "~store~" |= "~myToString(maskAllElse)~";"
90-
~"else "~store~" &= cast(typeof("~store~"))(-1-cast(typeof("~store~"))"~myToString(maskAllElse)~");}\n";
75+
~"if (v) "~store~" |= "~ maskAllElse.stringof ~";"
76+
~"else "~store~" &= cast(typeof("~store~"))(-1-cast(typeof("~store~"))"~ maskAllElse.stringof ~");}\n";
9177
}
9278
else
9379
{
9480
// getter
95-
enum result = "@property "~T.stringof~" "~name~"()() @safe pure nothrow @nogc const { auto result = "
96-
~"("~store~" & "
97-
~ myToString(maskAllElse) ~ ") >>"
98-
~ myToString(offset) ~ ";"
81+
enum result = "@property "~T.stringof~" "~name~"()() @safe pure nothrow @nogc const { ulong result = "
82+
~"(ulong("~store~") & "
83+
~ maskAllElse.stringof ~ ") >>"
84+
~ offset.stringof ~ ";"
9985
~ (T.min < 0
100-
? "if (result >= " ~ myToString(signBitCheck)
101-
~ ") result |= " ~ myToString(extendSign) ~ ";"
86+
? "if (result >= " ~ signBitCheck.stringof
87+
~ ") result |= " ~ extendSign.stringof ~ ";"
10288
: "")
10389
~ " return cast("~T.stringof~") result;}\n"
10490
// setter
10591
~"@property void "~name~"()("~T.stringof~" v) @safe pure nothrow @nogc { "
10692
~"assert(v >= "~name~`_min, "Value is smaller than the minimum value of bitfield '`~name~`'"); `
10793
~"assert(v <= "~name~`_max, "Value is greater than the maximum value of bitfield '`~name~`'"); `
10894
~store~" = cast(typeof("~store~"))"
109-
~" (("~store~" & (-1-cast(typeof("~store~"))"~myToString(maskAllElse)~"))"
110-
~" | ((cast(typeof("~store~")) v << "~myToString(offset)~")"
111-
~" & "~myToString(maskAllElse)~"));}\n"
95+
~" (("~store~" & (-1-cast(typeof("~store~"))"~ maskAllElse.stringof ~"))"
96+
~" | ((cast(typeof("~store~")) v << "~ offset.stringof ~")"
97+
~" & "~ maskAllElse.stringof ~"));}\n"
11298
// constants
11399
~"enum "~T.stringof~" "~name~"_min = cast("~T.stringof~")"
114-
~myToString(minVal)~"; "
100+
~ (minVal == minVal.min && minVal.min < 0 ? "long.min" : minVal.stringof) ~"; "
115101
~" enum "~T.stringof~" "~name~"_max = cast("~T.stringof~")"
116-
~myToString(maxVal)~"; ";
102+
~ maxVal.stringof ~"; ";
117103
}
118104
}
119105
}
@@ -179,17 +165,18 @@ private template createReferenceAccessor(string store, T, ulong bits, string nam
179165
~ "" ~ store ~ "_ptr = cast(void*) v;}\n";
180166

181167
enum mask = (1UL << bits) - 1;
168+
enum maskInv = ~mask;
182169
// getter
183170
enum ref_accessor = "@property "~T.stringof~" "~name~"()() @trusted pure nothrow @nogc const { auto result = "
184-
~ "("~store~" & "~myToString(~mask)~"); "
171+
~ "("~store~" & "~ maskInv.stringof ~"); "
185172
~ "return cast("~T.stringof~") cast(void*) result;}\n"
186173
// setter
187174
~"@property void "~name~"()("~T.stringof~" v) @trusted pure nothrow @nogc { "
188-
~"assert(((cast(typeof("~store~")) cast(void*) v) & "~myToString(mask)
175+
~"assert(((cast(typeof("~store~")) cast(void*) v) & "~ mask.stringof
189176
~`) == 0, "Value not properly aligned for '`~name~`'"); `
190177
~store~" = cast(typeof("~store~"))"
191-
~" (("~store~" & (cast(typeof("~store~")) "~myToString(mask)~"))"
192-
~" | ((cast(typeof("~store~")) cast(void*) v) & (cast(typeof("~store~")) "~myToString(~mask)~")));}\n";
178+
~" (("~store~" & (cast(typeof("~store~")) "~ mask.stringof ~"))"
179+
~" | ((cast(typeof("~store~")) cast(void*) v) & (cast(typeof("~store~")) "~ maskInv.stringof ~")));}\n";
193180

194181
enum result = storage ~ storage_accessor ~ ref_accessor;
195182
}

0 commit comments

Comments
 (0)