Lamdonn fa4bd85f2e 1. Add math category folder
2. Add the floatl initial version
3. Add the intl_print function
4. Fix intl_from issue
5. Add intl unit test code
2025-03-04 11:15:49 +08:00

114 lines
4.1 KiB
C

/*********************************************************************************************************
* ------------------------------------------------------------------------------------------------------
* file description
* ------------------------------------------------------------------------------------------------------
* \file intl.h
* \unit intl
* \brief This is a simple large inter number calculate module for C language
* \author Lamdonn
* \version v1.1.0
* \license GPL-2.0
* \copyright Copyright (C) 2023 Lamdonn.
********************************************************************************************************/
#ifndef __intl_H
#define __intl_H
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include "intl_cfg.h"
/* Version infomation */
#define INTL_V_MAJOR 1
#define INTL_V_MINOR 1
#define INTL_V_PATCH 0
/* Universal constant */
#define INTL_MAX __INTL_MAX__
#define INTL_MIN __INTL_MIN__
#define INTL_ZERO __INTL_ZERO__
/* Print the max buffer size, no width is specified in `intl_print` format */
#define INTL_PRINT_MAX __INTL_P_MAX_BIN__
/* Error type */
#define INTL_E_DIV_0 0 // Division by zero
#define INTL_E_INVALID_CAHRACTER 1 // Invalid character in input string
/**
* \brief A structure to represent a long bits integer.
*
* This structure uses a union to store the long bits integer in two different ways:
* - An array of `__INTL_U16_PARTS__` uint16_t values, allowing for operations on individual 16-bit segments.
* - An array of `__INTL_U32_PARTS__` uint32_t values, providing a way to work with 32-bit segments.
*
* The union allows for flexibility in how the data is accessed and manipulated,
* depending on the needs of the operations being performed.
*/
typedef struct {
union {
uint16_t u16[__INTL_U16_PARTS__]; ///< Array of uint16_t values representing the long bit integer in 16-bit segments.
uint32_t u32[__INTL_U32_PARTS__]; ///< Array of uint32_t values representing the long bit integer in 32-bit segments.
};
} intl;
/**
* \brief intl large integer api declaration, support for basic addition, subtraction, multiplication, division, etc.
*/
intl intl_add(intl a, intl b);
intl intl_sub(intl a, intl b);
intl intl_mul(intl a, intl b);
intl intl_div(intl a, intl b);
intl intl_mod(intl a, intl b);
intl intl_and(intl a, intl b);
intl intl_xor(intl a, intl b);
intl intl_or(intl a, intl b);
intl intl_shl(intl a, uint32_t b);
intl intl_shr(intl a, uint32_t b);
intl intl_not(intl a);
intl intl_abs(intl a);
intl intl_inc(intl a);
intl intl_dec(intl a);
intl intl_neg(intl a);
intl intl_from(const char *str);
intl intl_from2(int value);
int intl_print(intl a, char *buffer, uint32_t size, const char *format);
int intl_lt(intl a, intl b);
int intl_le(intl a, intl b);
int intl_eq(intl a, intl b);
int intl_ne(intl a, intl b);
int intl_gt(intl a, intl b);
int intl_ge(intl a, intl b);
int intl_sign(intl a);
/**
* \brief Converts a integer to an intl number.
* \param[in] value: The integer to convert.
* \return The corresponding intl number initialized with the given value.
*/
#define intl(value) intl_from2(value)
/**
* \brief Converts an intl number to string.
* \param[in] a: The intl number to convert.
* \param[out] buffer: The buffer to store the resulting string.
* It should be large enough to hold the representation.
* \param[in] size: The size of buffer, can refer to `INTL_PRINT_MAX`.
* \param[in] format: Printf-like format, [flags][width][type].
* flags: '0' '-' '+' ' ' '#'
* width: dec numeber
* type: 'x' 'X' 'o' 'O' 'b' 'B' 'd' 'i' 'u'
* \return String converted or "invalid" fail.
*/
#define intl_show(a, b, s, f) (intl_print((a), (b), (s), (f)) > 0 ? (b) : "invalid")
#endif