#ifndef _SYS__ATOMIC_SUBWORD_H_
#define _SYS__ATOMIC_SUBWORD_H_
#ifndef _MACHINE_ATOMIC_H_
#error do not include this header, use machine/atomic.h
#endif
#include <machine/endian.h>
#ifndef _KERNEL
#include <stdbool.h>
#endif
#ifndef NBBY
#define NBBY 8
#endif
#define _ATOMIC_WORD_ALIGNED(p) \
(uint32_t *)((__uintptr_t)(p) - ((__uintptr_t)(p) % 4))
#if _BYTE_ORDER == _BIG_ENDIAN
#define _ATOMIC_BYTE_SHIFT(p) \
((3 - ((__uintptr_t)(p) % 4)) * NBBY)
#define _ATOMIC_HWORD_SHIFT(p) \
((2 - ((__uintptr_t)(p) % 4)) * NBBY)
#else
#define _ATOMIC_BYTE_SHIFT(p) \
((((__uintptr_t)(p) % 4)) * NBBY)
#define _ATOMIC_HWORD_SHIFT(p) \
((((__uintptr_t)(p) % 4)) * NBBY)
#endif
#ifndef _atomic_cmpset_masked_word
static __inline int
_atomic_cmpset_masked_word(uint32_t *addr, uint32_t old, uint32_t val,
uint32_t mask)
{
int ret;
uint32_t wcomp;
wcomp = old;
do {
old = (*addr & ~mask) | wcomp;
ret = atomic_fcmpset_32(addr, &old, (old & ~mask) | val);
} while (ret == 0 && (old & mask) == wcomp);
return (ret);
}
#endif
#ifndef _atomic_fcmpset_masked_word
static __inline int
_atomic_fcmpset_masked_word(uint32_t *addr, uint32_t *old, uint32_t val,
uint32_t mask)
{
*old = (*addr & ~mask) | *old;
return (atomic_fcmpset_32(addr, old, (*old & ~mask) | val));
}
#endif
#ifndef atomic_cmpset_8
static __inline int
atomic_cmpset_8(__volatile uint8_t *addr, uint8_t old, uint8_t val)
{
int shift;
shift = _ATOMIC_BYTE_SHIFT(addr);
return (_atomic_cmpset_masked_word(_ATOMIC_WORD_ALIGNED(addr),
old << shift, val << shift, 0xff << shift));
}
#endif
#ifndef atomic_fcmpset_8
static __inline int
atomic_fcmpset_8(__volatile uint8_t *addr, uint8_t *old, uint8_t val)
{
int ret, shift;
uint32_t wold;
shift = _ATOMIC_BYTE_SHIFT(addr);
wold = *old << shift;
ret = _atomic_fcmpset_masked_word(_ATOMIC_WORD_ALIGNED(addr),
&wold, val << shift, 0xff << shift);
if (ret == 0)
*old = (wold >> shift) & 0xff;
return (ret);
}
#endif
#ifndef atomic_cmpset_16
static __inline int
atomic_cmpset_16(__volatile uint16_t *addr, uint16_t old, uint16_t val)
{
int shift;
shift = _ATOMIC_HWORD_SHIFT(addr);
return (_atomic_cmpset_masked_word(_ATOMIC_WORD_ALIGNED(addr),
old << shift, val << shift, 0xffff << shift));
}
#endif
#ifndef atomic_fcmpset_16
static __inline int
atomic_fcmpset_16(__volatile uint16_t *addr, uint16_t *old, uint16_t val)
{
int ret, shift;
uint32_t wold;
shift = _ATOMIC_HWORD_SHIFT(addr);
wold = *old << shift;
ret = _atomic_fcmpset_masked_word(_ATOMIC_WORD_ALIGNED(addr),
&wold, val << shift, 0xffff << shift);
if (ret == 0)
*old = (wold >> shift) & 0xffff;
return (ret);
}
#endif
#ifndef atomic_load_acq_8
static __inline uint8_t
atomic_load_acq_8(const volatile uint8_t *p)
{
int shift;
uint8_t ret;
shift = _ATOMIC_BYTE_SHIFT(p);
ret = (atomic_load_acq_32(_ATOMIC_WORD_ALIGNED(p)) >> shift) & 0xff;
return (ret);
}
#endif
#ifndef atomic_load_acq_16
static __inline uint16_t
atomic_load_acq_16(const volatile uint16_t *p)
{
int shift;
uint16_t ret;
shift = _ATOMIC_HWORD_SHIFT(p);
ret = (atomic_load_acq_32(_ATOMIC_WORD_ALIGNED(p)) >> shift) &
0xffff;
return (ret);
}
#endif
#undef _ATOMIC_WORD_ALIGNED
#undef _ATOMIC_BYTE_SHIFT
#undef _ATOMIC_HWORD_SHIFT
#ifndef atomic_set_16
static __inline void
atomic_set_16(volatile uint16_t *p, uint16_t bit)
{
uint16_t v;
v = atomic_load_16(p);
for (;;) {
if (atomic_fcmpset_16(p, &v, v | bit))
break;
}
}
#endif
#ifndef atomic_clear_16
static __inline void
atomic_clear_16(volatile uint16_t *p, uint16_t bit)
{
uint16_t v;
v = atomic_load_16(p);
for (;;) {
if (atomic_fcmpset_16(p, &v, v & ~bit))
break;
}
}
#endif
#endif