// Code generated by modernc.org/undup from the per-target sqlite_*.go files; DO NOT EDIT.

//go:build (freebsd && amd64) || (freebsd && arm64) || (linux && amd64) || (linux && arm64) || (linux && loong64) || (linux && ppc64le) || (linux && riscv64) || (netbsd && amd64) || (openbsd && amd64) || (openbsd && arm64) || (windows && (amd64 || arm64))

package sqlite3

import (
	"unsafe"

	"modernc.org/libc"
)

// C documentation
//
//	/*
//	** Check the leaf RTree cell given by pCellData against constraint p.
//	** If this constraint is not satisfied, set *peWithin to NOT_WITHIN.
//	** If the constraint is satisfied, leave *peWithin unchanged.
//	**
//	** The constraint is of the form:  xN op $val
//	**
//	** The op is given by p->op.  The xN is p->iCoord-th coordinate in
//	** pCellData.  $val is given by p->u.rValue.
//	*/
func _rtreeLeafConstraint(tls *libc.TLS, p uintptr, eInt int32, pCellData uintptr, peWithin uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var xN TRtreeDValue
	var v1 Tsqlite3_rtree_dbl
	var _ /* c at bp+0 */ TRtreeCoord
	_, _ = xN, v1 /* Coordinate value converted to a double */
	pCellData = pCellData + uintptr(int32(8)+(*TRtreeConstraint)(unsafe.Pointer(p)).FiCoord*int32(4))
	/* Coordinate decoded */
	libc.Xmemcpy(tls, bp, pCellData, uint64(4))
	*(*Tu32)(unsafe.Pointer(bp)) = *(*Tu32)(unsafe.Pointer(bp))>>libc.Int32FromInt32(24)&uint32(0xff) | *(*Tu32)(unsafe.Pointer(bp))>>libc.Int32FromInt32(8)&uint32(0xff00) | *(*Tu32)(unsafe.Pointer(bp))&uint32(0xff)<<int32(24) | *(*Tu32)(unsafe.Pointer(bp))&uint32(0xff00)<<int32(8)
	if eInt != 0 {
		v1 = float64(*(*int32)(unsafe.Pointer(bp)))
	} else {
		v1 = float64(*(*TRtreeValue)(unsafe.Pointer(bp)))
	}
	xN = v1
	switch (*TRtreeConstraint)(unsafe.Pointer(p)).Fop {
	case int32(RTREE_TRUE):
		return /* Always satisfied */
	case int32(RTREE_FALSE):
	case int32(RTREE_LE):
		if xN <= *(*TRtreeDValue)(unsafe.Pointer(p + 8)) {
			return
		}
	case int32(RTREE_LT):
		if xN < *(*TRtreeDValue)(unsafe.Pointer(p + 8)) {
			return
		}
	case int32(RTREE_GE):
		if xN >= *(*TRtreeDValue)(unsafe.Pointer(p + 8)) {
			return
		}
	case int32(RTREE_GT):
		if xN > *(*TRtreeDValue)(unsafe.Pointer(p + 8)) {
			return
		}
	default:
		if xN == *(*TRtreeDValue)(unsafe.Pointer(p + 8)) {
			return
		}
		break
	}
	**(**int32)(__ccgo_up(peWithin)) = NOT_WITHIN
}

