text:
package codec

import (
	"reflect"
	_ "runtime" // needed for go linkname(s)
	"sync/atomic"
	"time"
	"unsafe"
)

// This file has unsafe variants of some helper functions.
// MARKER: See helper_unsafe.go for the usage documentation.

// There are a number of helper_*unsafe*.go files.
//
// - helper_unsafe
//   unsafe variants of dependent functions
// - helper_unsafe_compiler_gc (gc)
//   unsafe variants of dependent functions which cannot be shared with gollvm or gccgo
// - helper_not_unsafe_not_gc (gccgo/gollvm or safe)
//   safe variants of functions in helper_unsafe_compiler_gc
// - helper_not_unsafe (safe)
//   safe variants of functions in helper_unsafe
// - helper_unsafe_compiler_not_gc (gccgo, gollvm)
//   unsafe variants of functions/variables which non-standard compilers need
//
// This way, we can judiciously use build tags to include the right set of files
// for any compiler, and make it run optimally in unsafe mode.
//
// As of March 2021, we cannot differentiate whether running with gccgo or gollvm
// using a build constraint, as both satisfy 'gccgo' build tag.
// Consequently, we must use the lowest common denominator to support both.

// For reflect.Value code, we decided to do the following:
//    - if we know the kind, we can elide conditional checks for
//      - SetXXX (Int, Uint, String, Bool, etc)
//      - SetLen
//
// We can also optimize
//      - IsNil

// MARKER: Some functions here will not be hit during code coverage runs due to optimizations, e.g.
//   - rvCopySlice:      decode calls it if rvGrowSlice didn't set the new slice into the pointer to the orig slice.
//                       however, helper_unsafe sets it, so there's no need to call rvCopySlice later
//   - rvSlice:          same as above
//   - rvGetArray4Bytes: only called within kArray for []byte, but that is now handled
//                       within the fast-path directly

const safeMode = false

// helperUnsafeDirectAssignMapEntry says that we should not copy the pointer in the map
// to another value during mapRange/iteration and mapGet calls, but directly assign it.
//
// The only callers of mapRange/iteration is encode.
// Here, we just walk through the values and encode them
//
// The only caller of mapGet is decode.
// Here, it does a Get if the underlying value is a pointer, and decodes into that.
//
// For both users, we are very careful NOT to modify or keep the pointers around.
// Consequently, it is ok for take advantage of the performance that the map is not modified
// during an iteration and we can just "peek" at the internal value" in the map and use it.
const helperUnsafeDirectAssignMapEntry = true

// MARKER: keep in sync with GO_ROOT/src/reflect/value.go
const (
	unsafeFlagStickyRO = 1 << 5
	unsafeFlagEmbedRO  = 1 << 6
	unsafeFlagIndir    = 1 << 7
	unsafeFlagAddr     = 1 << 8
	unsafeFlagRO       = unsafeFlagStickyRO | unsafeFlagEmbedRO
	// unsafeFlagKindMask = (1 << 5) - 1 // 5 bits for 27 kinds (up to 31)
	// unsafeTypeKindDirectIface = 1 << 5
)

// transientSizeMax below is used in TransientAddr as the backing storage.
//
// Must be >= 16 as the maximum size is a complex128 (or string on 64-bit machines).
const transientSizeMax = 64

// should struct/array support internal strings and slices?
const transientValueHasStringSlice = false

type unsafeString struct {
	Data unsafe.Pointer
	Len  int
}

type unsafeSlice struct {
	Data unsafe.Pointer
	Len  int
	Cap  int
}

type unsafeIntf struct {
	typ unsafe.Pointer
	ptr unsafe.Pointer
}

type unsafeReflectValue struct {
	unsafeIntf
	flag uintptr
}

// keep in sync with stdlib runtime/type.go
type unsafeRuntimeType struct {
	size uintptr
	// ... many other fields here
}

// unsafeZeroAddr and unsafeZeroSlice points to a read-only block of memory
// used for setting a zero value for most types or creating a read-only
// zero value for a given type.
var (
	unsafeZeroAddr  = unsafe.Pointer(&unsafeZeroArr[0])
	unsafeZeroSlice = unsafeSlice{unsafeZeroAddr, 0, 0}
)

// We use a scratch memory and an unsafeSlice for transient values:
//
// unsafeSlice is used for standalone strings and slices (outside an array or struct).
// scratch memory is used for other kinds, based on contract below:
// - numbers, bool are always transient
// - structs and arrays are transient iff they have no pointers i.e.
//   no string, slice, chan, func, interface, map, etc only numbers and bools.
// - slices and strings are transient (using the unsafeSlice)

type unsafePerTypeElem struct {
	arr   [transientSizeMax]byte // for bool, number, struct, array kinds
	slice unsafeSlice            // for string and slice kinds
}

func (x *unsafePerTypeElem) addrFor(k reflect.Kind) unsafe.Pointer {
	if k == reflect.String || k == reflect.Slice {
		x.slice = unsafeSlice{} // memclr
		return unsafe.Pointer(&x.slice)
	}
	x.arr = [transientSizeMax]byte{} // memclr
	return unsafe.Pointer(&x.arr)
}

type perType struct {
	elems [2]unsafePerTypeElem
}

type decPerType struct {
	perType
}

type encPerType struct{}

// TransientAddrK is used for getting a *transient* value to be decoded into,
// which will right away be used for something else.
//
// See notes in helper.go about "Transient values during decoding"

func (x *perType) TransientAddrK(t reflect.Type, k reflect.Kind) reflect.Value {
	return rvZeroAddrTransientAnyK(t, k, x.elems[0].addrFor(k))
}

func (x *perType) TransientAddr2K(t reflect.Type, k reflect.Kind) reflect.Value {
	return rvZeroAddrTransientAnyK(t, k, x.elems[1].addrFor(k))
}

func (encPerType) AddressableRO(v reflect.Value) reflect.Value {
	return rvAddressableReadonly(v)
}

// stringView returns a view of the []byte as a string.
// In unsafe mode, it doesn't incur allocation and copying caused by conversion.
// In regular safe mode, it is an allocation and copy.
func stringView(v []byte) string {
	return *(*string)(unsafe.Pointer(&v))
}

// bytesView returns a view of the string as a []byte.
// In unsafe mode, it doesn't incur allocation and copying caused by conversion.
// In regular safe mode, it is an allocation and copy.
func bytesView(v string) (b []byte) {
	sx := (*unsafeString)(unsafe.Pointer(&v))
	bx := (*unsafeSlice)(unsafe.Pointer(&b))
	bx.Data, bx.Len, bx.Cap = sx.Data, sx.Len, sx.Len
	return
}

func byteSliceSameData(v1 []byte, v2 []byte) bool {
	return (*unsafeSlice)(unsafe.Pointer(&v1)).Data == (*unsafeSlice)(unsafe.Pointer(&v2)).Data
}

// MARKER: okBytesN functions will copy N bytes into the top slots of the return array.
// These functions expect that the bounds are valid, and have been checked before this is called.
// copy(...) does a number of checks which are unnecessary in this situation when in bounds.

func okBytes3(b []byte) (v [4]byte) {
	*(*[3]byte)(unsafe.Pointer(&v[1])) = *((*[3]byte)(((*unsafeSlice)(unsafe.Pointer(&b))).Data))
	return
}

func okBytes4(b []byte) [4]byte {
	return *((*[4]byte)(((*unsafeSlice)(unsafe.Pointer(&b))).Data))
}

func okBytes8(b []byte) [8]byte {
	return *((*[8]byte)(((*unsafeSlice)(unsafe.Pointer(&b))).Data))
}

// isNil says whether the value v is nil.
// This applies to references like map/ptr/unsafepointer/chan/func,
// and non-reference values like interface/slice.
func isNil(v interface{}) (rv reflect.Value, isnil bool) {
	var ui = (*unsafeIntf)(unsafe.Pointer(&v))
	isnil = ui.ptr == nil
	if !isnil {
		rv, isnil = unsafeIsNilIntfOrSlice(ui, v)
	}
	return
}

func unsafeIsNilIntfOrSlice(ui *unsafeIntf, v interface{}) (rv reflect.Value, isnil bool) {
	rv = reflect.ValueOf(v) // reflect.ValueOf is currently not inline'able - so call it directly
	tk := rv.Kind()
	isnil = (tk == reflect.Interface || tk == reflect.Slice) && *(*unsafe.Pointer)(ui.ptr) == nil
	return
}

// return the pointer for a reference (map/chan/func/pointer/unsafe.Pointer).
// true references (map, func, chan, ptr - NOT slice) may be double-referenced? as flagIndir
//
// Assumes that v is a reference (map/func/chan/ptr/func)
func rvRefPtr(v *unsafeReflectValue) unsafe.Pointer {
	if v.flag&unsafeFlagIndir != 0 {
		return *(*unsafe.Pointer)(v.ptr)
	}
	return v.ptr
}

func eq4i(i0, i1 interface{}) bool {
	v0 := (*unsafeIntf)(unsafe.Pointer(&i0))
	v1 := (*unsafeIntf)(unsafe.Pointer(&i1))
	return v0.typ == v1.typ && v0.ptr == v1.ptr
}

func rv4iptr(i interface{}) (v reflect.Value) {
	// Main advantage here is that it is inlined, nothing escapes to heap, i is never nil
	uv := (*unsafeReflectValue)(unsafe.Pointer(&v))
	uv.unsafeIntf = *(*unsafeIntf)(unsafe.Pointer(&i))
	uv.flag = uintptr(rkindPtr)
	return
}

func rv4istr(i interface{}) (v reflect.Value) {
	// Main advantage here is that it is inlined, nothing escapes to heap, i is never nil
	uv := (*unsafeReflectValue)(unsafe.Pointer(&v))
	uv.unsafeIntf = *(*unsafeIntf)(unsafe.Pointer(&i))
	uv.flag = uintptr(rkindString) | unsafeFlagIndir
	return
}

func rv2i(rv reflect.Value) (i interface{}) {
	// We tap into implememtation details from
	// the source go stdlib reflect/value.go, and trims the implementation.
	//
	// e.g.
	// - a map/ptr is a reference,        thus flagIndir is not set on it
	// - an int/slice is not a reference, thus flagIndir is set on it

	urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	if refBitset.isset(byte(rv.Kind())) && urv.flag&unsafeFlagIndir != 0 {
		urv.ptr = *(*unsafe.Pointer)(urv.ptr)
	}
	return *(*interface{})(unsafe.Pointer(&urv.unsafeIntf))
}

func rvAddr(rv reflect.Value, ptrType reflect.Type) reflect.Value {
	urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	urv.flag = (urv.flag & unsafeFlagRO) | uintptr(reflect.Ptr)
	urv.typ = ((*unsafeIntf)(unsafe.Pointer(&ptrType))).ptr
	return rv
}

func rvIsNil(rv reflect.Value) bool {
	urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	if urv.flag&unsafeFlagIndir != 0 {
		return *(*unsafe.Pointer)(urv.ptr) == nil
	}
	return urv.ptr == nil
}

func rvSetSliceLen(rv reflect.Value, length int) {
	urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	(*unsafeString)(urv.ptr).Len = length
}

func rvZeroAddrK(t reflect.Type, k reflect.Kind) (rv reflect.Value) {
	urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	urv.typ = ((*unsafeIntf)(unsafe.Pointer(&t))).ptr
	urv.flag = uintptr(k) | unsafeFlagIndir | unsafeFlagAddr
	urv.ptr = unsafeNew(urv.typ)
	return
}

