// 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)

package vec

import (
	"unsafe"

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

// C documentation
//
//	/**
//	 * @brief Initial an array with the given element size and capacity.
//	 *
//	 * @param array
//	 * @param element_size
//	 * @param init_capacity
//	 * @return SQLITE_OK on success, error code on failure. Only error is
//	 * SQLITE_NOMEM
//	 */
func Xarray_init(tls *libc.TLS, array uintptr, element_size Tsize_t, init_capacity Tsize_t) (r int32) {
	var sz int32
	var z uintptr
	_, _ = sz, z
	sz = libc.Int32FromUint64(element_size * init_capacity)
	z = libsqlite3.Xsqlite3_malloc(tls, sz)
	if !(z != 0) {
		return int32(m_SQLITE_NOMEM)
	}
	libc.Xmemset(tls, z, 0, libc.Uint64FromInt32(sz))
	(*TArray)(unsafe.Pointer(array)).Felement_size = element_size
	(*TArray)(unsafe.Pointer(array)).Flength = uint64(0)
	(*TArray)(unsafe.Pointer(array)).Fcapacity = init_capacity
	(*TArray)(unsafe.Pointer(array)).Fz = z
	return m_SQLITE_OK
}

func Xparse_npy_buffer(tls *libc.TLS, pVTab uintptr, buffer uintptr, bufferLength int32, data uintptr, numElements uintptr, numDimensions uintptr, element_type uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var dataSize, expectedDataSize, totalHeaderLength Ti32
	var header uintptr
	var major, minor Tu8
	var rc int32
	var _ /* fortran_order at bp+4 */ int32
	var _ /* headerLength at bp+0 */ Tuint16_t
	_, _, _, _, _, _, _ = dataSize, expectedDataSize, header, major, minor, rc, totalHeaderLength
	if bufferLength < int32(10) {
		// IMP: V03312_20150
		Xvtab_set_error(tls, pVTab, __ccgo_ts+3171, 0)
		return int32(m_SQLITE_ERROR)
	}
	if libc.Xmemcmp(tls, uintptr(unsafe.Pointer(&_NPY_MAGIC)), buffer, uint64(6)) != 0 {
		// V11954_28792
		Xvtab_set_error(tls, pVTab, __ccgo_ts+3193, 0)
		return int32(m_SQLITE_ERROR)
	}
	major = **(**uint8)(__ccgo_up(buffer + 6))
	minor = **(**uint8)(__ccgo_up(buffer + 7))
	**(**Tuint16_t)(__ccgo_up(bp)) = uint16(0)
	libc.Xmemcpy(tls, bp, buffer+8, uint64(2))
	totalHeaderLength = libc.Int32FromUint64(libc.Uint64FromInt64(6) + libc.Uint64FromInt64(1) + libc.Uint64FromInt64(1) + libc.Uint64FromInt64(2) + uint64(**(**Tuint16_t)(__ccgo_up(bp))))
	dataSize = bufferLength - totalHeaderLength
	if dataSize < 0 {
		Xvtab_set_error(tls, pVTab, __ccgo_ts+3241, 0)
		return int32(m_SQLITE_ERROR)
	}
	header = buffer + 10
	rc = Xparse_npy_header(tls, pVTab, header, uint64(**(**Tuint16_t)(__ccgo_up(bp))), element_type, bp+4, numElements, numDimensions)
	if rc != m_SQLITE_OK {
		return rc
	}
	expectedDataSize = libc.Int32FromUint64(**(**Tsize_t)(__ccgo_up(numElements)) * Xvector_byte_size(tls, **(**_VectorElementType)(__ccgo_up(element_type)), **(**Tsize_t)(__ccgo_up(numDimensions))))
	if expectedDataSize != dataSize {
		Xvtab_set_error(tls, pVTab, __ccgo_ts+3278, libc.VaList(bp+16, expectedDataSize, dataSize))
		return int32(m_SQLITE_ERROR)
	}
	**(**uintptr)(__ccgo_up(data)) = buffer + uintptr(totalHeaderLength)
	return m_SQLITE_OK
}

func Xvec0Update_Delete_ClearVectors(tls *libc.TLS, p uintptr, chunk_id Ti64, chunk_offset Tu64) (r int32) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var brc, i, rc int32
	var n Tsize_t
	var zeroBuf uintptr
	var _ /* blobVectors at bp+0 */ uintptr
	_, _, _, _, _ = brc, i, n, rc, zeroBuf
	i = 0
	for {
		if !(i < (*Tvec0_vtab)(unsafe.Pointer(p)).FnumVectorColumns) {
			break
		}
		**(**uintptr)(__ccgo_up(bp)) = libc.UintptrFromInt32(0)
		n = Xvector_column_byte_size(tls, **(**TVectorColumnDefinition)(__ccgo_up(p + 608 + uintptr(i)*32)))
		rc = libsqlite3.Xsqlite3_blob_open(tls, (*Tvec0_vtab)(unsafe.Pointer(p)).Fdb, (*Tvec0_vtab)(unsafe.Pointer(p)).FschemaName, **(**uintptr)(__ccgo_up(p + 352 + uintptr(i)*8)), __ccgo_ts+3712, chunk_id, int32(1), bp)
		if rc != m_SQLITE_OK {
			Xvtab_set_error(tls, p, __ccgo_ts+14213, libc.VaList(bp+16, (*Tvec0_vtab)(unsafe.Pointer(p)).FschemaName, **(**uintptr)(__ccgo_up(p + 352 + uintptr(i)*8)), chunk_id, i))
			return int32(m_SQLITE_ERROR)
		}
		zeroBuf = libsqlite3.Xsqlite3_malloc(tls, libc.Int32FromUint64(n))
		if !(zeroBuf != 0) {
			libsqlite3.Xsqlite3_blob_close(tls, **(**uintptr)(__ccgo_up(bp)))
			return int32(m_SQLITE_NOMEM)
		}
		libc.Xmemset(tls, zeroBuf, 0, n)
		rc = libsqlite3.Xsqlite3_blob_write(tls, **(**uintptr)(__ccgo_up(bp)), zeroBuf, libc.Int32FromUint64(n), libc.Int32FromUint64(chunk_offset*n))
		libsqlite3.Xsqlite3_free(tls, zeroBuf)
		if rc != m_SQLITE_OK {
			Xvtab_set_error(tls, p, __ccgo_ts+14265, libc.VaList(bp+16, (*Tvec0_vtab)(unsafe.Pointer(p)).FschemaName, **(**uintptr)(__ccgo_up(p + 352 + uintptr(i)*8)), chunk_id, chunk_offset, i))
		}
		brc = libsqlite3.Xsqlite3_blob_close(tls, **(**uintptr)(__ccgo_up(bp)))
		if rc != m_SQLITE_OK {
			return rc
		}
		if brc != m_SQLITE_OK {
			Xvtab_set_error(tls, p, __ccgo_ts+14329, libc.VaList(bp+16, (*Tvec0_vtab)(unsafe.Pointer(p)).FschemaName, **(**uintptr)(__ccgo_up(p + 352 + uintptr(i)*8)), chunk_id, i))
			return brc
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	return m_SQLITE_OK
}

func _vec0_init(tls *libc.TLS, db uintptr, pAux uintptr, argc int32, argv uintptr, ppVtab uintptr, pzErr uintptr, isCreate uint8) (r int32) {
	bp := tls.Alloc(208)
	defer tls.Free(208)
	var auxiliary_idx, chunk_size, i, i1, i2, i3, i4, i5, i6, i7, metadata_idx, numAuxiliaryColumns, numMetadataColumns, numPartitionColumns, numVectorColumns, partition_idx, pkColumnNameLength, pkColumnType, rc, rc1, user_column_idx, vector_idx int32
	var createStr, pNew, pkColumnName, s, s1, schemaName, tableName, zCreateInfo, zCreateShadowChunks, zCreateShadowRowids, zSeedInfo, zSql, zSql1, zSql2, zSql3, zSql4 uintptr
	var _ /* auxColumn at bp+56 */ TVec0AuxiliaryColumnDefinition
	var _ /* cName at bp+104 */ uintptr
	var _ /* cNameLength at bp+112 */ int32
	var _ /* cType at bp+116 */ int32
	var _ /* key at bp+128 */ uintptr
	var _ /* keyLength at bp+144 */ int32
	var _ /* kind at bp+120 */ Tvec0_metadata_column_kind
	var _ /* metadataColumn at bp+80 */ TVec0MetadataColumnDefinition
	var _ /* partitionColumn at bp+32 */ TVec0PartitionColumnDefinition
	var _ /* stmt at bp+152 */ uintptr
	var _ /* stmt at bp+160 */ uintptr
	var _ /* value at bp+136 */ uintptr
	var _ /* valueLength at bp+148 */ int32
	var _ /* vecColumn at bp+0 */ TVectorColumnDefinition
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = auxiliary_idx, chunk_size, createStr, i, i1, i2, i3, i4, i5, i6, i7, metadata_idx, numAuxiliaryColumns, numMetadataColumns, numPartitionColumns, numVectorColumns, pNew, partition_idx, pkColumnName, pkColumnNameLength, pkColumnType, rc, rc1, s, s1, schemaName, tableName, user_column_idx, vector_idx, zCreateInfo, zCreateShadowChunks, zCreateShadowRowids, zSeedInfo, zSql, zSql1, zSql2, zSql3, zSql4
	_ = pAux
	pNew = libsqlite3.Xsqlite3_malloc(tls, int32(2032))
	if pNew == uintptr(0) {
		return int32(m_SQLITE_NOMEM)
	}
	libc.Xmemset(tls, pNew, 0, uint64(2032))
	// Declared chunk_size=N for entire table.
	// -1 to use the defualt, otherwise will get re-assigned on `chunk_size=N`
	// option
	chunk_size = -int32(1)
	numVectorColumns = 0
	numPartitionColumns = 0
	numAuxiliaryColumns = 0
	numMetadataColumns = 0
	user_column_idx = 0
	// track if a "primary key" column is defined
	pkColumnName = libc.UintptrFromInt32(0)
	pkColumnType = int32(m_SQLITE_INTEGER)
	i = int32(3)
	for {
		if !(i < argc) {
			break
		}
		**(**uintptr)(__ccgo_up(bp + 104)) = libc.UintptrFromInt32(0)
		// Scenario #1: Constructor argument is a vector column definition, ie `foo float[1024]`
		rc = Xvec0_parse_vector_column(tls, **(**uintptr)(__ccgo_up(argv + uintptr(i)*8)), libc.Int32FromUint64(libc.Xstrlen(tls, **(**uintptr)(__ccgo_up(argv + uintptr(i)*8)))), bp)
		if rc == int32(m_SQLITE_ERROR) {
			**(**uintptr)(__ccgo_up(pzErr)) = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+5489, libc.VaList(bp+176, **(**uintptr)(__ccgo_up(argv + uintptr(i)*8))))
			goto error
		}
		if rc == m_SQLITE_OK {
			if numVectorColumns >= int32(m_VEC0_MAX_VECTOR_COLUMNS) {
				libsqlite3.Xsqlite3_free(tls, (**(**TVectorColumnDefinition)(__ccgo_up(bp))).Fname)
				**(**uintptr)(__ccgo_up(pzErr)) = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+5548, libc.VaList(bp+176, int32(m_VEC0_MAX_VECTOR_COLUMNS)))
				goto error
			}
			if (**(**TVectorColumnDefinition)(__ccgo_up(bp))).Fdimensions > uint64(m_SQLITE_VEC_VEC0_MAX_DIMENSIONS) {
				libsqlite3.Xsqlite3_free(tls, (**(**TVectorColumnDefinition)(__ccgo_up(bp))).Fname)
				**(**uintptr)(__ccgo_up(pzErr)) = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+5617, libc.VaList(bp+176, libc.Int64FromUint64((**(**TVectorColumnDefinition)(__ccgo_up(bp))).Fdimensions), int32(m_SQLITE_VEC_VEC0_MAX_DIMENSIONS)))
				goto error
			}
			**(**Tvec0_user_column_kind)(__ccgo_up(pNew + 88 + uintptr(user_column_idx)*4)) = int32(_SQLITE_VEC0_USER_COLUMN_KIND_VECTOR)
			**(**Tuint8_t)(__ccgo_up(pNew + 296 + uintptr(user_column_idx))) = libc.Uint8FromInt32(numVectorColumns)
			libc.Xmemcpy(tls, pNew+608+uintptr(numVectorColumns)*32, bp, uint64(32))
			numVectorColumns = numVectorColumns + 1
			(*Tvec0_vtab)(unsafe.Pointer(pNew)).FnumVectorColumns = numVectorColumns
			user_column_idx = user_column_idx + 1
			goto _1
		}
		// Scenario #2: Constructor argument is a partition key column definition, ie `user_id text partition key`
		rc = Xvec0_parse_partition_key_definition(tls, **(**uintptr)(__ccgo_up(argv + uintptr(i)*8)), libc.Int32FromUint64(libc.Xstrlen(tls, **(**uintptr)(__ccgo_up(argv + uintptr(i)*8)))), bp+104, bp+112, bp+116)
		if rc == m_SQLITE_OK {
			if numPartitionColumns >= int32(m_VEC0_MAX_PARTITION_COLUMNS) {
				**(**uintptr)(__ccgo_up(pzErr)) = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+5707, libc.VaList(bp+176, int32(m_VEC0_MAX_PARTITION_COLUMNS)))
				goto error
			}
			(**(**TVec0PartitionColumnDefinition)(__ccgo_up(bp + 32))).Ftype1 = **(**int32)(__ccgo_up(bp + 116))
			(**(**TVec0PartitionColumnDefinition)(__ccgo_up(bp + 32))).Fname_length = **(**int32)(__ccgo_up(bp + 112))
			(**(**TVec0PartitionColumnDefinition)(__ccgo_up(bp + 32))).Fname = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+1874, libc.VaList(bp+176, **(**int32)(__ccgo_up(bp + 112)), **(**uintptr)(__ccgo_up(bp + 104))))
			if !((**(**TVec0PartitionColumnDefinition)(__ccgo_up(bp + 32))).Fname != 0) {
				rc = int32(m_SQLITE_NOMEM)
				goto error
			}
			**(**Tvec0_user_column_kind)(__ccgo_up(pNew + 88 + uintptr(user_column_idx)*4)) = int32(_SQLITE_VEC0_USER_COLUMN_KIND_PARTITION)
			**(**Tuint8_t)(__ccgo_up(pNew + 296 + uintptr(user_column_idx))) = libc.Uint8FromInt32(numPartitionColumns)
			libc.Xmemcpy(tls, pNew+1120+uintptr(numPartitionColumns)*24, bp+32, uint64(24))
			numPartitionColumns = numPartitionColumns + 1
			(*Tvec0_vtab)(unsafe.Pointer(pNew)).FnumPartitionColumns = numPartitionColumns
			user_column_idx = user_column_idx + 1
			goto _1
		}
		// Scenario #3: Constructor argument is a primary key column definition, ie `article_id text primary key`
		rc = Xvec0_parse_primary_key_definition(tls, **(**uintptr)(__ccgo_up(argv + uintptr(i)*8)), libc.Int32FromUint64(libc.Xstrlen(tls, **(**uintptr)(__ccgo_up(argv + uintptr(i)*8)))), bp+104, bp+112, bp+116)
		if rc == m_SQLITE_OK {
			if pkColumnName != 0 {
				**(**uintptr)(__ccgo_up(pzErr)) = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+5780, libc.VaList(bp+176, **(**uintptr)(__ccgo_up(argv + uintptr(i)*8))))
				goto error
			}
			pkColumnName = **(**uintptr)(__ccgo_up(bp + 104))
			pkColumnNameLength = **(**int32)(__ccgo_up(bp + 112))
			pkColumnType = **(**int32)(__ccgo_up(bp + 116))
			goto _1
		}
		// Scenario #4: Constructor argument is a auxiliary column definition, ie `+contents text`
		rc = Xvec0_parse_auxiliary_column_definition(tls, **(**uintptr)(__ccgo_up(argv + uintptr(i)*8)), libc.Int32FromUint64(libc.Xstrlen(tls, **(**uintptr)(__ccgo_up(argv + uintptr(i)*8)))), bp+104, bp+112, bp+116)
		if rc == m_SQLITE_OK {
			if numAuxiliaryColumns >= int32(m_VEC0_MAX_AUXILIARY_COLUMNS) {
				**(**uintptr)(__ccgo_up(pzErr)) = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+5901, libc.VaList(bp+176, int32(m_VEC0_MAX_AUXILIARY_COLUMNS)))
				goto error
			}
			(**(**TVec0AuxiliaryColumnDefinition)(__ccgo_up(bp + 56))).Ftype1 = **(**int32)(__ccgo_up(bp + 116))
			(**(**TVec0AuxiliaryColumnDefinition)(__ccgo_up(bp + 56))).Fname_length = **(**int32)(__ccgo_up(bp + 112))
			(**(**TVec0AuxiliaryColumnDefinition)(__ccgo_up(bp + 56))).Fname = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+1874, libc.VaList(bp+176, **(**int32)(__ccgo_up(bp + 112)), **(**uintptr)(__ccgo_up(bp + 104))))
			if !((**(**TVec0AuxiliaryColumnDefinition)(__ccgo_up(bp + 56))).Fname != 0) {
				rc = int32(m_SQLITE_NOMEM)
				goto error
			}
			**(**Tvec0_user_column_kind)(__ccgo_up(pNew + 88 + uintptr(user_column_idx)*4)) = int32(_SQLITE_VEC0_USER_COLUMN_KIND_AUXILIARY)
			**(**Tuint8_t)(__ccgo_up(pNew + 296 + uintptr(user_column_idx))) = libc.Uint8FromInt32(numAuxiliaryColumns)
			libc.Xmemcpy(tls, pNew+1216+uintptr(numAuxiliaryColumns)*24, bp+56, uint64(24))
			numAuxiliaryColumns = numAuxiliaryColumns + 1
			(*Tvec0_vtab)(unsafe.Pointer(pNew)).FnumAuxiliaryColumns = numAuxiliaryColumns
			user_column_idx = user_column_idx + 1
			goto _1
		}
		rc = Xvec0_parse_metadata_column_definition(tls, **(**uintptr)(__ccgo_up(argv + uintptr(i)*8)), libc.Int32FromUint64(libc.Xstrlen(tls, **(**uintptr)(__ccgo_up(argv + uintptr(i)*8)))), bp+104, bp+112, bp+120)
		if rc == m_SQLITE_OK {
			if numMetadataColumns >= int32(m_VEC0_MAX_METADATA_COLUMNS) {
				**(**uintptr)(__ccgo_up(pzErr)) = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+5970, libc.VaList(bp+176, int32(m_VEC0_MAX_METADATA_COLUMNS)))
				goto error
			}
			(**(**TVec0MetadataColumnDefinition)(__ccgo_up(bp + 80))).Fkind = **(**Tvec0_metadata_column_kind)(__ccgo_up(bp + 120))
			(**(**TVec0MetadataColumnDefinition)(__ccgo_up(bp + 80))).Fname_length = **(**int32)(__ccgo_up(bp + 112))
			(**(**TVec0MetadataColumnDefinition)(__ccgo_up(bp + 80))).Fname = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+1874, libc.VaList(bp+176, **(**int32)(__ccgo_up(bp + 112)), **(**uintptr)(__ccgo_up(bp + 104))))
			if !((**(**TVec0MetadataColumnDefinition)(__ccgo_up(bp + 80))).Fname != 0) {
				rc = int32(m_SQLITE_NOMEM)
				goto error
			}
			**(**Tvec0_user_column_kind)(__ccgo_up(pNew + 88 + uintptr(user_column_idx)*4)) = int32(_SQLITE_VEC0_USER_COLUMN_KIND_METADATA)
			**(**Tuint8_t)(__ccgo_up(pNew + 296 + uintptr(user_column_idx))) = libc.Uint8FromInt32(numMetadataColumns)
			libc.Xmemcpy(tls, pNew+1600+uintptr(numMetadataColumns)*24, bp+80, uint64(24))
			numMetadataColumns = numMetadataColumns + 1
			(*Tvec0_vtab)(unsafe.Pointer(pNew)).FnumMetadataColumns = numMetadataColumns
			user_column_idx = user_column_idx + 1
			goto _1
		}
		rc = Xvec0_parse_table_option(tls, **(**uintptr)(__ccgo_up(argv + uintptr(i)*8)), libc.Int32FromUint64(libc.Xstrlen(tls, **(**uintptr)(__ccgo_up(argv + uintptr(i)*8)))), bp+128, bp+144, bp+136, bp+148)
		if rc == int32(m_SQLITE_ERROR) {
			**(**uintptr)(__ccgo_up(pzErr)) = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+6038, libc.VaList(bp+176, **(**uintptr)(__ccgo_up(argv + uintptr(i)*8))))
			goto error
		}
		if rc == m_SQLITE_OK {
			if libsqlite3.Xsqlite3_strnicmp(tls, **(**uintptr)(__ccgo_up(bp + 128)), __ccgo_ts+6096, **(**int32)(__ccgo_up(bp + 144))) == 0 {
				chunk_size = libc.Xatoi(tls, **(**uintptr)(__ccgo_up(bp + 136)))
				if chunk_size <= 0 {
					// IMP: V01931_18769
					**(**uintptr)(__ccgo_up(pzErr)) = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+6107, 0)
					goto error
				}
				if chunk_size%int32(8) != 0 {
					// IMP: V14110_30948
					**(**uintptr)(__ccgo_up(pzErr)) = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+6178, 0)
					goto error
				}
				if chunk_size > int32(m_SQLITE_VEC_CHUNK_SIZE_MAX) {
					**(**uintptr)(__ccgo_up(pzErr)) = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+6236, 0)
					goto error
				}
			} else {
				// IMP: V27642_11712
				**(**uintptr)(__ccgo_up(pzErr)) = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+6281, libc.VaList(bp+176, **(**int32)(__ccgo_up(bp + 144)), **(**uintptr)(__ccgo_up(bp + 128))))
				goto error
			}
			goto _1
		}
		// Scenario #5: Unknown constructor argument
		**(**uintptr)(__ccgo_up(pzErr)) = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+6332, libc.VaList(bp+176, **(**uintptr)(__ccgo_up(argv + uintptr(i)*8))))
		goto error
		goto _1
	_1:
		;
		i = i + 1
	}
	if chunk_size < 0 {
		chunk_size = int32(1024)
	}
	if numVectorColumns <= 0 {
		**(**uintptr)(__ccgo_up(pzErr)) = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+6377, 0)
		goto error
	}
	createStr = libsqlite3.Xsqlite3_str_new(tls, libc.UintptrFromInt32(0))
	libsqlite3.Xsqlite3_str_appendall(tls, createStr, __ccgo_ts+6440)
	if pkColumnName != 0 {
		libsqlite3.Xsqlite3_str_appendf(tls, createStr, __ccgo_ts+6456, libc.VaList(bp+176, pkColumnNameLength, pkColumnName))
	} else {
		libsqlite3.Xsqlite3_str_appendall(tls, createStr, __ccgo_ts+6477)
	}
	i1 = 0
	for {
		if !(i1 < numVectorColumns+numPartitionColumns+numAuxiliaryColumns+numMetadataColumns) {
			break
		}
		switch **(**Tvec0_user_column_kind)(__ccgo_up(pNew + 88 + uintptr(i1)*4)) {
		case int32(_SQLITE_VEC0_USER_COLUMN_KIND_VECTOR):
			vector_idx = libc.Int32FromUint8(**(**Tuint8_t)(__ccgo_up(pNew + 296 + uintptr(i1))))
			libsqlite3.Xsqlite3_str_appendf(tls, createStr, __ccgo_ts+6485, libc.VaList(bp+176, (**(**TVectorColumnDefinition)(__ccgo_up(pNew + 608 + uintptr(vector_idx)*32))).Fname_length, (**(**TVectorColumnDefinition)(__ccgo_up(pNew + 608 + uintptr(vector_idx)*32))).Fname))
		case int32(_SQLITE_VEC0_USER_COLUMN_KIND_PARTITION):
			partition_idx = libc.Int32FromUint8(**(**Tuint8_t)(__ccgo_up(pNew + 296 + uintptr(i1))))
			libsqlite3.Xsqlite3_str_appendf(tls, createStr, __ccgo_ts+6485, libc.VaList(bp+176, (**(**TVec0PartitionColumnDefinition)(__ccgo_up(pNew + 1120 + uintptr(partition_idx)*24))).Fname_length, (**(**TVec0PartitionColumnDefinition)(__ccgo_up(pNew + 1120 + uintptr(partition_idx)*24))).Fname))
		case int32(_SQLITE_VEC0_USER_COLUMN_KIND_AUXILIARY):
			auxiliary_idx = libc.Int32FromUint8(**(**Tuint8_t)(__ccgo_up(pNew + 296 + uintptr(i1))))
			libsqlite3.Xsqlite3_str_appendf(tls, createStr, __ccgo_ts+6485, libc.VaList(bp+176, (**(**TVec0AuxiliaryColumnDefinition)(__ccgo_up(pNew + 1216 + uintptr(auxiliary_idx)*24))).Fname_length, (**(**TVec0AuxiliaryColumnDefinition)(__ccgo_up(pNew + 1216 + uintptr(auxiliary_idx)*24))).Fname))
		case int32(_SQLITE_VEC0_USER_COLUMN_KIND_METADATA):
			metadata_idx = libc.Int32FromUint8(**(**Tuint8_t)(__ccgo_up(pNew + 296 + uintptr(i1))))
			libsqlite3.Xsqlite3_str_appendf(tls, createStr, __ccgo_ts+6485, libc.VaList(bp+176, (**(**TVec0MetadataColumnDefinition)(__ccgo_up(pNew + 1600 + uintptr(metadata_idx)*24))).Fname_length, (**(**TVec0MetadataColumnDefinition)(__ccgo_up(pNew + 1600 + uintptr(metadata_idx)*24))).Fname))
			break
		}
		goto _2
	_2:
		;
		i1 = i1 + 1
	}
	libsqlite3.Xsqlite3_str_appendall(tls, createStr, __ccgo_ts+6494)
	if pkColumnName != 0 {
		libsqlite3.Xsqlite3_str_appendall(tls, createStr, __ccgo_ts+6523)
	}
	zSql = libsqlite3.Xsqlite3_str_finish(tls, createStr)
	if !(zSql != 0) {
		goto error
	}
	rc = libsqlite3.Xsqlite3_declare_vtab(tls, db, zSql)
	libsqlite3.Xsqlite3_free(tls, zSql)
	if rc != m_SQLITE_OK {
		**(**uintptr)(__ccgo_up(pzErr)) = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+6538, libc.VaList(bp+176, libsqlite3.Xsqlite3_errmsg(tls, db)))
		goto error
	}
	schemaName = **(**uintptr)(__ccgo_up(argv + 1*8))
	tableName = **(**uintptr)(__ccgo_up(argv + 2*8))
	(*Tvec0_vtab)(unsafe.Pointer(pNew)).Fdb = db
	(*Tvec0_vtab)(unsafe.Pointer(pNew)).FpkIsText = libc.BoolInt32(pkColumnType == int32(m_SQLITE_TEXT))
	(*Tvec0_vtab)(unsafe.Pointer(pNew)).FschemaName = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+6600, libc.VaList(bp+176, schemaName))
	if !((*Tvec0_vtab)(unsafe.Pointer(pNew)).FschemaName != 0) {
		goto error
	}
	(*Tvec0_vtab)(unsafe.Pointer(pNew)).FtableName = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+6600, libc.VaList(bp+176, tableName))
	if !((*Tvec0_vtab)(unsafe.Pointer(pNew)).FtableName != 0) {
		goto error
	}
	(*Tvec0_vtab)(unsafe.Pointer(pNew)).FshadowRowidsName = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+6603, libc.VaList(bp+176, tableName))
	if !((*Tvec0_vtab)(unsafe.Pointer(pNew)).FshadowRowidsName != 0) {
		goto error
	}
	(*Tvec0_vtab)(unsafe.Pointer(pNew)).FshadowChunksName = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+6613, libc.VaList(bp+176, tableName))
	if !((*Tvec0_vtab)(unsafe.Pointer(pNew)).FshadowChunksName != 0) {
		goto error
	}
	(*Tvec0_vtab)(unsafe.Pointer(pNew)).FnumVectorColumns = numVectorColumns
	(*Tvec0_vtab)(unsafe.Pointer(pNew)).FnumPartitionColumns = numPartitionColumns
	(*Tvec0_vtab)(unsafe.Pointer(pNew)).FnumAuxiliaryColumns = numAuxiliaryColumns
	(*Tvec0_vtab)(unsafe.Pointer(pNew)).FnumMetadataColumns = numMetadataColumns
	i2 = 0
	for {
		if !(i2 < (*Tvec0_vtab)(unsafe.Pointer(pNew)).FnumVectorColumns) {
			break
		}
		**(**uintptr)(__ccgo_up(pNew + 352 + uintptr(i2)*8)) = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+6623, libc.VaList(bp+176, tableName, i2))
		if !(**(**uintptr)(__ccgo_up(pNew + 352 + uintptr(i2)*8)) != 0) {
			goto error
		}
		goto _3
	_3:
		;
		i2 = i2 + 1
	}
	i3 = 0
	for {
		if !(i3 < (*Tvec0_vtab)(unsafe.Pointer(pNew)).FnumMetadataColumns) {
			break
		}
		**(**uintptr)(__ccgo_up(pNew + 480 + uintptr(i3)*8)) = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+6644, libc.VaList(bp+176, tableName, i3))
		if !(**(**uintptr)(__ccgo_up(pNew + 480 + uintptr(i3)*8)) != 0) {
			goto error
		}
		goto _4
	_4:
		;
		i3 = i3 + 1
	}
	(*Tvec0_vtab)(unsafe.Pointer(pNew)).Fchunk_size = chunk_size
	// if xCreate, then create the necessary shadow tables
	if isCreate != 0 {
		zCreateInfo = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+6666, libc.VaList(bp+176, (*Tvec0_vtab)(unsafe.Pointer(pNew)).FschemaName, (*Tvec0_vtab)(unsafe.Pointer(pNew)).FtableName))
		if !(zCreateInfo != 0) {
			goto error
		}
		rc1 = libsqlite3.Xsqlite3_prepare_v2(tls, db, zCreateInfo, -int32(1), bp+152, libc.UintptrFromInt32(0))
		libsqlite3.Xsqlite3_free(tls, zCreateInfo)
		if rc1 != m_SQLITE_OK || libsqlite3.Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp + 152))) != int32(m_SQLITE_DONE) {
			// TODO(IMP)
			libsqlite3.Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp + 152)))
			**(**uintptr)(__ccgo_up(pzErr)) = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+6728, libc.VaList(bp+176, libsqlite3.Xsqlite3_errmsg(tls, db)))
			goto error
		}
		libsqlite3.Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp + 152)))
		zSeedInfo = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+6770, libc.VaList(bp+176, (*Tvec0_vtab)(unsafe.Pointer(pNew)).FschemaName, (*Tvec0_vtab)(unsafe.Pointer(pNew)).FtableName))
		if !(zSeedInfo != 0) {
			goto error
		}
		rc1 = libsqlite3.Xsqlite3_prepare_v2(tls, db, zSeedInfo, -int32(1), bp+152, libc.UintptrFromInt32(0))
		libsqlite3.Xsqlite3_free(tls, zSeedInfo)
		if rc1 != m_SQLITE_OK {
			// TODO(IMP)
			libsqlite3.Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp + 152)))
			**(**uintptr)(__ccgo_up(pzErr)) = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+6856, libc.VaList(bp+176, libsqlite3.Xsqlite3_errmsg(tls, db)))
			goto error
		}
		libsqlite3.Xsqlite3_bind_text(tls, **(**uintptr)(__ccgo_up(bp + 152)), int32(1), __ccgo_ts+6896, -int32(1), libc.UintptrFromInt32(0))
		libsqlite3.Xsqlite3_bind_text(tls, **(**uintptr)(__ccgo_up(bp + 152)), int32(2), __ccgo_ts+6911, -int32(1), libc.UintptrFromInt32(0))
		libsqlite3.Xsqlite3_bind_text(tls, **(**uintptr)(__ccgo_up(bp + 152)), int32(3), __ccgo_ts+6918, -int32(1), libc.UintptrFromInt32(0))
		libsqlite3.Xsqlite3_bind_int(tls, **(**uintptr)(__ccgo_up(bp + 152)), int32(4), m_SQLITE_VEC_VERSION_MAJOR)
		libsqlite3.Xsqlite3_bind_text(tls, **(**uintptr)(__ccgo_up(bp + 152)), int32(5), __ccgo_ts+6939, -int32(1), libc.UintptrFromInt32(0))
		libsqlite3.Xsqlite3_bind_int(tls, **(**uintptr)(__ccgo_up(bp + 152)), int32(6), int32(m_SQLITE_VEC_VERSION_MINOR))
		libsqlite3.Xsqlite3_bind_text(tls, **(**uintptr)(__ccgo_up(bp + 152)), int32(7), __ccgo_ts+6960, -int32(1), libc.UintptrFromInt32(0))
		libsqlite3.Xsqlite3_bind_int(tls, **(**uintptr)(__ccgo_up(bp + 152)), int32(8), int32(m_SQLITE_VEC_VERSION_PATCH))
		if libsqlite3.Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp + 152))) != int32(m_SQLITE_DONE) {
			// TODO(IMP)
			libsqlite3.Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp + 152)))
			**(**uintptr)(__ccgo_up(pzErr)) = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+6856, libc.VaList(bp+176, libsqlite3.Xsqlite3_errmsg(tls, db)))
			goto error
		}
		libsqlite3.Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp + 152)))
		// create the _chunks shadow table
		zCreateShadowChunks = libc.UintptrFromInt32(0)
		if (*Tvec0_vtab)(unsafe.Pointer(pNew)).FnumPartitionColumns != 0 {
			s = libsqlite3.Xsqlite3_str_new(tls, libc.UintptrFromInt32(0))
			libsqlite3.Xsqlite3_str_appendf(tls, s, __ccgo_ts+6981, libc.VaList(bp+176, (*Tvec0_vtab)(unsafe.Pointer(pNew)).FschemaName, (*Tvec0_vtab)(unsafe.Pointer(pNew)).FtableName))
			libsqlite3.Xsqlite3_str_appendall(tls, s, __ccgo_ts+7012)
			libsqlite3.Xsqlite3_str_appendall(tls, s, __ccgo_ts+7078)
			i4 = 0
			for {
				if !(i4 < (*Tvec0_vtab)(unsafe.Pointer(pNew)).FnumPartitionColumns) {
					break
				}
				libsqlite3.Xsqlite3_str_appendf(tls, s, __ccgo_ts+7099, libc.VaList(bp+176, i4))
				goto _5
			_5:
				;
				i4 = i4 + 1
			}
			libsqlite3.Xsqlite3_str_appendall(tls, s, __ccgo_ts+7114)
			zCreateShadowChunks = libsqlite3.Xsqlite3_str_finish(tls, s)
		} else {
			zCreateShadowChunks = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+7161, libc.VaList(bp+176, (*Tvec0_vtab)(unsafe.Pointer(pNew)).FschemaName, (*Tvec0_vtab)(unsafe.Pointer(pNew)).FtableName))
		}
		if !(zCreateShadowChunks != 0) {
			goto error
		}
		rc1 = libsqlite3.Xsqlite3_prepare_v2(tls, db, zCreateShadowChunks, -int32(1), bp+152, uintptr(0))
		libsqlite3.Xsqlite3_free(tls, zCreateShadowChunks)
		if rc1 != m_SQLITE_OK || libsqlite3.Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp + 152))) != int32(m_SQLITE_DONE) {
			// IMP: V17740_01811
			libsqlite3.Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp + 152)))
			**(**uintptr)(__ccgo_up(pzErr)) = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+7302, libc.VaList(bp+176, libsqlite3.Xsqlite3_errmsg(tls, db)))
			goto error
		}
		libsqlite3.Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp + 152)))
		if (*Tvec0_vtab)(unsafe.Pointer(pNew)).FpkIsText != 0 {
			// adds a "text unique not null" constraint to the id column
			zCreateShadowRowids = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+7346, libc.VaList(bp+176, (*Tvec0_vtab)(unsafe.Pointer(pNew)).FschemaName, (*Tvec0_vtab)(unsafe.Pointer(pNew)).FtableName))
		} else {
			zCreateShadowRowids = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+7480, libc.VaList(bp+176, (*Tvec0_vtab)(unsafe.Pointer(pNew)).FschemaName, (*Tvec0_vtab)(unsafe.Pointer(pNew)).FtableName))
		}
		if !(zCreateShadowRowids != 0) {
			goto error
		}
		rc1 = libsqlite3.Xsqlite3_prepare_v2(tls, db, zCreateShadowRowids, -int32(1), bp+152, uintptr(0))
		libsqlite3.Xsqlite3_free(tls, zCreateShadowRowids)
		if rc1 != m_SQLITE_OK || libsqlite3.Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp + 152))) != int32(m_SQLITE_DONE) {
			// IMP: V11631_28470
			libsqlite3.Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp + 152)))
			**(**uintptr)(__ccgo_up(pzErr)) = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+7593, libc.VaList(bp+176, libsqlite3.Xsqlite3_errmsg(tls, db)))
			goto error
		}
		libsqlite3.Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp + 152)))
		i5 = 0
		for {
			if !(i5 < (*Tvec0_vtab)(unsafe.Pointer(pNew)).FnumVectorColumns) {
				break
			}
			zSql1 = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+7637, libc.VaList(bp+176, (*Tvec0_vtab)(unsafe.Pointer(pNew)).FschemaName, (*Tvec0_vtab)(unsafe.Pointer(pNew)).FtableName, i5))
			if !(zSql1 != 0) {
				goto error
			}
			rc1 = libsqlite3.Xsqlite3_prepare_v2(tls, db, zSql1, -int32(1), bp+152, uintptr(0))
			libsqlite3.Xsqlite3_free(tls, zSql1)
			if rc1 != m_SQLITE_OK || libsqlite3.Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp + 152))) != int32(m_SQLITE_DONE) {
				// IMP: V25919_09989
				libsqlite3.Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp + 152)))
				**(**uintptr)(__ccgo_up(pzErr)) = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+7720, libc.VaList(bp+176, i5, libsqlite3.Xsqlite3_errmsg(tls, db)))
				goto error
			}
			libsqlite3.Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp + 152)))
			goto _6
		_6:
			;
			i5 = i5 + 1
		}
		// See SHADOW_TABLE_ROWID_QUIRK in vec0_new_chunk() — same "rowid PRIMARY KEY"
		// without INTEGER type issue applies here.
		i6 = 0
		for {
			if !(i6 < (*Tvec0_vtab)(unsafe.Pointer(pNew)).FnumMetadataColumns) {
				break
			}
			zSql2 = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+7775, libc.VaList(bp+176, (*Tvec0_vtab)(unsafe.Pointer(pNew)).FschemaName, (*Tvec0_vtab)(unsafe.Pointer(pNew)).FtableName, i6))
			if !(zSql2 != 0) {
				goto error
			}
			rc1 = libsqlite3.Xsqlite3_prepare_v2(tls, db, zSql2, -int32(1), bp+152, uintptr(0))
			libsqlite3.Xsqlite3_free(tls, zSql2)
			if rc1 != m_SQLITE_OK || libsqlite3.Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp + 152))) != int32(m_SQLITE_DONE) {
				libsqlite3.Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp + 152)))
				**(**uintptr)(__ccgo_up(pzErr)) = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+7857, libc.VaList(bp+176, i6, libsqlite3.Xsqlite3_errmsg(tls, db)))
				goto error
			}
			libsqlite3.Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp + 152)))
			if (**(**TVec0MetadataColumnDefinition)(__ccgo_up(pNew + 1600 + uintptr(i6)*24))).Fkind == int32(_VEC0_METADATA_COLUMN_KIND_TEXT) {
				zSql3 = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+7912, libc.VaList(bp+176, (*Tvec0_vtab)(unsafe.Pointer(pNew)).FschemaName, (*Tvec0_vtab)(unsafe.Pointer(pNew)).FtableName, i6))
				if !(zSql3 != 0) {
					goto error
				}
				rc1 = libsqlite3.Xsqlite3_prepare_v2(tls, db, zSql3, -int32(1), bp+152, uintptr(0))
				libsqlite3.Xsqlite3_free(tls, zSql3)
				if rc1 != m_SQLITE_OK || libsqlite3.Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp + 152))) != int32(m_SQLITE_DONE) {
					libsqlite3.Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp + 152)))
					**(**uintptr)(__ccgo_up(pzErr)) = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+7983, libc.VaList(bp+176, i6, libsqlite3.Xsqlite3_errmsg(tls, db)))
					goto error
				}
				libsqlite3.Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp + 152)))
			}
			goto _7
		_7:
			;
			i6 = i6 + 1
		}
		if (*Tvec0_vtab)(unsafe.Pointer(pNew)).FnumAuxiliaryColumns > 0 {
			s1 = libsqlite3.Xsqlite3_str_new(tls, libc.UintptrFromInt32(0))
			libsqlite3.Xsqlite3_str_appendf(tls, s1, __ccgo_ts+8037, libc.VaList(bp+176, (*Tvec0_vtab)(unsafe.Pointer(pNew)).FschemaName, (*Tvec0_vtab)(unsafe.Pointer(pNew)).FtableName))
			i7 = 0
			for {
				if !(i7 < (*Tvec0_vtab)(unsafe.Pointer(pNew)).FnumAuxiliaryColumns) {
					break
				}
				libsqlite3.Xsqlite3_str_appendf(tls, s1, __ccgo_ts+8098, libc.VaList(bp+176, i7))
				goto _8
			_8:
				;
				i7 = i7 + 1
			}
			libsqlite3.Xsqlite3_str_appendall(tls, s1, __ccgo_ts+5256)
			zSql4 = libsqlite3.Xsqlite3_str_finish(tls, s1)
			if !(zSql4 != 0) {
				goto error
			}
			rc1 = libsqlite3.Xsqlite3_prepare_v2(tls, db, zSql4, -int32(1), bp+160, libc.UintptrFromInt32(0))
			if rc1 != m_SQLITE_OK || libsqlite3.Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp + 160))) != int32(m_SQLITE_DONE) {
				libsqlite3.Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp + 160)))
				**(**uintptr)(__ccgo_up(pzErr)) = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+8110, libc.VaList(bp+176, libsqlite3.Xsqlite3_errmsg(tls, db)))
				goto error
			}
			libsqlite3.Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp + 160)))
		}
	}
	**(**uintptr)(__ccgo_up(ppVtab)) = pNew
	return m_SQLITE_OK
	goto error
