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

//go:build (linux && amd64) || (linux && arm64) || (linux && loong64) || (linux && ppc64le) || (linux && riscv64) || (linux && s390x)

package vec

import (
	"unsafe"

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

func Xparse_npy_file(tls *libc.TLS, pVTab uintptr, file uintptr, pCur uintptr) (r int32) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var dataSize, expectedDataSize Ti32
	var fileSize int64
	var headerX uintptr
	var major, minor Tu8
	var n, rc int32
	var totalHeaderLength Tsize_t
	var _ /* element_type at bp+16 */ _VectorElementType
	var _ /* fortran_order at bp+12 */ int32
	var _ /* header at bp+0 */ [10]uint8
	var _ /* headerLength at bp+10 */ Tuint16_t
	var _ /* numDimensions at bp+32 */ Tsize_t
	var _ /* numElements at bp+24 */ Tsize_t
	_, _, _, _, _, _, _, _, _ = dataSize, expectedDataSize, fileSize, headerX, major, minor, n, rc, totalHeaderLength
	libc.Xfseek(tls, file, 0, int32(2))
	fileSize = libc.Xftell(tls, file)
	libc.Xfseek(tls, file, 0, 0)
	n = libc.Int32FromUint64(libc.Xfread(tls, bp, uint64(1), uint64(10), file))
	if n != int32(10) {
		Xvtab_set_error(tls, pVTab, __ccgo_ts+2988, 0)
		return int32(m_SQLITE_ERROR)
	}
	if libc.Xmemcmp(tls, uintptr(unsafe.Pointer(&_NPY_MAGIC)), bp, uint64(6)) != 0 {
		Xvtab_set_error(tls, pVTab, __ccgo_ts+3015, 0)
		return int32(m_SQLITE_ERROR)
	}
	major = (**(**[10]uint8)(__ccgo_up(bp)))[int32(6)]
	minor = (**(**[10]uint8)(__ccgo_up(bp)))[int32(7)]
	**(**Tuint16_t)(__ccgo_up(bp + 10)) = uint16(0)
	libc.Xmemcpy(tls, bp+10, bp+8, uint64(2))
	totalHeaderLength = libc.Uint64FromInt64(6) + libc.Uint64FromInt64(1) + libc.Uint64FromInt64(1) + libc.Uint64FromInt64(2) + uint64(**(**Tuint16_t)(__ccgo_up(bp + 10)))
	dataSize = libc.Int32FromUint64(libc.Uint64FromInt64(fileSize) - totalHeaderLength)
	if dataSize < 0 {
		Xvtab_set_error(tls, pVTab, __ccgo_ts+3068, 0)
		return int32(m_SQLITE_ERROR)
	}
	headerX = libsqlite3.Xsqlite3_malloc(tls, libc.Int32FromUint16(**(**Tuint16_t)(__ccgo_up(bp + 10))))
	if **(**Tuint16_t)(__ccgo_up(bp + 10)) != 0 && !(headerX != 0) {
		return int32(m_SQLITE_NOMEM)
	}
	n = libc.Int32FromUint64(libc.Xfread(tls, headerX, uint64(1), uint64(**(**Tuint16_t)(__ccgo_up(bp + 10))), file))
	if n != libc.Int32FromUint16(**(**Tuint16_t)(__ccgo_up(bp + 10))) {
		libsqlite3.Xsqlite3_free(tls, headerX)
		Xvtab_set_error(tls, pVTab, __ccgo_ts+3068, 0)
		return int32(m_SQLITE_ERROR)
	}
	rc = Xparse_npy_header(tls, pVTab, headerX, uint64(**(**Tuint16_t)(__ccgo_up(bp + 10))), bp+16, bp+12, bp+24, bp+32)
	libsqlite3.Xsqlite3_free(tls, headerX)
	if rc != m_SQLITE_OK {
		// parse_npy_header already attackes an error emssage
		return rc
	}
	expectedDataSize = libc.Int32FromUint64(**(**Tsize_t)(__ccgo_up(bp + 24)) * Xvector_byte_size(tls, **(**_VectorElementType)(__ccgo_up(bp + 16)), **(**Tsize_t)(__ccgo_up(bp + 32))))
	if expectedDataSize != dataSize {
		Xvtab_set_error(tls, pVTab, __ccgo_ts+3110, libc.VaList(bp+48, expectedDataSize, dataSize))
		return int32(m_SQLITE_ERROR)
	}
	(*Tvec_npy_each_cursor)(unsafe.Pointer(pCur)).FmaxChunks = uint64(1024)
	(*Tvec_npy_each_cursor)(unsafe.Pointer(pCur)).FchunksBufferSize = Xvector_byte_size(tls, **(**_VectorElementType)(__ccgo_up(bp + 16)), **(**Tsize_t)(__ccgo_up(bp + 32))) * (*Tvec_npy_each_cursor)(unsafe.Pointer(pCur)).FmaxChunks
	(*Tvec_npy_each_cursor)(unsafe.Pointer(pCur)).FchunksBuffer = libsqlite3.Xsqlite3_malloc(tls, libc.Int32FromUint64((*Tvec_npy_each_cursor)(unsafe.Pointer(pCur)).FchunksBufferSize))
	if (*Tvec_npy_each_cursor)(unsafe.Pointer(pCur)).FchunksBufferSize != 0 && !((*Tvec_npy_each_cursor)(unsafe.Pointer(pCur)).FchunksBuffer != 0) {
		return int32(m_SQLITE_NOMEM)
	}
	(*Tvec_npy_each_cursor)(unsafe.Pointer(pCur)).FcurrentChunkSize = libc.Xfread(tls, (*Tvec_npy_each_cursor)(unsafe.Pointer(pCur)).FchunksBuffer, Xvector_byte_size(tls, **(**_VectorElementType)(__ccgo_up(bp + 16)), **(**Tsize_t)(__ccgo_up(bp + 32))), (*Tvec_npy_each_cursor)(unsafe.Pointer(pCur)).FmaxChunks, file)
	(*Tvec_npy_each_cursor)(unsafe.Pointer(pCur)).FcurrentChunkIndex = uint64(0)
	(*Tvec_npy_each_cursor)(unsafe.Pointer(pCur)).FelementType = **(**_VectorElementType)(__ccgo_up(bp + 16))
	(*Tvec_npy_each_cursor)(unsafe.Pointer(pCur)).FnElements = **(**Tsize_t)(__ccgo_up(bp + 24))
	(*Tvec_npy_each_cursor)(unsafe.Pointer(pCur)).FnDimensions = **(**Tsize_t)(__ccgo_up(bp + 32))
	(*Tvec_npy_each_cursor)(unsafe.Pointer(pCur)).Finput_type = int32(_VEC_NPY_EACH_INPUT_FILE)
	(*Tvec_npy_each_cursor)(unsafe.Pointer(pCur)).Feof = libc.BoolInt32((*Tvec_npy_each_cursor)(unsafe.Pointer(pCur)).FcurrentChunkSize == uint64(0))
	(*Tvec_npy_each_cursor)(unsafe.Pointer(pCur)).Ffile = file
	return m_SQLITE_OK
}