// C documentation
//
//	/*
//	** Check the internal RTree node given by pCellData against constraint p.
//	** If this constraint cannot be satisfied by any child within the node,
//	** set *peWithin to NOT_WITHIN.
//	*/
func _rtreeNonleafConstraint(tls *libc.TLS, p uintptr, eInt int32, pCellData uintptr, peWithin uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var val, v1 Tsqlite3_rtree_dbl
	var _ /* c at bp+0 */ TRtreeCoord
	var _ /* c at bp+12 */ TRtreeCoord
	var _ /* c at bp+4 */ TRtreeCoord
	var _ /* c at bp+8 */ TRtreeCoord
	_, _ = val, v1 /* Coordinate value convert to a double */
	/* p->iCoord might point to either a lower or upper bound coordinate
	 ** in a coordinate pair.  But make pCellData point to the lower bound.
	 */
	pCellData = pCellData + uintptr(int32(8)+int32(4)*((*TRtreeConstraint)(unsafe.Pointer(p)).FiCoord&int32(0xfe)))
	switch (*TRtreeConstraint)(unsafe.Pointer(p)).Fop {
	case int32(RTREE_TRUE):
		return /* Always satisfied */
	case int32(RTREE_FALSE):
	case int32(RTREE_EQ):
		/* Coordinate decoded */ libc.Xmemcpy(tls, bp, pCellData, uint64(4))
		*(*Tu32)(unsafe.Pointer(bp)) = *(*Tu32)(unsafe.Pointer(bp))>>libc.Int32FromInt32(24)&uint32(0xff) | *(*Tu32)(unsafe.Pointer(bp))>>libc.Int32FromInt32(8)&uint32(0xff00) | *(*Tu32)(unsafe.Pointer(bp))&uint32(0xff)<<int32(24) | *(*Tu32)(unsafe.Pointer(bp))&uint32(0xff00)<<int32(8)
		if eInt != 0 {
			v1 = float64(*(*int32)(unsafe.Pointer(bp)))
		} else {
			v1 = float64(*(*TRtreeValue)(unsafe.Pointer(bp)))
		}
		val = v1
		/* val now holds the lower bound of the coordinate pair */
		if *(*TRtreeDValue)(unsafe.Pointer(p + 8)) >= val {
			pCellData = pCellData + uintptr(4)
			/* Coordinate decoded */ libc.Xmemcpy(tls, bp+4, pCellData, uint64(4))
			*(*Tu32)(unsafe.Pointer(bp + 4)) = *(*Tu32)(unsafe.Pointer(bp + 4))>>libc.Int32FromInt32(24)&uint32(0xff) | *(*Tu32)(unsafe.Pointer(bp + 4))>>libc.Int32FromInt32(8)&uint32(0xff00) | *(*Tu32)(unsafe.Pointer(bp + 4))&uint32(0xff)<<int32(24) | *(*Tu32)(unsafe.Pointer(bp + 4))&uint32(0xff00)<<int32(8)
			if eInt != 0 {
				v1 = float64(*(*int32)(unsafe.Pointer(bp + 4)))
			} else {
				v1 = float64(*(*TRtreeValue)(unsafe.Pointer(bp + 4)))
			}
			val = v1
			/* val now holds the upper bound of the coordinate pair */
			if *(*TRtreeDValue)(unsafe.Pointer(p + 8)) <= val {
				return
			}
		}
	case int32(RTREE_LE):
		fallthrough
	case int32(RTREE_LT):
		/* Coordinate decoded */ libc.Xmemcpy(tls, bp+8, pCellData, uint64(4))
		*(*Tu32)(unsafe.Pointer(bp + 8)) = *(*Tu32)(unsafe.Pointer(bp + 8))>>libc.Int32FromInt32(24)&uint32(0xff) | *(*Tu32)(unsafe.Pointer(bp + 8))>>libc.Int32FromInt32(8)&uint32(0xff00) | *(*Tu32)(unsafe.Pointer(bp + 8))&uint32(0xff)<<int32(24) | *(*Tu32)(unsafe.Pointer(bp + 8))&uint32(0xff00)<<int32(8)
		if eInt != 0 {
			v1 = float64(*(*int32)(unsafe.Pointer(bp + 8)))
		} else {
			v1 = float64(*(*TRtreeValue)(unsafe.Pointer(bp + 8)))
		}
		val = v1
		/* val now holds the lower bound of the coordinate pair */
		if *(*TRtreeDValue)(unsafe.Pointer(p + 8)) >= val {
			return
		}
	default:
		pCellData = pCellData + uintptr(4)
		/* Coordinate decoded */ libc.Xmemcpy(tls, bp+12, pCellData, uint64(4))
		*(*Tu32)(unsafe.Pointer(bp + 12)) = *(*Tu32)(unsafe.Pointer(bp + 12))>>libc.Int32FromInt32(24)&uint32(0xff) | *(*Tu32)(unsafe.Pointer(bp + 12))>>libc.Int32FromInt32(8)&uint32(0xff00) | *(*Tu32)(unsafe.Pointer(bp + 12))&uint32(0xff)<<int32(24) | *(*Tu32)(unsafe.Pointer(bp + 12))&uint32(0xff00)<<int32(8)
		if eInt != 0 {
			v1 = float64(*(*int32)(unsafe.Pointer(bp + 12)))
		} else {
			v1 = float64(*(*TRtreeValue)(unsafe.Pointer(bp + 12)))
		}
		val = v1
		/* val now holds the upper bound of the coordinate pair */
		if *(*TRtreeDValue)(unsafe.Pointer(p + 8)) <= val {
			return
		}
		break
	}
	**(**int32)(__ccgo_up(peWithin)) = NOT_WITHIN
}
