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

//go:build (darwin && amd64) || (darwin && arm64) || (freebsd && 386) || (freebsd && amd64) || (freebsd && arm) || (linux && 386) || (linux && amd64) || (linux && arm) || (linux && arm64) || (linux && loong64) || (linux && ppc64le) || (linux && riscv64) || (linux && s390x) || (netbsd && amd64) || (openbsd && amd64) || (openbsd && arm64)

package sqlite3

import (
	"unsafe"

	"modernc.org/libc"
)

func _cellOverlap(tls *libc.TLS, pRtree uintptr, p uintptr, aCell uintptr, nCell int32) (r TRtreeDValue) {
	var ii, jj int32
	var o, overlap, x1, x2 TRtreeDValue
	var v3, v4, v5, v6, v7 float64
	_, _, _, _, _, _, _, _, _, _, _ = ii, jj, o, overlap, x1, x2, v3, v4, v5, v6, v7
	overlap = float64(0)
	ii = 0
	for {
		if !(ii < nCell) {
			break
		}
		o = libc.Float64FromInt32(1)
		jj = 0
		for {
			if !(jj < libc.Int32FromUint8((*TRtree)(unsafe.Pointer(pRtree)).FnDim2)) {
				break
			}
			if libc.Int32FromUint8((*TRtree)(unsafe.Pointer(pRtree)).FeCoordType) == RTREE_COORD_REAL32 {
				v4 = float64(*(*TRtreeValue)(unsafe.Pointer(p + 8 + uintptr(jj)*4)))
			} else {
				v4 = float64(*(*int32)(unsafe.Pointer(p + 8 + uintptr(jj)*4)))
			}
			if libc.Int32FromUint8((*TRtree)(unsafe.Pointer(pRtree)).FeCoordType) == RTREE_COORD_REAL32 {
				v5 = float64(*(*TRtreeValue)(unsafe.Pointer(aCell + uintptr(ii)*48 + 8 + uintptr(jj)*4)))
			} else {
				v5 = float64(*(*int32)(unsafe.Pointer(aCell + uintptr(ii)*48 + 8 + uintptr(jj)*4)))
			}
			if v4 > v5 {
				if libc.Int32FromUint8((*TRtree)(unsafe.Pointer(pRtree)).FeCoordType) == RTREE_COORD_REAL32 {
					v6 = float64(*(*TRtreeValue)(unsafe.Pointer(p + 8 + uintptr(jj)*4)))
				} else {
					v6 = float64(*(*int32)(unsafe.Pointer(p + 8 + uintptr(jj)*4)))
				}
				v3 = v6
			} else {
				if libc.Int32FromUint8((*TRtree)(unsafe.Pointer(pRtree)).FeCoordType) == RTREE_COORD_REAL32 {
					v7 = float64(*(*TRtreeValue)(unsafe.Pointer(aCell + uintptr(ii)*48 + 8 + uintptr(jj)*4)))
				} else {
					v7 = float64(*(*int32)(unsafe.Pointer(aCell + uintptr(ii)*48 + 8 + uintptr(jj)*4)))
				}
				v3 = v7
			}
			x1 = v3
			if libc.Int32FromUint8((*TRtree)(unsafe.Pointer(pRtree)).FeCoordType) == RTREE_COORD_REAL32 {
				v4 = float64(*(*TRtreeValue)(unsafe.Pointer(p + 8 + uintptr(jj+int32(1))*4)))
			} else {
				v4 = float64(*(*int32)(unsafe.Pointer(p + 8 + uintptr(jj+int32(1))*4)))
			}
			if libc.Int32FromUint8((*TRtree)(unsafe.Pointer(pRtree)).FeCoordType) == RTREE_COORD_REAL32 {
				v5 = float64(*(*TRtreeValue)(unsafe.Pointer(aCell + uintptr(ii)*48 + 8 + uintptr(jj+int32(1))*4)))
			} else {
				v5 = float64(*(*int32)(unsafe.Pointer(aCell + uintptr(ii)*48 + 8 + uintptr(jj+int32(1))*4)))
			}
			if v4 < v5 {
				if libc.Int32FromUint8((*TRtree)(unsafe.Pointer(pRtree)).FeCoordType) == RTREE_COORD_REAL32 {
					v6 = float64(*(*TRtreeValue)(unsafe.Pointer(p + 8 + uintptr(jj+int32(1))*4)))
				} else {
					v6 = float64(*(*int32)(unsafe.Pointer(p + 8 + uintptr(jj+int32(1))*4)))
				}
				v3 = v6
			} else {
				if libc.Int32FromUint8((*TRtree)(unsafe.Pointer(pRtree)).FeCoordType) == RTREE_COORD_REAL32 {
					v7 = float64(*(*TRtreeValue)(unsafe.Pointer(aCell + uintptr(ii)*48 + 8 + uintptr(jj+int32(1))*4)))
				} else {
					v7 = float64(*(*int32)(unsafe.Pointer(aCell + uintptr(ii)*48 + 8 + uintptr(jj+int32(1))*4)))
				}
				v3 = v7
			}
			x2 = v3
			if x2 < x1 {
				o = libc.Float64FromInt32(0)
				break
			} else {
				o = TRtreeDValue(o * (x2 - x1))
			}
			goto _2
		_2:
			;
			jj = jj + int32(2)
		}
		overlap = overlap + o
		goto _1
	_1:
		;
		ii = ii + 1
	}
	return overlap
}

// C documentation
//
//	/*
//	** Expression p should encode a floating point value between 1.0 and 0.0.
//	** Return 134,217,728 (2^27) times this value.  Or return -1 if p is not
//	** a floating point value between 1.0 and 0.0.
//	*/
func _exprProbability(tls *libc.TLS, p uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var _ /* r at bp+0 */ float64
	**(**float64)(__ccgo_up(bp)) = -libc.Float64FromFloat64(1)
	if libc.Int32FromUint8((*TExpr)(unsafe.Pointer(p)).Fop) != int32(TK_FLOAT) {
		return -int32(1)
	}
	_sqlite3AtoF(tls, *(*uintptr)(unsafe.Pointer(p + 8)), bp)
	if **(**float64)(__ccgo_up(bp)) > float64(1) {
		return -int32(1)
	}
	return int32(float64(**(**float64)(__ccgo_up(bp)) * libc.Float64FromFloat64(1.34217728e+08)))
}