func rvZeroAddrTransientAnyK(t reflect.Type, k reflect.Kind, addr unsafe.Pointer) (rv reflect.Value) {
	urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	urv.typ = ((*unsafeIntf)(unsafe.Pointer(&t))).ptr
	urv.flag = uintptr(k) | unsafeFlagIndir | unsafeFlagAddr
	urv.ptr = addr
	return
}

func rvZeroK(t reflect.Type, k reflect.Kind) (rv reflect.Value) {
	urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	urv.typ = ((*unsafeIntf)(unsafe.Pointer(&t))).ptr
	if refBitset.isset(byte(k)) {
		urv.flag = uintptr(k)
	} else if rtsize2(urv.typ) <= uintptr(len(unsafeZeroArr)) {
		urv.flag = uintptr(k) | unsafeFlagIndir
		urv.ptr = unsafeZeroAddr
	} else { // meaning struct or array
		urv.flag = uintptr(k) | unsafeFlagIndir | unsafeFlagAddr
		urv.ptr = unsafeNew(urv.typ)
	}
	return
}

// rvConvert will convert a value to a different type directly,
// ensuring that they still point to the same underlying value.
func rvConvert(v reflect.Value, t reflect.Type) reflect.Value {
	uv := (*unsafeReflectValue)(unsafe.Pointer(&v))
	uv.typ = ((*unsafeIntf)(unsafe.Pointer(&t))).ptr
	return v
}

// rvAddressableReadonly returns an addressable reflect.Value.
//
// Use it within encode calls, when you just want to "read" the underlying ptr
// without modifying the value.
//
// Note that it cannot be used for r/w use, as those non-addressable values
// may have been stored in read-only memory, and trying to write the pointer
// may cause a segfault.
func rvAddressableReadonly(v reflect.Value) reflect.Value {
	// hack to make an addressable value out of a non-addressable one.
	// Assume folks calling it are passing a value that can be addressable, but isn't.
	// This assumes that the flagIndir is already set on it.
	// so we just set the flagAddr bit on the flag (and do not set the flagIndir).

	uv := (*unsafeReflectValue)(unsafe.Pointer(&v))
	uv.flag = uv.flag | unsafeFlagAddr // | unsafeFlagIndir

	return v
}

func rtsize2(rt unsafe.Pointer) uintptr {
	return ((*unsafeRuntimeType)(rt)).size
}

func rt2id(rt reflect.Type) uintptr {
	return uintptr(((*unsafeIntf)(unsafe.Pointer(&rt))).ptr)
}

func i2rtid(i interface{}) uintptr {
	return uintptr(((*unsafeIntf)(unsafe.Pointer(&i))).typ)
}

// --------------------------

func unsafeCmpZero(ptr unsafe.Pointer, size int) bool {
	// verified that size is always within right range, so no chance of OOM
	var s1 = unsafeString{ptr, size}
	var s2 = unsafeString{unsafeZeroAddr, size}
	if size > len(unsafeZeroArr) {
		arr := make([]byte, size)
		s2.Data = unsafe.Pointer(&arr[0])
	}
	return *(*string)(unsafe.Pointer(&s1)) == *(*string)(unsafe.Pointer(&s2)) // memcmp
}

func isEmptyValue(v reflect.Value, tinfos *TypeInfos, recursive bool) bool {
	urv := (*unsafeReflectValue)(unsafe.Pointer(&v))
	if urv.flag == 0 {
		return true
	}
	if recursive {
		return isEmptyValueFallbackRecur(urv, v, tinfos)
	}
	return unsafeCmpZero(urv.ptr, int(rtsize2(urv.typ)))
}

func isEmptyValueFallbackRecur(urv *unsafeReflectValue, v reflect.Value, tinfos *TypeInfos) bool {
	const recursive = true

	switch v.Kind() {
	case reflect.Invalid:
		return true
	case reflect.String:
		return (*unsafeString)(urv.ptr).Len == 0
	case reflect.Slice:
		return (*unsafeSlice)(urv.ptr).Len == 0
	case reflect.Bool:
		return !*(*bool)(urv.ptr)
	case reflect.Int:
		return *(*int)(urv.ptr) == 0
	case reflect.Int8:
		return *(*int8)(urv.ptr) == 0
	case reflect.Int16:
		return *(*int16)(urv.ptr) == 0
	case reflect.Int32:
		return *(*int32)(urv.ptr) == 0
	case reflect.Int64:
		return *(*int64)(urv.ptr) == 0
	case reflect.Uint:
		return *(*uint)(urv.ptr) == 0
	case reflect.Uint8:
		return *(*uint8)(urv.ptr) == 0
	case reflect.Uint16:
		return *(*uint16)(urv.ptr) == 0
	case reflect.Uint32:
		return *(*uint32)(urv.ptr) == 0
	case reflect.Uint64:
		return *(*uint64)(urv.ptr) == 0
	case reflect.Uintptr:
		return *(*uintptr)(urv.ptr) == 0
	case reflect.Float32:
		return *(*float32)(urv.ptr) == 0
	case reflect.Float64:
		return *(*float64)(urv.ptr) == 0
	case reflect.Complex64:
		return unsafeCmpZero(urv.ptr, 8)
	case reflect.Complex128:
		return unsafeCmpZero(urv.ptr, 16)
	case reflect.Struct:
		// return isEmptyStruct(v, tinfos, recursive)
		if tinfos == nil {
			tinfos = defTypeInfos
		}
		ti := tinfos.find(uintptr(urv.typ))
		if ti == nil {
			ti = tinfos.load(rvType(v))
		}
		return unsafeCmpZero(urv.ptr, int(ti.size))
	case reflect.Interface, reflect.Ptr:
		// isnil := urv.ptr == nil // (not sufficient, as a pointer value encodes the type)
		isnil := urv.ptr == nil || *(*unsafe.Pointer)(urv.ptr) == nil
		if recursive && !isnil {
			return isEmptyValue(v.Elem(), tinfos, recursive)
		}
		return isnil
	case reflect.UnsafePointer:
		return urv.ptr == nil || *(*unsafe.Pointer)(urv.ptr) == nil
	case reflect.Chan:
		return urv.ptr == nil || len_chan(rvRefPtr(urv)) == 0
	case reflect.Map:
		return urv.ptr == nil || len_map(rvRefPtr(urv)) == 0
	case reflect.Array:
		return v.Len() == 0
	}
	return false
}

// --------------------------

type structFieldInfos struct {
	c      unsafe.Pointer // source
	s      unsafe.Pointer // sorted
	length int
}

func (x *structFieldInfos) load(source, sorted []*structFieldInfo) {
	s := (*unsafeSlice)(unsafe.Pointer(&sorted))
	x.s = s.Data
	x.length = s.Len
	s = (*unsafeSlice)(unsafe.Pointer(&source))
	x.c = s.Data
}

func (x *structFieldInfos) sorted() (v []*structFieldInfo) {
	*(*unsafeSlice)(unsafe.Pointer(&v)) = unsafeSlice{x.s, x.length, x.length}
	// s := (*unsafeSlice)(unsafe.Pointer(&v))
	// s.Data = x.sorted0
	// s.Len = x.length
	// s.Cap = s.Len
	return
}

func (x *structFieldInfos) source() (v []*structFieldInfo) {
	*(*unsafeSlice)(unsafe.Pointer(&v)) = unsafeSlice{x.c, x.length, x.length}
	return
}

// atomicXXX is expected to be 2 words (for symmetry with atomic.Value)
//
// Note that we do not atomically load/store length and data pointer separately,
// as this could lead to some races. Instead, we atomically load/store cappedSlice.
//
// Note: with atomic.(Load|Store)Pointer, we MUST work with an unsafe.Pointer directly.

// ----------------------
type atomicTypeInfoSlice struct {
	v unsafe.Pointer // *[]rtid2ti
}

func (x *atomicTypeInfoSlice) load() (s []rtid2ti) {
	x2 := atomic.LoadPointer(&x.v)
	if x2 != nil {
		s = *(*[]rtid2ti)(x2)
	}
	return
}

func (x *atomicTypeInfoSlice) store(p []rtid2ti) {
	atomic.StorePointer(&x.v, unsafe.Pointer(&p))
}

// MARKER: in safe mode, atomicXXX are atomic.Value, which contains an interface{}.
// This is 2 words.
// consider padding atomicXXX here with a uintptr, so they fit into 2 words also.

// --------------------------
type atomicRtidFnSlice struct {
	v unsafe.Pointer // *[]codecRtidFn
}

func (x *atomicRtidFnSlice) load() (s []codecRtidFn) {
	x2 := atomic.LoadPointer(&x.v)
	if x2 != nil {
		s = *(*[]codecRtidFn)(x2)
	}
	return
}

func (x *atomicRtidFnSlice) store(p []codecRtidFn) {
	atomic.StorePointer(&x.v, unsafe.Pointer(&p))
}

// --------------------------
type atomicClsErr struct {
	v unsafe.Pointer // *clsErr
}

func (x *atomicClsErr) load() (e clsErr) {
	x2 := (*clsErr)(atomic.LoadPointer(&x.v))
	if x2 != nil {
		e = *x2
	}
	return
}

func (x *atomicClsErr) store(p clsErr) {
	atomic.StorePointer(&x.v, unsafe.Pointer(&p))
}

// --------------------------

// to create a reflect.Value for each member field of fauxUnion,
// we first create a global fauxUnion, and create reflect.Value
// for them all.
// This way, we have the flags and type in the reflect.Value.
// Then, when a reflect.Value is called, we just copy it,
// update the ptr to the fauxUnion's, and return it.

type unsafeDecNakedWrapper struct {
	fauxUnion
	ru, ri, rf, rl, rs, rb, rt reflect.Value // mapping to the primitives above
}

func (n *unsafeDecNakedWrapper) init() {
	n.ru = rv4iptr(&n.u).Elem()
	n.ri = rv4iptr(&n.i).Elem()
	n.rf = rv4iptr(&n.f).Elem()
	n.rl = rv4iptr(&n.l).Elem()
	n.rs = rv4iptr(&n.s).Elem()
	n.rt = rv4iptr(&n.t).Elem()
	n.rb = rv4iptr(&n.b).Elem()
	// n.rr[] = reflect.ValueOf(&n.)
}

var defUnsafeDecNakedWrapper unsafeDecNakedWrapper

func init() {
	defUnsafeDecNakedWrapper.init()
}

func (n *fauxUnion) ru() (v reflect.Value) {
	v = defUnsafeDecNakedWrapper.ru
	((*unsafeReflectValue)(unsafe.Pointer(&v))).ptr = unsafe.Pointer(&n.u)
	return
}
func (n *fauxUnion) ri() (v reflect.Value) {
	v = defUnsafeDecNakedWrapper.ri
	((*unsafeReflectValue)(unsafe.Pointer(&v))).ptr = unsafe.Pointer(&n.i)
	return
}
func (n *fauxUnion) rf() (v reflect.Value) {
	v = defUnsafeDecNakedWrapper.rf
	((*unsafeReflectValue)(unsafe.Pointer(&v))).ptr = unsafe.Pointer(&n.f)
	return
}
func (n *fauxUnion) rl() (v reflect.Value) {
	v = defUnsafeDecNakedWrapper.rl
	((*unsafeReflectValue)(unsafe.Pointer(&v))).ptr = unsafe.Pointer(&n.l)
	return
}
func (n *fauxUnion) rs() (v reflect.Value) {
	v = defUnsafeDecNakedWrapper.rs
	((*unsafeReflectValue)(unsafe.Pointer(&v))).ptr = unsafe.Pointer(&n.s)
	return
}
func (n *fauxUnion) rt() (v reflect.Value) {
	v = defUnsafeDecNakedWrapper.rt
	((*unsafeReflectValue)(unsafe.Pointer(&v))).ptr = unsafe.Pointer(&n.t)
	return
}
func (n *fauxUnion) rb() (v reflect.Value) {
	v = defUnsafeDecNakedWrapper.rb
	((*unsafeReflectValue)(unsafe.Pointer(&v))).ptr = unsafe.Pointer(&n.b)
	return
}

// --------------------------
func rvSetBytes(rv reflect.Value, v []byte) {
	urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	*(*[]byte)(urv.ptr) = v
}

func rvSetString(rv reflect.Value, v string) {
	urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	*(*string)(urv.ptr) = v
}

func rvSetBool(rv reflect.Value, v bool) {
	urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	*(*bool)(urv.ptr) = v
}

func rvSetTime(rv reflect.Value, v time.Time) {
	urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	*(*time.Time)(urv.ptr) = v
}

func rvSetFloat32(rv reflect.Value, v float32) {
	urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	*(*float32)(urv.ptr) = v
}

func rvSetFloat64(rv reflect.Value, v float64) {
	urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	*(*float64)(urv.ptr) = v
}

func rvSetComplex64(rv reflect.Value, v complex64) {
	urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	*(*complex64)(urv.ptr) = v
}

func rvSetComplex128(rv reflect.Value, v complex128) {
	urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	*(*complex128)(urv.ptr) = v
}

func rvSetInt(rv reflect.Value, v int) {
	urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	*(*int)(urv.ptr) = v
}

func rvSetInt8(rv reflect.Value, v int8) {
	urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	*(*int8)(urv.ptr) = v
}

func rvSetInt16(rv reflect.Value, v int16) {
	urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	*(*int16)(urv.ptr) = v
}

func rvSetInt32(rv reflect.Value, v int32) {
	urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	*(*int32)(urv.ptr) = v
}

func rvSetInt64(rv reflect.Value, v int64) {
	urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	*(*int64)(urv.ptr) = v
}

func rvSetUint(rv reflect.Value, v uint) {
	urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	*(*uint)(urv.ptr) = v
}

func rvSetUintptr(rv reflect.Value, v uintptr) {
	urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	*(*uintptr)(urv.ptr) = v
}

func rvSetUint8(rv reflect.Value, v uint8) {
	urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	*(*uint8)(urv.ptr) = v
}

func rvSetUint16(rv reflect.Value, v uint16) {
	urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	*(*uint16)(urv.ptr) = v
}

func rvSetUint32(rv reflect.Value, v uint32) {
	urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	*(*uint32)(urv.ptr) = v
}

func rvSetUint64(rv reflect.Value, v uint64) {
	urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	*(*uint64)(urv.ptr) = v
}

// ----------------

// rvSetZero is rv.Set(reflect.Zero(rv.Type()) for all kinds (including reflect.Interface).
func rvSetZero(rv reflect.Value) {
	rvSetDirectZero(rv)
}

func rvSetIntf(rv reflect.Value, v reflect.Value) {
	rv.Set(v)
}

// rvSetDirect is rv.Set for all kinds except reflect.Interface.
//
// Callers MUST not pass a value of kind reflect.Interface, as it may cause unexpected segfaults.
func rvSetDirect(rv reflect.Value, v reflect.Value) {
	// MARKER: rv.Set for kind reflect.Interface may do a separate allocation if a scalar value.
	// The book-keeping is onerous, so we just do the simple ones where a memmove is sufficient.
	urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	uv := (*unsafeReflectValue)(unsafe.Pointer(&v))
	if uv.flag&unsafeFlagIndir == 0 {
		*(*unsafe.Pointer)(urv.ptr) = uv.ptr
	} else if uv.ptr == unsafeZeroAddr {
		if urv.ptr != unsafeZeroAddr {
			typedmemclr(urv.typ, urv.ptr)
		}
	} else {
		typedmemmove(urv.typ, urv.ptr, uv.ptr)
	}
}

// rvSetDirectZero is rv.Set(reflect.Zero(rv.Type()) for all kinds except reflect.Interface.
func rvSetDirectZero(rv reflect.Value) {
	urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	if urv.ptr != unsafeZeroAddr {
		typedmemclr(urv.typ, urv.ptr)
	}
}

// rvMakeSlice updates the slice to point to a new array.
// It copies data from old slice to new slice.
// It returns set=true iff it updates it, else it just returns a new slice pointing to a newly made array.
func rvMakeSlice(rv reflect.Value, ti *typeInfo, xlen, xcap int) (_ reflect.Value, set bool) {
	urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	ux := (*unsafeSlice)(urv.ptr)
	t := ((*unsafeIntf)(unsafe.Pointer(&ti.elem))).ptr
	s := unsafeSlice{newarray(t, xcap), xlen, xcap}
	if ux.Len > 0 {
		typedslicecopy(t, s, *ux)
	}
	*ux = s
	return rv, true
}

// rvSlice returns a sub-slice of the slice given new lenth,
// without modifying passed in value.
// It is typically called when we know that SetLen(...) cannot be done.
func rvSlice(rv reflect.Value, length int) reflect.Value {
	urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	var x []struct{}
	ux := (*unsafeSlice)(unsafe.Pointer(&x))
	*ux = *(*unsafeSlice)(urv.ptr)
	ux.Len = length
	urv.ptr = unsafe.Pointer(ux)
	return rv
}

// rcGrowSlice updates the slice to point to a new array with the cap incremented, and len set to the new cap value.
// It copies data from old slice to new slice.
// It returns set=true iff it updates it, else it just returns a new slice pointing to a newly made array.
func rvGrowSlice(rv reflect.Value, ti *typeInfo, cap, incr int) (v reflect.Value, newcap int, set bool) {
	urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	ux := (*unsafeSlice)(urv.ptr)
	t := ((*unsafeIntf)(unsafe.Pointer(&ti.elem))).ptr
	*ux = unsafeGrowslice(t, *ux, cap, incr)
	ux.Len = ux.Cap
	return rv, ux.Cap, true
}

// ------------

func rvSliceIndex(rv reflect.Value, i int, ti *typeInfo) (v reflect.Value) {
	urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	uv := (*unsafeReflectValue)(unsafe.Pointer(&v))
	uv.ptr = unsafe.Pointer(uintptr(((*unsafeSlice)(urv.ptr)).Data) + uintptr(int(ti.elemsize)*i))
	uv.typ = ((*unsafeIntf)(unsafe.Pointer(&ti.elem))).ptr
	uv.flag = uintptr(ti.elemkind) | unsafeFlagIndir | unsafeFlagAddr
	return
}

func rvSliceZeroCap(t reflect.Type) (v reflect.Value) {
	urv := (*unsafeReflectValue)(unsafe.Pointer(&v))
	urv.typ = ((*unsafeIntf)(unsafe.Pointer(&t))).ptr
	urv.flag = uintptr(reflect.Slice) | unsafeFlagIndir
	urv.ptr = unsafe.Pointer(&unsafeZeroSlice)
	return
}

func rvLenSlice(rv reflect.Value) int {
	urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	return (*unsafeSlice)(urv.ptr).Len
}

func rvCapSlice(rv reflect.Value) int {
	urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	return (*unsafeSlice)(urv.ptr).Cap
}

func rvArrayIndex(rv reflect.Value, i int, ti *typeInfo) (v reflect.Value) {
	urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	uv := (*unsafeReflectValue)(unsafe.Pointer(&v))
	uv.ptr = unsafe.Pointer(uintptr(urv.ptr) + uintptr(int(ti.elemsize)*i))
	uv.typ = ((*unsafeIntf)(unsafe.Pointer(&ti.elem))).ptr
	uv.flag = uintptr(ti.elemkind) | unsafeFlagIndir | unsafeFlagAddr
	return
}

// if scratch is nil, then return a writable view (assuming canAddr=true)
func rvGetArrayBytes(rv reflect.Value, scratch []byte) (bs []byte) {
	urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	bx := (*unsafeSlice)(unsafe.Pointer(&bs))
	bx.Data = urv.ptr
	bx.Len = rv.Len()
	bx.Cap = bx.Len
	return
}

func rvGetArray4Slice(rv reflect.Value) (v reflect.Value) {
	// It is possible that this slice is based off an array with a larger
	// len that we want (where array len == slice cap).
	// However, it is ok to create an array type that is a subset of the full
	// e.g. full slice is based off a *[16]byte, but we can create a *[4]byte
	// off of it. That is ok.
	//
	// Consequently, we use rvLenSlice, not rvCapSlice.

	t := reflectArrayOf(rvLenSlice(rv), rvType(rv).Elem())
	// v = rvZeroAddrK(t, reflect.Array)

	uv := (*unsafeReflectValue)(unsafe.Pointer(&v))
	uv.flag = uintptr(reflect.Array) | unsafeFlagIndir | unsafeFlagAddr
	uv.typ = ((*unsafeIntf)(unsafe.Pointer(&t))).ptr

	urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	uv.ptr = *(*unsafe.Pointer)(urv.ptr) // slice rv has a ptr to the slice.

	return
}

func rvGetSlice4Array(rv reflect.Value, v interface{}) {
	// v is a pointer to a slice to be populated
	uv := (*unsafeIntf)(unsafe.Pointer(&v))
	urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))

	s := (*unsafeSlice)(uv.ptr)
	s.Data = urv.ptr
	s.Len = rv.Len()
	s.Cap = s.Len
}

func rvCopySlice(dest, src reflect.Value, elemType reflect.Type) {
	typedslicecopy((*unsafeIntf)(unsafe.Pointer(&elemType)).ptr,
		*(*unsafeSlice)((*unsafeReflectValue)(unsafe.Pointer(&dest)).ptr),
		*(*unsafeSlice)((*unsafeReflectValue)(unsafe.Pointer(&src)).ptr))
}

// ------------

func rvGetBool(rv reflect.Value) bool {
	v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	return *(*bool)(v.ptr)
}

func rvGetBytes(rv reflect.Value) []byte {
	v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	return *(*[]byte)(v.ptr)
}

func rvGetTime(rv reflect.Value) time.Time {
	v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	return *(*time.Time)(v.ptr)
}

func rvGetString(rv reflect.Value) string {
	v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	return *(*string)(v.ptr)
}

func rvGetFloat64(rv reflect.Value) float64 {
	v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	return *(*float64)(v.ptr)
}

func rvGetFloat32(rv reflect.Value) float32 {
	v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	return *(*float32)(v.ptr)
}

func rvGetComplex64(rv reflect.Value) complex64 {
	v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	return *(*complex64)(v.ptr)
}

func rvGetComplex128(rv reflect.Value) complex128 {
	v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	return *(*complex128)(v.ptr)
}

func rvGetInt(rv reflect.Value) int {
	v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	return *(*int)(v.ptr)
}

func rvGetInt8(rv reflect.Value) int8 {
	v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	return *(*int8)(v.ptr)
}

func rvGetInt16(rv reflect.Value) int16 {
	v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	return *(*int16)(v.ptr)
}

func rvGetInt32(rv reflect.Value) int32 {
	v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	return *(*int32)(v.ptr)
}

func rvGetInt64(rv reflect.Value) int64 {
	v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	return *(*int64)(v.ptr)
}

func rvGetUint(rv reflect.Value) uint {
	v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	return *(*uint)(v.ptr)
}

