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

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

package vec

import (
	"unsafe"

	"modernc.org/libc"
	libsqlite3 "modernc.org/sqlite/lib"
)

func Xarray_append(tls *libc.TLS, array uintptr, element uintptr) (r int32) {
	var new_capacity Tsize_t
	var z uintptr
	_, _ = new_capacity, z
	if (*TArray)(unsafe.Pointer(array)).Flength == (*TArray)(unsafe.Pointer(array)).Fcapacity {
		new_capacity = (*TArray)(unsafe.Pointer(array)).Fcapacity*uint64(2) + uint64(100)
		z = libsqlite3.Xsqlite3_realloc64(tls, (*TArray)(unsafe.Pointer(array)).Fz, (*TArray)(unsafe.Pointer(array)).Felement_size*new_capacity)
		if z != 0 {
			(*TArray)(unsafe.Pointer(array)).Fcapacity = new_capacity
			(*TArray)(unsafe.Pointer(array)).Fz = z
		} else {
			return int32(m_SQLITE_NOMEM)
		}
	}
	libc.Xmemcpy(tls, (*TArray)(unsafe.Pointer(array)).Fz+uintptr((*TArray)(unsafe.Pointer(array)).Flength*(*TArray)(unsafe.Pointer(array)).Felement_size), element, (*TArray)(unsafe.Pointer(array)).Felement_size)
	(*TArray)(unsafe.Pointer(array)).Flength = (*TArray)(unsafe.Pointer(array)).Flength + 1
	return m_SQLITE_OK
}

func Xvec0Filter_fullscan(tls *libc.TLS, p uintptr, pCur uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var fullscan_data, zSql uintptr
	var rc int32
	_, _, _ = fullscan_data, rc, zSql
	fullscan_data = libsqlite3.Xsqlite3_malloc(tls, int32(16))
	if !(fullscan_data != 0) {
		return int32(m_SQLITE_NOMEM)
	}
	libc.Xmemset(tls, fullscan_data, 0, uint64(16))
	zSql = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+10644, libc.VaList(bp+8, (*Tvec0_vtab)(unsafe.Pointer(p)).FschemaName, (*Tvec0_vtab)(unsafe.Pointer(p)).FtableName))
	if !(zSql != 0) {
		rc = int32(m_SQLITE_NOMEM)
		goto error
	}
	rc = libsqlite3.Xsqlite3_prepare_v2(tls, (*Tvec0_vtab)(unsafe.Pointer(p)).Fdb, zSql, -int32(1), fullscan_data, libc.UintptrFromInt32(0))
	libsqlite3.Xsqlite3_free(tls, zSql)
	if rc != m_SQLITE_OK {
		// IMP: V09901_26739
		Xvtab_set_error(tls, p, __ccgo_ts+10714, libc.VaList(bp+8, libsqlite3.Xsqlite3_errmsg(tls, (*Tvec0_vtab)(unsafe.Pointer(p)).Fdb)))
		goto error
	}
	rc = libsqlite3.Xsqlite3_step(tls, (*Tvec0_query_fullscan_data)(unsafe.Pointer(fullscan_data)).Frowids_stmt)
	// DONE when there's no rowids, ROW when there are, both "success"
	if !(rc == int32(m_SQLITE_ROW) || rc == int32(m_SQLITE_DONE)) {
		goto error
	}
	(*Tvec0_query_fullscan_data)(unsafe.Pointer(fullscan_data)).Fdone = libc.BoolInt8(rc == int32(m_SQLITE_DONE))
	(*Tvec0_cursor)(unsafe.Pointer(pCur)).Fquery_plan = int32(_VEC0_QUERY_PLAN_FULLSCAN)
	(*Tvec0_cursor)(unsafe.Pointer(pCur)).Ffullscan_data = fullscan_data
	return m_SQLITE_OK
	goto error
error:
	;
	Xvec0_query_fullscan_data_clear(tls, fullscan_data)
	libsqlite3.Xsqlite3_free(tls, fullscan_data)
	return rc
}

