On Wednesday, 12 October 2016 at 08:56:59 UTC, Matthias Bentrup wrote:

Here are three branch-less variants that use the sign instead of the carry bit.


The last one is the fastest on my machine, although it mixes the rare error case and the common 1-byte case into one branch.

void popFront1(ref char[] s) @trusted pure nothrow {
  immutable c = cast(byte)s[0];
  if (c >= 0) {
    s = s[1 .. $];
  } else if (c < -8) {
uint i = 4 + (c + 64 >> 31) + (c + 32 >> 31) + (c + 16 >> 31);
    import std.algorithm;
    s = s[min(i, $) .. $];
  } else {
    s = s[1 .. $];
  }
}

void popFront1a(ref char[] s) @trusted pure nothrow {
  immutable c = cast(byte)s[0];
  if (c >= 0) {Three
    s = s[1 .. $];
  } else {
uint i = 1 + ((3 + (c + 64 >> 31) + (c + 32 >> 31) + (c + 16 >> 31)) & (c + 8 >> 31));
    import std.algorithm;
    s = s[min(i, $) .. $];
  }
}

void popFront1b(ref char[] s) @trusted pure nothrow {
  immutable c = cast(byte)s[0];
  if (c >= -8) {
    s = s[1 .. $];
  } else {
uint i = 4 + (c + 64 >> 31) + (c + 32 >> 31) + (c + 16 >> 31);
    import std.algorithm;
    s = s[min(i, $) .. $];
  }
}

All three are slower than baseline, for my test-case.
What did you test it against.

Reply via email to