Skip to content

Instantly share code, notes, and snippets.

@Napat
Last active March 6, 2018 07:49
Show Gist options
  • Save Napat/94888e75a1b2aa02fa4c8aa9de0152a5 to your computer and use it in GitHub Desktop.
Save Napat/94888e75a1b2aa02fa4c8aa9de0152a5 to your computer and use it in GitHub Desktop.
#include<stdio.h>
//// Begin copy //////////////////////
#ifndef GENMASK
#ifndef U32_C
#define U32_C(x) x ## U
#endif /* U32_C */
/*
* Create a contiguous bitmask starting at bit position @l and ending at
* position @h. For example
* GENMASK_ULL(39, 21) gives us the 64bit vector 0x000000ffffe00000.
*/
#define GENMASK(h, l) (((U32_C(1) << ((h) - (l) + 1)) - 1) << (l))
//#define GENMASK_ULL(h, l) (((U64_C(1) << ((h) - (l) + 1)) - 1) << (l))
#endif /* GENMASK */
//// End copy //////////////////////
int main() {
printf("1: %x\n", GENMASK(39,21) ); // should output only 32bit
printf("2: %x\n", GENMASK(14,2) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment