1

I got the following code:

p = B[m] & B[m + 5] & B[m + 6] & B[m + 11];

m -= d * (l > 0) * 11 + !d * (c % 5 > 0);

p += m ^ M ? B[m] & B[m + 5] & B[m + 6] & B[m + 11] : 0;

I know it's hard to read, but here's a TL;DR for it : I check multiple bits (all are related to m) in a bitset, then i change the value of variable m and i check again (other bits). Is there a way i can acces those bits in less code, or to template the check (cuz are the same formulas for bits)?

B[m] & B[m + 5] & B[m + 6] & B[m + 11]

Thank you :D.

aleeN1
  • 43
  • 8
  • 1
    would be easier to read if you would declare the variables – 463035818_is_not_an_ai Apr 17 '17 at 18:10
  • static int P, m, p, M; static bitset<99> B; – aleeN1 Apr 17 '17 at 18:12
  • Interestingly enough, your bitset fits into one SSE register, which would allow to precompute the `B[m]&B[m+5]&...` extremely fast for all `m` values at the same time – Ap31 Apr 17 '17 at 18:24
  • Idk what you talking about, but i don't want to change the structure(bitset) into something else that would ruine the whole code. Could you link me something? – aleeN1 Apr 17 '17 at 18:34
  • @aleeN1 I don't think you have to change anything, chances are the compiler will make use of it. Just notice that `bitset<99>` is very lightweight so it's perfectly okay to copy it around – Ap31 Apr 17 '17 at 18:37

2 Answers2

3

Make a function that takes B and m.

So p = yourFunc(B, m) and p += m ^M ? yourFunc(B, m) : 0

The function is something like:

TYPEOFP yourFunc(TYPEOFB b, TYPEOFM m) {
    return b[m] & b[m + 5] & b[m + 6] & b[m + 11];
}

I don't know your types, so you need to fill it in.

I wouldn't recommend a macro, but if you want that it's

#define yourMACRO(b, m) ((b)[(m)] & (b)[(m) + 5] & (b)[(m) + 6] & (b)[(m) + 11])

All of those extra parens are to protect you if you ever pass in an expression for b or m. The macro will fail if you pass in something with side-effects (like ++m).

EDIT: From your comments, you said you can't write outside the function.

It's unorthodox, but you can do the #define in the function and #undef it at the end of the function.

Depending on the version of C++ you have, you might have lambdas, which let you make function expressions.

If you are desperate, you can define an inner class or struct with a static function: C++ can we have functions inside functions?

Community
  • 1
  • 1
Lou Franco
  • 87,846
  • 14
  • 132
  • 192
3

I suggest using a function to precompute a helper bitset for that:

bitset<99> prepare_bitset(const bitset<99>& B)
{
   return B & (B<<5) & (B<<6) & (B<<11);
}

Then you can just use it like this:

auto HB = prepare_bitset(B);
p = HB[m];
m -= d * (l > 0) * 11 + !d * (c % 5 > 0);
p += m ^ M ? HB[m] : 0;

UPD: Another option is to just define HB in place:

auto HB = B & (B<<5) & (B<<6) & (B<<11);
p = HB[m];
m -= d * (l > 0) * 11 + !d * (c % 5 > 0);
p += m ^ M ? HB[m] : 0;
Ap31
  • 3,244
  • 1
  • 18
  • 25
  • can i do that inside the function? I'm talking about prepare_bitset function, can i write in inside another function? – aleeN1 Apr 17 '17 at 18:36
  • I'll try it out. I thought lambdas can only be used as parameters for functions. – aleeN1 Apr 17 '17 at 18:41