func rvGetUint8(rv reflect.Value) uint8 {
	v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	return *(*uint8)(v.ptr)
}

func rvGetUint16(rv reflect.Value) uint16 {
	v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	return *(*uint16)(v.ptr)
}

func rvGetUint32(rv reflect.Value) uint32 {
	v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	return *(*uint32)(v.ptr)
}

func rvGetUint64(rv reflect.Value) uint64 {
	v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	return *(*uint64)(v.ptr)
}

func rvGetUintptr(rv reflect.Value) uintptr {
	v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	return *(*uintptr)(v.ptr)
}

func rvLenMap(rv reflect.Value) int {
	// maplen is not inlined, because as of go1.16beta, go:linkname's are not inlined.
	// thus, faster to call rv.Len() directly.
	//
	// MARKER: review after https://github.com/golang/go/issues/20019 fixed.

	// return rv.Len()

	return len_map(rvRefPtr((*unsafeReflectValue)(unsafe.Pointer(&rv))))
}

// Note: it is hard to find len(...) of an array type,
// as that is a field in the arrayType representing the array, and hard to introspect.
//
// func rvLenArray(rv reflect.Value) int {	return rv.Len() }

// ------------ map range and map indexing ----------

// regular calls to map via reflection: MapKeys, MapIndex, MapRange/MapIter etc
// will always allocate for each map key or value.
//
// It is more performant to provide a value that the map entry is set into,
// and that elides the allocation.

// go 1.4+ has runtime/hashmap.go or runtime/map.go which has a
// hIter struct with the first 2 values being key and value
// of the current iteration.
//
// This *hIter is passed to mapiterinit, mapiternext, mapiterkey, mapiterelem.
// We bypass the reflect wrapper functions and just use the *hIter directly.
//
// Though *hIter has many fields, we only care about the first 2.
//
// We directly embed this in unsafeMapIter below
//
// hiter is typically about 12 words, but we just fill up unsafeMapIter to 32 words,
// so it fills multiple cache lines and can give some extra space to accomodate small growth.

type unsafeMapIter struct {
	mtyp, mptr unsafe.Pointer
	k, v       reflect.Value
	kisref     bool
	visref     bool
	mapvalues  bool
	done       bool
	started    bool
	_          [3]byte // padding
	it         struct {
		key   unsafe.Pointer
		value unsafe.Pointer
		_     [20]uintptr // padding for other fields (to make up 32 words for enclosing struct)
	}
}

func (t *unsafeMapIter) Next() (r bool) {
	if t == nil || t.done {
		return
	}
	if t.started {
		mapiternext((unsafe.Pointer)(&t.it))
	} else {
		t.started = true
	}

	t.done = t.it.key == nil
	if t.done {
		return
	}

	if helperUnsafeDirectAssignMapEntry || t.kisref {
		(*unsafeReflectValue)(unsafe.Pointer(&t.k)).ptr = t.it.key
	} else {
		k := (*unsafeReflectValue)(unsafe.Pointer(&t.k))
		typedmemmove(k.typ, k.ptr, t.it.key)
	}

	if t.mapvalues {
		if helperUnsafeDirectAssignMapEntry || t.visref {
			(*unsafeReflectValue)(unsafe.Pointer(&t.v)).ptr = t.it.value
		} else {
			v := (*unsafeReflectValue)(unsafe.Pointer(&t.v))
			typedmemmove(v.typ, v.ptr, t.it.value)
		}
	}

	return true
}

func (t *unsafeMapIter) Key() (r reflect.Value) {
	return t.k
}

func (t *unsafeMapIter) Value() (r reflect.Value) {
	return t.v
}

func (t *unsafeMapIter) Done() {}

type mapIter struct {
	unsafeMapIter
}

func mapRange(t *mapIter, m, k, v reflect.Value, mapvalues bool) {
	if rvIsNil(m) {
		t.done = true
		return
	}
	t.done = false
	t.started = false
	t.mapvalues = mapvalues

	// var urv *unsafeReflectValue

	urv := (*unsafeReflectValue)(unsafe.Pointer(&m))
	t.mtyp = urv.typ
	t.mptr = rvRefPtr(urv)

	// t.it = (*unsafeMapHashIter)(reflect_mapiterinit(t.mtyp, t.mptr))
	mapiterinit(t.mtyp, t.mptr, unsafe.Pointer(&t.it))

	t.k = k
	t.kisref = refBitset.isset(byte(k.Kind()))

	if mapvalues {
		t.v = v
		t.visref = refBitset.isset(byte(v.Kind()))
	} else {
		t.v = reflect.Value{}
	}
}

// unsafeMapKVPtr returns the pointer if flagIndir, else it returns a pointer to the pointer.
// It is needed as maps always keep a reference to the underlying value.
func unsafeMapKVPtr(urv *unsafeReflectValue) unsafe.Pointer {
	if urv.flag&unsafeFlagIndir == 0 {
		return unsafe.Pointer(&urv.ptr)
	}
	return urv.ptr
}

// func mapDelete(m, k reflect.Value) {
// 	var urv = (*unsafeReflectValue)(unsafe.Pointer(&k))
// 	var kptr = unsafeMapKVPtr(urv)
// 	urv = (*unsafeReflectValue)(unsafe.Pointer(&m))
// 	mapdelete(urv.typ, rv2ptr(urv), kptr)
// }

// return an addressable reflect value that can be used in mapRange and mapGet operations.
//
// all calls to mapGet or mapRange will call here to get an addressable reflect.Value.
func mapAddrLoopvarRV(t reflect.Type, k reflect.Kind) (rv reflect.Value) {
	// return rvZeroAddrK(t, k)
	urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	urv.flag = uintptr(k) | unsafeFlagIndir | unsafeFlagAddr
	urv.typ = ((*unsafeIntf)(unsafe.Pointer(&t))).ptr
	// since we always set the ptr when helperUnsafeDirectAssignMapEntry=true,
	// we should only allocate if it is not true
	if !helperUnsafeDirectAssignMapEntry {
		urv.ptr = unsafeNew(urv.typ)
	}
	return
}

// ---------- ENCODER optimized ---------------

func (e *Encoder) jsondriver() *jsonEncDriver {
	return (*jsonEncDriver)((*unsafeIntf)(unsafe.Pointer(&e.e)).ptr)
}

func (d *Decoder) zerocopystate() bool {
	return d.decByteState == decByteStateZerocopy && d.h.ZeroCopy
}

func (d *Decoder) stringZC(v []byte) (s string) {
	if d.zerocopystate() {
		return stringView(v)
	}
	return d.string(v)
}

func (d *Decoder) mapKeyString(callFnRvk *bool, kstrbs, kstr2bs *[]byte) string {
	if !d.zerocopystate() {
		*callFnRvk = true
		if d.decByteState == decByteStateReuseBuf {
			*kstrbs = append((*kstrbs)[:0], (*kstr2bs)...)
			*kstr2bs = *kstrbs
		}
	}
	return stringView(*kstr2bs)
}

// ---------- DECODER optimized ---------------

func (d *Decoder) checkBreak() bool {
	// MARKER: jsonDecDriver.CheckBreak() costs over 80, and this isn't inlined.
	// Consequently, there's no benefit in incurring the cost of this
	// wrapping function checkBreak.
	//
	// It is faster to just call the interface method directly.

	// if d.js {
	// 	return d.jsondriver().CheckBreak()
	// }
	// if d.cbor {
	// 	return d.cbordriver().CheckBreak()
	// }
	return d.d.CheckBreak()
}

func (d *Decoder) jsondriver() *jsonDecDriver {
	return (*jsonDecDriver)((*unsafeIntf)(unsafe.Pointer(&d.d)).ptr)
}

// ---------- structFieldInfo optimized ---------------

func (n *structFieldInfoPathNode) rvField(v reflect.Value) (rv reflect.Value) {
	// we already know this is exported, and maybe embedded (based on what si says)
	uv := (*unsafeReflectValue)(unsafe.Pointer(&v))
	urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
	// clear flagEmbedRO if necessary, and inherit permission bits from v
	urv.flag = uv.flag&(unsafeFlagStickyRO|unsafeFlagIndir|unsafeFlagAddr) | uintptr(n.kind)
	urv.typ = ((*unsafeIntf)(unsafe.Pointer(&n.typ))).ptr
	urv.ptr = unsafe.Pointer(uintptr(uv.ptr) + uintptr(n.offset))
	return
}

// runtime chan and map are designed such that the first field is the count.
// len builtin uses this to get the length of a chan/map easily.
// leverage this knowledge, since maplen and chanlen functions from runtime package
// are go:linkname'd here, and thus not inlined as of go1.16beta

func len_map_chan(m unsafe.Pointer) int {
	if m == nil {
		return 0
	}
	return *((*int)(m))
}

func len_map(m unsafe.Pointer) int {
	// return maplen(m)
	return len_map_chan(m)
}
func len_chan(m unsafe.Pointer) int {
	// return chanlen(m)
	return len_map_chan(m)
}

func unsafeNew(typ unsafe.Pointer) unsafe.Pointer {
	return mallocgc(rtsize2(typ), typ, true)
}

// ---------- go linknames (LINKED to runtime/reflect) ---------------

// MARKER: always check that these linknames match subsequent versions of go
//
// Note that as of Jan 2021 (go 1.16 release), go:linkname(s) are not inlined
// outside of the standard library use (e.g. within sync, reflect, etc).
// If these link'ed functions were normally inlined, calling them here would
// not necessarily give a performance boost, due to function overhead.
//
// However, it seems most of these functions are not inlined anyway,
// as only maplen, chanlen and mapaccess are small enough to get inlined.
//
//   We checked this by going into $GOROOT/src/runtime and running:
//   $ go build -tags codec.notfastpath -gcflags "-m=2"

// reflect.{unsafe_New, unsafe_NewArray} are not supported in gollvm,
// failing with "error: undefined reference" error.
// however, runtime.{mallocgc, newarray} are supported, so use that instead.

//go:linkname mallocgc runtime.mallocgc
//go:noescape
func mallocgc(size uintptr, typ unsafe.Pointer, needzero bool) unsafe.Pointer

//go:linkname newarray runtime.newarray
//go:noescape
func newarray(typ unsafe.Pointer, n int) unsafe.Pointer

//go:linkname mapiterinit runtime.mapiterinit
//go:noescape
func mapiterinit(typ unsafe.Pointer, m unsafe.Pointer, it unsafe.Pointer)

//go:linkname mapiternext runtime.mapiternext
//go:noescape
func mapiternext(it unsafe.Pointer) (key unsafe.Pointer)

//go:linkname mapdelete runtime.mapdelete
//go:noescape
func mapdelete(typ unsafe.Pointer, m unsafe.Pointer, key unsafe.Pointer)

//go:linkname mapassign runtime.mapassign
//go:noescape
func mapassign(typ unsafe.Pointer, m unsafe.Pointer, key unsafe.Pointer) unsafe.Pointer

//go:linkname mapaccess2 runtime.mapaccess2
//go:noescape
func mapaccess2(typ unsafe.Pointer, m unsafe.Pointer, key unsafe.Pointer) (val unsafe.Pointer, ok bool)

// reflect.typed{memmove, memclr, slicecopy} will handle checking if the type has pointers or not,
// and if a writeBarrier is needed, before delegating to the right method in the runtime.
//
// This is why we use the functions in reflect, and not the ones in runtime directly.
// Calling runtime.XXX here will lead to memory issues.