error:
	;
	Xvec0_free(tls, pNew)
	libsqlite3.Xsqlite3_free(tls, pNew)
	return int32(m_SQLITE_ERROR)
}

func _vec_add(tls *libc.TLS, context uintptr, argc int32, argv uintptr) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var i, i1, outSize, outSize1 Tsize_t
	var out, out1 uintptr
	var rc int32
	var _ /* a at bp+0 */ uintptr
	var _ /* aCleanup at bp+24 */ Tvector_cleanup
	var _ /* b at bp+8 */ uintptr
	var _ /* bCleanup at bp+32 */ Tvector_cleanup
	var _ /* dimensions at bp+16 */ Tsize_t
	var _ /* elementType at bp+48 */ _VectorElementType
	var _ /* error at bp+40 */ uintptr
	_, _, _, _, _, _, _ = i, i1, out, out1, outSize, outSize1, rc
	**(**uintptr)(__ccgo_up(bp)) = libc.UintptrFromInt32(0)
	**(**uintptr)(__ccgo_up(bp + 8)) = libc.UintptrFromInt32(0)
	rc = Xensure_vector_match(tls, **(**uintptr)(__ccgo_up(argv)), **(**uintptr)(__ccgo_up(argv + 1*8)), bp, bp+8, bp+48, bp+16, bp+24, bp+32, bp+40)
	if rc != m_SQLITE_OK {
		libsqlite3.Xsqlite3_result_error(tls, context, **(**uintptr)(__ccgo_up(bp + 40)), -int32(1))
		libsqlite3.Xsqlite3_free(tls, **(**uintptr)(__ccgo_up(bp + 40)))
		return
	}
	switch **(**_VectorElementType)(__ccgo_up(bp + 48)) {
	case int32(_SQLITE_VEC_ELEMENT_TYPE_BIT):
		libsqlite3.Xsqlite3_result_error(tls, context, __ccgo_ts+1183, -int32(1))
		goto finish
	case int32(_SQLITE_VEC_ELEMENT_TYPE_FLOAT32):
		outSize = **(**Tsize_t)(__ccgo_up(bp + 16)) * uint64(4)
		out = libsqlite3.Xsqlite3_malloc(tls, libc.Int32FromUint64(outSize))
		if !(out != 0) {
			libsqlite3.Xsqlite3_result_error_nomem(tls, context)
			goto finish
		}
		libc.Xmemset(tls, out, 0, outSize)
		i = uint64(0)
		for {
			if !(i < **(**Tsize_t)(__ccgo_up(bp + 16))) {
				break
			}
			**(**Tf32)(__ccgo_up(out + uintptr(i)*4)) = **(**Tf32)(__ccgo_up(**(**uintptr)(__ccgo_up(bp)) + uintptr(i)*4)) + **(**Tf32)(__ccgo_up(**(**uintptr)(__ccgo_up(bp + 8)) + uintptr(i)*4))
			goto _1
		_1:
			;
			i = i + 1
		}
		libsqlite3.Xsqlite3_result_blob(tls, context, out, libc.Int32FromUint64(outSize), __ccgo_fp(libsqlite3.Xsqlite3_free))
		libsqlite3.Xsqlite3_result_subtype(tls, context, uint32(_SQLITE_VEC_ELEMENT_TYPE_FLOAT32))
		goto finish
	case int32(_SQLITE_VEC_ELEMENT_TYPE_INT8):
		outSize1 = **(**Tsize_t)(__ccgo_up(bp + 16)) * uint64(1)
		out1 = libsqlite3.Xsqlite3_malloc(tls, libc.Int32FromUint64(outSize1))
		if !(out1 != 0) {
			libsqlite3.Xsqlite3_result_error_nomem(tls, context)
			goto finish
		}
		libc.Xmemset(tls, out1, 0, outSize1)
		i1 = uint64(0)
		for {
			if !(i1 < **(**Tsize_t)(__ccgo_up(bp + 16))) {
				break
			}
			**(**Ti8)(__ccgo_up(out1 + uintptr(i1))) = int8(int32(**(**Ti8)(__ccgo_up(**(**uintptr)(__ccgo_up(bp)) + uintptr(i1)))) + int32(**(**Ti8)(__ccgo_up(**(**uintptr)(__ccgo_up(bp + 8)) + uintptr(i1)))))
			goto _2
		_2:
			;
			i1 = i1 + 1
		}
		libsqlite3.Xsqlite3_result_blob(tls, context, out1, libc.Int32FromUint64(outSize1), __ccgo_fp(libsqlite3.Xsqlite3_free))
		libsqlite3.Xsqlite3_result_subtype(tls, context, uint32(_SQLITE_VEC_ELEMENT_TYPE_INT8))
		goto finish
	}
	goto finish
finish:
	;
	(*(*func(*libc.TLS, uintptr))(unsafe.Pointer(bp + 24)))(tls, **(**uintptr)(__ccgo_up(bp)))
	(*(*func(*libc.TLS, uintptr))(unsafe.Pointer(bp + 32)))(tls, **(**uintptr)(__ccgo_up(bp + 8)))
	return
}

func _vec_npy_file(tls *libc.TLS, context uintptr, argc int32, argv uintptr) {
	var f, path uintptr
	var pathLength Tsize_t
	_, _, _ = f, path, pathLength
	path = libsqlite3.Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv)))
	pathLength = libc.Uint64FromInt32(libsqlite3.Xsqlite3_value_bytes(tls, **(**uintptr)(__ccgo_up(argv))))
	f = libsqlite3.Xsqlite3_malloc(tls, int32(16))
	if !(f != 0) {
		libsqlite3.Xsqlite3_result_error_nomem(tls, context)
		return
	}
	libc.Xmemset(tls, f, 0, uint64(16))
	(*TVecNpyFile)(unsafe.Pointer(f)).Fpath = path
	(*TVecNpyFile)(unsafe.Pointer(f)).FpathLength = pathLength
	libsqlite3.Xsqlite3_result_pointer(tls, context, f, __ccgo_ts+674, __ccgo_fp(libsqlite3.Xsqlite3_free))
}

