📄 u2long.c
字号:
/* * $Log: u2long.c,v $ * Revision 1.1 2000/05/03 14:30:04 bjc97r * Initial revision * */char *_u2long_id = "$Id: u2long.c,v 1.1 2000/05/03 14:30:04 bjc97r Exp $";#include <stdio.h>#include <stdlib.h>#include <string.h>#include "u2long.h"/* right shift */u2long u2long_rshift( u2long *src, unsigned n ){ unsigned long temp; unsigned long mask; unsigned long pos; unsigned i; if ( n >= sizeof(unsigned long)*8 ) { src->l = src->h; src->h = 0; n -= sizeof(unsigned long)*8; src->l >>= n; } else { src->l >>= n; for( mask=0, pos=1, i=0; i < n; pos <<=1, i++ ) mask |= pos; temp = mask & src->h; src->h >>= n; temp <<= sizeof(unsigned long)*8 - n; src->l |= temp; } return *src;}/* left shift */u2long u2long_lshift( u2long *src, unsigned n ){ unsigned long temp; unsigned long mask; unsigned long pos; unsigned i; if ( n >= sizeof(unsigned long)*8 ) { src->h = src->l; src->l = 0; n -= sizeof(unsigned long)*8; src->h <<= n; } else { src->h <<= n; pos = 1 << (sizeof(unsigned long) * 8 - 1); for( mask=0, i=0; i < n; pos >>= 1, i++ ) mask |= pos; temp = mask & src->l; src->l <<= n; temp >>= sizeof(unsigned long)*8 - n; src->h |= temp; } return *src;}/* increment by 1 */u2long u2long_inc( u2long *src ){ if ( src->l == 0xffffffff ) src->h ++; src->l ++; return *src;}/* decrement by 1 */u2long u2long_dec( u2long *src ){ if ( !src->l ) src->h --; src->l --; return *src;}/* * compare: return either negative, zero or positive integer * based on "src1 - src2". */int u2long_cmp( u2long src1, u2long src2 ){ int ans; if ( src1.h < src2.h ) ans = -1; else if ( src1.h > src2.h ) ans = +1; else if ( src1.l < src2.l ) ans = -1; else if ( src1.l > src2.l ) ans = +1; else ans = 0; return ans;}/* * strtou2long() converts a string representation of a primitive polynomial * into two unsigned long intergers. The max degree of the polynomails * this function can handle is 63. The string should be an octal representaion * of the polynomial without leading zero. */u2long strtou2long( char *P ){ u2long poly; size_t len; while(*P == '0') P++; /* Remove possible leading zeros. */ len = strlen( P ); /* Convert the string P into two unsigned long. ------------------ */ if ( (len < 11) || ( (len == 11) && (P[0] < '4')) ) { /* deg < 32 */ poly.h = 0; poly.l = strtoul( P, NULL, 8 ); } else if ( len == 11 ) { /* deg == 32 */ char str0[12]; strncpy( str0, P, 12 ); str0[0] -= '4'; /* Make the string a valid 32bit unsigned long. */ poly.h = 1; poly.l = strtoul( str0, NULL, 8 ); } else { /* 32 < deg < 64 */ unsigned int offset; /* string starting offset for lower 32 bits */ char substr[12]; offset = len - 11; strncpy( substr, P, offset ); substr[offset] = 0; poly.h = strtoul( substr, NULL, 8 ) << 1; strncpy( substr, P+offset, 12 ); if ( substr[0] < '4' ) poly.l = strtoul( substr, NULL, 8 ); else { poly.h |= 1; substr[0] -= '4'; poly.l = strtoul( substr, NULL, 8 ); } } return poly;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -