;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; LUSH Lisp Universal Shell
;;;   Copyright (C) 2002 Leon Bottou, Yann Le Cun, AT&T Corp, NECI.
;;; Includes parts of TL3:
;;;   Copyright (C) 1987-1999 Leon Bottou and Neuristique.
;;; Includes selected parts of SN3.2:
;;;   Copyright (C) 1991-2001 AT&T Corp.
;;;
;;; This program is free software; you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 2 of the License, or
;;; (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program; if not, write to the Free Software
;;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA
;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; $Id: idx-map.lsh,v 1.2 2003/04/18 21:29:30 profshadoko Exp $
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

#? *** IDX mapping from files
;; functions to map files into virtual memory
;; and to access the data as a matrix.

(libload "libc/stdio")

(de int-swapbytes (n)
    ((-int-) n)
    (int #{ (((($n)&0xff000000)>>24)|((($n)&0x00ff0000)>> 8) |
	     ((($n)&0x0000ff00)<< 8)|((($n)&0x000000ff)<<24) ) #}))

#? (mmap-idx <s> <stor> <ndim> <magic>)
;; low-level function to map a file as
;; an IDX.
(de mmap-idx (s stor ndim magic)
    (let ((file (fopen s "rb"))
	  (flip 0))
      (when (not file) (error "can't open file"))
      (let ((themagic (fread-int file)))
	(when (<> themagic (+ ndim magic))
	  (if (<> (int-swapbytes themagic) (+ ndim magic))
	      (error (sprintf "bad idx%d file (%d)" ndim themagic))
	    (setq flip 1)
	    (when (and (<> 0x0800 magic) (<> 0x0900 magic))
	      (error "can't map multi-byte types on a wrong-endian machine")))))
      (let ((dims (int-matrix ndim)))
	(for (i 0 (1- ndim)) 
	  (dims i (if (= 0 flip) 
		      (fread-int file) (int-swapbytes (fread-int file)))))
	(fclose file)
	(storage-mmap stor s (* 4 (1+ ndim)))
	(new-index stor (dims ())))))

#? (mmap-idx1-ubyte <s>)
;; map the IDX file <s> into virtual memory and returns an IDX to access it

#? (mmap-idx2-ubyte <s>)
;; map the IDX file <s> into virtual memory and returns an IDX to access it

#? (mmap-idx3-ubyte <s>)
;; map the IDX file <s> into virtual memory and returns an IDX to access it

#? (mmap-idx4-ubyte <s>)
;; map the IDX file <s> into virtual memory and returns an IDX to access it

#? (mmap-idx1-byte <s>)
;; map the IDX file <s> into virtual memory and returns an IDX to access it

#? (mmap-idx2-byte <s>)
;; map the IDX file <s> into virtual memory and returns an IDX to access it

#? (mmap-idx3-byte <s>)
;; map the IDX file <s> into virtual memory and returns an IDX to access it

#? (mmap-idx4-byte <s>)
;; map the IDX file <s> into virtual memory and returns an IDX to access it

#? (mmap-idx1-short <s>)
;; map the IDX file <s> into virtual memory and returns an IDX to access it

#? (mmap-idx2-short <s>)
;; map the IDX file <s> into virtual memory and returns an IDX to access it

#? (mmap-idx3-short <s>)
;; map the IDX file <s> into virtual memory and returns an IDX to access it

#? (mmap-idx4-short <s>)
;; map the IDX file <s> into virtual memory and returns an IDX to access it

#? (mmap-idx1-int <s>)
;; map the IDX file <s> into virtual memory and returns an IDX to access it

#? (mmap-idx2-int <s>)
;; map the IDX file <s> into virtual memory and returns an IDX to access it

#? (mmap-idx3-int <s>)
;; map the IDX file <s> into virtual memory and returns an IDX to access it

#? (mmap-idx4-int <s>)
;; map the IDX file <s> into virtual memory and returns an IDX to access it

#? (mmap-idx1-float <s>)
;; map the IDX file <s> into virtual memory and returns an IDX to access it

#? (mmap-idx2-float <s>)
;; map the IDX file <s> into virtual memory and returns an IDX to access it

#? (mmap-idx3-float <s>)
;; map the IDX file <s> into virtual memory and returns an IDX to access it

#? (mmap-idx4-float <s>)
;; map the IDX file <s> into virtual memory and returns an IDX to access it

#? (mmap-idx1-double <s>)
;; map the IDX file <s> into virtual memory and returns an IDX to access it

#? (mmap-idx2-double <s>)
;; map the IDX file <s> into virtual memory and returns an IDX to access it

#? (mmap-idx3-double <s>)
;; map the IDX file <s> into virtual memory and returns an IDX to access it

#? (mmap-idx4-double <s>)
;; map the IDX file <s> into virtual memory and returns an IDX to access it


(de mmap-idx1-ubyte (s) (mmap-idx s (ubyte-storage) 1 0x0800))
(de mmap-idx2-ubyte (s) (mmap-idx s (ubyte-storage) 2 0x0800))
(de mmap-idx3-ubyte (s) (mmap-idx s (ubyte-storage) 3 0x0800))
(de mmap-idx4-ubyte (s) (mmap-idx s (ubyte-storage) 4 0x0800))

(de mmap-idx1-byte (s) (mmap-idx s (byte-storage) 1 0x0900))
(de mmap-idx2-byte (s) (mmap-idx s (byte-storage) 2 0x0900))
(de mmap-idx3-byte (s) (mmap-idx s (byte-storage) 3 0x0900))
(de mmap-idx4-byte (s) (mmap-idx s (byte-storage) 4 0x0900))

(de mmap-idx1-short (s) (mmap-idx s (short-storage) 1 0x0B00))
(de mmap-idx2-short (s) (mmap-idx s (short-storage) 2 0x0B00))
(de mmap-idx3-short (s) (mmap-idx s (short-storage) 3 0x0B00))
(de mmap-idx4-short (s) (mmap-idx s (short-storage) 4 0x0B00))

(de mmap-idx1-int (s) (mmap-idx s (int-storage) 1 0x0C00))
(de mmap-idx2-int (s) (mmap-idx s (int-storage) 2 0x0C00))
(de mmap-idx3-int (s) (mmap-idx s (int-storage) 3 0x0C00))
(de mmap-idx4-int (s) (mmap-idx s (int-storage) 4 0x0C00))

(de mmap-idx1-float (s) (mmap-idx s (float-storage) 1 0x0D00))
(de mmap-idx2-float (s) (mmap-idx s (float-storage) 2 0x0D00))
(de mmap-idx3-float (s) (mmap-idx s (float-storage) 3 0x0D00))
(de mmap-idx4-float (s) (mmap-idx s (float-storage) 4 0x0D00))

(de mmap-idx1-double (s) (mmap-idx s (double-storage) 1 0x0E00))
(de mmap-idx2-double (s) (mmap-idx s (double-storage) 2 0x0E00))
(de mmap-idx3-double (s) (mmap-idx s (double-storage) 3 0x0E00))
(de mmap-idx4-double (s) (mmap-idx s (double-storage) 4 0x0E00))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(dhc-make () int-swapbytes)