func _vec_quantize_int8(tls *libc.TLS, context uintptr, argc int32, argv uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var i Tsize_t
	var out uintptr
	var rc, sz int32
	var step Tf32
	var val float64
	var _ /* dimensions at bp+8 */ Tsize_t
	var _ /* err at bp+24 */ uintptr
	var _ /* srcCleanup at bp+16 */ Tfvec_cleanup
	var _ /* srcVector at bp+0 */ uintptr
	_, _, _, _, _, _ = i, out, rc, step, sz, val
	out = libc.UintptrFromInt32(0)
	rc = _fvec_from_value(tls, **(**uintptr)(__ccgo_up(argv)), bp, bp+8, bp+16, bp+24)
	if rc != m_SQLITE_OK {
		libsqlite3.Xsqlite3_result_error(tls, context, **(**uintptr)(__ccgo_up(bp + 24)), -int32(1))
		libsqlite3.Xsqlite3_free(tls, **(**uintptr)(__ccgo_up(bp + 24)))
		return
	}
	sz = libc.Int32FromUint64(**(**Tsize_t)(__ccgo_up(bp + 8)) * uint64(1))
	out = libsqlite3.Xsqlite3_malloc(tls, sz)
	if !(out != 0) {
		libsqlite3.Xsqlite3_result_error_nomem(tls, context)
		goto cleanup
	}
	libc.Xmemset(tls, out, 0, libc.Uint64FromInt32(sz))
	if libsqlite3.Xsqlite3_value_type(tls, **(**uintptr)(__ccgo_up(argv + 1*8))) != int32(m_SQLITE_TEXT) || libc.Uint64FromInt32(libsqlite3.Xsqlite3_value_bytes(tls, **(**uintptr)(__ccgo_up(argv + 1*8)))) != libc.Xstrlen(tls, __ccgo_ts+1126) || libsqlite3.Xsqlite3_stricmp(tls, libsqlite3.Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv + 1*8))), __ccgo_ts+1126) != 0 {
		libsqlite3.Xsqlite3_result_error(tls, context, __ccgo_ts+1131, -int32(1))
		libsqlite3.Xsqlite3_free(tls, out)
		goto cleanup
	}
	step = float32((float64(1) - -libc.Float64FromFloat64(1)) / libc.Float64FromInt32(255))
	i = uint64(0)
	for {
		if !(i < **(**Tsize_t)(__ccgo_up(bp + 8))) {
			break
		}
		val = (float64(**(**Tf32)(__ccgo_up(**(**uintptr)(__ccgo_up(bp)) + uintptr(i)*4))) - -libc.Float64FromFloat64(1))/float64(step) - libc.Float64FromInt32(128)
		if !(val <= libc.Float64FromFloat64(127)) {
			val = float64(127)
		} /* also clamps NaN */
		if !(val >= -libc.Float64FromFloat64(128)) {
			val = -libc.Float64FromFloat64(128)
		}
		**(**Ti8)(__ccgo_up(out + uintptr(i))) = int8(val)
		goto _1
	_1:
		;
		i = i + 1
	}
	libsqlite3.Xsqlite3_result_blob(tls, context, out, libc.Int32FromUint64(**(**Tsize_t)(__ccgo_up(bp + 8))*uint64(1)), __ccgo_fp(libsqlite3.Xsqlite3_free))
	libsqlite3.Xsqlite3_result_subtype(tls, context, uint32(_SQLITE_VEC_ELEMENT_TYPE_INT8))
	goto cleanup
