// viewer-io.h: memory-mapped file access and dequantization
//
// Maps a file into memory (read-only) and provides dequant_to_float()
// which dispatches to ggml's to_float for all supported quantization types.

#pragma once

#include <ggml.h>

#include <cstddef>
#include <string>

// memory-mapped read-only file handle
struct mapped_file {
  const uint8_t *data = nullptr;
  size_t size = 0;

  // platform handle (fd on unix, HANDLE on windows)
#ifdef _WIN32
  void *file_handle = nullptr;
  void *map_handle = nullptr;
#else
  int fd = -1;
#endif
};

// open a file and map it into memory (read-only)
bool mmap_open(const char *path, mapped_file &out, std::string &error);

// release the mapping
void mmap_close(mapped_file &file);

// dequantize one block of the given ggml type to float.
// src points to raw block bytes, dst receives block_size floats.
// returns the number of floats written, or 0 if the type has no to_float.
size_t dequant_to_float(enum ggml_type type, const void *src, float *dst,
                        size_t block_size);
