คณิตศาสตร์จำนวนเต็มที่ไม่ได้ลงนามแบบพกพาขนาดเล็กใน C สำหรับการคำนวณด้วยจำนวนมาก
ใช้อาร์เรย์ของ uint8_t , uint16_t หรือ uint32_t เป็นประเภทข้อมูลที่ใช้บิตทั้งหมดในแต่ละคำ
หมายเลขฐานคือ 0x100, 0x10000 หรือ 0x100000000 ขึ้นอยู่กับขนาดคำที่เลือก-ดูไฟล์ส่วนหัว BN.H สำหรับการชี้แจง
ไม่มีการใช้การจัดการหน่วยความจำแบบไดนามิกและ stdio.h ใช้สำหรับการทดสอบฟังก์ชั่นการแยกวิเคราะห์และจาก hex-strings เท่านั้น
เลขคณิตพื้นฐาน (+, -, *, /, /, %) และการดำเนินการ bitwise (&, |, ^. <<, >>) บวกเพิ่มขึ้นการลดลงและการเปรียบเทียบได้รับการสนับสนุน
เป้าหมายการออกแบบหลักของห้องสมุดนี้คือขนาดเล็กถูกต้องถูกต้องอยู่ในตัวเองและใช้ทรัพยากรไม่กี่อย่างในขณะที่ยังคงรักษาประสิทธิภาพและคุณสมบัติที่สมบูรณ์ ความชัดเจนของรหัสยังมีมูลค่าสูงเช่นกัน
malloc / free ) นี่คือโครงสร้างข้อมูลที่ใช้โดยที่ dtype คือ #define 'd ถึง uint8_t , uint16_t หรือ uint32_t
struct bn
{
DTYPE array [ BN_ARRAY_SIZE ];
};นี่คือ API สาธารณะ / ส่งออก:
/* Initialization functions: */
void bignum_init ( struct bn * n ); /* n gets zero-initialized */
void bignum_from_int ( struct bn * n , DTYPE_TMP i );
int bignum_to_int ( struct bn * n );
/* NOTE: The functions below are meant for testing mainly and expects input in hex-format and of a certain length */
/* See the implementation for details or the test-files for examples of how to use them. */
void bignum_from_string ( struct bn * n , char * str , int nbytes );
void bignum_to_string ( struct bn * n , char * str , int maxsize );
/* Basic arithmetic operations: */
void bignum_add ( struct bn * a , struct bn * b , struct bn * c ); /* c = a + b */
void bignum_sub ( struct bn * a , struct bn * b , struct bn * c ); /* c = a - b */
void bignum_mul ( struct bn * a , struct bn * b , struct bn * c ); /* c = a * b */
void bignum_div ( struct bn * a , struct bn * b , struct bn * c ); /* c = a / b */
void bignum_mod ( struct bn * a , struct bn * b , struct bn * c ); /* c = a % b */
void bignum_divmod ( struct bn * a , struct bn * b , struct bn * c , struct bn * d ); /* c = a/b, d = a%b */
/* Bitwise operations: */
void bignum_and ( struct bn * a , struct bn * b , struct bn * c ); /* c = a & b */
void bignum_or ( struct bn * a , struct bn * b , struct bn * c ); /* c = a | b */
void bignum_xor ( struct bn * a , struct bn * b , struct bn * c ); /* c = a ^ b */
void bignum_lshift ( struct bn * a , struct bn * b , int nbits ); /* b = a << nbits */
void bignum_rshift ( struct bn * a , struct bn * b , int nbits ); /* b = a >> nbits */
/* Special operators and comparison */
int bignum_cmp ( struct bn * a , struct bn * b ); /* Compare: returns LARGER, EQUAL or SMALLER */
int bignum_is_zero ( struct bn * n ); /* For comparison with zero */
void bignum_inc ( struct bn * n ); /* Increment: add one to n */
void bignum_dec ( struct bn * n ); /* Decrement: subtract one from n */
void bignum_pow ( struct bn * a , struct bn * b , struct bn * c ); /* Calculate a^b -- e.g. 2^10 => 1024 */
void bignum_isqrt ( struct bn * a , struct bn * b ); /* Integer square root -- e.g. isqrt(5) => 2 */
void bignum_assign ( struct bn * dst , struct bn * src ); /* Copy src into dst -- dst := src */ ตั้งค่า BN_ARRAY_SIZE ใน bn.h เพื่อกำหนดขนาดของตัวเลขที่คุณต้องการใช้ ตัวเลือกเริ่มต้นคือหมายเลข 1024 บิต ตั้ง WORD_SIZE เป็น {1,2,4} เพื่อใช้ uint8_t , uint16_t หรือ uint32_t เป็นโครงสร้างข้อมูลพื้นฐาน
เรียกใช้ make clean all test สำหรับตัวอย่างการใช้งานและสำหรับการทดสอบแบบสุ่ม
ดู tests/factorial.c สำหรับตัวอย่างของวิธีการคำนวณแฟคทอเรียล (100) หรือ 100! (หมายเลข 150+ หลัก)
ถาม: อะไรคือความแตกต่างของไลบรารีนี้จากการใช้งานจำนวนเต็มขนาดใหญ่ C อื่น ๆ ?
A: ขนาดเล็กสำหรับหนึ่ง ~ 500 บรรทัดของ Code ที่รวบรวมเป็น ROM 2-3KB โดยใช้ RAM เพียงเล็กน้อยเท่านั้น การใช้บิตทั้งหมดโดยใช้ฐานตัวเลข 2^{8,16,32} แทน 10 ซึ่งเป็นตัวเลือกปกติ
ถาม: ทำไมไม่สนับสนุนขนาดคำ 64 บิต?
ตอบ: การคำนวณทั้งหมดจะทำในตัวแปรชั่วคราวซึ่งต้องใหญ่กว่าขนาดคำ (เพื่อตรวจจับล้น ฯลฯ ) ดังนั้นขนาดคำ 64 บิตจะต้องใช้เช่นอุณหภูมิ 128 บิต C99 รองรับจำนวนเต็มแบบพกพาสูงสุด 64 บิต
วัสดุทั้งหมดในที่เก็บนี้อยู่ในโดเมนสาธารณะ