//go:linkname typedslicecopy reflect.typedslicecopy
//go:noescape
func typedslicecopy(elemType unsafe.Pointer, dst, src unsafeSlice) int

//go:linkname typedmemmove reflect.typedmemmove
//go:noescape
func typedmemmove(typ unsafe.Pointer, dst, src unsafe.Pointer)

//go:linkname typedmemclr reflect.typedmemclr
//go:noescape
func typedmemclr(typ unsafe.Pointer, dst unsafe.Pointer)


text:
package org.apache.cassandra.net.async;

import org.junit.BeforeClass;
import org.junit.Test;

import org.apache.cassandra.config.DatabaseDescriptor;
import org.apache.cassandra.net.MessagingService;

public class OutboundConnectionParamsTest
{
    static int version;

    @BeforeClass
    public static void before()
    {
        DatabaseDescriptor.daemonInitialization();
        version = MessagingService.current_version;
    }

    @Test (expected = IllegalArgumentException.class)
    public void build_SendSizeLessThanZero()
    {
        OutboundConnectionParams.builder().protocolVersion(version).sendBufferSize(-1).build();
    }

    @Test (expected = IllegalArgumentException.class)
    public void build_SendSizeHuge()
    {
        OutboundConnectionParams.builder().protocolVersion(version).sendBufferSize(1 << 30).build();
    }

    @Test (expected = IllegalArgumentException.class)
    public void build_TcpConnectTimeoutLessThanZero()
    {
        OutboundConnectionParams.builder().protocolVersion(version).tcpConnectTimeoutInMS(-1).build();
    }

    @Test (expected = IllegalArgumentException.class)
    public void build_TcpUserTimeoutLessThanZero()
    {
        OutboundConnectionParams.builder().protocolVersion(version).tcpUserTimeoutInMS(-1).build();
    }

    @Test
    public void build_TcpUserTimeoutEqualsZero()
    {
        OutboundConnectionParams.builder().protocolVersion(version).tcpUserTimeoutInMS(0).build();
    }
}

redpajama_set_name: RedPajamaGithub


text:
'use strict'

angular.module('lmisChromeApp')
  .config(function ($stateProvider) {
    $stateProvider
      .state('appConfig', {
        parent: 'root.index',
        abstract: true,
        templateUrl: 'views/home/index.html'
      })
  })


text:
class RemoteReposController < ApplicationController
  layout 'admin'

  before_filter :require_admin

  def refetch
    Workers::RemoteRepoFetcher.defer(remote_repo.id, :refetch)
    flash[:notice] = l(:notice_refetch_started)
    redirect_to remote_repo_sites_path
  end

  private

  def remote_repo_site
    @remote_repo_site ||= RemoteRepoSite.find(params[:remote_repo_site_id])
  end

  def remote_repo
    @remote_repo ||= remote_repo_site.repos.find(params[:id])
  end
end


text:
package org.openbaton.integration.test.interfaces;

import java.io.FileNotFoundException;
import org.openbaton.catalogue.nfvo.Action;
import org.openbaton.catalogue.nfvo.EventEndpoint;
import org.openbaton.integration.test.exceptions.SubscriptionException;
import org.openbaton.sdk.api.exception.SDKException;

/** Created by mob on 31.07.15. */
public interface WaiterInterface {
  void subscribe(EventEndpoint endpoint)
      throws SubscriptionException, SDKException, FileNotFoundException;

  void unSubscribe() throws SDKException, FileNotFoundException;

  boolean waitForEvent(int timeOut) throws InterruptedException;

  Action getAction();

  String getPayload();
}


text:
package org.apache.hadoop.hdfs.server.namenode;

import java.net.InetSocketAddress;
import java.util.*;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import org.apache.hadoop.net.NetUtils;

class Host2NodesMap {
  private HashMap<String, DatanodeDescriptor[]> map
    = new HashMap<String, DatanodeDescriptor[]>();
  private Random r = new Random();
  private ReadWriteLock hostmapLock = new ReentrantReadWriteLock();

  /** Check if node is already in the map. */
  boolean contains(DatanodeDescriptor node) {
    if (node==null) {
      return false;
    }

    String host = node.getHost();
    hostmapLock.readLock().lock();
    try {
      DatanodeDescriptor[] nodes = map.get(host);
      if (nodes != null) {
        for(DatanodeDescriptor containedNode:nodes) {
          if (node==containedNode) {
            return true;
          }
        }
      }
    } finally {
      hostmapLock.readLock().unlock();
    }
    return false;
  }

  /** add node to the map
   * return true if the node is added; false otherwise.
   */
  boolean add(DatanodeDescriptor node) {
    hostmapLock.writeLock().lock();
    try {
      if (node==null || contains(node)) {
        return false;
      }

      String host = node.getHost();
      DatanodeDescriptor[] nodes = map.get(host);
      DatanodeDescriptor[] newNodes;
      if (nodes==null) {
        newNodes = new DatanodeDescriptor[1];
        newNodes[0]=node;
      } else { // rare case: more than one datanode on the host
        newNodes = new DatanodeDescriptor[nodes.length+1];
        System.arraycopy(nodes, 0, newNodes, 0, nodes.length);
        newNodes[nodes.length] = node;
      }
      map.put(host, newNodes);
      return true;
    } finally {
      hostmapLock.writeLock().unlock();
    }
  }

  /** remove node from the map
   * return true if the node is removed; false otherwise.
   */
  boolean remove(DatanodeDescriptor node) {
    if (node==null) {
      return false;
    }

    String host = node.getHost();
    hostmapLock.writeLock().lock();
    try {

      DatanodeDescriptor[] nodes = map.get(host);
      if (nodes==null) {
        return false;
      }
      if (nodes.length==1) {
        if (nodes[0]==node) {
          map.remove(host);
          return true;
        } else {
          return false;
        }
      }
      //rare case
      int i=0;
      for(; i<nodes.length; i++) {
        if (nodes[i]==node) {
          break;
        }
      }
      if (i==nodes.length) {
        return false;
      } else {
        DatanodeDescriptor[] newNodes;
        newNodes = new DatanodeDescriptor[nodes.length-1];
        System.arraycopy(nodes, 0, newNodes, 0, i);
        System.arraycopy(nodes, i+1, newNodes, i, nodes.length-i-1);
        map.put(host, newNodes);
        return true;
      }
    } finally {
      hostmapLock.writeLock().unlock();
    }
  }

  /** get a data node by its host.
   * @deprecated use {@link #getDataNodeByIpPort(String)}
   * @return DatanodeDescriptor if found; otherwise null.
   */
  @Deprecated
  DatanodeDescriptor getDatanodeByHost(String host) {
    if (host==null) {
      return null;
    }

    hostmapLock.readLock().lock();
    try {
      DatanodeDescriptor[] nodes = map.get(host);
      // no entry
      if (nodes== null) {
        return null;
      }
      // one node
      if (nodes.length == 1) {
        return nodes[0];
      }
      // more than one node
      return nodes[r.nextInt(nodes.length)];
    } finally {
      hostmapLock.readLock().unlock();
    }
  }

  DatanodeDescriptor getDataNodeByIpPort(String ipPort) {
    if (ipPort == null) {
      return null;
    }
    hostmapLock.readLock().lock();
    try {
      InetSocketAddress address = NetUtils.createSocketAddr(ipPort);
      DatanodeDescriptor[] all = map.get(address.getAddress().getHostAddress());
      if (all != null) {
        for (DatanodeDescriptor aDatanode : all) {
          if (aDatanode.getPort() == address.getPort()) {
            return aDatanode;
          }
        }
      }
      return null;
    } finally {
      hostmapLock.readLock().unlock();
    }
  }

  /**
   * Find data node by its name.
   *
   * @deprecated use {@link #getDataNodeByIpPort(String)}
   * @return DatanodeDescriptor if found or null otherwise
   */
  @Deprecated
  public DatanodeDescriptor getDatanodeByName(String name) {
    if (name == null) {
      return null;
    }
    int colon = name.indexOf(":");
    String host;
    if (colon < 0) {
      host = name;
    } else {
      host = name.substring(0, colon);
    }
    hostmapLock.readLock().lock();
    try {
      DatanodeDescriptor[] nodes = map.get(host);
      // no entry
      if (nodes == null) {
        return null;
      }
      for (DatanodeDescriptor containedNode : nodes) {
        if (name.equals(containedNode.getName())) {
          return containedNode;
        }
      }
      return null;
    } finally {
      hostmapLock.readLock().unlock();
    }
  }
}


text:
 Improve this Doc

Error: $interpolate:dupvalue

Duplicate choice in plural/select

The choice "{0}" is specified more than once. Duplicate key is at line {1}, column {2} in text "{3}"

Description

You have repeated a match selection for your plural or select MessageFormat
        extension in your interpolation expression. The different choices have to be unique.

For more information about the MessageFormat syntax in interpolation
        expressions, please refer to MessageFormat extensions section at
        Angular i18n MessageFormat


text:
"""
Created on Dec 20, 2015

@author: sohara
"""
import shapely.geometry as sg
import pyvision3 as pv3

def annotation_demo():
    """
    Demonstrates several annotation methods of the Image class
    """
    img = pv3.Image(pv3.IMG_DRIVEWAY, desc="Annotations Demo")  # load a sample image

    # annotate a point as a small filled circle
    img.annotate_point((250, 250), color=pv3.RGB_BLACK)
    img.annotate_point((300, 300))  # default color is red

    # annotate text next to the brown point, blue on white background
    img.annotate_text(
        "Waldo was here", (254, 246), color=pv3.RGB_BLUE, bg_color=pv3.RGB_WHITE
    )

    # complex polygon with an exterior and an interior 'hole'
    exterior = ((50, 50), (25, 125), (50, 200), (200, 200), (200, 50), (50, 50))
    interior = ((100, 100), (100, 150), (150, 150), (175, 125), (150, 100), (100, 100))
    poly = sg.Polygon(exterior, [interior])
    img.annotate_shape(poly, color=pv3.RGB_CYAN, fill_color=pv3.RGB_GREEN, thickness=2)

    # two concentric circles
    img.annotate_circle((400, 400), 75, color=pv3.RGB_PURPLE, thickness=5)
    img.annotate_circle(
        (400, 400), 60, color=pv3.RGB_PURPLE, thickness=-1
    )  # filled circle

    # a LineString
    lines = sg.LineString(((10, 25), (382, 122), (251, 470)))
    img.annotate_shape(lines, thickness=4, color=pv3.RGB_RED)

    # a Rectangle
    img.annotate_rect(
        (538, 212), (600, 300), color=pv3.RGB_ORANGE, thickness=3
    )  # -1 for filled

    # show the image with annotations
    # NOTE: to NOT show the annotations, set annotations=False in the following
    # img.show_annotation(window_title="Mask Layer", highgui=True, delay=1)
    img.show(
        window_title="Annotated Image", highgui=True, delay=0, annotations_opacity=0.5
    )

if __name__ == "__main__":
    print("====================================================")
    print("Set focus to display window and hit any key to exit.")
    print("====================================================")
    annotation_demo()


text:
#ifndef __HW_USB_H__
#define __HW_USB_H__

//*****************************************************************************
//
// The following are defines for the USB register offsets.
//
//*****************************************************************************
#define USB_ADDR                0x40089000  // Function address
#define USB_POW                 0x40089004  // Power management and control
                                            // register
#define USB_IIF                 0x40089008  // Interrupt flags for endpoint 0
                                            // and IN endpoints 1-5
#define USB_OIF                 0x40089010  // Interrupt flags for OUT
                                            // endpoints 1-5
