public int Cast(long a) { if (a < int.MinValue || a > int.MaxValue) { MathOperationError.Overflow(); } return((int)a); }
public Vector <long> Add(Vector <long> lhs, Vector <int> rhs) { // Adapted from Hacker's Delight p26, where both inputs need to be of the same sign, and that sign is different from result // (i.e. xors for diff, and for both, less and zero for checking sign bit set) Vector.Widen(rhs, out var rhs1, out var rhs2); var partialResult = Vector.Add(lhs, rhs1); var partialResultCheck = Vector.BitwiseAnd(Vector.Xor(partialResult, lhs), Vector.Xor(partialResult, rhs1)); var finalResult = Vector.Add(partialResult, rhs2); var finalResultCheck = Vector.BitwiseAnd(Vector.Xor(finalResult, partialResult), Vector.Xor(finalResult, rhs2)); if (Vector.LessThanAny(Vector.BitwiseOr(partialResultCheck, finalResultCheck), Vector <long> .Zero)) { MathOperationError.Overflow(); } return(finalResult); }