<|start_filename|>libs/detex/bits.h<|end_filename|>
/*

Copyright (c) 2015 <NAME> <<EMAIL>>

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

*/

/* Data structure used to extract bits from 128-bit bitstring. */

typedef struct {
	uint64_t data0;
	uint64_t data1;
	int index;
} detexBlock128;

uint32_t detexBlock128ExtractBits(detexBlock128 *block, int nu_bits);

/* Return bitfield from bit0 to bit1 from 64-bit bitstring. */
static DETEX_INLINE_ONLY uint32_t detexGetBits64(uint64_t data, int bit0, int bit1) {
	return (data & (((uint64_t)1 << (bit1 + 1)) - 1)) >> bit0;
}

/* Return reversed bitfield (bit1 to bit0) from 64-bit bitstring. */
static DETEX_INLINE_ONLY uint32_t detexGetBits64Reversed(uint64_t data, int bit0, int bit1) {
	// Assumes bit0 > bit1.
	// Reverse the bits.
	uint32_t val = 0;
	for (int i = 0; i <= bit0 - bit1; i++) {
		int shift_right = bit0 - 2 * i;
		if (shift_right >= 0)
			val |= (data & ((uint64_t)1 << (bit0 - i))) >> shift_right;
		else
			val |= (data & ((uint64_t)1 << (bit0 - i))) << (- shift_right);
	}
	return val;
}

/* Clear bit0 to bit1 of 64-bit bitstring. */
static DETEX_INLINE_ONLY uint64_t detexClearBits64(uint64_t data, int bit0, int bit1) {
	uint64_t mask = ~(((uint64_t)1 << (bit1 + 1)) - 1);
	mask |= ((uint64_t)1 << bit0) - 1;
	return data & mask;
}

/* Set bit0 to bit1 of 64-bit bitstring. */
static DETEX_INLINE_ONLY uint64_t detexSetBits64(uint64_t data, int bit0, int bit1, uint64_t val) {
	uint64_t d = detexClearBits64(data, bit0, bit1);
	d |= val << bit0;
	return d;
}



<|start_filename|>libs/mspack/readbits.h<|end_filename|>
/* This file is part of libmspack.
 * (C) 2003-2010 <NAME>.
 *
 * libmspack is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License (LGPL) version 2.1
 *
 * For further details, see the file COPYING.LIB distributed with libmspack
 */

#ifndef MSPACK_READBITS_H
#define MSPACK_READBITS_H 1

/* this header defines macros that read data streams by
 * the individual bits
 *
 * INIT_BITS         initialises bitstream state in state structure
 * STORE_BITS        stores bitstream state in state structure
 * RESTORE_BITS      restores bitstream state from state structure
 * ENSURE_BITS(n)    ensure there are at least N bits in the bit buffer
 * READ_BITS(var,n)  takes N bits from the buffer and puts them in var
 * PEEK_BITS(n)      extracts without removing N bits from the bit buffer
 * REMOVE_BITS(n)    removes N bits from the bit buffer
 *
 * READ_BITS simply calls ENSURE_BITS, PEEK_BITS and REMOVE_BITS,
 * which means it's limited to reading the number of bits you can
 * ensure at any one time. It also fails if asked to read zero bits.
 * If you need to read zero bits, or more bits than can be ensured in
 * one go, use READ_MANY_BITS instead.
 *
 * These macros have variable names baked into them, so to use them
 * you have to define some macros:
 * - BITS_TYPE: the type name of your state structure
 * - BITS_VAR: the variable that points to your state structure
 * - define BITS_ORDER_MSB if bits are read from the MSB, or
 *   define BITS_ORDER_LSB if bits are read from the LSB
 * - READ_BYTES: some code that reads more data into the bit buffer,
 *   it should use READ_IF_NEEDED (calls read_input if the byte buffer
 *   is empty), then INJECT_BITS(data,n) to put data from the byte
 *   buffer into the bit buffer.
 *
 * You also need to define some variables and structure members:
 * - unsigned char *i_ptr;    // current position in the byte buffer
 * - unsigned char *i_end;    // end of the byte buffer
 * - unsigned int bit_buffer; // the bit buffer itself
 * - unsigned int bits_left;  // number of bits remaining
 *
 * If you use read_input() and READ_IF_NEEDED, they also expect these
 * structure members:
 * - struct mspack_system *sys;  // to access sys->read()
 * - unsigned int error;         // to record/return read errors
 * - unsigned char input_end;    // to mark reaching the EOF
 * - unsigned char *inbuf;       // the input byte buffer
 * - unsigned int inbuf_size;    // the size of the input byte buffer
 *
 * Your READ_BYTES implementation should read data from *i_ptr and
 * put them in the bit buffer. READ_IF_NEEDED will call read_input()
 * if i_ptr reaches i_end, and will fill up inbuf and set i_ptr to
 * the start of inbuf and i_end to the end of inbuf.
 *
 * If you're reading in MSB order, the routines work by using the area
 * beyond the MSB and the LSB of the bit buffer as a free source of
 * zeroes when shifting. This avoids having to mask any bits. So we
 * have to know the bit width of the bit buffer variable. We use
 * <limits.h> and CHAR_BIT to find the size of the bit buffer in bits.
 *
 * If you are reading in LSB order, bits need to be masked. Normally
 * this is done by computing the mask: N bits are masked by the value
 * (1<<N)-1). However, you can define BITS_LSB_TABLE to use a lookup
 * table instead of computing this. This adds two new macros,
 * PEEK_BITS_T and READ_BITS_T which work the same way as PEEK_BITS
 * and READ_BITS, except they use this lookup table. This is useful if
 * you need to look up a number of bits that are only known at
 * runtime, so the bit mask can't be turned into a constant by the
 * compiler.

 * The bit buffer datatype should be at least 32 bits wide: it must be
 * possible to ENSURE_BITS(17), so it must be possible to add 16 new bits
 * to the bit buffer when the bit buffer already has 1 to 15 bits left.
 */

#ifndef BITS_VAR
# error "define BITS_VAR as the state structure poiner variable name"
#endif
#ifndef BITS_TYPE
# error "define BITS_TYPE as the state structure type"
#endif
#if defined(BITS_ORDER_MSB) && defined(BITS_ORDER_LSB)
# error "you must define either BITS_ORDER_MSB or BITS_ORDER_LSB"
#else
# if !(defined(BITS_ORDER_MSB) || defined(BITS_ORDER_LSB))
#  error "you must define BITS_ORDER_MSB or BITS_ORDER_LSB"
# endif
#endif

#if HAVE_LIMITS_H
# include <limits.h>
#endif
#ifndef CHAR_BIT
# define CHAR_BIT (8)
#endif
#define BITBUF_WIDTH (sizeof(bit_buffer) * CHAR_BIT)

#define INIT_BITS do {				\
    BITS_VAR->i_ptr      = &BITS_VAR->inbuf[0];	\
    BITS_VAR->i_end      = &BITS_VAR->inbuf[0];	\
    BITS_VAR->bit_buffer = 0;			\
    BITS_VAR->bits_left  = 0;			\
    BITS_VAR->input_end  = 0;			\
} while (0)

#define STORE_BITS do {			\
    BITS_VAR->i_ptr      = i_ptr;	\
    BITS_VAR->i_end      = i_end;	\
    BITS_VAR->bit_buffer = bit_buffer;	\
    BITS_VAR->bits_left  = bits_left;	\
} while (0)

#define RESTORE_BITS do {		\
    i_ptr      = BITS_VAR->i_ptr;	\
    i_end      = BITS_VAR->i_end;	\
    bit_buffer = BITS_VAR->bit_buffer;	\
    bits_left  = BITS_VAR->bits_left;	\
} while (0)

#define ENSURE_BITS(nbits) do {			\
    while (bits_left < (nbits)) READ_BYTES;	\
} while (0)

#define READ_BITS(val, nbits) do {		\
    ENSURE_BITS(nbits);				\
    (val) = PEEK_BITS(nbits);			\
    REMOVE_BITS(nbits);				\
} while (0)

#define READ_MANY_BITS(val, bits) do {				\
    unsigned char needed = (bits), bitrun;			\
    (val) = 0;							\
    while (needed > 0) {					\
	if (bits_left <= (BITBUF_WIDTH - 16)) READ_BYTES;	\
	bitrun = (bits_left < needed) ? bits_left : needed;	\
	(val) = ((val) << bitrun) | PEEK_BITS(bitrun);		\
	REMOVE_BITS(bitrun);					\
	needed -= bitrun;					\
    }								\
} while (0)

#ifdef BITS_ORDER_MSB
# define PEEK_BITS(nbits)   (bit_buffer >> (BITBUF_WIDTH - (nbits)))
# define REMOVE_BITS(nbits) ((bit_buffer <<= (nbits)), (bits_left -= (nbits)))
# define INJECT_BITS(bitdata,nbits) ((bit_buffer |= \
    (bitdata) << (BITBUF_WIDTH - (nbits) - bits_left)), (bits_left += (nbits)))
#else /* BITS_ORDER_LSB */
# define PEEK_BITS(nbits)   (bit_buffer & ((1 << (nbits))-1))
# define REMOVE_BITS(nbits) ((bit_buffer >>= (nbits)), (bits_left -= (nbits)))
# define INJECT_BITS(bitdata,nbits) ((bit_buffer |= \
    (bitdata) << bits_left), (bits_left += (nbits)))
#endif

#ifdef BITS_LSB_TABLE
/* lsb_bit_mask[n] = (1 << n) - 1 */
static const unsigned short lsb_bit_mask[17] = {
    0x0000, 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
    0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
};
# define PEEK_BITS_T(nbits) (bit_buffer & lsb_bit_mask[(nbits)])
# define READ_BITS_T(val, nbits) do {	\
    ENSURE_BITS(nbits);			\
    (val) = PEEK_BITS_T(nbits);		\
    REMOVE_BITS(nbits);			\
} while (0)
#endif

#ifndef BITS_NO_READ_INPUT
# define READ_IF_NEEDED do {		\
    if (i_ptr >= i_end) {		\
	if (read_input(BITS_VAR))	\
	    return BITS_VAR->error;	\
	i_ptr = BITS_VAR->i_ptr;	\
	i_end = BITS_VAR->i_end;	\
    }					\
} while (0)

static int read_input(BITS_TYPE *p) {
    int read = p->sys->read(p->input, &p->inbuf[0], (int)p->inbuf_size);
    if (read < 0) return p->error = MSPACK_ERR_READ;

    /* we might overrun the input stream by asking for bits we don't use,
     * so fake 2 more bytes at the end of input */
    if (read == 0) {
	if (p->input_end) {
	    D(("out of input bytes"))
	    return p->error = MSPACK_ERR_READ;
	}
	else {
	    read = 2;
	    p->inbuf[0] = p->inbuf[1] = 0;
	    p->input_end = 1;
	}
    }

    /* update i_ptr and i_end */
    p->i_ptr = &p->inbuf[0];
    p->i_end = &p->inbuf[read];
    return MSPACK_ERR_OK;
}
#endif
#endif


<|start_filename|>libs/detex/file-info.h<|end_filename|>
/*

Copyright (c) 2015 <NAME> <<EMAIL>>

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

*/

typedef struct {
	uint32_t texture_format;
	int ktx_support;
	int dds_support;
	const char *text1;
	const char *text2;
	int block_width;		// The block width (1 for uncompressed textures).
	int block_height;		// The block height (1 for uncompressed textures).
	int gl_internal_format;
	uint32_t gl_format;
	uint32_t gl_type;
	const char *dx_four_cc;
	int dx10_format;
} detexTextureFileInfo;

// Look-up texture file info for texture format.
const detexTextureFileInfo *detexLookupTextureFormatFileInfo(uint32_t texture_format);

// Look-up texture file info for texture description.
const detexTextureFileInfo *detexLookupTextureDescription(const char *s);

// Look-up texture file info for KTX file format based on GL format parameters.
const detexTextureFileInfo *detexLookupKTXFileInfo(int gl_internal_format, int gl_format, int gl_type);

// Look-up texture file info for DDS file format based on DX format parameters.
const detexTextureFileInfo *detexLookupDDSFileInfo(const char *four_cc, int dx10_format, uint32_t pixel_format_flags, int bitcount, uint32_t red_mask, uint32_t green_mask, uint32_t blue_mask, uint32_t alpha_mask);



<|start_filename|>libs/nvtt/nvmath/nvmath.h<|end_filename|>
// This code is in the public domain -- <EMAIL>

#ifndef NV_MATH_H
#define NV_MATH_H

#include <nvcore/nvcore.h>
#include <nvcore/Debug.h>

#include <math.h>

// Function linkage
#if NVMATH_SHARED
#ifdef NVMATH_EXPORTS
#define NVMATH_API DLL_EXPORT
#define NVMATH_CLASS DLL_EXPORT_CLASS
#else
#define NVMATH_API DLL_IMPORT
#define NVMATH_CLASS DLL_IMPORT
#endif
#else // NVMATH_SHARED
#define NVMATH_API
#define NVMATH_CLASS
#endif // NVMATH_SHARED

#ifndef PI
#define PI      			float(3.1415926535897932384626433833)
#endif

#define NV_EPSILON			(0.0001f)
#define NV_NORMAL_EPSILON	(0.001f)

/*
#define SQ(r)				((r)*(r))

#define	SIGN_BITMASK		0x80000000

/// Integer representation of a floating-point value.
#define IR(x)					((uint32 &)(x))

/// Absolute integer representation of a floating-point value
#define AIR(x)					(IR(x) & 0x7fffffff)

/// Floating-point representation of an integer value.
#define FR(x)					((float&)(x))

/// Integer-based comparison of a floating point value.
/// Don't use it blindly, it can be faster or slower than the FPU comparison, depends on the context.
#define IS_NEGATIVE_FLOAT(x)	(IR(x)&SIGN_BITMASK)
*/

inline double sqrt_assert(const double f)
{
	nvDebugCheck(f >= 0.0f);
	return sqrt(f);
}

inline float sqrtf_assert(const float f)
{
	nvDebugCheck(f >= 0.0f);
	return sqrtf(f);
}

inline double acos_assert(const double f)
{
	nvDebugCheck(f >= -1.0f && f <= 1.0f);
	return acos(f);
}

inline float acosf_assert(const float f)
{
	nvDebugCheck(f >= -1.0f && f <= 1.0f);
	return acosf(f);
}

inline double asin_assert(const double f)
{
	nvDebugCheck(f >= -1.0f && f <= 1.0f);
	return asin(f);
}

inline float asinf_assert(const float f)
{
	nvDebugCheck(f >= -1.0f && f <= 1.0f);
	return asinf(f);
}

// Replace default functions with asserting ones.
#define sqrt sqrt_assert
#define sqrtf sqrtf_assert
#define acos acos_assert
#define acosf acosf_assert
#define asin asin_assert
#define asinf asinf_assert

#if NV_OS_WIN32
#include <float.h>
#endif

namespace nv
{
inline float toRadian(float degree) { return degree * (PI / 180.0f); }
inline float toDegree(float radian) { return radian * (180.0f / PI); }
	
inline bool equal(const float f0, const float f1, const float epsilon = NV_EPSILON)
{
	return fabs(f0-f1) <= epsilon;
}

inline bool isZero(const float f, const float epsilon = NV_EPSILON)
{
	return fabs(f) <= epsilon;
}

inline bool isFinite(const float f)
{
#if NV_OS_WIN32
	return _finite(f) != 0;
#elif NV_OS_DARWIN
	return isfinite(f);
#elif NV_OS_LINUX
	return finitef(f);
#else
#	error "isFinite not supported"
#endif
//return std::isfinite (f);
//return finite (f);
}

inline bool isNan(const float f)
{
#if NV_OS_WIN32
	return _isnan(f) != 0;
#elif NV_OS_DARWIN
	return isnan(f);
#elif NV_OS_LINUX
	return isnanf(f);
#else
#	error "isNan not supported"
#endif
}

inline uint log2(uint i)
{
	uint value = 0;
	while( i >>= 1 ) {
		value++;
	}
	return value;
}

inline float lerp(float f0, float f1, float t)
{
	const float s = 1.0f - t;
	return f0 * s + f1 * t;
}

inline float square(float f)
{
	return f * f;
}

} // nv

#endif // NV_MATH_H


<|start_filename|>libs/mspack/readhuff.h<|end_filename|>
/* This file is part of libmspack.
 * (C) 2003-2010 <NAME>.
 *
 * libmspack is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License (LGPL) version 2.1
 *
 * For further details, see the file COPYING.LIB distributed with libmspack
 */

#ifndef MSPACK_READHUFF_H
#define MSPACK_READHUFF_H 1

/* This implements a fast Huffman tree decoding system.
 */

#if !(defined(BITS_ORDER_MSB) || defined(BITS_ORDER_LSB))
# error "readhuff.h is used in conjunction with readbits.h, include that first"
#endif
#if !(defined(TABLEBITS) && defined(MAXSYMBOLS))
# error "define TABLEBITS(tbl) and MAXSYMBOLS(tbl) before using readhuff.h"
#endif
#if !(defined(HUFF_TABLE) && defined(HUFF_LEN))
# error "define HUFF_TABLE(tbl) and HUFF_LEN(tbl) before using readhuff.h"
#endif
#ifndef HUFF_ERROR
# error "define HUFF_ERROR before using readhuff.h"
#endif
#ifndef HUFF_MAXBITS
# define HUFF_MAXBITS 16
#endif

/* Decodes the next huffman symbol from the input bitstream into var.
 * Do not use this macro on a table unless build_decode_table() succeeded.
 */
#define READ_HUFFSYM(tbl, var) do {			\
    ENSURE_BITS(HUFF_MAXBITS);				\
    sym = HUFF_TABLE(tbl, PEEK_BITS(TABLEBITS(tbl)));	\
    if (sym >= MAXSYMBOLS(tbl))	HUFF_TRAVERSE(tbl);	\
    (var) = sym;					\
    i = HUFF_LEN(tbl, sym);				\
    REMOVE_BITS(i);					\
} while (0)

#ifdef BITS_ORDER_LSB
# define HUFF_TRAVERSE(tbl) do {			\
    i = TABLEBITS(tbl) - 1;				\
    do {						\
	if (i++ > HUFF_MAXBITS) HUFF_ERROR;		\
	sym = HUFF_TABLE(tbl,				\
            (sym << 1) | ((bit_buffer >> i) & 1));	\
    } while (sym >= MAXSYMBOLS(tbl));			\
} while (0)
#else
#define HUFF_TRAVERSE(tbl) do {				\
    i = 1 << (BITBUF_WIDTH - TABLEBITS(tbl));		\
    do {						\
	if ((i >>= 1) == 0) HUFF_ERROR;			\
	sym = HUFF_TABLE(tbl,				\
	    (sym << 1) | ((bit_buffer & i) ? 1 : 0));	\
    } while (sym >= MAXSYMBOLS(tbl));			\
} while (0)
#endif

/* make_decode_table(nsyms, nbits, length[], table[])
 *
 * This function was originally coded by <NAME>.
 * It builds a fast huffman decoding table from
 * a canonical huffman code lengths table.
 *
 * nsyms  = total number of symbols in this huffman tree.
 * nbits  = any symbols with a code length of nbits or less can be decoded
 *          in one lookup of the table.
 * length = A table to get code lengths from [0 to nsyms-1]
 * table  = The table to fill up with decoded symbols and pointers.
 *          Should be ((1<<nbits) + (nsyms*2)) in length.
 *
 * Returns 0 for OK or 1 for error
 */
static int make_decode_table(unsigned int nsyms, unsigned int nbits,
			     unsigned char *length, unsigned short *table)
{
    register unsigned short sym, next_symbol;
    register unsigned int leaf, fill;
#ifdef BITS_ORDER_LSB
    register unsigned int reverse;
#endif
    register unsigned char bit_num;
    unsigned int pos         = 0; /* the current position in the decode table */
    unsigned int table_mask  = 1 << nbits;
    unsigned int bit_mask    = table_mask >> 1; /* don't do 0 length codes */

    /* fill entries for codes short enough for a direct mapping */
    for (bit_num = 1; bit_num <= nbits; bit_num++) {
	for (sym = 0; sym < nsyms; sym++) {
	    if (length[sym] != bit_num) continue;
#ifdef BITS_ORDER_MSB
	    leaf = pos;
#else
	    /* reverse the significant bits */
	    fill = length[sym]; reverse = pos >> (nbits - fill); leaf = 0;
	    do {leaf <<= 1; leaf |= reverse & 1; reverse >>= 1;} while (--fill);
#endif

	    if((pos += bit_mask) > table_mask) return 1; /* table overrun */

	    /* fill all possible lookups of this symbol with the symbol itself */
#ifdef BITS_ORDER_MSB
	    for (fill = bit_mask; fill-- > 0;) table[leaf++] = sym;
#else
	    fill = bit_mask; next_symbol = 1 << bit_num;
	    do { table[leaf] = sym; leaf += next_symbol; } while (--fill);
#endif
	}
	bit_mask >>= 1;
    }

    /* exit with success if table is now complete */
    if (pos == table_mask) return 0;

    /* mark all remaining table entries as unused */
    for (sym = pos; sym < table_mask; sym++) {
#ifdef BITS_ORDER_MSB
	table[sym] = 0xFFFF;
#else
	reverse = sym; leaf = 0; fill = nbits;
	do { leaf <<= 1; leaf |= reverse & 1; reverse >>= 1; } while (--fill);
	table[leaf] = 0xFFFF;
#endif
    }

    /* next_symbol = base of allocation for long codes */
    next_symbol = ((table_mask >> 1) < nsyms) ? nsyms : (table_mask >> 1);

    /* give ourselves room for codes to grow by up to 16 more bits.
     * codes now start at bit nbits+16 and end at (nbits+16-codelength) */
    pos <<= 16;
    table_mask <<= 16;
    bit_mask = 1 << 15;

    for (bit_num = nbits+1; bit_num <= HUFF_MAXBITS; bit_num++) {
	for (sym = 0; sym < nsyms; sym++) {
	    if (length[sym] != bit_num) continue;

#ifdef BITS_ORDER_MSB
	    leaf = pos >> 16;
#else
	    /* leaf = the first nbits of the code, reversed */
	    reverse = pos >> 16; leaf = 0; fill = nbits;
	    do {leaf <<= 1; leaf |= reverse & 1; reverse >>= 1;} while (--fill);
#endif
	    for (fill = 0; fill < (bit_num - nbits); fill++) {
		/* if this path hasn't been taken yet, 'allocate' two entries */
		if (table[leaf] == 0xFFFF) {
		    table[(next_symbol << 1)     ] = 0xFFFF;
		    table[(next_symbol << 1) + 1 ] = 0xFFFF;
		    table[leaf] = next_symbol++;
		}

		/* follow the path and select either left or right for next bit */
		leaf = table[leaf] << 1;
		if ((pos >> (15-fill)) & 1) leaf++;
	    }
	    table[leaf] = sym;

	    if ((pos += bit_mask) > table_mask) return 1; /* table overflow */
	}
	bit_mask >>= 1;
    }

    /* full table? */
    return (pos == table_mask) ? 0 : 1;
}
#endif