cleanup:
	;
	(*(*func(*libc.TLS, uintptr))(unsafe.Pointer(bp + 16)))(tls, **(**uintptr)(__ccgo_up(bp)))
}

func _vec_static_blob_entriesFilter(tls *libc.TLS, pVtabCursor uintptr, idxNum int32, idxStr uintptr, argc int32, argv uintptr) (r int32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var bsize, i, i1 Tsize_t
	var candidates, distances, knn_data, p, pCur, taken, topk_rowids, v uintptr
	var k Ti64
	var rc int32
	var v1 int64
	var _ /* cleanup at bp+24 */ Tvector_cleanup
	var _ /* dimensions at bp+8 */ Tsize_t
	var _ /* elementType at bp+16 */ _VectorElementType
	var _ /* err at bp+32 */ uintptr
	var _ /* k_used at bp+40 */ Ti32
	var _ /* queryVector at bp+0 */ uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _ = bsize, candidates, distances, i, i1, k, knn_data, p, pCur, rc, taken, topk_rowids, v, v1
	_ = idxStr
	pCur = pVtabCursor
	p = (*Tvec_static_blob_entries_cursor)(unsafe.Pointer(pCur)).Fbase.FpVtab
	if idxNum == int32(_VEC_SBE__QUERYPLAN_KNN) {
		(*Tvec_static_blob_entries_cursor)(unsafe.Pointer(pCur)).Fquery_plan = int32(_VEC_SBE__QUERYPLAN_KNN)
		knn_data = libsqlite3.Xsqlite3_malloc(tls, int32(40))
		if !(knn_data != 0) {
			return int32(m_SQLITE_NOMEM)
		}
		libc.Xmemset(tls, knn_data, 0, uint64(40))
		rc = Xvector_from_value(tls, **(**uintptr)(__ccgo_up(argv)), bp, bp+8, bp+16, bp+24, bp+32)
		if rc != m_SQLITE_OK {
			return int32(m_SQLITE_ERROR)
		}
		if **(**_VectorElementType)(__ccgo_up(bp + 16)) != (*Tstatic_blob)(unsafe.Pointer((*Tvec_static_blob_entries_vtab)(unsafe.Pointer(p)).Fblob)).Felement_type {
			return int32(m_SQLITE_ERROR)
		}
		if **(**Tsize_t)(__ccgo_up(bp + 8)) != (*Tstatic_blob)(unsafe.Pointer((*Tvec_static_blob_entries_vtab)(unsafe.Pointer(p)).Fblob)).Fdimensions {
			return int32(m_SQLITE_ERROR)
		}
		if libsqlite3.Xsqlite3_value_int64(tls, **(**uintptr)(__ccgo_up(argv + 1*8))) <= libc.Int64FromUint64((*Tstatic_blob)(unsafe.Pointer((*Tvec_static_blob_entries_vtab)(unsafe.Pointer(p)).Fblob)).Fnvectors) {
			v1 = libsqlite3.Xsqlite3_value_int64(tls, **(**uintptr)(__ccgo_up(argv + 1*8)))
		} else {
			v1 = libc.Int64FromUint64((*Tstatic_blob)(unsafe.Pointer((*Tvec_static_blob_entries_vtab)(unsafe.Pointer(p)).Fblob)).Fnvectors)
		}
		k = v1
		if k < 0 {
			// HANDLE https://github.com/asg017/sqlite-vec/issues/55
			return int32(m_SQLITE_ERROR)
		}
		if k == 0 {
			(*Tsbe_query_knn_data)(unsafe.Pointer(knn_data)).Fk = 0
			(*Tvec_static_blob_entries_cursor)(unsafe.Pointer(pCur)).Fknn_data = knn_data
			return m_SQLITE_OK
		}
		bsize = ((*Tstatic_blob)(unsafe.Pointer((*Tvec_static_blob_entries_vtab)(unsafe.Pointer(p)).Fblob)).Fnvectors + uint64(7)) & libc.Uint64FromInt32(^libc.Int32FromInt32(7))
		topk_rowids = libsqlite3.Xsqlite3_malloc(tls, libc.Int32FromUint64(libc.Uint64FromInt64(k)*uint64(4)))
		if !(topk_rowids != 0) {
			// HANDLE https://github.com/asg017/sqlite-vec/issues/55
			return int32(m_SQLITE_ERROR)
		}
		distances = libsqlite3.Xsqlite3_malloc(tls, libc.Int32FromUint64(bsize*uint64(4)))
		if !(distances != 0) {
			// HANDLE https://github.com/asg017/sqlite-vec/issues/55
			return int32(m_SQLITE_ERROR)
		}
		i = uint64(0)
		for {
			if !(i < (*Tstatic_blob)(unsafe.Pointer((*Tvec_static_blob_entries_vtab)(unsafe.Pointer(p)).Fblob)).Fnvectors) {
				break
			}
			// https://github.com/asg017/sqlite-vec/issues/52
			v = (*Tstatic_blob)(unsafe.Pointer((*Tvec_static_blob_entries_vtab)(unsafe.Pointer(p)).Fblob)).Fp + uintptr(i*(*Tstatic_blob)(unsafe.Pointer((*Tvec_static_blob_entries_vtab)(unsafe.Pointer(p)).Fblob)).Fdimensions)*4
			**(**Tf32)(__ccgo_up(distances + uintptr(i)*4)) = _distance_l2_sqr_float(tls, v, **(**uintptr)(__ccgo_up(bp)), (*Tvec_static_blob_entries_vtab)(unsafe.Pointer(p)).Fblob+16)
			goto _2
		_2:
			;
			i = i + 1
		}
		candidates = Xbitmap_new(tls, libc.Int32FromUint64(bsize))
		taken = Xbitmap_new(tls, libc.Int32FromUint64(bsize))
		Xbitmap_fill(tls, candidates, libc.Int32FromUint64(bsize))
		i1 = bsize
		for {
			if !(i1 >= (*Tstatic_blob)(unsafe.Pointer((*Tvec_static_blob_entries_vtab)(unsafe.Pointer(p)).Fblob)).Fnvectors) {
				break
			}
			Xbitmap_set(tls, candidates, libc.Int32FromUint64(i1), 0)
			goto _3
		_3:
			;
			i1 = i1 - 1
		}
		**(**Ti32)(__ccgo_up(bp + 40)) = 0
		Xmin_idx(tls, distances, libc.Int32FromUint64(bsize), candidates, topk_rowids, int32(k), taken, bp+40)
		(*Tsbe_query_knn_data)(unsafe.Pointer(knn_data)).Fcurrent_idx = 0
		(*Tsbe_query_knn_data)(unsafe.Pointer(knn_data)).Fdistances = distances
		(*Tsbe_query_knn_data)(unsafe.Pointer(knn_data)).Fk = k
		(*Tsbe_query_knn_data)(unsafe.Pointer(knn_data)).Frowids = topk_rowids
		(*Tvec_static_blob_entries_cursor)(unsafe.Pointer(pCur)).Fknn_data = knn_data
	} else {
		(*Tvec_static_blob_entries_cursor)(unsafe.Pointer(pCur)).Fquery_plan = int32(_VEC_SBE__QUERYPLAN_FULLSCAN)
		(*Tvec_static_blob_entries_cursor)(unsafe.Pointer(pCur)).FiRowid = 0
	}
	return m_SQLITE_OK
}

