@@ -1315,12 +1315,6 @@ static inline void *term_intn_data(term t)
13151315 return (void * ) (boxed_value + 1 );
13161316}
13171317
1318- static inline size_t term_intn_size (term t )
1319- {
1320- const term * boxed_value = term_to_const_term_ptr (t );
1321- return term_get_size_from_boxed_header (boxed_value [0 ]);
1322- }
1323-
13241318static inline void term_intn_to_term_size (size_t n , size_t * intn_data_size , size_t * rounded_num_len )
13251319{
13261320 size_t bytes = n * sizeof (intn_digit_t );
@@ -1340,6 +1334,85 @@ static inline void term_intn_to_term_size(size_t n, size_t *intn_data_size, size
13401334 * rounded_num_len = rounded / sizeof (intn_digit_t );
13411335}
13421336
1337+ /**
1338+ * @brief Check if term is a multi-precision integer larger than \c int64_t
1339+ *
1340+ * Tests whether a term represents a boxed integer that requires multi-precision
1341+ * representation (i.e., larger than can fit in \c int64_t). These are integers
1342+ * that need more than \c INTN_INT64_LEN digits for their representation.
1343+ *
1344+ * In the current implementation, a bigint is defined as a boxed integer with
1345+ * size greater than:
1346+ * - \c BOXED_TERMS_REQUIRED_FOR_INT64 on 32-bit systems
1347+ * - \c BOXED_TERMS_REQUIRED_FOR_INT on 64-bit systems
1348+ *
1349+ * This effectively identifies integers that cannot be represented in the
1350+ * platform's native integer types and require multi-precision arithmetic,
1351+ * while avoiding confusion with regular boxed \c int64_t values that still
1352+ * fit within standard integer ranges.
1353+ *
1354+ * @param t Term to check
1355+ * @return true if term is a multi-precision integer, false otherwise
1356+ *
1357+ * @note Returns false for integers that fit in \c int64_t, even if boxed
1358+ * @note This is the correct check before calling \c term_to_bigint()
1359+ *
1360+ * @see term_to_bigint() to extract the multi-precision integer data
1361+ * @see term_is_boxed_integer() for checking any boxed integer
1362+ * @see term_is_any_integer() for checking all integer representations
1363+ */
1364+ static inline bool term_is_bigint (term t )
1365+ {
1366+ return term_is_boxed_integer (t )
1367+ && (term_boxed_size (t ) > (INTN_INT64_LEN * sizeof (intn_digit_t )) / sizeof (term ));
1368+ }
1369+
1370+ // intn doesn't depend on term
1371+ _Static_assert (
1372+ (int ) TermPositiveInteger == (int ) IntNPositiveInteger , "term/intn definition mismatch" );
1373+ _Static_assert (
1374+ (int ) TermNegativeInteger == (int ) IntNNegativeInteger , "term/intn definition mismatch" );
1375+
1376+ /**
1377+ * @brief Extract multi-precision integer data from boxed term
1378+ *
1379+ * Extracts the raw multi-precision integer representation from a boxed
1380+ * integer term. This function provides direct access to the internal
1381+ * digit array without copying, returning a pointer to the data within
1382+ * the term structure.
1383+ *
1384+ * @param t Boxed integer term to extract from
1385+ * @param[out] bigint Pointer to the digit array within the term (borrowed reference)
1386+ * @param[out] bigint_len Number of digits in the integer
1387+ * @param[out] bigint_sign Sign of the integer
1388+ *
1389+ * @pre \c term_is_bigint(t) must be true
1390+ * @pre bigint != NULL
1391+ * @pre bigint_len != NULL
1392+ * @pre bigint_sign != NULL
1393+ *
1394+ * @warning Returned pointer is a borrowed reference into the term structure
1395+ * @warning Data becomes invalid if term is garbage collected or modified
1396+ * @warning Caller must not free the returned pointer
1397+ *
1398+ * @note The digit array may not be normalized (may have leading zeros)
1399+ * @note Length is calculated as boxed_size * (sizeof(term) / sizeof(intn_digit_t))
1400+ *
1401+ * @see term_is_bigint() to check if term is a multi-precision integer
1402+ * @see term_boxed_integer_sign() to get the sign
1403+ */
1404+ static inline void term_to_bigint (
1405+ term t , const intn_digit_t * bigint [], size_t * bigint_len , intn_integer_sign_t * bigint_sign )
1406+ {
1407+ * bigint = (const intn_digit_t * ) term_intn_data (t );
1408+
1409+ const term * boxed_value = term_to_const_term_ptr (t );
1410+ size_t boxed_size = term_get_size_from_boxed_header (boxed_value [0 ]);
1411+ * bigint_len = boxed_size * (sizeof (term ) / sizeof (intn_digit_t ));
1412+
1413+ * bigint_sign = (intn_integer_sign_t ) term_boxed_integer_sign (t );
1414+ }
1415+
13431416static inline term term_from_catch_label (unsigned int module_index , unsigned int label )
13441417{
13451418 return (term ) ((module_index << 24 ) | (label << 6 ) | TERM_IMMED2_CATCH );
0 commit comments