#define USB_CIF                 0x40089018  // Common USB interrupt flags
#define USB_IIE                 0x4008901C  // Interrupt enable mask for IN
                                            // endpoints 1-5 and endpoint 0
#define USB_OIE                 0x40089024  // Interrupt enable mask for OUT
                                            // endpoints 1-5
#define USB_CIE                 0x4008902C  // Common USB interrupt enable
                                            // mask
#define USB_FRML                0x40089030  // Frame number (low byte)
#define USB_FRMH                0x40089034  // Frame number (high byte)
#define USB_INDEX               0x40089038  // Index register for selecting
                                            // the endpoint status and control
                                            // registers
#define USB_CTRL                0x4008903C  // USB peripheral control register
#define USB_MAXI                0x40089040  // Indexed register: For USB_INDEX
                                            // = 1-5: Maximum packet size for
                                            // IN endpoint {1-5}
#define USB_CS0_CSIL            0x40089044  // Indexed register: For USB_INDEX
                                            // = 0: Endpoint 0 control and
                                            // status For USB_INDEX = 1-5: IN
                                            // endpoint {1-5} control and
                                            // status (low byte)
#define USB_CSIH                0x40089048  // Indexed register: For USB_INDEX
                                            // = 1-5: IN endpoint {1-5} control
                                            // and status (high byte)
#define USB_MAXO                0x4008904C  // Indexed register: For USB_INDEX
                                            // = 1-5: Maximum packet size for
                                            // OUT endpoint {1-5}
#define USB_CSOL                0x40089050  // Indexed register: For USB_INDEX
                                            // = 1-5: OUT endpoint {1-5}
                                            // control and status (low byte)
#define USB_CSOH                0x40089054  // Indexed register: For USB_INDEX
                                            // = 1-5: OUT endpoint {1-5}
                                            // control and status (high byte)
#define USB_CNT0_CNTL           0x40089058  // Indexed register: For USB_INDEX
                                            // = 0: Number of received bytes in
                                            // the endpoint 0 FIFO For
                                            // USB_INDEX = 1-5: Number of
                                            // received bytes in the OUT
                                            // endpoint {1-5} FIFO (low byte)
#define USB_CNTH                0x4008905C  // Indexed register: For USB_INDEX
                                            // = 1-5: Number of received in the
                                            // OUT endpoint {1-5} FIFO (high
                                            // byte)
#define USB_F0                  0x40089080  // Endpoint 0 FIFO
#define USB_F1                  0x40089088  // IN/OUT endpoint 1 FIFO
#define USB_F2                  0x40089090  // IN/OUT endpoint 2 FIFO
#define USB_F3                  0x40089098  // IN/OUT endpoint 3 FIFO
#define USB_F4                  0x400890A0  // IN/OUT endpoint 4 FIFO
#define USB_F5                  0x400890A8  // IN/OUT endpoint 5 FIFO

//*****************************************************************************
//
// The following are defines for the bit fields in the USB_ADDR register.
//
//*****************************************************************************
#define USB_ADDR_UPDATE         0x00000080  // This bit is set by hardware
                                            // when writing to this register,
                                            // and is cleared by hardware when
                                            // the new address becomes
                                            // effective.
#define USB_ADDR_UPDATE_M       0x00000080
#define USB_ADDR_UPDATE_S       7
#define USB_ADDR_USBADDR_M      0x0000007F  // Device address. The address
                                            // shall be updated upon successful
                                            // completion of the status stage
                                            // of the SET_ADDRESS request.
#define USB_ADDR_USBADDR_S      0
//*****************************************************************************
//
// The following are defines for the bit fields in the USB_POW register.
//
//*****************************************************************************
#define USB_POW_ISOWAITSOF      0x00000080  // For isochronous mode IN
                                            // endpoints: When set, the USB
                                            // controller will wait for an SOF
                                            // token from the time
                                            // USB_CSIL.INPKTRDY is set before
                                            // sending the packet. If an IN
                                            // token is received before an SOF
                                            // token, then a zero length data
                                            // packet will be sent.
#define USB_POW_ISOWAITSOF_M    0x00000080
#define USB_POW_ISOWAITSOF_S    7
#define USB_POW_RST             0x00000008  // Indicates that reset signaling
                                            // is present on the bus
#define USB_POW_RST_M           0x00000008
#define USB_POW_RST_S           3
#define USB_POW_RESUME          0x00000004  // Drives resume signaling for
                                            // remote wakeup According to the
                                            // USB Specification, the resume
                                            // signal must be held active for
                                            // at least 1 ms and no more than
                                            // 15 ms. It is recommended to keep
                                            // this bit set for approximately
                                            // 10 ms.
#define USB_POW_RESUME_M        0x00000004
#define USB_POW_RESUME_S        2
#define USB_POW_SUSPEND         0x00000002  // Indicates entry into suspend
                                            // mode Suspend mode must be
                                            // enabled by setting
                                            // USB_POW.SUSPENDEN Software
                                            // clears this bit by reading the
                                            // USB_CIF register or by asserting
                                            // USB_POW.RESUME
#define USB_POW_SUSPEND_M       0x00000002
#define USB_POW_SUSPEND_S       1
#define USB_POW_SUSPENDEN       0x00000001  // Enables detection of and entry
                                            // into suspend mode.
#define USB_POW_SUSPENDEN_M     0x00000001
#define USB_POW_SUSPENDEN_S     0
//*****************************************************************************
//
// The following are defines for the bit fields in the USB_IIF register.
//
//*****************************************************************************
#define USB_IIF_INEP5IF         0x00000020  // Interrupt flag for IN endpoint
                                            // 5 Cleared by hardware when read
#define USB_IIF_INEP5IF_M       0x00000020
#define USB_IIF_INEP5IF_S       5
#define USB_IIF_INEP4IF         0x00000010  // Interrupt flag for IN endpoint
                                            // 4 Cleared by hardware when read
#define USB_IIF_INEP4IF_M       0x00000010
#define USB_IIF_INEP4IF_S       4
#define USB_IIF_INEP3IF         0x00000008  // Interrupt flag for IN endpoint
                                            // 3 Cleared by hardware when read
#define USB_IIF_INEP3IF_M       0x00000008
#define USB_IIF_INEP3IF_S       3
#define USB_IIF_INEP2IF         0x00000004  // Interrupt flag for IN endpoint
                                            // 2 Cleared by hardware when read
#define USB_IIF_INEP2IF_M       0x00000004
#define USB_IIF_INEP2IF_S       2
#define USB_IIF_INEP1IF         0x00000002  // Interrupt flag for IN endpoint
                                            // 1 Cleared by hardware when read
#define USB_IIF_INEP1IF_M       0x00000002
#define USB_IIF_INEP1IF_S       1
#define USB_IIF_EP0IF           0x00000001  // Interrupt flag for endpoint 0
                                            // Cleared by hardware when read
#define USB_IIF_EP0IF_M         0x00000001
#define USB_IIF_EP0IF_S         0
//*****************************************************************************
//
// The following are defines for the bit fields in the USB_OIF register.
//
//*****************************************************************************
#define USB_OIF_OUTEP5IF        0x00000020  // Interrupt flag for OUT endpoint
                                            // 5 Cleared by hardware when read
#define USB_OIF_OUTEP5IF_M      0x00000020
#define USB_OIF_OUTEP5IF_S      5
#define USB_OIF_OUTEP4IF        0x00000010  // Interrupt flag for OUT endpoint
                                            // 4 Cleared by hardware when read
#define USB_OIF_OUTEP4IF_M      0x00000010
#define USB_OIF_OUTEP4IF_S      4
#define USB_OIF_OUTEP3IF        0x00000008  // Interrupt flag for OUT endpoint
                                            // 3 Cleared by hardware when read
#define USB_OIF_OUTEP3IF_M      0x00000008
#define USB_OIF_OUTEP3IF_S      3
#define USB_OIF_OUTEP2IF        0x00000004  // Interrupt flag for OUT endpoint
                                            // 2 Cleared by hardware when read
#define USB_OIF_OUTEP2IF_M      0x00000004
#define USB_OIF_OUTEP2IF_S      2
#define USB_OIF_OUTEP1IF        0x00000002  // Interrupt flag for OUT endpoint
                                            // 1 Cleared by hardware when read
#define USB_OIF_OUTEP1IF_M      0x00000002
#define USB_OIF_OUTEP1IF_S      1
//*****************************************************************************
//
// The following are defines for the bit fields in the USB_CIF register.
//
//*****************************************************************************
#define USB_CIF_SOFIF           0x00000008  // Start-of-frame interrupt flag
                                            // Cleared by hardware when read
#define USB_CIF_SOFIF_M         0x00000008
#define USB_CIF_SOFIF_S         3
#define USB_CIF_RSTIF           0x00000004  // Reset interrupt flag Cleared by
                                            // hardware when read
#define USB_CIF_RSTIF_M         0x00000004
#define USB_CIF_RSTIF_S         2
#define USB_CIF_RESUMEIF        0x00000002  // Resume interrupt flag Cleared
                                            // by hardware when read
#define USB_CIF_RESUMEIF_M      0x00000002
#define USB_CIF_RESUMEIF_S      1
#define USB_CIF_SUSPENDIF       0x00000001  // Suspend interrupt flag Cleared
                                            // by hardware when read
#define USB_CIF_SUSPENDIF_M     0x00000001
#define USB_CIF_SUSPENDIF_S     0
//*****************************************************************************
//
// The following are defines for the bit fields in the USB_IIE register.
//
//*****************************************************************************
#define USB_IIE_INEP5IE         0x00000020  // Interrupt enable for IN
                                            // endpoint 5 0: Interrupt disabled
                                            // 1: Interrupt enabled
#define USB_IIE_INEP5IE_M       0x00000020
#define USB_IIE_INEP5IE_S       5
#define USB_IIE_INEP4IE         0x00000010  // Interrupt enable for IN
                                            // endpoint 4 0: Interrupt disabled
                                            // 1: Interrupt enabled
#define USB_IIE_INEP4IE_M       0x00000010
#define USB_IIE_INEP4IE_S       4
#define USB_IIE_INEP3IE         0x00000008  // Interrupt enable for IN
                                            // endpoint 3 0: Interrupt disabled
                                            // 1: Interrupt enabled
#define USB_IIE_INEP3IE_M       0x00000008
#define USB_IIE_INEP3IE_S       3
#define USB_IIE_INEP2IE         0x00000004  // Interrupt enable for IN
                                            // endpoint 2 0: Interrupt disabled
                                            // 1: Interrupt enabled
#define USB_IIE_INEP2IE_M       0x00000004
#define USB_IIE_INEP2IE_S       2
#define USB_IIE_INEP1IE         0x00000002  // Interrupt enable for IN
                                            // endpoint 1 0: Interrupt disabled
                                            // 1: Interrupt enabled
#define USB_IIE_INEP1IE_M       0x00000002
#define USB_IIE_INEP1IE_S       1
#define USB_IIE_EP0IE           0x00000001  // Interrupt enable for endpoint 0
                                            // 0: Interrupt disabled 1:
                                            // Interrupt enabled
#define USB_IIE_EP0IE_M         0x00000001
#define USB_IIE_EP0IE_S         0
//*****************************************************************************
//
// The following are defines for the bit fields in the USB_OIE register.
//
//*****************************************************************************
#define USB_OIE_reserved8_M     0x000000C0  // Reserved
#define USB_OIE_reserved8_S     6
#define USB_OIE_OUTEP5IE        0x00000020  // Interrupt enable for OUT
                                            // endpoint 5 0: Interrupt disabled
                                            // 1: Interrupt enabled
