Implement deterministic int64 wrapping arithmetic
This commit is contained in:
@@ -1642,6 +1642,28 @@ enum json_err_code {
|
||||
JSON_ERROR_SYNTAX, /* Syntax error. */
|
||||
JSON_ERROR_UTF8 /* Malformed UTF-8 characters */
|
||||
};
|
||||
/* Integer operators retain the low 64 bits, interpreted as two's complement.
|
||||
* Convert only representable unsigned values to signed; arithmetic wraps unsigned.
|
||||
*/
|
||||
static inline sxi64 PH7_Int64FromUnsigned(sxu64 u) {
|
||||
return u <= (sxu64)SXI64_HIGH ? (sxi64)u : -1 - (sxi64)~u;
|
||||
}
|
||||
static inline sxi64 PH7_Int64Add(sxi64 a, sxi64 b) {
|
||||
return PH7_Int64FromUnsigned((sxu64)a + (sxu64)b);
|
||||
}
|
||||
static inline sxi64 PH7_Int64Sub(sxi64 a, sxi64 b) {
|
||||
return PH7_Int64FromUnsigned((sxu64)a - (sxu64)b);
|
||||
}
|
||||
static inline sxi64 PH7_Int64Mul(sxi64 a, sxi64 b) {
|
||||
return PH7_Int64FromUnsigned((sxu64)a * (sxu64)b);
|
||||
}
|
||||
static inline sxi64 PH7_Int64Neg(sxi64 a) {
|
||||
return PH7_Int64FromUnsigned((sxu64)0 - (sxu64)a);
|
||||
}
|
||||
/* The caller supplies a valid shift count (0..63). Avoid signed right shift. */
|
||||
static inline sxi64 PH7_Int64ShiftRight(sxi64 a, sxi32 b) {
|
||||
return a < 0 ? -1 - (sxi64)((~(sxu64)a) >> b) : (sxi64)((sxu64)a >> b);
|
||||
}
|
||||
/* memobj.c function prototypes */
|
||||
PH7_PRIVATE sxi32 PH7_MemObjDump(SyBlob *pOut, ph7_value *pObj, int ShowType, int nTab, int nDepth);
|
||||
PH7_PRIVATE const char *PH7_MemObjTypeDump(ph7_value *pVal);
|
||||
|
||||
Reference in New Issue
Block a user