func Xvec0Filter_point(tls *libc.TLS, pCur uintptr, p uintptr, argc int32, argv uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var i, rc int32
	var point_data uintptr
	var _ /* rowid at bp+0 */ Ti64
	_, _, _ = i, point_data, rc
	point_data = libc.UintptrFromInt32(0)
	point_data = libsqlite3.Xsqlite3_malloc(tls, int32(144))
	if !(point_data != 0) {
		rc = int32(m_SQLITE_NOMEM)
		goto error
	}
	libc.Xmemset(tls, point_data, 0, uint64(144))
	if (*Tvec0_vtab)(unsafe.Pointer(p)).FpkIsText != 0 {
		rc = Xvec0_rowid_from_id(tls, p, **(**uintptr)(__ccgo_up(argv)), bp)
		if rc == int32(m_SQLITE_EMPTY) {
			goto eof
		}
		if rc != m_SQLITE_OK {
			goto error
		}
	} else {
		**(**Ti64)(__ccgo_up(bp)) = libsqlite3.Xsqlite3_value_int64(tls, **(**uintptr)(__ccgo_up(argv)))
	}
	i = 0
	for {
		if !(i < (*Tvec0_vtab)(unsafe.Pointer(p)).FnumVectorColumns) {
			break
		}
		rc = Xvec0_get_vector_data(tls, p, **(**Ti64)(__ccgo_up(bp)), i, point_data+8+uintptr(i)*8, libc.UintptrFromInt32(0))
		if rc == int32(m_SQLITE_EMPTY) {
			goto eof
		}
		if rc != m_SQLITE_OK {
			goto error
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	(*Tvec0_query_point_data)(unsafe.Pointer(point_data)).Frowid = **(**Ti64)(__ccgo_up(bp))
	(*Tvec0_query_point_data)(unsafe.Pointer(point_data)).Fdone = 0
	(*Tvec0_cursor)(unsafe.Pointer(pCur)).Fpoint_data = point_data
	(*Tvec0_cursor)(unsafe.Pointer(pCur)).Fquery_plan = int32(_VEC0_QUERY_PLAN_POINT)
	return m_SQLITE_OK
	goto eof
eof:
	;
	(*Tvec0_query_point_data)(unsafe.Pointer(point_data)).Frowid = **(**Ti64)(__ccgo_up(bp))
	(*Tvec0_query_point_data)(unsafe.Pointer(point_data)).Fdone = int32(1)
	(*Tvec0_cursor)(unsafe.Pointer(pCur)).Fpoint_data = point_data
	(*Tvec0_cursor)(unsafe.Pointer(pCur)).Fquery_plan = int32(_VEC0_QUERY_PLAN_POINT)
	return m_SQLITE_OK
	goto error
error:
	;
	Xvec0_query_point_data_clear(tls, point_data)
	libsqlite3.Xsqlite3_free(tls, point_data)
	return rc
}

func _vec0Open(tls *libc.TLS, p uintptr, ppCursor uintptr) (r int32) {
	var pCur uintptr
	_ = pCur
	_ = p
	pCur = libsqlite3.Xsqlite3_malloc(tls, int32(40))
	if pCur == uintptr(0) {
		return int32(m_SQLITE_NOMEM)
	}
	libc.Xmemset(tls, pCur, 0, uint64(40))
	**(**uintptr)(__ccgo_up(ppCursor)) = pCur
	return m_SQLITE_OK
}

func _vec_eachConnect(tls *libc.TLS, db uintptr, pAux uintptr, argc int32, argv uintptr, ppVtab uintptr, pzErr uintptr) (r int32) {
	var pNew uintptr
	var rc int32
	_, _ = pNew, rc
	_ = pAux
	_ = argc
	_ = argv
	_ = pzErr
	rc = libsqlite3.Xsqlite3_declare_vtab(tls, db, __ccgo_ts+1879)
	if rc == m_SQLITE_OK {
		pNew = libsqlite3.Xsqlite3_malloc(tls, int32(24))
		**(**uintptr)(__ccgo_up(ppVtab)) = pNew
		if pNew == uintptr(0) {
			return int32(m_SQLITE_NOMEM)
		}
		libc.Xmemset(tls, pNew, 0, uint64(24))
	}
	return rc
}

func _vec_eachOpen(tls *libc.TLS, p uintptr, ppCursor uintptr) (r int32) {
	var pCur uintptr
	_ = pCur
	_ = p
	pCur = libsqlite3.Xsqlite3_malloc(tls, int32(48))
	if pCur == uintptr(0) {
		return int32(m_SQLITE_NOMEM)
	}
	libc.Xmemset(tls, pCur, 0, uint64(48))
	**(**uintptr)(__ccgo_up(ppCursor)) = pCur
	return m_SQLITE_OK
}

func _vec_npy_eachConnect(tls *libc.TLS, db uintptr, pAux uintptr, argc int32, argv uintptr, ppVtab uintptr, pzErr uintptr) (r int32) {
	var pNew uintptr
	var rc int32
	_, _ = pNew, rc
	_ = pAux
	_ = argc
	_ = argv
	_ = pzErr
	rc = libsqlite3.Xsqlite3_declare_vtab(tls, db, __ccgo_ts+3334)
	if rc == m_SQLITE_OK {
		pNew = libsqlite3.Xsqlite3_malloc(tls, int32(24))
		**(**uintptr)(__ccgo_up(ppVtab)) = pNew
		if pNew == uintptr(0) {
			return int32(m_SQLITE_NOMEM)
		}
		libc.Xmemset(tls, pNew, 0, uint64(24))
	}
	return rc
}

func _vec_npy_eachOpen(tls *libc.TLS, p uintptr, ppCursor uintptr) (r int32) {
	var pCur uintptr
	_ = pCur
	_ = p
	pCur = libsqlite3.Xsqlite3_malloc(tls, int32(112))
	if pCur == uintptr(0) {
		return int32(m_SQLITE_NOMEM)
	}
	libc.Xmemset(tls, pCur, 0, uint64(112))
	**(**uintptr)(__ccgo_up(ppCursor)) = pCur
	return m_SQLITE_OK
}

func _vec_static_blob_entriesConnect(tls *libc.TLS, db uintptr, pAux uintptr, argc int32, argv uintptr, ppVtab uintptr, pzErr uintptr) (r int32) {
	var blob_data, pNew uintptr
	var i, idx, rc int32
	_, _, _, _, _ = blob_data, i, idx, pNew, rc
	_ = argc
	_ = argv
	_ = pzErr
	blob_data = pAux
	idx = -int32(1)
	i = 0
	for {
		if !(i < int32(m_MAX_STATIC_BLOBS)) {
			break
		}
		if !((**(**Tstatic_blob)(__ccgo_up(blob_data + uintptr(i)*40))).Fname != 0) {
			goto _1
		}
		if libc.Xstrncmp(tls, (**(**Tstatic_blob)(__ccgo_up(blob_data + uintptr(i)*40))).Fname, **(**uintptr)(__ccgo_up(argv + 3*8)), libc.Xstrlen(tls, (**(**Tstatic_blob)(__ccgo_up(blob_data + uintptr(i)*40))).Fname)) == 0 {
			idx = i
			break
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	if idx < 0 {
		libc.Xabort(tls)
	}
	rc = libsqlite3.Xsqlite3_declare_vtab(tls, db, __ccgo_ts+15953)
	if rc == m_SQLITE_OK {
		pNew = libsqlite3.Xsqlite3_malloc(tls, int32(32))
		**(**uintptr)(__ccgo_up(ppVtab)) = pNew
		if pNew == uintptr(0) {
			return int32(m_SQLITE_NOMEM)
		}
		libc.Xmemset(tls, pNew, 0, uint64(32))
		(*Tvec_static_blob_entries_vtab)(unsafe.Pointer(pNew)).Fblob = blob_data + uintptr(idx)*40
	}
	return rc
}

func _vec_static_blob_entriesOpen(tls *libc.TLS, p uintptr, ppCursor uintptr) (r int32) {
	var pCur uintptr
	_ = pCur
	_ = p
	pCur = libsqlite3.Xsqlite3_malloc(tls, int32(32))
	if pCur == uintptr(0) {
		return int32(m_SQLITE_NOMEM)
	}
	libc.Xmemset(tls, pCur, 0, uint64(32))
	**(**uintptr)(__ccgo_up(ppCursor)) = pCur
	return m_SQLITE_OK
}

func _vec_static_blobsConnect(tls *libc.TLS, db uintptr, pAux uintptr, argc int32, argv uintptr, ppVtab uintptr, pzErr uintptr) (r int32) {
	var pNew uintptr
	var rc int32
	_, _ = pNew, rc
	_ = argc
	_ = argv
	_ = pzErr
	rc = libsqlite3.Xsqlite3_declare_vtab(tls, db, __ccgo_ts+15893)
	if rc == m_SQLITE_OK {
		pNew = libsqlite3.Xsqlite3_malloc(tls, int32(32))
		**(**uintptr)(__ccgo_up(ppVtab)) = pNew
		if pNew == uintptr(0) {
			return int32(m_SQLITE_NOMEM)
		}
		libc.Xmemset(tls, pNew, 0, uint64(32))
		(*Tvec_static_blobs_vtab)(unsafe.Pointer(pNew)).Fdata = pAux
	}
	return rc
}

func _vec_static_blobsOpen(tls *libc.TLS, p uintptr, ppCursor uintptr) (r int32) {
	var pCur uintptr
	_ = pCur
	_ = p
	pCur = libsqlite3.Xsqlite3_malloc(tls, int32(16))
	if pCur == uintptr(0) {
		return int32(m_SQLITE_NOMEM)
	}
	libc.Xmemset(tls, pCur, 0, uint64(16))
	**(**uintptr)(__ccgo_up(ppCursor)) = pCur
	return m_SQLITE_OK
}