#define USB_OIE_OUTEP5IE_M      0x00000020
#define USB_OIE_OUTEP5IE_S      5
#define USB_OIE_OUTEP4IE        0x00000010  // Interrupt enable for OUT
                                            // endpoint 4 0: Interrupt disabled
                                            // 1: Interrupt enabled
#define USB_OIE_OUTEP4IE_M      0x00000010
#define USB_OIE_OUTEP4IE_S      4
#define USB_OIE_OUTEP3IE        0x00000008  // Interrupt enable for OUT
                                            // endpoint 3 0: Interrupt disabled
                                            // 1: Interrupt enabled
#define USB_OIE_OUTEP3IE_M      0x00000008
#define USB_OIE_OUTEP3IE_S      3
#define USB_OIE_OUTEP2IE        0x00000004  // Interrupt enable for OUT
                                            // endpoint 2 0: Interrupt disabled
                                            // 1: Interrupt enabled
#define USB_OIE_OUTEP2IE_M      0x00000004
#define USB_OIE_OUTEP2IE_S      2
#define USB_OIE_OUTEP1IE        0x00000002  // Interrupt enable for OUT
                                            // endpoint 1 0: Interrupt disabled
                                            // 1: Interrupt enabled
#define USB_OIE_OUTEP1IE_M      0x00000002
#define USB_OIE_OUTEP1IE_S      1
//*****************************************************************************
//
// The following are defines for the bit fields in the USB_CIE register.
//
//*****************************************************************************
#define USB_CIE_SOFIE           0x00000008  // Start-of-frame interrupt enable
                                            // 0: Interrupt disabled 1:
                                            // Interrupt enabled
#define USB_CIE_SOFIE_M         0x00000008
#define USB_CIE_SOFIE_S         3
#define USB_CIE_RSTIE           0x00000004  // Reset interrupt enable 0:
                                            // Interrupt disabled 1: Interrupt
                                            // enabled
#define USB_CIE_RSTIE_M         0x00000004
#define USB_CIE_RSTIE_S         2
#define USB_CIE_RESUMEIE        0x00000002  // Resume interrupt enable 0:
                                            // Interrupt disabled 1: Interrupt
                                            // enabled
#define USB_CIE_RESUMEIE_M      0x00000002
#define USB_CIE_RESUMEIE_S      1
#define USB_CIE_SUSPENDIE       0x00000001  // Suspend interrupt enable 0:
                                            // Interrupt disabled 1: Interrupt
                                            // enabled
#define USB_CIE_SUSPENDIE_M     0x00000001
#define USB_CIE_SUSPENDIE_S     0
//*****************************************************************************
//
// The following are defines for the bit fields in the USB_FRML register.
//
//*****************************************************************************
#define USB_FRML_FRAMEL_M       0x000000FF  // Bits 7:0 of the 11-bit frame
                                            // number The frame number is only
                                            // updated upon successful
                                            // reception of SOF tokens
#define USB_FRML_FRAMEL_S       0
//*****************************************************************************
//
// The following are defines for the bit fields in the USB_FRMH register.
//
//*****************************************************************************
#define USB_FRMH_FRAMEH_M       0x00000007  // Bits 10:8 of the 11-bit frame
                                            // number The frame number is only
                                            // updated upon successful
                                            // reception of SOF tokens
#define USB_FRMH_FRAMEH_S       0
//*****************************************************************************
//
// The following are defines for the bit fields in the USB_INDEX register.
//
//*****************************************************************************
#define USB_INDEX_USBINDEX_M    0x0000000F  // Index of the currently selected
                                            // endpoint The index is set to 0
                                            // to enable access to endpoint 0
                                            // control and status registers The
                                            // index is set to 1, 2, 3, 4 or 5
                                            // to enable access to IN/OUT
                                            // endpoint 1, 2, 3, 4 or 5 control
                                            // and status registers,
                                            // respectively
#define USB_INDEX_USBINDEX_S    0
//*****************************************************************************
//
// The following are defines for the bit fields in the USB_CTRL register.
//
//*****************************************************************************
#define USB_CTRL_PLLLOCKED      0x00000080  // PLL lock status. The PLL is
                                            // locked when USB_CTRL.PLLLOCKED
                                            // is 1.
#define USB_CTRL_PLLLOCKED_M    0x00000080
#define USB_CTRL_PLLLOCKED_S    7
#define USB_CTRL_PLLEN          0x00000002  // 48 MHz USB PLL enable When this
                                            // bit is set, the 48 MHz PLL is
                                            // started. Software must avoid
                                            // access to other USB registers
                                            // before the PLL has locked; that
                                            // is, USB_CTRL.PLLLOCKED is 1.
                                            // This bit can be set only when
                                            // USB_CTRL.USBEN is 1. The PLL
                                            // must be disabled before entering
                                            // PM1 when suspended, and must be
                                            // re-enabled when resuming
                                            // operation.
#define USB_CTRL_PLLEN_M        0x00000002
#define USB_CTRL_PLLEN_S        1
#define USB_CTRL_USBEN          0x00000001  // USB enable The USB controller
                                            // is reset when this bit is
                                            // cleared
#define USB_CTRL_USBEN_M        0x00000001
#define USB_CTRL_USBEN_S        0
//*****************************************************************************
//
// The following are defines for the bit fields in the USB_MAXI register.
//
//*****************************************************************************
#define USB_MAXI_USBMAXI_M      0x000000FF  // Maximum packet size, in units
                                            // of 8 bytes, for the selected IN
                                            // endpoint The value of this
                                            // register should match the
                                            // wMaxPacketSize field in the
                                            // standard endpoint descriptor for
                                            // the endpoint. The value must not
                                            // exceed the available memory.
#define USB_MAXI_USBMAXI_S      0
//*****************************************************************************
//
// The following are defines for the bit fields in the USB_CS0_CSIL register.
//
//*****************************************************************************
#define USB_CS0_CSIL_CLROUTPKTRDY_or_CLRDATATOG \
                                0x00000040  // USB_CS0.CLROUTPKTRDY [RW]:
                                            // Software sets this bit to clear
                                            // the USB_CS0.OUTPKTRDY bit. It is
                                            // cleared automatically.
                                            // USB_CSIL.CLRDATATOG [RW]:
                                            // Software sets this bit to reset
                                            // the IN endpoint data toggle to
                                            // 0.

#define USB_CS0_CSIL_CLROUTPKTRDY_or_CLRDATATOG_M \
                                0x00000040
#define USB_CS0_CSIL_CLROUTPKTRDY_or_CLRDATATOG_S 6
#define USB_CS0_CSIL_SENDSTALL_or_SENTSTALL \
                                0x00000020  // USB_CS0.SENDSTALL [RW]:
                                            // Software sets this bit to
                                            // terminate the current
                                            // transaction with a STALL
                                            // handshake. The bit is cleared
                                            // automatically when the STALL
                                            // handshake has been transmitted.
                                            // USB_CSIL.SENTSTALL [RW]: For
                                            // bulk/interrupt mode IN
                                            // endpoints: This bit is set when
                                            // a STALL handshake is
                                            // transmitted. The FIFO is flushed
                                            // and the USB_CSIL.INPKTRDY bit
                                            // cleared. Software should clear
                                            // this bit.

#define USB_CS0_CSIL_SENDSTALL_or_SENTSTALL_M \
                                0x00000020
#define USB_CS0_CSIL_SENDSTALL_or_SENTSTALL_S 5
#define USB_CS0_CSIL_SETUPEND_or_SENDSTALL \
                                0x00000010  // USB_CS0.SETUPEND [RO]: This bit
                                            // is set when a control
                                            // transaction ends before the
                                            // USB_CS0.DATAEND bit has been
                                            // set. An interrupt is generated
                                            // and the FIFO flushed at this
                                            // time. Software clears this bit
                                            // by setting USB_CS0.CLRSETUPEND.
                                            // CSIL.SENDSTALL [RW]: For
                                            // bulk/interrupt mode IN
                                            // endpoints: Software sets this
                                            // bit to issue a STALL handshake.
                                            // Software clears this bit to
                                            // terminate the stall condition.

#define USB_CS0_CSIL_SETUPEND_or_SENDSTALL_M \
                                0x00000010
#define USB_CS0_CSIL_SETUPEND_or_SENDSTALL_S 4
#define USB_CS0_CSIL_DATAEND_or_FLUSHPACKET \
                                0x00000008  // USB_CS0.DATAEND [RW]: This bit
                                            // is used to signal the end of the
                                            // data stage, and must be set: 1.
                                            // When the last data packet is
                                            // loaded and USB_CS0.INPKTRDY is
                                            // set. 2. When the last data
                                            // packet is unloaded and
                                            // USB_CS0.CLROUTPKTRDY is set. 3.
                                            // When USB_CS0.INPKTRDY is set to
                                            // send a zero-length packet. The
                                            // USB controller clears this bit
                                            // automatically.
                                            // USB_CSIL.FLUSHPACKET [RW]:
                                            // Software sets this bit to flush
                                            // the next packet to be
                                            // transmitted from the IN endpoint
                                            // FIFO. The FIFO pointer is reset
                                            // and the USB_CSIL.INPKTRDY bit is
                                            // cleared. Note: If the FIFO
                                            // contains two packets,
                                            // USB_CSIL.FLUSHPACKET will need
                                            // to be set twice to completely
                                            // clear the FIFO.

#define USB_CS0_CSIL_DATAEND_or_FLUSHPACKET_M \
                                0x00000008
#define USB_CS0_CSIL_DATAEND_or_FLUSHPACKET_S 3
#define USB_CS0_CSIL_SENTSTALL_or_UNDERRUN \
                                0x00000004  // USB_CS0.SENTSTALL [RW]: This
                                            // bit is set when a STALL
                                            // handshake is sent. An interrupt
                                            // is generated is generated when
                                            // this bit is set. Software must
                                            // clear this bit.
                                            // USB_CSIL.UNDERRUN [RW]: In
                                            // isochronous mode, this bit is
                                            // set when a zero length data
                                            // packet is sent after receiving
                                            // an IN token with
                                            // USB_CSIL.INPKTRDY not set. In
                                            // bulk/interrupt mode, this bit is
                                            // set when a NAK is returned in
                                            // response to an IN token.
                                            // Software should clear this bit.

#define USB_CS0_CSIL_SENTSTALL_or_UNDERRUN_M \
                                0x00000004
#define USB_CS0_CSIL_SENTSTALL_or_UNDERRUN_S 2
#define USB_CS0_CSIL_INPKTRDY_or_PKTPRESENT \
                                0x00000002  // USB_CS0. INPKTRDY [RW]:
                                            // Software sets this bit after
                                            // loading a data packet into the
                                            // endpoint 0 FIFO. It is cleared
                                            // automatically when the data
                                            // packet has been transmitted. An
                                            // interrupt is generated when the
                                            // bit is cleared.
                                            // USB_CSIL.PKTPRESENT [RO]: This
                                            // bit is set when there is at
                                            // least one packet in the IN
                                            // endpoint FIFO.

#define USB_CS0_CSIL_INPKTRDY_or_PKTPRESENT_M \
                                0x00000002
