Now I stumbled acorss the problem of multiply 2 scores and create the score of the result.
IOW I want a multiply function that does the following:
Code: Select all
enum Score;
inline Score make_score(int mg, int eg) { return Score((mg << 16) + eg); }
Score s1 = make_score(5, 6);
Score s2 = make_score(3, 4);
Score s3 = multiply(s1, s2);
assert(s3 == make_score(15, 24));
Of course I can do something like
Code: Select all
Score multiply(Score s1, Score s2)
{
return make_score(mg_value(s1) * mg_value(s2), eg_value(s1) * eg_value(s2));
}
So I was thinking of the mutiplication of two polinomials:
Code: Select all
(aK + b) * (cK + d) == acK^2 + bd + K(ad + bc)
Code: Select all
Score multiply(Score s1, Score s2)
{
int64_t v64 = int64_t(s1) * int64_t(s2);
short bd = short(v64);
short ac = short(v64 >> 32);
if (ac < 0) ac++;
return make_score(ac, bd);
}
Code: Select all
Score s1 = make_score(-3, 14);
Score s2 = make_score(248, 271);
I got
s3 == (-743, 3794)
instead of the correct result
s3 == (-744, 3794)
Thanks in advance.