36 #include <m4ri/m4ri_config.h>
38 #if __M4RI_USE_MM_MALLOC
39 #include <mm_malloc.h>
45 #define __STDC_LIMIT_MACROS
78 #include <m4ri/wordwrapper.h>
97 #define __M4RI_CONVERT_TO_INT(w) ((int)(w))
109 #define __M4RI_CONVERT_TO_BIT(w) ((BIT)(w))
123 #define __M4RI_CONVERT_TO_UINT64_T(w) (w)
133 #define __M4RI_CONVERT_TO_WORD(i) ((word)(i))
163 #define MAX(x,y) (((x) > (y))?(x):(y))
174 #define MIN(x,y) (((x) < (y))?(x):(y))
199 #define __M4RI_TWOPOW(i) ((uint64_t)1 << (i))
208 #define __M4RI_CLR_BIT(w, spot) ((w) &= ~(m4ri_one << (spot))
217 #define __M4RI_SET_BIT(w, spot) ((w) |= (m4ri_one << (spot)))
226 #define __M4RI_GET_BIT(w, spot) __M4RI_CONVERT_TO_BIT(((w) >> (spot)) & m4ri_one)
236 #define __M4RI_WRITE_BIT(w, spot, value) ((w) = (((w) & ~(m4ri_one << (spot))) | (-__M4RI_CONVERT_TO_WORD(value) & (m4ri_one << (spot)))))
245 #define __M4RI_FLIP_BIT(w, spot) ((w) ^= (m4ri_one << (spot)))
271 #define __M4RI_LEFT_BITMASK(n) (m4ri_ffff >> (m4ri_radix - (n)) % m4ri_radix)
296 #define __M4RI_RIGHT_BITMASK(n) (m4ri_ffff << (m4ri_radix - (n)))
313 #define __M4RI_MIDDLE_BITMASK(n, offset) (__M4RI_LEFT_BITMASK(n) << (offset))
322 v = ((v >> 1) & 0x5555555555555555ULL) | ((v & 0x5555555555555555ULL) << 1);
323 v = ((v >> 2) & 0x3333333333333333ULL) | ((v & 0x3333333333333333ULL) << 2);
324 v = ((v >> 4) & 0x0F0F0F0F0F0F0F0FULL) | ((v & 0x0F0F0F0F0F0F0F0FULL) << 4);
325 v = ((v >> 8) & 0x00FF00FF00FF00FFULL) | ((v & 0x00FF00FF00FF00FFULL) << 8);
326 v = ((v >> 16) & 0x0000FFFF0000FFFFULL) | ((v & 0x0000FFFF0000FFFFULL) << 16);
327 v = (v >> 32) | (v << 32);
347 case 15: to |= (from & (
m4ri_one << (Q[15] - base))) >> (Q[15] - 15 - base);
348 case 14: to |= (from & (
m4ri_one << (Q[14] - base))) >> (Q[14] - 14 - base);
349 case 13: to |= (from & (
m4ri_one << (Q[13] - base))) >> (Q[13] - 13 - base);
350 case 12: to |= (from & (
m4ri_one << (Q[12] - base))) >> (Q[12] - 12 - base);
351 case 11: to |= (from & (
m4ri_one << (Q[11] - base))) >> (Q[11] - 11 - base);
352 case 10: to |= (from & (
m4ri_one << (Q[10] - base))) >> (Q[10] - 10 - base);
353 case 9: to |= (from & (
m4ri_one << (Q[ 9] - base))) >> (Q[ 9] - 9 - base);
354 case 8: to |= (from & (
m4ri_one << (Q[ 8] - base))) >> (Q[ 8] - 8 - base);
355 case 7: to |= (from & (
m4ri_one << (Q[ 7] - base))) >> (Q[ 7] - 7 - base);
356 case 6: to |= (from & (
m4ri_one << (Q[ 6] - base))) >> (Q[ 6] - 6 - base);
357 case 5: to |= (from & (
m4ri_one << (Q[ 5] - base))) >> (Q[ 5] - 5 - base);
358 case 4: to |= (from & (
m4ri_one << (Q[ 4] - base))) >> (Q[ 4] - 4 - base);
359 case 3: to |= (from & (
m4ri_one << (Q[ 3] - base))) >> (Q[ 3] - 3 - base);
360 case 2: to |= (from & (
m4ri_one << (Q[ 2] - base))) >> (Q[ 2] - 2 - base);
361 case 1: to |= (from & (
m4ri_one << (Q[ 1] - base))) >> (Q[ 1] - 1 - base);
362 case 0: to |= (from & (
m4ri_one << (Q[ 0] - base))) >> (Q[ 0] - 0 - base);
390 case 15: to |= (from & (
m4ri_one << (15))) << (Q[15]-15-base);
391 case 14: to |= (from & (
m4ri_one << (14))) << (Q[14]-14-base);
392 case 13: to |= (from & (
m4ri_one << (13))) << (Q[13]-13-base);
393 case 12: to |= (from & (
m4ri_one << (12))) << (Q[12]-12-base);
394 case 11: to |= (from & (
m4ri_one << (11))) << (Q[11]-11-base);
395 case 10: to |= (from & (
m4ri_one << (10))) << (Q[10]-10-base);
396 case 9: to |= (from & (
m4ri_one << ( 9))) << (Q[ 9]- 9-base);
397 case 8: to |= (from & (
m4ri_one << ( 8))) << (Q[ 8]- 8-base);
398 case 7: to |= (from & (
m4ri_one << ( 7))) << (Q[ 7]- 7-base);
399 case 6: to |= (from & (
m4ri_one << ( 6))) << (Q[ 6]- 6-base);
400 case 5: to |= (from & (
m4ri_one << ( 5))) << (Q[ 5]- 5-base);
401 case 4: to |= (from & (
m4ri_one << ( 4))) << (Q[ 4]- 4-base);
402 case 3: to |= (from & (
m4ri_one << ( 3))) << (Q[ 3]- 3-base);
403 case 2: to |= (from & (
m4ri_one << ( 2))) << (Q[ 2]- 2-base);
404 case 1: to |= (from & (
m4ri_one << ( 1))) << (Q[ 1]- 1-base);
405 case 0: to |= (from & (
m4ri_one << ( 0))) << (Q[ 0]- 0-base);
421 #define __M4RI_ALIGNMENT(addr, n) (((unsigned long)(addr))%(n))
430 #if defined(__GNUC__) && defined(__GNUC_MINOR__)
431 #define __M4RI_GNUC_PREREQ(maj, min) ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
433 #define __M4RI_GNUC_PREREQ(maj, min) FALSE
437 #if __M4RI_GNUC_PREREQ(3,0) || defined(M4RI_DOXYGEN)
443 #define __M4RI_LIKELY(cond) __builtin_expect ((cond) != 0, 1)
449 #define __M4RI_UNLIKELY(cond) __builtin_expect ((cond) != 0, 0)
452 #define __M4RI_LIKELY(cond) (cond)
453 #define __M4RI_UNLIKELY(cond) (cond)
474 return !(ib ? ((ia - 1) ^ ia) & ib : !ia);
495 void m4ri_die(
const char *errormessage, ...);
507 void m4ri_word_to_str(
char *destination,
word data,
int colon);
516 if (rand() < RAND_MAX/2) {
529 word m4ri_random_word();
540 #if defined(__GNUC__)
541 void __attribute__ ((constructor)) m4ri_init(
void);
543 void m4ri_init(
void);
547 #pragma init(m4ri_init)
557 #if defined(__GNUC__)
558 void __attribute__ ((destructor)) m4ri_fini(
void);
560 void m4ri_fini(
void);
564 #pragma fini(m4ri_fini)
569 #if __M4RI_CPU_L3_CACHE == 0
574 #if __M4RI_CPU_L2_CACHE
575 #define __M4RI_CPU_L3_CACHE __M4RI_CPU_L2_CACHE
577 #define __M4RI_CPU_L3_CACHE 4194304
578 #endif // __M4RI_CPU_L2_CACHE
579 #endif // __M4RI_CPU_L3_CACHE
581 #if __M4RI_CPU_L2_CACHE == 0
586 #define __M4RI_CPU_L2_CACHE 262144
587 #endif // __M4RI_CPU_L2_CACHE
590 #if __M4RI_CPU_L1_CACHE == 0
595 #define __M4RI_CPU_L1_CACHE 16384
596 #endif // __M4RI_CPU_L1_CACHE
611 #if __M4RI_USE_MM_MALLOC
612 newthing = _mm_malloc(count * size, 64);
613 #elif __M4RI_USE_POSIX_MEMALIGN
614 int error = posix_memalign(&newthing, 64, count * size);
615 if (error) newthing = NULL;
617 newthing = calloc(count, size);
620 if (newthing == NULL) {
621 m4ri_die(
"m4ri_mm_calloc: calloc returned NULL\n");
624 #if __M4RI_USE_MM_MALLOC || __M4RI_USE_POSIX_MEMALIGN
625 char *b = (
char*)newthing;
626 memset(b, 0, count * size);
648 #if __M4RI_USE_MM_MALLOC
649 newthing = _mm_malloc(size, alignment);
650 #elif __M4RI_USE_POSIX_MEMALIGN
651 int error = posix_memalign(&newthing, alignment, size);
655 newthing = malloc(size);
658 if (newthing==NULL && (size>0)) {
659 m4ri_die(
"m4ri_mm_malloc: malloc returned NULL\n");
662 else return newthing;
677 #if __M4RI_USE_MM_MALLOC
678 newthing = _mm_malloc(size, 64);
679 #elif __M4RI_USE_POSIX_MEMALIGN
680 int error = posix_memalign(&newthing, 64, size);
681 if (error) newthing = NULL;
683 newthing = malloc(size);
684 #endif //__M4RI_USE_MM_MALLOC
685 if (newthing==NULL && (size>0)) {
686 m4ri_die(
"m4ri_mm_malloc: malloc returned NULL\n");
689 else return newthing;
702 #if __M4RI_USE_MM_MALLOC
713 #if defined (__GNUC__)
714 #define RESTRICT __restrict__
721 #endif // M4RI_MISC_H