/* ospUtil.h: OSP utility header file */

#ifndef __OSPUTILH__

#define __OSPUTILH__



#ifdef __cplusplus

extern "C" {

#endif


struct list_head {
    struct list_head *next, *prev;
};

struct hlist_head {
    struct hlist_node *first;
};

struct hlist_node {
    struct hlist_node *next, **pprev;
};



#define OSP_NTOHL(x) (x)
#define OSP_NTOHS(x) (x)
#define OSP_HTONL(x) (x)
#define OSP_HTONS(x) (x)
#define OSP_NTOH64(x) (x)
#define OSP_HTON64(x) (x)



#define OSP_OFFSET(structure, member)    \
    ((uint64_t)&(((structure *)0)->member))


#define __OSP_MEMBEROF(s, f, p) \
((s *)((char *)(p) - OSP_OFFSET(s, f)))

#define OSP_MEMBEROF(s, f, p) \
    (((p) ? __OSP_MEMBEROF(s, f, p) : ((s *)0)))

#define OSP_MEMBER_SIZE(structure, member)  \
        (sizeof (((structure *)0)->member))


#define OSP_NELEMENTS(array)        \
((sizeof(array) / sizeof((array)[0])))


#define  OSP_ROUND_UP(x, align)    \
(((uint64_t) (x) + ((align) - 1)) & ~((align) - 1))


#define  OSP_ROUND_DOWN(x, align)    \
((uint64_t)(x) & ~((align) - 1))



/*32bit byte reversion */

#define OSP_LONGSWAP(x) \
((((x) & 0xFF000000) >> 24) | \
 (((x) & 0x00FF0000) >> 8) | \
 (((x) & 0x0000FF00) << 8) | \
 (((x) & 0x000000FF) << 24)  \
)

#define OSP_IS_POWEOF2(x) ((x & (x-1)) == 0)


/*reversing bit order*/






#define OSP_SETBIT(x, pos) ((x) | (1<<(pos)))
#define OSP_CLEARBIT(x, pos) ((x) & (~(1 << (pos))))
#define OSP_GET_BITS(x, start, len) (((x) >> (start)) & ((1 << (len)) - 1))
#define OSP_SET_BITS(x, start, len, y)  \
    (x) = (((x) & (~(((1 << (len)) - 1) << (start)))) | ((y) << (start)))

    

/* reverse one specified bit:

 * a: U32/U16/U8

 * pos: 0(lsb)~31(msb)

 */

#define OSP_REVERSE_BIT(a,pos) \
(((((~(a)) >> (pos)) & 0x01) << (pos)) | ((a) & (~(1 << (pos)))))



#ifndef min
#define min(a, b)     (((a) < (b)) ? (a) : (b))
#endif



#ifndef max
#define max(a, b)     (((a) > (b)) ? (a) : (b))
#endif
#define ISPOW2(x)     ((x & x - 1) == 0)

#ifdef __cplusplus
}
#endif

#endif  /* __OSPUTILH__ */