func _vec_to_json(tls *libc.TLS, context uintptr, argc int32, argv uintptr) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var b Tu8
	var i Tsize_t
	var len1, rc int32
	var s, str uintptr
	var value Tf32
	var v2 uint32
	var _ /* __u at bp+0 */ struct {
		F__i [0]uint32
		F__f float32
	}
	var _ /* cleanup at bp+24 */ Tvector_cleanup
	var _ /* dimensions at bp+16 */ Tsize_t
	var _ /* elementType at bp+40 */ _VectorElementType
	var _ /* err at bp+32 */ uintptr
	var _ /* vector at bp+8 */ uintptr
	_, _, _, _, _, _, _, _ = b, i, len1, rc, s, str, value, v2
	rc = Xvector_from_value(tls, **(**uintptr)(__ccgo_up(argv)), bp+8, bp+16, bp+40, bp+24, bp+32)
	if rc != m_SQLITE_OK {
		libsqlite3.Xsqlite3_result_error(tls, context, **(**uintptr)(__ccgo_up(bp + 32)), -int32(1))
		libsqlite3.Xsqlite3_free(tls, **(**uintptr)(__ccgo_up(bp + 32)))
		return
	}
	str = libsqlite3.Xsqlite3_str_new(tls, libsqlite3.Xsqlite3_context_db_handle(tls, context))
	libsqlite3.Xsqlite3_str_appendall(tls, str, __ccgo_ts+1671)
	i = uint64(0)
	for {
		if !(i < **(**Tsize_t)(__ccgo_up(bp + 16))) {
			break
		}
		if i != uint64(0) {
			libsqlite3.Xsqlite3_str_appendall(tls, str, __ccgo_ts+1673)
		}
		if **(**_VectorElementType)(__ccgo_up(bp + 40)) == int32(_SQLITE_VEC_ELEMENT_TYPE_FLOAT32) {
			value = **(**Tf32)(__ccgo_up(**(**uintptr)(__ccgo_up(bp + 8)) + uintptr(i)*4))
			*(*float32)(unsafe.Pointer(bp)) = value
			v2 = *(*uint32)(unsafe.Pointer(bp))
			goto _3
		_3:
			if libc.BoolInt32(v2&uint32(0x7fffffff) > uint32(0x7f800000)) != 0 {
				libsqlite3.Xsqlite3_str_appendall(tls, str, __ccgo_ts+1675)
			} else {
				libsqlite3.Xsqlite3_str_appendf(tls, str, __ccgo_ts+1680, libc.VaList(bp+56, float64(value)))
			}
		} else {
			if **(**_VectorElementType)(__ccgo_up(bp + 40)) == int32(_SQLITE_VEC_ELEMENT_TYPE_INT8) {
				libsqlite3.Xsqlite3_str_appendf(tls, str, __ccgo_ts+1683, libc.VaList(bp+56, int32(**(**Ti8)(__ccgo_up(**(**uintptr)(__ccgo_up(bp + 8)) + uintptr(i))))))
			} else {
				if **(**_VectorElementType)(__ccgo_up(bp + 40)) == int32(_SQLITE_VEC_ELEMENT_TYPE_BIT) {
					b = libc.Uint8FromInt32(libc.Int32FromUint8(**(**Tu8)(__ccgo_up(**(**uintptr)(__ccgo_up(bp + 8)) + uintptr(i/uint64(8))))) >> (i % uint64(m_CHAR_BIT)) & int32(1))
					libsqlite3.Xsqlite3_str_appendf(tls, str, __ccgo_ts+1683, libc.VaList(bp+56, libc.Int32FromUint8(b)))
				}
			}
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	libsqlite3.Xsqlite3_str_appendall(tls, str, __ccgo_ts+1686)
	len1 = libsqlite3.Xsqlite3_str_length(tls, str)
	s = libsqlite3.Xsqlite3_str_finish(tls, str)
	if s != 0 {
		libsqlite3.Xsqlite3_result_text(tls, context, s, len1, __ccgo_fp(libsqlite3.Xsqlite3_free))
		libsqlite3.Xsqlite3_result_subtype(tls, context, uint32(m_JSON_SUBTYPE))
	} else {
		libsqlite3.Xsqlite3_result_error_nomem(tls, context)
	}
	(*(*func(*libc.TLS, uintptr))(unsafe.Pointer(bp + 24)))(tls, **(**uintptr)(__ccgo_up(bp + 8)))
}

const m___INT_FAST16_MAX__ = 0x7fffffffffffffff

const m___INT_FAST16_WIDTH__ = 64

const m___INT_FAST32_MAX__ = 0x7fffffffffffffff

const m___INT_FAST32_WIDTH__ = 64

const m___PRIPTR = "l"

const m___UINT_FAST16_MAX__ = 0xffffffffffffffff

const m___UINT_FAST32_MAX__ = 0xffffffffffffffff