func _vec_static_blob_from_raw(tls *libc.TLS, context uintptr, argc int32, argv uintptr) {
	var p uintptr
	_ = p
	p = libsqlite3.Xsqlite3_malloc(tls, int32(32))
	if !(p != 0) {
		libsqlite3.Xsqlite3_result_error_nomem(tls, context)
		return
	}
	libc.Xmemset(tls, p, 0, uint64(32))
	(*Tstatic_blob_definition)(unsafe.Pointer(p)).Fp = uintptr(libsqlite3.Xsqlite3_value_int64(tls, **(**uintptr)(__ccgo_up(argv))))
	(*Tstatic_blob_definition)(unsafe.Pointer(p)).Felement_type = int32(_SQLITE_VEC_ELEMENT_TYPE_FLOAT32)
	(*Tstatic_blob_definition)(unsafe.Pointer(p)).Fdimensions = libc.Uint64FromInt64(libsqlite3.Xsqlite3_value_int64(tls, **(**uintptr)(__ccgo_up(argv + 2*8))))
	(*Tstatic_blob_definition)(unsafe.Pointer(p)).Fnvectors = libc.Uint64FromInt64(libsqlite3.Xsqlite3_value_int64(tls, **(**uintptr)(__ccgo_up(argv + 3*8))))
	libsqlite3.Xsqlite3_result_pointer(tls, context, p, _POINTER_NAME_STATIC_BLOB_DEF, __ccgo_fp(libsqlite3.Xsqlite3_free))
}

