Thread: Question regarding bitmask

  1. #1
    Registered User
    Join Date
    Oct 2018
    Posts
    1

    Question regarding bitmask

    I have this AVX code - and it prints the value of the bitmask as 2 which is correct i.e. the second element of the array (counted from zero) is greater than the value of 509. Is there a way to get all elements of array that are greater than 509 with AVX intrinsics ? In this case two elements i.e. 510 and 515 are greater than 509 but the way I have implemented my test case only the first one is available. Is there a way to get the second element as well ?
    Code:
    #include <stdint.h>
    #include <immintrin.h>
    #include <assert.h>
    #include <limits.h>
    #include <math.h>
    #include <stdalign.h>
    
    main()
    {
      __m256d avx_creg, res, avx_sreg;
      int bitmask;
      uint64_t key = 509;
    
      avx_sreg = _mm256_castsi256_pd(_mm256_set1_epi64x(key));
      alignas(32) uint64_t v[4]; 
      _mm256_store_pd((double*)v, avx_sreg);
      printf("v2_u64: %lld %lld %lld %lld\n", v[0], v[1],v[2],v[3]);
      uint64_t b[4]= {500,505,510,515};
      avx_creg = _mm256_castsi256_pd(
                     _mm256_loadu_si256((__m256i const *)&b));
        //
      alignas(32) uint64_t v1[4]; 
      _mm256_store_pd((double*)v1, avx_creg);
      printf("v2_u64: %lld %lld %lld %lld\n", v1[0], v1[1],v1[2],v1[3]);
      //
      res      = _mm256_cmp_pd(avx_sreg, avx_creg, 30);
      bitmask  = _mm256_movemask_pd(res);
      int mmask = __builtin_popcount(bitmask);
      printf("mmask is %d\n",mmask);
    }
    Last edited by gansub; 10-07-2018 at 04:52 AM.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    834
    I'm assuming that the 2 bits that are set in bitmask represent either the two values that are greater than 509 or possibly the two values that are less than 509. You should run it again with a key of 503. What's the answer?

    So assuming the 2 bits that are set in bitmask are in positions that correspond to the indices of the elements that are greater(/less?) than the key, you can extract the values by looking at the bits. Maybe something like:
    Code:
    if (bitmask & 1) printf("%ull\n", b[0]);
    if (bitmask & 2) printf("%ull\n", b[1]);
    if (bitmask & 4) printf("%ull\n", b[2]);
    if (bitmask & 8) printf("%ull\n", b[3]);
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,662
    > and it prints the value of the bitmask as 2 which is correct
    So why are you printing the population count of the bitmask?

    Code:
      bitmask  = _mm256_movemask_pd(res);
      int mmask = __builtin_popcount(bitmask);
      printf("mmask is %d\n",mmask);
    To me, 2 means there are 2 bits set in bitmask, not that the first value matching your condition is at index 2 (or anything else).

    Try john.c's suggestion.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitmask (example from The Audio Programming Book pg 22)
    By Fauveboy in forum C Programming
    Replies: 2
    Last Post: 01-02-2018, 11:10 PM
  2. Upper or Lower...Bitmask and bitwise operators
    By orion314 in forum C Programming
    Replies: 3
    Last Post: 09-16-2014, 06:14 AM
  3. Replies: 1
    Last Post: 03-23-2011, 09:00 AM
  4. bitmask
    By zu1u in forum C Programming
    Replies: 6
    Last Post: 09-06-2010, 02:35 PM
  5. bitmask
    By potbelle in forum C Programming
    Replies: 3
    Last Post: 03-31-2002, 08:09 AM

Tags for this Thread