#define USB_CS0_CSIL_INPKTRDY_or_PKTPRESENT_S 1
#define USB_CS0_CSIL_OUTPKTRDY_or_INPKTRDY \
                                0x00000001  // USB_CS0.OUTPKTRDY [RO]:
                                            // Endpoint 0 data packet received
                                            // An interrupt request (EP0) is
                                            // generated if the interrupt is
                                            // enabled. Software must read the
                                            // endpoint 0 FIFO empty, and clear
                                            // this bit by setting
                                            // USB_CS0.CLROUTPKTRDY
                                            // USB_CSIL.INPKTRDY [RW]: IN
                                            // endpoint {1-5} packet transfer
                                            // pending Software sets this bit
                                            // after loading a data packet into
                                            // the FIFO. It is cleared
                                            // automatically when a data packet
                                            // has been transmitted. An
                                            // interrupt is generated (if
                                            // enabled) when the bit is
                                            // cleared. When using
                                            // double-buffering, the bit is
                                            // cleared immediately if the other
                                            // FIFO is empty.

#define USB_CS0_CSIL_OUTPKTRDY_or_INPKTRDY_M \
                                0x00000001
#define USB_CS0_CSIL_OUTPKTRDY_or_INPKTRDY_S 0
//*****************************************************************************
//
// The following are defines for the bit fields in the USB_CSIH register.
//
//*****************************************************************************
#define USB_CSIH_AUTISET        0x00000080  // If set by software, the
                                            // USB_CSIL.INPKTRDY bit is
                                            // automatically set when a data
                                            // packet of maximum size
                                            // (specified by USBMAXI) is loaded
                                            // into the IN endpoint FIFO. If a
                                            // packet of less than the maximum
                                            // packet size is loaded, then
                                            // USB_CSIL.INPKTRDY will have to
                                            // be set manually.
#define USB_CSIH_AUTISET_M      0x00000080
#define USB_CSIH_AUTISET_S      7
#define USB_CSIH_ISO            0x00000040  // Selects IN endpoint type: 0:
                                            // Bulk/interrupt 1: Isochronous
#define USB_CSIH_ISO_M          0x00000040
#define USB_CSIH_ISO_S          6
#define USB_CSIH_FORCEDATATOG   0x00000008  // Software sets this bit to force
                                            // the IN endpoint's data toggle to
                                            // switch after each data packet is
                                            // sent regardless of whether an
                                            // ACK was received. This can be
                                            // used by interrupt IN endpoints
                                            // which are used to communicate
                                            // rate feedback for isochronous
                                            // endpoints.
#define USB_CSIH_FORCEDATATOG_M 0x00000008
#define USB_CSIH_FORCEDATATOG_S 3
#define USB_CSIH_INDBLBUF       0x00000001  // IN endpoint FIFO
                                            // double-buffering enable: 0:
                                            // Double buffering disabled 1:
                                            // Double buffering enabled
#define USB_CSIH_INDBLBUF_M     0x00000001
#define USB_CSIH_INDBLBUF_S     0
//*****************************************************************************
//
// The following are defines for the bit fields in the USB_MAXO register.
//
//*****************************************************************************
#define USB_MAXO_USBMAXO_M      0x000000FF  // Maximum packet size, in units
                                            // of 8 bytes, for the selected OUT
                                            // endpoint The value of this
                                            // register should match the
                                            // wMaxPacketSize field in the
                                            // standard endpoint descriptor for
                                            // the endpoint. The value must not
                                            // exceed the available memory.
#define USB_MAXO_USBMAXO_S      0
//*****************************************************************************
//
// The following are defines for the bit fields in the USB_CSOL register.
//
//*****************************************************************************
#define USB_CSOL_CLRDATATOG     0x00000080  // Software sets this bit to reset
                                            // the endpoint data toggle to 0.
#define USB_CSOL_CLRDATATOG_M   0x00000080
#define USB_CSOL_CLRDATATOG_S   7
#define USB_CSOL_SENTSTALL      0x00000040  // This bit is set when a STALL
                                            // handshake is transmitted. An
                                            // interrupt is generated when this
                                            // bit is set. Software should
                                            // clear this bit.
#define USB_CSOL_SENTSTALL_M    0x00000040
#define USB_CSOL_SENTSTALL_S    6
#define USB_CSOL_SENDSTALL      0x00000020  // For bulk/interrupt mode OUT
                                            // endpoints: Software sets this
                                            // bit to issue a STALL handshake.
                                            // Software clears this bit to
                                            // terminate the stall condition.
#define USB_CSOL_SENDSTALL_M    0x00000020
#define USB_CSOL_SENDSTALL_S    5
#define USB_CSOL_FLUSHPACKET    0x00000010  // Software sets this bit to flush
                                            // the next packet to be read from
                                            // the endpoint OUT FIFO. Note: If
                                            // the FIFO contains two packets,
                                            // USB_CSOL.FLUSHPACKET will need
                                            // to be set twice to completely
                                            // clear the FIFO.
#define USB_CSOL_FLUSHPACKET_M  0x00000010
#define USB_CSOL_FLUSHPACKET_S  4
#define USB_CSOL_DATAERROR      0x00000008  // For isochronous mode OUT
                                            // endpoints: This bit is set when
                                            // USB_CSOL.OUTPKTRDY is set if the
                                            // data packet has a CRC or
                                            // bit-stuff error. It is cleared
                                            // automatically when
                                            // USB_CSOL.OUTPKTRDY is cleared.
#define USB_CSOL_DATAERROR_M    0x00000008
#define USB_CSOL_DATAERROR_S    3
#define USB_CSOL_OVERRUN        0x00000004  // For isochronous mode OUT
                                            // endpoints: This bit is set when
                                            // an OUT packet cannot be loaded
                                            // into the OUT endpoint FIFO.
                                            // Firmware should clear this bit.
#define USB_CSOL_OVERRUN_M      0x00000004
#define USB_CSOL_OVERRUN_S      2
#define USB_CSOL_FIFOFULL       0x00000002  // This bit is set when no more
                                            // packets can be loaded into the
                                            // OUT endpoint FIFO.
#define USB_CSOL_FIFOFULL_M     0x00000002
#define USB_CSOL_FIFOFULL_S     1
#define USB_CSOL_OUTPKTRDY      0x00000001  // This bit is set when a data
                                            // packet has been received.
                                            // Software should clear this bit
                                            // when the packet has been
                                            // unloaded from the OUT endpoint
                                            // FIFO. An interrupt is generated
                                            // when the bit is set.
#define USB_CSOL_OUTPKTRDY_M    0x00000001
#define USB_CSOL_OUTPKTRDY_S    0
//*****************************************************************************
//
// The following are defines for the bit fields in the USB_CSOH register.
//
//*****************************************************************************
#define USB_CSOH_AUTOCLEAR      0x00000080  // If software sets this bit, the
                                            // USB_CSOL.OUTPKTRDY bit will be
                                            // automatically cleared when a
                                            // packet of maximum size
                                            // (specified by USB_MAXO) has been
                                            // unloaded from the OUT FIFO. When
                                            // packets of less than the maximum
                                            // packet size are unloaded,
                                            // USB_CSOL.OUTPKTRDY will have to
                                            // be cleared manually.
#define USB_CSOH_AUTOCLEAR_M    0x00000080
#define USB_CSOH_AUTOCLEAR_S    7
#define USB_CSOH_ISO            0x00000040  // Selects OUT endpoint type: 0:
                                            // Bulk/interrupt 1: Isochronous
#define USB_CSOH_ISO_M          0x00000040
#define USB_CSOH_ISO_S          6
#define USB_CSOH_OUTDBLBUF      0x00000001  // OUT endpoint FIFO
                                            // double-buffering enable: 0:
                                            // Double buffering disabled 1:
                                            // Double buffering enabled
#define USB_CSOH_OUTDBLBUF_M    0x00000001
#define USB_CSOH_OUTDBLBUF_S    0
//*****************************************************************************
//
// The following are defines for the bit fields in the
// USB_CNT0_CNTL register.
//
//*****************************************************************************
#define USB_CNT0_CNTL_FIFOCNT_or_FIFOCNTL_M \
                                0x000000FF  // USB_CS0.FIFOCNT (USBINDEX = 0)
                                            // [RO]: Number of bytes received
                                            // in the packet in the endpoint 0
                                            // FIFO Valid only when
                                            // USB_CS0.OUTPKTRDY is set
                                            // USB_CSIL.FIFOCNTL (USBINDEX = 1
                                            // to 5) [RW]: Bits 7:0 of the of
                                            // the number of bytes received in
                                            // the packet in the OUT endpoint
                                            // {1-5} FIFO Valid only when
                                            // USB_CSOL.OUTPKTRDY is set

#define USB_CNT0_CNTL_FIFOCNT_or_FIFOCNTL_S 0
//*****************************************************************************
//
// The following are defines for the bit fields in the USB_CNTH register.
//
//*****************************************************************************
#define USB_CNTH_FIFOCNTH_M     0x00000007  // Bits 10:8 of the of the number
                                            // of bytes received in the packet
                                            // in the OUT endpoint {1-5} FIFO
                                            // Valid only when
                                            // USB_CSOL.OUTPKTRDY is set
#define USB_CNTH_FIFOCNTH_S     0
//*****************************************************************************
//
// The following are defines for the bit fields in the USB_F0 register.
//
//*****************************************************************************
#define USB_F0_USBF0_M          0x000000FF  // Endpoint 0 FIFO Reading this
                                            // register unloads one byte from
                                            // the endpoint 0 FIFO. Writing to
                                            // this register loads one byte
                                            // into the endpoint 0 FIFO. The
                                            // FIFO memory for EP0 is used for
                                            // incoming and outgoing data
                                            // packets.
#define USB_F0_USBF0_S          0
//*****************************************************************************
//
// The following are defines for the bit fields in the USB_F1 register.
//
//*****************************************************************************
#define USB_F1_USBF1_M          0x000000FF  // Endpoint 1 FIFO register
                                            // Reading this register unloads
                                            // one byte from the EP1 OUT FIFO.
                                            // Writing to this register loads
                                            // one byte into the EP1 IN FIFO.
#define USB_F1_USBF1_S          0
//*****************************************************************************
//
// The following are defines for the bit fields in the USB_F2 register.
//
//*****************************************************************************
#define USB_F2_USBF2_M          0x000000FF  // Endpoint 2 FIFO register
                                            // Reading this register unloads
                                            // one byte from the EP2 OUT FIFO.
                                            // Writing to this register loads
                                            // one byte into the EP2 IN FIFO.
#define USB_F2_USBF2_S          0
//*****************************************************************************
//
// The following are defines for the bit fields in the USB_F3 register.
//
//*****************************************************************************
#define USB_F3_USBF3_M          0x000000FF  // Endpoint 3 FIFO register
                                            // Reading this register unloads
                                            // one byte from the EP3 OUT FIFO.
                                            // Writing to this register loads
                                            // one byte into the EP3 IN FIFO.
#define USB_F3_USBF3_S          0
//*****************************************************************************
//
// The following are defines for the bit fields in the USB_F4 register.
//
//*****************************************************************************
#define USB_F4_USBF4_M          0x000000FF  // Endpoint 4 FIFO register
                                            // Reading this register unloads
                                            // one byte from the EP4 OUT FIFO.
                                            // Writing to this register loads
                                            // one byte into the EP4 IN FIFO.
#define USB_F4_USBF4_S          0
//*****************************************************************************
//
// The following are defines for the bit fields in the USB_F5 register.
//
//*****************************************************************************
#define USB_F5_USBF5_M          0x000000FF  // Endpoint 5 FIFO register
                                            // Reading this register unloads
                                            // one byte from the EP5 OUT FIFO.
                                            // Writing to this register loads
                                            // one byte into the EP5 IN FIFO.
#define USB_F5_USBF5_S          0

#endif // __HW_USB_H__