Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Decimal: forbid mantissae aliasing & improve Add/Sub performance
We need to distinguish mantissa aliasing, the test performed by the alias() function, from "same" mantissa, the test performed by the same() function. Mantissa aliasing is not to be confused with Decimal argument aliasing either. Decimal aliasing happens when calling for example z.Add(z, foo): within the (z *Decimal).Add(x, y *Decimal) function, argument z will alias x. In uadd/usub, there was a check al := alias(z.mant, x.mant) || alias(z.mant, y.mant) that suggested that the mantissa of some Decimal could "alias" the mantissa of another. i.e. that the following could happen: z.mant = x.mant[n:] (where n > 0). This is not desirable: some dec functions handle aliasing gracefuly but under some unchecked or undocumented conditions. For example (dec).shl is written to support z.shl(z, foo) but not z.shl(z[n:], foo). The first part of this change is to forbid mantissa aliasing (but not "same" mantissa, i.e. Decimal aliasing). The second part is in uadd/usub: the alias() calls are replaced by same() calls, and instead of making a temp copy if either same(z.mant, x.mant) or same(z.mant, y.mant) is true, the copy is mode if only the relevant condition is true. With mantissa aliasing forbidden, there is no need to check the arguments of the z.shl() calls.
- Loading branch information