#ifndef _BITFIELDS_H
#define _BITFIELDS_H
/*
 * Make a mask for 1 bit at position 'n'
 */

#define _MM_MAKEMASK1(n)   ((1) << (n))
#define _MM64_MAKEMASK1(n) ((1ULL) << (n))

/*
 * Make a mask for 'v' bits at position 'n'
 */

#define _MM_MAKEMASK(v, n)   ((((1) << (v)) - 1) << (n))
#define _MM64_MAKEMASK(v, n) ((((1ULL) << (v)) - 1) << (n))

/*
 * Make a value at 'v' at bit position 'n'
 */

#define _MM_MAKEVALUE(v, n)   ((v) << (n))
#define _MM64_MAKEVALUE(v, n) ((uint64_t)(v) << (n))

#define _MM_GETVALUE(v, n, m) (((v) & (m)) >> (n))

// Insert a bit pattern 'b' of width 'w' into value 'v' at pos 'p'.
#define _MM_SETFIELD(v, b, w, p)   ((v & ~(_MM_MAKEMASK(w, p))) | (b << p))
#define _MM64_SETFIELD(v, b, w, p) ((v & ~(_MM64_MAKEMASK(w, p))) | (b << p))

#endif // ifndef _BITFIELDS_H