func _vec_sub(tls *libc.TLS, context uintptr, argc int32, argv uintptr) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var i, i1, outSize, outSize1 Tsize_t
	var out, out1 uintptr
	var rc int32
	var _ /* a at bp+0 */ uintptr
	var _ /* aCleanup at bp+24 */ Tvector_cleanup
	var _ /* b at bp+8 */ uintptr
	var _ /* bCleanup at bp+32 */ Tvector_cleanup
	var _ /* dimensions at bp+16 */ Tsize_t
	var _ /* elementType at bp+48 */ _VectorElementType
	var _ /* error at bp+40 */ uintptr
	_, _, _, _, _, _, _ = i, i1, out, out1, outSize, outSize1, rc
	**(**uintptr)(__ccgo_up(bp)) = libc.UintptrFromInt32(0)
	**(**uintptr)(__ccgo_up(bp + 8)) = libc.UintptrFromInt32(0)
	rc = Xensure_vector_match(tls, **(**uintptr)(__ccgo_up(argv)), **(**uintptr)(__ccgo_up(argv + 1*8)), bp, bp+8, bp+48, bp+16, bp+24, bp+32, bp+40)
	if rc != m_SQLITE_OK {
		libsqlite3.Xsqlite3_result_error(tls, context, **(**uintptr)(__ccgo_up(bp + 40)), -int32(1))
		libsqlite3.Xsqlite3_free(tls, **(**uintptr)(__ccgo_up(bp + 40)))
		return
	}
	switch **(**_VectorElementType)(__ccgo_up(bp + 48)) {
	case int32(_SQLITE_VEC_ELEMENT_TYPE_BIT):
		libsqlite3.Xsqlite3_result_error(tls, context, __ccgo_ts+1219, -int32(1))
		goto finish
	case int32(_SQLITE_VEC_ELEMENT_TYPE_FLOAT32):
		outSize = **(**Tsize_t)(__ccgo_up(bp + 16)) * uint64(4)
		out = libsqlite3.Xsqlite3_malloc(tls, libc.Int32FromUint64(outSize))
		if !(out != 0) {
			libsqlite3.Xsqlite3_result_error_nomem(tls, context)
			goto finish
		}
		libc.Xmemset(tls, out, 0, outSize)
		i = uint64(0)
		for {
			if !(i < **(**Tsize_t)(__ccgo_up(bp + 16))) {
				break
			}
			**(**Tf32)(__ccgo_up(out + uintptr(i)*4)) = **(**Tf32)(__ccgo_up(**(**uintptr)(__ccgo_up(bp)) + uintptr(i)*4)) - **(**Tf32)(__ccgo_up(**(**uintptr)(__ccgo_up(bp + 8)) + uintptr(i)*4))
			goto _1
		_1:
			;
			i = i + 1
		}
		libsqlite3.Xsqlite3_result_blob(tls, context, out, libc.Int32FromUint64(outSize), __ccgo_fp(libsqlite3.Xsqlite3_free))
		libsqlite3.Xsqlite3_result_subtype(tls, context, uint32(_SQLITE_VEC_ELEMENT_TYPE_FLOAT32))
		goto finish
	case int32(_SQLITE_VEC_ELEMENT_TYPE_INT8):
		outSize1 = **(**Tsize_t)(__ccgo_up(bp + 16)) * uint64(1)
		out1 = libsqlite3.Xsqlite3_malloc(tls, libc.Int32FromUint64(outSize1))
		if !(out1 != 0) {
			libsqlite3.Xsqlite3_result_error_nomem(tls, context)
			goto finish
		}
		libc.Xmemset(tls, out1, 0, outSize1)
		i1 = uint64(0)
		for {
			if !(i1 < **(**Tsize_t)(__ccgo_up(bp + 16))) {
				break
			}
			**(**Ti8)(__ccgo_up(out1 + uintptr(i1))) = int8(int32(**(**Ti8)(__ccgo_up(**(**uintptr)(__ccgo_up(bp)) + uintptr(i1)))) - int32(**(**Ti8)(__ccgo_up(**(**uintptr)(__ccgo_up(bp + 8)) + uintptr(i1)))))
			goto _2
		_2:
			;
			i1 = i1 + 1
		}
		libsqlite3.Xsqlite3_result_blob(tls, context, out1, libc.Int32FromUint64(outSize1), __ccgo_fp(libsqlite3.Xsqlite3_free))
		libsqlite3.Xsqlite3_result_subtype(tls, context, uint32(_SQLITE_VEC_ELEMENT_TYPE_INT8))
		goto finish
	}
	goto finish
finish:
	;
	(*(*func(*libc.TLS, uintptr))(unsafe.Pointer(bp + 24)))(tls, **(**uintptr)(__ccgo_up(bp)))
	(*(*func(*libc.TLS, uintptr))(unsafe.Pointer(bp + 32)))(tls, **(**uintptr)(__ccgo_up(bp + 8)))
	return
}
