[3745] | 1 | /**************************************************************************
|
---|
| 2 | *
|
---|
| 3 | * bitio_gen.c -- General supoport routines for bitio
|
---|
| 4 | * Copyright (C) 1994 Neil Sharman and Alistair Moffat
|
---|
| 5 | *
|
---|
| 6 | * This program is free software; you can redistribute it and/or modify
|
---|
| 7 | * it under the terms of the GNU General Public License as published by
|
---|
| 8 | * the Free Software Foundation; either version 2 of the License, or
|
---|
| 9 | * (at your option) any later version.
|
---|
| 10 | *
|
---|
| 11 | * This program is distributed in the hope that it will be useful,
|
---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 14 | * GNU General Public License for more details.
|
---|
| 15 | *
|
---|
| 16 | * You should have received a copy of the GNU General Public License
|
---|
| 17 | * along with this program; if not, write to the Free Software
|
---|
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 19 | *
|
---|
| 20 | * $Id: bitio_gen.c 3745 2003-02-20 21:20:24Z mdewsnip $
|
---|
| 21 | *
|
---|
| 22 | **************************************************************************/
|
---|
| 23 |
|
---|
| 24 | static char *RCSID = "$Id: bitio_gen.c 3745 2003-02-20 21:20:24Z mdewsnip $";
|
---|
| 25 |
|
---|
| 26 | #include "sysfuncs.h"
|
---|
| 27 | #include "bitio_m.h"
|
---|
| 28 |
|
---|
| 29 |
|
---|
| 30 |
|
---|
| 31 |
|
---|
| 32 | int fprintf (FILE *, const char *,...);
|
---|
| 33 |
|
---|
| 34 | unsigned long
|
---|
| 35 | BIO_Unary_Length (unsigned long val)
|
---|
| 36 | {
|
---|
| 37 | register unsigned long num;
|
---|
| 38 | UNARY_LENGTH (val, num);
|
---|
| 39 | return (num);
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 |
|
---|
| 43 | unsigned long
|
---|
| 44 | BIO_Binary_Length (unsigned long val, unsigned long b)
|
---|
| 45 | {
|
---|
| 46 | register unsigned long num;
|
---|
| 47 | BINARY_LENGTH (val, b, num);
|
---|
| 48 | return (num);
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 |
|
---|
| 52 | unsigned long
|
---|
| 53 | BIO_Gamma_Length (unsigned long val)
|
---|
| 54 | {
|
---|
| 55 | register unsigned long num;
|
---|
| 56 | GAMMA_LENGTH (val, num);
|
---|
| 57 | return (num);
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 |
|
---|
| 61 | unsigned long
|
---|
| 62 | BIO_Delta_Length (unsigned long val)
|
---|
| 63 | {
|
---|
| 64 | register unsigned long num;
|
---|
| 65 | DELTA_LENGTH (val, num);
|
---|
| 66 | return (num);
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 |
|
---|
| 70 | unsigned long
|
---|
| 71 | BIO_Elias_Length (unsigned long val, unsigned long b, double s)
|
---|
| 72 | {
|
---|
| 73 | register unsigned long num;
|
---|
| 74 | ELIAS_LENGTH (val, b, s, num);
|
---|
| 75 | return (num);
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | unsigned long
|
---|
| 79 | BIO_Bblock_Length (unsigned long val, unsigned long b)
|
---|
| 80 | {
|
---|
| 81 | register unsigned long num;
|
---|
| 82 | BBLOCK_LENGTH (val, b, num);
|
---|
| 83 | return (num);
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 |
|
---|
| 87 | int
|
---|
| 88 | BIO_Bblock_Init (int N, int p)
|
---|
| 89 | {
|
---|
| 90 | int b;
|
---|
| 91 | b = (int) (0.5 + 0.6931471 * N / p);
|
---|
| 92 | return (b ? b : 1);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 |
|
---|
| 96 | int
|
---|
| 97 | BIO_Bblock_Init_W (int N, int p)
|
---|
| 98 | {
|
---|
| 99 | int logb;
|
---|
| 100 | FLOORLOG_2 ((N - p) / p, logb);
|
---|
| 101 | return (logb < 0 ? 1 : (1 << logb));
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | int
|
---|
| 105 | BIO_Bblock_Bound_b (int N, int p, int b)
|
---|
| 106 | {
|
---|
| 107 | int clogb;
|
---|
| 108 | CEILLOG_2 (b, clogb);
|
---|
| 109 | return (p * (1 + clogb) + (N - p * ((1 << clogb) - b + 1)) / b);
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | int
|
---|
| 113 | BIO_Bblock_Bound (int N, int p)
|
---|
| 114 | {
|
---|
| 115 | int b;
|
---|
| 116 | b = BIO_Bblock_Init_W (N, p);
|
---|
| 117 | return (BIO_Bblock_Bound_b (N, p, b));
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 |
|
---|
| 121 | /* adjustment is a hack to overcome inaccuracies in floating point,
|
---|
| 122 | where (int)(ln4/ln2) can equal 1, not 2. */
|
---|
| 123 | /* copied from mg-1.3f, 17-09-01 kjm18 */
|
---|
| 124 | int
|
---|
| 125 | BIO_Gamma_Bound (int N, int p)
|
---|
| 126 | {
|
---|
| 127 | /* return ((int) (p * (2 * log2 ((double) N / p) + 1))); */
|
---|
| 128 | return ((int) (p * (2 * log2 (((double) N * 1.0001) / p) + 1)));
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | int
|
---|
| 132 | floorlog_2 (int b)
|
---|
| 133 | {
|
---|
| 134 | int logb;
|
---|
| 135 | FLOORLOG_2 (b, logb);
|
---|
| 136 | return logb;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | int
|
---|
| 140 | ceillog_2 (int b)
|
---|
| 141 | {
|
---|
| 142 | int logb;
|
---|
| 143 | CEILLOG_2 (b, logb);
|
---|
| 144 | return logb;
|
---|
| 145 | }
|
---|