Changeset 87 for trunk/lib/lib_simd.h


Ignore:
Timestamp:
Apr 2, 2008, 3:44:45 PM (12 years ago)
Author:
cameron
Message:

Adaptations for MSVC

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/lib_simd.h

    r75 r87  
    2424
    2525/* Useful definitions from Linux kernel*/
     26#ifdef __GNUC__
    2627#define likely(x) __builtin_expect((x),1)
    2728#define unlikely(x) __builtin_expect((x),0)
     29#endif
     30#ifdef _MSC_VER
     31#define likely(x) (x)
     32#define unlikely(x) (x)
     33#endif
    2834
    2935/* Shift forward and back operations, based on endianness */
     
    4450#define sb_op(x, n) ((x)>>(n))
    4551#define sf_op(x, n) ((x)<<(n))
     52#ifdef __GNUC__
    4653#define cfzl __builtin_ctzl
    4754#endif
     55#ifdef _MSC_VER
     56#include <intrin.h>
     57#pragma intrinsic(_BitScanForward)
     58//  precondition: x > 0
     59static inline unsigned long cfzl(unsigned long x) {
     60        unsigned long zeroes;
     61        _BitScanForward(&zeroes, x);
     62        return zeroes;
     63}
     64#endif
     65#endif
     66
     67
     68static inline int count_forward_zeroes(SIMD_type bits) {
     69  union {SIMD_type vec; unsigned long elems[sizeof(SIMD_type)/sizeof(long)];} v;
     70  v.vec = bits;
     71  if (v.elems[0] != 0) return cfzl(v.elems[0]);
     72  else if (v.elems[1] != 0) return LONG_BIT + cfzl(v.elems[1]);
     73#if LONG_BIT < 64
     74  else if (v.elems[2] != 0) return 2*LONG_BIT + cfzl(v.elems[2]);
     75  else if (v.elems[3] != 0) return 3*LONG_BIT + cfzl(v.elems[3]);
     76#endif
     77  else return 8*sizeof(SIMD_type);
     78}
     79
    4880
    4981/* Scans for a 1 as long as it takes.  Use a sentinel to fence.
     
    87119        return new SIMD_type [SIMD_packs];
    88120#endif
    89 #ifndef __APPLE__
     121#ifdef _MSC_VER
     122        SIMD_type * v = (SIMD_type*)_aligned_malloc(sizeof(SIMD_type) * SIMD_packs, sizeof(SIMD_type));
     123        if (v != 0) return v;
     124        else {
     125                printf("Failed to allocated new array of %i SIMD packs.\n", SIMD_packs);
     126                exit(-1);
     127        }
     128#endif
     129#if !defined(__APPLE__) && !defined(_MSC_VER)
    90130        SIMD_type * v;
    91131        int rslt = posix_memalign((void **) &v,
     
    94134        if (rslt == 0) return v;
    95135        else {
    96                 printf("Failed to allocated new array of %i SIMD packs.\n");
     136                printf("Failed to allocated new array of %i SIMD packs.\n", SIMD_packs);
    97137                exit(-1);
    98138        }
Note: See TracChangeset for help on using the changeset viewer.