00001
00013 #ifndef C4E_ALGO32_H
00014 #define C4E_ALGO32_H
00015
00016
00017
00018
00019 #include "c4e_sys.h"
00020
00021
00022 #ifdef __cplusplus
00023 extern "C" {
00024 #endif
00025
00026
00027
00028
00029
00036 #define C4E_ALGO32_DIGIT_SIZE sizeof(C4eAlgo32Digit)
00037
00038
00039
00056 #if (C4E_ARCH_DIGIT_SIZE == 4U)
00057 #define C4E_ALGO32_DIGITS_PTR(pvar32) (pvar32)
00058 #elif (C4E_ARCH_DIGIT_SIZE < 4U)
00059 #define C4E_ALGO32_DIGITS_PTR(pvar32) ((pvar32)->digits)
00060
00069 #define C4E_ALGO32_NUM_DIGITS (4U / C4E_ARCH_DIGIT_SIZE)
00070
00071 #endif
00072
00073
00074
00075
00076
00077 #if (C4E_ARCH_DIGIT_SIZE == 4U)
00078
00082 typedef C4eArchDigit C4eAlgo32Uint;
00084 #elif (C4E_ARCH_DIGIT_SIZE < 4U)
00085
00089 typedef struct
00090 {
00091 C4eArchDigit digits[C4E_ALGO32_NUM_DIGITS];
00092 } C4eAlgo32Uint;
00093
00094 #elif defined(C4E_HAVE_UINT32)
00095
00103 typedef C4eArchUint32 C4eAlgo32Uint;
00104
00105 #else
00106 #error "Cannot determine a 32-bit unsigned integer datatype."
00107 #endif
00108
00109
00117 #ifdef C4E_ALGO32_DIGITS_PTR
00118 typedef C4eAlgo32Uint C4eAlgo32Digit;
00119 #else
00120 #error "Currently there is no support for a 64-bit type of C4eArchDigit."
00121 typedef C4eArchDigit C4eAlgo32Digit;
00122 #endif
00123
00124
00125
00126
00127
00128
00129
00130
00142
00153 #ifdef C4E_ALGO32_DIGITS_PTR
00154 #define C4E_ALGO32_CAST_DIGITS(p) C4E_ALGO32_DIGITS_PTR(p)
00155 #define C4E_ALGO32_CAST_UINT(p) (p)
00156 #else
00157 #define C4E_ALGO32_CAST_DIGITS(p) (p)
00158 #define C4E_ALGO32_CAST_UINT(p) C4E_SYS_CAST(C4eAlgo32Uint*, (p))
00159 #endif
00160
00161
00162
00172 #define C4E_ALGO32_DIGITS_NUM(size) C4E_SYS_DIV_CEIL((size), C4E_ALGO32_DIGIT_SIZE)
00173
00174
00175
00176 #ifndef C4E_ALGO32_NUM_DIGITS
00177
00178
00191 #define C4E_ALGO32_INIT(msb, midh, midl, lsb) \
00192 C4E_SYS_CAST(C4eAlgo32Uint, \
00193 (C4E_SYS_CAST(C4eAlgo32Uint, (msb)) << 24U) | \
00194 (C4E_SYS_CAST(C4eAlgo32Uint, (midh)) << 16U) | \
00195 (C4E_SYS_CAST(C4eAlgo32Uint, (midl)) << 8U) | \
00196 C4E_SYS_CAST(C4eAlgo32Uint, (lsb)))
00197
00198
00199
00213 #define C4E_ALGO32_ASGN(var32, msb, midh, midl, lsb) \
00214 ((var32) = C4E_ALGO32_INIT((msb), (midh), (midl), (lsb)))
00215
00216
00217
00218
00230 #define C4E_ALGO32_BYTE(var32, pos) \
00231 C4E_SYS_CAST(C4eArchUint8, ((var32) >> ((pos) * 8U)) & C4E_SYS_DIGIT_LSBYTE)
00232
00233
00234
00244 #define C4E_ALGO32_INC(var32) (++(var32))
00245
00246
00247
00248
00259 #define C4E_ALGO32_ADD(var32a, var32b) ((var32a) + (var32b))
00260
00261
00262
00274 #define C4E_ALGO32_AND(var32a, var32b) ((var32a) & (var32b))
00275
00276
00277
00289 #define C4E_ALGO32_OR(var32a, var32b) ((var32a) | (var32b))
00290
00291
00292
00303 #define C4E_ALGO32_NOT(var32) C4E_SYS_CAST(C4eAlgo32Uint, ~(var32))
00304
00305
00306
00318 #define C4E_ALGO32_XOR(var32a, var32b) ((var32a) ^ (var32b))
00319
00320
00321
00335 #define C4E_ALGO32_ROL(var32, n) \
00336 C4E_SYS_CAST(C4eAlgo32Uint, ((var32) << (n)) | ((var32) >> (32U - (n))))
00337
00338
00339
00353 #define C4E_ALGO32_ROR(var32, n) \
00354 C4E_SYS_CAST(C4eAlgo32Uint, ((var32) >> (n)) | ((var32) << (32U - (n))))
00355
00356
00357
00376 #define C4E_ALGO32_SHL(var32, n, lsb) \
00377 C4E_SYS_CAST(C4eAlgo32Uint, ((var32) << (n)) | (lsb))
00378
00379
00380
00394 #define C4E_ALGO32_SHR(var32, n) \
00395 C4E_SYS_CAST(C4eAlgo32Uint, (var32) >> (n))
00396
00397
00398
00399 #else
00400
00401 #if (C4E_ARCH_DIGIT_SIZE == 2U)
00402
00403 #define C4E_ALGO32_INIT(msb, midh, midl, lsb) \
00404 { \
00405 .digits = \
00406 { \
00407 C4E_SYS_CAST(C4eArchDigit, \
00408 (C4E_SYS_CAST(C4eArchDigit, (midl)) << 8U) | \
00409 C4E_SYS_CAST(C4eArchDigit, (lsb))), \
00410 C4E_SYS_CAST(C4eArchDigit, \
00411 (C4E_SYS_CAST(C4eArchDigit, (msb)) << 8U) | \
00412 C4E_SYS_CAST(C4eArchDigit, (midh))) \
00413 } \
00414 }
00415
00416
00417 #define C4E_ALGO32_ASGN(var32, msb, midh, midl, lsb) \
00418 do \
00419 { \
00420 (var32).digits[0] = C4E_SYS_CAST(C4eArchDigit, (midl) << 8U) | \
00421 C4E_SYS_CAST(C4eArchDigit, (lsb)); \
00422 (var32).digits[1] = C4E_SYS_CAST(C4eArchDigit, (msb) << 8U) | \
00423 C4E_SYS_CAST(C4eArchDigit, (midh)); \
00424 } \
00425 while (C4E_FALSE)
00426
00427
00428 #define C4E_ALGO32_BYTE(var32, pos) \
00429 C4E_SYS_CAST(C4eArchUint8, (((var32).digits[(pos) >> 1U] >> \
00430 (((pos) & 1U) * 8U)) & C4E_SYS_DIGIT_LSBYTE))
00431
00432 #elif (C4E_ARCH_DIGIT_SIZE == 1U)
00433
00434 #define C4E_ALGO32_INIT(msb, midh, midl, lsb) \
00435 { \
00436 .digits = \
00437 { \
00438 C4E_SYS_CAST(C4eArchDigit, (lsb)), \
00439 C4E_SYS_CAST(C4eArchDigit, (midl)), \
00440 C4E_SYS_CAST(C4eArchDigit, (midh)), \
00441 C4E_SYS_CAST(C4eArchDigit, (msb)) \
00442 } \
00443 }
00444
00445
00446 #define C4E_ALGO32_ASGN(var32, msb, midh, midl, lsb) \
00447 do \
00448 { \
00449 (var32).digits[0] = C4E_SYS_CAST(C4eArchDigit, (lsb)); \
00450 (var32).digits[1] = C4E_SYS_CAST(C4eArchDigit, (midl)); \
00451 (var32).digits[2] = C4E_SYS_CAST(C4eArchDigit, (midh)); \
00452 (var32).digits[3] = C4E_SYS_CAST(C4eArchDigit, (msb)); \
00453 } \
00454 while (C4E_FALSE)
00455
00456
00457 #define C4E_ALGO32_BYTE(var32, pos) \
00458 C4E_SYS_CAST(C4eArchUint8, (var32).digits[(pos)] & C4E_SYS_DIGIT_LSBYTE)
00459
00460 #else
00461 #error
00462 #endif
00463
00464
00465 #define C4E_ALGO32_INC(var32) c4e_algo32_inc(&(var32))
00466 #define C4E_ALGO32_ADD(var32a, var32b) c4e_algo32_add((var32a), (var32b))
00467 #define C4E_ALGO32_AND(var32a, var32b) c4e_algo32_and((var32a), (var32b))
00468 #define C4E_ALGO32_OR(var32a, var32b) c4e_algo32_or((var32a), (var32b))
00469 #define C4E_ALGO32_NOT(var32) c4e_algo32_not(var32)
00470 #define C4E_ALGO32_XOR(var32a, var32b) c4e_algo32_xor((var32a), (var32b))
00471 #define C4E_ALGO32_ROL(var32, n) c4e_algo32_rol((var32), (n))
00472 #define C4E_ALGO32_ROR(var32, n) c4e_algo32_ror((var32), (n))
00473 #define C4E_ALGO32_SHL(var32, n, lsb) c4e_algo32_shl((var32), (n), (lsb))
00474 #define C4E_ALGO32_SHR(var32, n) c4e_algo32_shr((var32), (n))
00475
00476
00477 #endif
00478
00479
00480
00481
00482
00483
00484 #ifdef C4E_ALGO32_NUM_DIGITS
00485
00486
00494 C4eAlgo32Uint c4e_algo32_not(C4eAlgo32Uint var32);
00495
00496
00497
00507 C4eAlgo32Uint c4e_algo32_xor(C4eAlgo32Uint a, C4eAlgo32Uint b);
00508
00509
00510
00519 C4eAlgo32Uint c4e_algo32_or(C4eAlgo32Uint a, C4eAlgo32Uint b);
00520
00521
00522
00531 C4eAlgo32Uint c4e_algo32_and(C4eAlgo32Uint a, C4eAlgo32Uint b);
00532
00533
00534
00541 void c4e_algo32_inc(C4eAlgo32Uint* var32);
00542
00543
00544
00545
00554 C4eAlgo32Uint c4e_algo32_add(C4eAlgo32Uint a, C4eAlgo32Uint b);
00555
00556
00557
00571 C4eAlgo32Uint c4e_algo32_shl(C4eAlgo32Uint var32, C4eArchUint shift,
00572 C4eArchUint8 lsb);
00573
00574
00575
00584 C4eAlgo32Uint c4e_algo32_shr(C4eAlgo32Uint var32, C4eArchUint shift);
00585
00586
00587
00596 C4eAlgo32Uint c4e_algo32_rol(C4eAlgo32Uint var32, C4eArchUint shift);
00597
00598
00599
00608 C4eAlgo32Uint c4e_algo32_ror(C4eAlgo32Uint var32, C4eArchUint shift);
00609
00610
00611
00612 #endif
00613
00614
00615
00616 #ifdef __cplusplus
00617 }
00618 #endif
00619
00620
00621 #endif
00622
00623
00624
00625
00626