Skip to content

Commit

Permalink
Code reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
Antares0982 committed Dec 31, 2024
1 parent cdccacb commit c6b478d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 90 deletions.
45 changes: 0 additions & 45 deletions src/decode/decode_float.inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,51 +98,6 @@ force_inline bool pyyjson_decode_double(DecodeObjStackInfo *restrict decode_obj_

force_inline bool pyyjson_decode_longlong(DecodeObjStackInfo *restrict decode_obj_stack_info, i64 val);

/*==============================================================================
* 128-bit Integer Utils
* These functions are used by the floating-point number reader and writer.
*============================================================================*/

/** Multiplies two 64-bit unsigned integers (a * b),
returns the 128-bit result as 'hi' and 'lo'. */
force_inline void u128_mul(u64 a, u64 b, u64 *hi, u64 *lo) {
#if PYYJSON_HAS_INT128
u128 m = (u128)a * b;
*hi = (u64)(m >> 64);
*lo = (u64)(m);
#elif MSC_HAS_UMUL128
*lo = _umul128(a, b, hi);
#else
u32 a0 = (u32)(a), a1 = (u32)(a >> 32);
u32 b0 = (u32)(b), b1 = (u32)(b >> 32);
u64 p00 = (u64)a0 * b0, p01 = (u64)a0 * b1;
u64 p10 = (u64)a1 * b0, p11 = (u64)a1 * b1;
u64 m0 = p01 + (p00 >> 32);
u32 m00 = (u32)(m0), m01 = (u32)(m0 >> 32);
u64 m1 = p10 + m00;
u32 m10 = (u32)(m1), m11 = (u32)(m1 >> 32);
*hi = p11 + m01 + m11;
*lo = ((u64)m10 << 32) | (u32)p00;
#endif
}

/** Multiplies two 64-bit unsigned integers and add a value (a * b + c),
returns the 128-bit result as 'hi' and 'lo'. */
force_inline void u128_mul_add(u64 a, u64 b, u64 c, u64 *hi, u64 *lo) {
#if PYYJSON_HAS_INT128
u128 m = (u128)a * b + c;
*hi = (u64)(m >> 64);
*lo = (u64)(m);
#else
u64 h, l, t;
u128_mul(a, b, &h, &l);
t = l + c;
h += (u64)(((t < l) | (t < c)));
*hi = h;
*lo = t;
#endif
}


#if PYYJSON_HAS_IEEE_754

Expand Down
45 changes: 0 additions & 45 deletions src/encode/encode_shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -959,51 +959,6 @@ force_inline f64 f64_from_raw(u64 u) {
return f;
}

/*==============================================================================
* 128-bit Integer Utils
* These functions are used by the floating-point number reader and writer.
*============================================================================*/

/** Multiplies two 64-bit unsigned integers (a * b),
returns the 128-bit result as 'hi' and 'lo'. */
force_inline void u128_mul(u64 a, u64 b, u64 *hi, u64 *lo) {
#if YYJSON_HAS_INT128
u128 m = (u128)a * b;
*hi = (u64)(m >> 64);
*lo = (u64)(m);
#elif MSC_HAS_UMUL128
*lo = _umul128(a, b, hi);
#else
u32 a0 = (u32)(a), a1 = (u32)(a >> 32);
u32 b0 = (u32)(b), b1 = (u32)(b >> 32);
u64 p00 = (u64)a0 * b0, p01 = (u64)a0 * b1;
u64 p10 = (u64)a1 * b0, p11 = (u64)a1 * b1;
u64 m0 = p01 + (p00 >> 32);
u32 m00 = (u32)(m0), m01 = (u32)(m0 >> 32);
u64 m1 = p10 + m00;
u32 m10 = (u32)(m1), m11 = (u32)(m1 >> 32);
*hi = p11 + m01 + m11;
*lo = ((u64)m10 << 32) | (u32)p00;
#endif
}

/** Multiplies two 64-bit unsigned integers and add a value (a * b + c),
returns the 128-bit result as 'hi' and 'lo'. */
force_inline void u128_mul_add(u64 a, u64 b, u64 c, u64 *hi, u64 *lo) {
#if YYJSON_HAS_INT128
u128 m = (u128)a * b + c;
*hi = (u64)(m >> 64);
*lo = (u64)(m);
#else
u64 h, l, t;
u128_mul(a, b, &h, &l);
t = l + c;
h += (u64)(((t < l) | (t < c)));
*hi = h;
*lo = t;
#endif
}

/**
Get the cached pow10 value from pow10_sig_table.
@param exp10 The exponent of pow(10, e). This value must in range
Expand Down
47 changes: 47 additions & 0 deletions src/pyyjson.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,53 @@ static_assert(false, "false");
} \
} while (0)


/*==============================================================================
* 128-bit Integer Utils
* These functions are used by the floating-point number reader and writer.
*============================================================================*/

/** Multiplies two 64-bit unsigned integers (a * b),
returns the 128-bit result as 'hi' and 'lo'. */
force_inline void u128_mul(u64 a, u64 b, u64 *hi, u64 *lo) {
#if PYYJSON_HAS_INT128
u128 m = (u128)a * b;
*hi = (u64)(m >> 64);
*lo = (u64)(m);
#elif MSC_HAS_UMUL128
*lo = _umul128(a, b, hi);
#else
u32 a0 = (u32)(a), a1 = (u32)(a >> 32);
u32 b0 = (u32)(b), b1 = (u32)(b >> 32);
u64 p00 = (u64)a0 * b0, p01 = (u64)a0 * b1;
u64 p10 = (u64)a1 * b0, p11 = (u64)a1 * b1;
u64 m0 = p01 + (p00 >> 32);
u32 m00 = (u32)(m0), m01 = (u32)(m0 >> 32);
u64 m1 = p10 + m00;
u32 m10 = (u32)(m1), m11 = (u32)(m1 >> 32);
*hi = p11 + m01 + m11;
*lo = ((u64)m10 << 32) | (u32)p00;
#endif
}

/** Multiplies two 64-bit unsigned integers and add a value (a * b + c),
returns the 128-bit result as 'hi' and 'lo'. */
force_inline void u128_mul_add(u64 a, u64 b, u64 c, u64 *hi, u64 *lo) {
#if PYYJSON_HAS_INT128
u128 m = (u128)a * b + c;
*hi = (u64)(m >> 64);
*lo = (u64)(m);
#else
u64 h, l, t;
u128_mul(a, b, &h, &l);
t = l + c;
h += (u64)(((t < l) | (t < c)));
*hi = h;
*lo = t;
#endif
}


/*==============================================================================
* Digit Character Matcher
*============================================================================*/
Expand Down

0 comments on commit c6b478d

Please sign in to comment.