/* Bio-Digital native kernel dispatch contracts. */
#ifndef BIO_KERNEL_H_
#define BIO_KERNEL_H_

#include <stddef.h>
#include <stdint.h>

#include "bio/bio.h"

#define BIO_ROUTE_MAX_BLOCKS 4096u
#define BIO_ROUTE_DEFAULT_BLOCK_BYTES 32u

typedef struct bio_sparse_masks {
    uint8_t zpm_skip[BIO_ROUTE_MAX_BLOCKS];
    uint8_t omega_active[BIO_ROUTE_MAX_BLOCKS];
    size_t block_count;
    size_t block_bytes;
    uint64_t baseline_updates;
    uint64_t executed_updates;
    uint64_t skipped_blocks;
    uint64_t active_blocks;
} bio_sparse_masks;

typedef bio_v1_status (*bio_pulse_int4_fn)(uint8_t *packed_potentials,
                                           const uint8_t *packed_inputs,
                                           const bio_sparse_masks *masks,
                                           size_t byte_count,
                                           uint8_t threshold,
                                           uint64_t *executed_updates_out,
                                           uint64_t *baseline_updates_out);

typedef struct bio_kernel_dispatch {
    const char *pulse_variant;
    bio_pulse_int4_fn pulse_int4;
} bio_kernel_dispatch;

extern bio_kernel_dispatch g_bio_kernels;

void bio_runtime_detect_features(void);
void bio_runtime_bind_kernels(void);
bio_v1_status bio_pulse_int4_scalar(uint8_t *packed_potentials,
                                    const uint8_t *packed_inputs,
                                    const bio_sparse_masks *masks,
                                    size_t byte_count,
                                    uint8_t threshold,
                                    uint64_t *executed_updates_out,
                                    uint64_t *baseline_updates_out);
bio_v1_status bio_pulse_int4_avx2(uint8_t *packed_potentials,
                                  const uint8_t *packed_inputs,
                                  const bio_sparse_masks *masks,
                                  size_t byte_count,
                                  uint8_t threshold,
                                  uint64_t *executed_updates_out,
                                  uint64_t *baseline_updates_out);
void bio_sparse_masks_reset(bio_sparse_masks *masks, size_t byte_count,
                            size_t block_bytes);
void bio_sparse_zpm_build(bio_sparse_masks *masks, const uint8_t *packed_inputs,
                          size_t byte_count, uint64_t zero_threshold);
void bio_sparse_omega_build(bio_sparse_masks *masks, const uint8_t *packed_inputs,
                            size_t byte_count, uint32_t wake_mod,
                            size_t min_active_blocks);
void bio_sparse_prune(bio_sparse_masks *masks);

#endif /* BIO_KERNEL_H_ */
