Skip to content

Instantly share code, notes, and snippets.

@pervognsen
Last active October 23, 2022 10:57
Show Gist options
  • Save pervognsen/cd705df3762c79a245e0c1eb4a7d5a0e to your computer and use it in GitHub Desktop.
Save pervognsen/cd705df3762c79a245e0c1eb4a7d5a0e to your computer and use it in GitHub Desktop.
INLINE
u32 index(Ref r) {
return r >> 4;
}
INLINE
u32 tag(Ref r) {
return r & 15;
}
extern u32 (*eval_table[16])(u32, u32);
INLINE
u32 eval(Ref r) {
u32 x = nodes[index(r)].x;
u32 y = nodes[index(r)].y;
return eval_table[tag(r)](x, y);
}
u32 eval_lit(u32 x, u32 y) {
return x;
}
u32 eval_neg(u32 x, u32 y) {
return -eval(x);
}
u32 eval_not(u32 x, u32 y) {
return ~eval(x);
}
u32 eval_add(u32 x, u32 y) {
return eval(x) + eval(y);
}
u32 eval_sub(u32 x, u32 y) {
return eval(x) - eval(y);
}
u32 eval_and(u32 x, u32 y) {
return eval(x) & eval(y);
}
u32 eval_or(u32 x, u32 y) {
return eval(x) | eval(y);
}
u32 eval_xor(u32 x, u32 y) {
return eval(x) ^ eval(y);
}
u32 eval_lsl(u32 x, u32 y) {
return eval(x) << (eval(y) & 31);
}
u32 eval_lsr(u32 x, u32 y) {
return eval(x) >> (eval(y) & 31);
}
u32 eval_asr(u32 x, u32 y) {
return (i32)eval(x) >> (eval(y) & 31);
}
u32 (*eval_table[16])(u32, u32) = {
[LIT] = eval_lit,
[NEG] = eval_neg,
[NOT] = eval_not,
[ADD] = eval_add,
[SUB] = eval_sub,
[AND] = eval_and,
[OR] = eval_or,
[XOR] = eval_xor,
[LSL] = eval_lsl,
[LSR] = eval_lsr,
[ASR] = eval_asr,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment