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

//go:build (freebsd && 386) || (freebsd && amd64) || (freebsd && arm) || (freebsd && arm64)

package vec

import (
	"unsafe"

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

type Tconstraint_handler_t = uintptr

func Xbitmap_and_inplace(tls *libc.TLS, base uintptr, other uintptr, n Ti32) {
	var i int32
	_ = i
	i = 0
	for {
		if !(i < n/int32(m___CHAR_BIT)) {
			break
		}
		**(**Tu8)(__ccgo_up(base + uintptr(i))) = libc.Uint8FromInt32(libc.Int32FromUint8(**(**Tu8)(__ccgo_up(base + uintptr(i)))) & libc.Int32FromUint8(**(**Tu8)(__ccgo_up(other + uintptr(i)))))
		goto _1
	_1:
		;
		i = i + 1
	}
}

func Xbitmap_get(tls *libc.TLS, bitmap uintptr, position Ti32) (r int32) {
	return libc.Int32FromUint8(**(**Tu8)(__ccgo_up(bitmap + uintptr(position/int32(m___CHAR_BIT))))) >> (position % int32(m___CHAR_BIT)) & int32(1)
}

func Xbitmap_set(tls *libc.TLS, bitmap uintptr, position Ti32, value int32) {
	var v1 uintptr
	_ = v1
	if value != 0 {
		v1 = bitmap + uintptr(position/int32(m___CHAR_BIT))
		*(*Tu8)(unsafe.Pointer(v1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v1))) | libc.Int32FromInt32(1)<<(position%libc.Int32FromInt32(m___CHAR_BIT)))
	} else {
		v1 = bitmap + uintptr(position/int32(m___CHAR_BIT))
		*(*Tu8)(unsafe.Pointer(v1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v1))) & ^(libc.Int32FromInt32(1) << (position % libc.Int32FromInt32(m___CHAR_BIT))))
	}
}

// C documentation
//
//	/**
//	 * @brief Determines the "next available" chunk position for a newly inserted
//	 * vec0 row.
//	 *
//	 * This operation may insert a new "blank" chunk the _chunks table, if there is
//	 * no more space in previous chunks.
//	 *
//	 * @param p: virtual table
//	 * @param partitionKeyValues: array of partition key column values, to constrain
//	 * against any partition key columns.
//	 * @param chunk_rowid: Output rowid of the chunk in the _chunks virtual table
//	 * that has the avialabiity.
//	 * @param chunk_offset: Output the index of the available space insert the
//	 * chunk, based on the index of the first available validity bit.
//	 * @param pBlobValidity: Output blob of the validity column of the available
//	 * chunk. Will be opened with read/write permissions.
//	 * @param pValidity: Output buffer of the original chunk's validity column.
//	 *    Needs to be cleaned up with sqlite3_free().
//	 * @return int SQLITE_OK on success, error code on failure
//	 */
func Xvec0Update_InsertNextAvailableStep(tls *libc.TLS, p uintptr, partitionKeyValues uintptr, chunk_rowid uintptr, chunk_offset uintptr, blobChunksValidity uintptr, bufferChunksValidity uintptr) (r int32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var i, j, rc int32
	var validitySize Ti64
	_, _, _, _ = i, j, rc, validitySize
	**(**Ti64)(__ccgo_up(chunk_offset)) = int64(-int32(1))
	rc = Xvec0_get_latest_chunk_rowid(tls, p, chunk_rowid, partitionKeyValues)
	if rc == int32(m_SQLITE_EMPTY) {
		goto done
	}
	if rc != m_SQLITE_OK {
		goto cleanup
	}
	rc = libsqlite3.Xsqlite3_blob_open(tls, (*Tvec0_vtab)(unsafe.Pointer(p)).Fdb, (*Tvec0_vtab)(unsafe.Pointer(p)).FschemaName, (*Tvec0_vtab)(unsafe.Pointer(p)).FshadowChunksName, __ccgo_ts+11207, **(**Ti64)(__ccgo_up(chunk_rowid)), int32(1), blobChunksValidity)
	if rc != m_SQLITE_OK {
		// IMP: V22053_06123
		Xvtab_set_error(tls, p, __ccgo_ts+11216, libc.VaList(bp+8, (*Tvec0_vtab)(unsafe.Pointer(p)).FschemaName, (*Tvec0_vtab)(unsafe.Pointer(p)).FshadowChunksName, **(**Ti64)(__ccgo_up(chunk_rowid))))
		goto cleanup
	}
	validitySize = int64(libsqlite3.Xsqlite3_blob_bytes(tls, **(**uintptr)(__ccgo_up(blobChunksValidity))))
	if validitySize != int64((*Tvec0_vtab)(unsafe.Pointer(p)).Fchunk_size/int32(m___CHAR_BIT)) {
		// IMP: V29362_13432
		Xvtab_set_error(tls, p, __ccgo_ts+11286, libc.VaList(bp+8, (*Tvec0_vtab)(unsafe.Pointer(p)).FschemaName, (*Tvec0_vtab)(unsafe.Pointer(p)).FshadowChunksName, **(**Ti64)(__ccgo_up(chunk_rowid)), int64((*Tvec0_vtab)(unsafe.Pointer(p)).Fchunk_size/libc.Int32FromInt32(m___CHAR_BIT)), validitySize))
		rc = int32(m_SQLITE_ERROR)
		goto cleanup
	}
	**(**uintptr)(__ccgo_up(bufferChunksValidity)) = libsqlite3.Xsqlite3_malloc(tls, int32(validitySize))
	if !(**(**uintptr)(__ccgo_up(bufferChunksValidity)) != 0) {
		Xvtab_set_error(tls, p, __ccgo_ts+11389, 0)
		rc = int32(m_SQLITE_NOMEM)
		goto cleanup
	}
	rc = libsqlite3.Xsqlite3_blob_read(tls, **(**uintptr)(__ccgo_up(blobChunksValidity)), **(**uintptr)(__ccgo_up(bufferChunksValidity)), int32(validitySize), 0)
	if rc != m_SQLITE_OK {
		Xvtab_set_error(tls, p, __ccgo_ts+11462, libc.VaList(bp+8, (*Tvec0_vtab)(unsafe.Pointer(p)).FschemaName, (*Tvec0_vtab)(unsafe.Pointer(p)).FshadowChunksName, **(**Ti64)(__ccgo_up(chunk_rowid))))
		goto cleanup
	}
	// find the next available offset, ie first `0` in the bitmap.
	i = 0
	for {
		if !(int64(i) < validitySize) {
			break
		}
		if libc.Int32FromUint8(**(**uint8)(__ccgo_up(**(**uintptr)(__ccgo_up(bufferChunksValidity)) + uintptr(i)))) == int32(0b11111111) {
			goto _1
		}
		j = 0
		for {
			if !(j < int32(m___CHAR_BIT)) {
				break
			}
			if libc.Int32FromUint8(**(**uint8)(__ccgo_up(**(**uintptr)(__ccgo_up(bufferChunksValidity)) + uintptr(i))))>>j&int32(1) == 0 {
				**(**Ti64)(__ccgo_up(chunk_offset)) = int64(i*int32(m___CHAR_BIT) + j)
				goto done
			}
			goto _2
		_2:
			;
			j = j + 1
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	goto done
done:
	;
	// latest chunk was full, so need to create a new one
	if **(**Ti64)(__ccgo_up(chunk_offset)) == int64(-int32(1)) {
		rc = Xvec0_new_chunk(tls, p, partitionKeyValues, chunk_rowid)
		if rc != m_SQLITE_OK {
			// IMP: V08441_25279
			Xvtab_set_error(tls, p, __ccgo_ts+11535, 0)
			rc = int32(m_SQLITE_ERROR) // otherwise raises a DatabaseError and not operational
			// error?
			goto cleanup
		}
		**(**Ti64)(__ccgo_up(chunk_offset)) = 0
		// blobChunksValidity and pValidity are stale, pointing to the previous
		// (full) chunk. to re-assign them
		rc = libsqlite3.Xsqlite3_blob_close(tls, **(**uintptr)(__ccgo_up(blobChunksValidity)))
		libsqlite3.Xsqlite3_free(tls, **(**uintptr)(__ccgo_up(bufferChunksValidity)))
		**(**uintptr)(__ccgo_up(blobChunksValidity)) = libc.UintptrFromInt32(0)
		**(**uintptr)(__ccgo_up(bufferChunksValidity)) = libc.UintptrFromInt32(0)
		if rc != m_SQLITE_OK {
			Xvtab_set_error(tls, p, __ccgo_ts+11598, 0)
			rc = int32(m_SQLITE_ERROR)
			goto cleanup
		}
		rc = libsqlite3.Xsqlite3_blob_open(tls, (*Tvec0_vtab)(unsafe.Pointer(p)).Fdb, (*Tvec0_vtab)(unsafe.Pointer(p)).FschemaName, (*Tvec0_vtab)(unsafe.Pointer(p)).FshadowChunksName, __ccgo_ts+11207, **(**Ti64)(__ccgo_up(chunk_rowid)), int32(1), blobChunksValidity)
		if rc != m_SQLITE_OK {
			Xvtab_set_error(tls, p, __ccgo_ts+11702, libc.VaList(bp+8, (*Tvec0_vtab)(unsafe.Pointer(p)).FschemaName, (*Tvec0_vtab)(unsafe.Pointer(p)).FshadowChunksName, **(**Ti64)(__ccgo_up(chunk_rowid))))
			goto cleanup
		}
		validitySize = int64(libsqlite3.Xsqlite3_blob_bytes(tls, **(**uintptr)(__ccgo_up(blobChunksValidity))))
		if validitySize != int64((*Tvec0_vtab)(unsafe.Pointer(p)).Fchunk_size/int32(m___CHAR_BIT)) {
			Xvtab_set_error(tls, p, __ccgo_ts+11793, libc.VaList(bp+8, (*Tvec0_vtab)(unsafe.Pointer(p)).FschemaName, (*Tvec0_vtab)(unsafe.Pointer(p)).FshadowChunksName, **(**Ti64)(__ccgo_up(chunk_rowid)), (*Tvec0_vtab)(unsafe.Pointer(p)).Fchunk_size/int32(m___CHAR_BIT), validitySize))
			goto cleanup
		}
		**(**uintptr)(__ccgo_up(bufferChunksValidity)) = libsqlite3.Xsqlite3_malloc(tls, int32(validitySize))
		rc = libsqlite3.Xsqlite3_blob_read(tls, **(**uintptr)(__ccgo_up(blobChunksValidity)), **(**uintptr)(__ccgo_up(bufferChunksValidity)), int32(validitySize), 0)
		if rc != m_SQLITE_OK {
			Xvtab_set_error(tls, p, __ccgo_ts+11908, libc.VaList(bp+8, (*Tvec0_vtab)(unsafe.Pointer(p)).FschemaName, (*Tvec0_vtab)(unsafe.Pointer(p)).FshadowChunksName, **(**Ti64)(__ccgo_up(chunk_rowid))))
			goto cleanup
		}
	}
	rc = m_SQLITE_OK
	goto cleanup
cleanup:
	;
	return rc
	return r
}

type ___wchar_t = T___wchar_t

func _vec_eachColumn(tls *libc.TLS, cur uintptr, context uintptr, i int32) (r int32) {
	var pCur uintptr
	var x Tu8
	_, _ = pCur, x
	pCur = cur
	switch i {
	case m_VEC_EACH_COLUMN_VALUE:
		switch (*Tvec_each_cursor)(unsafe.Pointer(pCur)).Fvector_type {
		case int32(_SQLITE_VEC_ELEMENT_TYPE_FLOAT32):
			libsqlite3.Xsqlite3_result_double(tls, context, float64(**(**Tf32)(__ccgo_up((*Tvec_each_cursor)(unsafe.Pointer(pCur)).Fvector + uintptr((*Tvec_each_cursor)(unsafe.Pointer(pCur)).FiRowid)*4))))
		case int32(_SQLITE_VEC_ELEMENT_TYPE_BIT):
			x = **(**Tu8)(__ccgo_up((*Tvec_each_cursor)(unsafe.Pointer(pCur)).Fvector + uintptr((*Tvec_each_cursor)(unsafe.Pointer(pCur)).FiRowid/int64(m___CHAR_BIT))))
			libsqlite3.Xsqlite3_result_int(tls, context, libc.BoolInt32(libc.Int32FromUint8(x)&(int32(0b10000000)>>((*Tvec_each_cursor)(unsafe.Pointer(pCur)).FiRowid%int64(m___CHAR_BIT))) > 0))
		case int32(_SQLITE_VEC_ELEMENT_TYPE_INT8):
			libsqlite3.Xsqlite3_result_int(tls, context, int32(**(**Ti8)(__ccgo_up((*Tvec_each_cursor)(unsafe.Pointer(pCur)).Fvector + uintptr((*Tvec_each_cursor)(unsafe.Pointer(pCur)).FiRowid)))))
			break
		}
		break
	}
	return m_SQLITE_OK
}

type constraint_handler_t = Tconstraint_handler_t

type cookie_io_functions_t = Tcookie_io_functions_t

/*
 * See ISO/IEC 9945-1 ANSI/IEEE Std 1003.1 Second Edition 1996-07-12
 * B.8.2.7 for the rationale behind the *_unlocked() macros.
 */

/*
** 2001-09-15
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** This header file defines the interface that the SQLite library
** presents to client programs.  If a C-function, structure, datatype,
** or constant definition does not appear in this file, then it is
** not a published API of SQLite, is subject to change without
** notice, and should not be referenced by programs that use SQLite.
**
** Some of the definitions that are in this file are marked as
** "experimental".  Experimental interfaces are normally new
** features recently added to SQLite.  We do not anticipate changes
** to experimental interfaces but reserve the right to make minor changes
** if experience from use "in the wild" suggest such changes are prudent.
**
** The official C-language API documentation for SQLite is derived
** from comments in this file.  This file is the authoritative source
** on how SQLite interfaces are supposed to operate.
**
** The name of this file under configuration management is "sqlite.h.in".
** The makefile makes some minor changes to this file (such as inserting
** the version number) and changes its name to "sqlite3.h" as
** part of the build process.
 */

const m_CHAR_BIT = "__CHAR_BIT"

const m_CHILD_MAX = 40

const m_COLL_WEIGHTS_MAX = 10

const m_DBL_EPSILON = 2.2204460492503131e-16

const m_DBL_MAX = 1.7976931348623157e+308

const m_DBL_MIN = 2.2250738585072014e-308

const m_DBL_TRUE_MIN = 4.9406564584124654e-324

const m_EBADMSG = 89

const m_ECANCELED = 85

const m_ECAPMODE = 94

const m_EDOOFUS = 88

const m_EILSEQ = 86

const m_EINTEGRITY = 97

const m_ELAST = 97

const m_EMULTIHOP = 90

const m_ENOATTR = 87

const m_ENOLINK = 91

const m_ENOTCAPABLE = 93

const m_ENOTRECOVERABLE = 95

const m_EOWNERDEAD = 96

const m_EPROTO = 92

const m_FLT_EPSILON = 1.19209290e-07

const m_FLT_MAX = 3.40282347e+38

const m_FLT_MIN = 1.17549435e-38

const m_FLT_TRUE_MIN = 1.40129846e-45

const m_FP_ILOGBNAN = "__INT_MAX"

const m_INT_FAST8_MAX = "INT32_MAX"

const m_INT_FAST8_MIN = "INT32_MIN"

const m_INT_MAX = "__INT_MAX"

const m_INT_MIN = "__INT_MIN"

const m_LLONG_MAX = "__LLONG_MAX"

const m_LLONG_MIN = "__LLONG_MIN"

const m_LONG_BIT = "__LONG_BIT"

const m_LONG_MIN = "__LONG_MIN"

const m_L_cuserid = 17

const m_MB_LEN_MAX = 6

const m_MQ_PRIO_MAX = 64

const m_NGROUPS_MAX = 1023

const m_NL_ARGMAX = 4096

const m_NL_LANGMAX = 31

const m_OFF_MAX = "__OFF_MAX"

const m_OFF_MIN = "__OFF_MIN"

const m_QUAD_MAX = "__QUAD_MAX"

const m_QUAD_MIN = "__QUAD_MIN"

const m_SCHAR_MAX = "__SCHAR_MAX"

const m_SCHAR_MIN = "__SCHAR_MIN"

const m_SHRT_MAX = "__SHRT_MAX"

const m_SHRT_MIN = "__SHRT_MIN"

const m_SIZE_T_MAX = "__SIZE_T_MAX"

const m_SSIZE_MAX = "__SSIZE_MAX"

const m_UCHAR_MAX = "__UCHAR_MAX"

const m_UINT_FAST8_MAX = "UINT32_MAX"

const m_UINT_MAX = "__UINT_MAX"

const m_ULLONG_MAX = "__ULLONG_MAX"

const m_ULONG_MAX = "__ULONG_MAX"

const m_UQUAD_MAX = "__UQUAD_MAX"

const m_USHRT_MAX = "__USHRT_MAX"

const m_WCHAR_MAX = "__WCHAR_MAX"

const m_WCHAR_MIN = "__WCHAR_MIN"

const m_WORD_BIT = "__WORD_BIT"

const m__POSIX2_EQUIV_CLASS_MAX = 2

const m__POSIX_RE_DUP_MAX = "_POSIX2_RE_DUP_MAX"

const m___CC_SUPPORTS___INLINE = 1

const m___CHAR_BIT = 8

const m___EXT1_VISIBLE = 1

const m___INT_MAX = 0x7fffffff

const m___ISO_C_VISIBLE = 2023

const m___KPRINTF_ATTRIBUTE__ = 1

const m___LLONG_MAX = 0x7fffffffffffffff

const m___S2OAP = 0x0001

const m___SCHAR_MAX = 0x7f

const m___SHRT_MAX = 0x7fff

const m___STDC_MB_MIGHT_NEQ_WC__ = 1

const m___UCHAR_MAX = 0xff

const m___UINT_MAX = 0xffffffff

const m___ULLONG_MAX = "0xffffffffffffffffU"

const m___USHRT_MAX = 0xffff

const m___VERSION__ = "FreeBSD Clang 19.1.7 (https://github.com/llvm/llvm-project.git llvmorg-19.1.7-0-gcd708029e0b2)"

const m___WORD_BIT = 32

const m___XSI_VISIBLE = 800

const m___clang_version__ = "19.1.7 (https://github.com/llvm/llvm-project.git llvmorg-19.1.7-0-gcd708029e0b2)"

const m___isnan = "__inline_isnan"

const m___isnanf = "__inline_isnanf"

const m___writeonly = "__unused"

type t__accmode_t = int32

type t__char16_t = uint16

type t__char32_t = uint32

type t__cpulevel_t = int32

type t__cpusetid_t = int32

type t__cpuwhich_t = int32

type t__ct_rune_t = int32

type t__daddr_t = int64

type t__dev_t = uint64

type t__fflags_t = uint32

type t__id_t = int64

type t__lwpid_t = int32

type t__mode_t = uint16

type t__mqd_t = uintptr

type t__nl_item = int32

type t__nlink_t = uint64

type t__off64_t = int64

type t__rlim_t = int64

type t__rman_res_t = uint64

type t__sbintime_t = int64

type t__timer_t = uintptr
