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 | **************************************************************************/
|
---|
21 |
|
---|
22 | #include "sysfuncs.h"
|
---|
23 | #include "bitio_m.h"
|
---|
24 |
|
---|
25 |
|
---|
26 |
|
---|
27 |
|
---|
28 | int fprintf (FILE *, const char *,...);
|
---|
29 |
|
---|
30 | mg_u_long
|
---|
31 | BIO_Unary_Length (mg_u_long val)
|
---|
32 | {
|
---|
33 | register mg_u_long num;
|
---|
34 | UNARY_LENGTH (val, num);
|
---|
35 | return (num);
|
---|
36 | }
|
---|
37 |
|
---|
38 |
|
---|
39 | mg_u_long
|
---|
40 | BIO_Binary_Length (mg_u_long val, mg_u_long b)
|
---|
41 | {
|
---|
42 | register mg_u_long num;
|
---|
43 | BINARY_LENGTH (val, b, num);
|
---|
44 | return (num);
|
---|
45 | }
|
---|
46 |
|
---|
47 |
|
---|
48 | mg_u_long
|
---|
49 | BIO_Gamma_Length (mg_u_long val)
|
---|
50 | {
|
---|
51 | register mg_u_long num;
|
---|
52 | GAMMA_LENGTH (val, num);
|
---|
53 | return (num);
|
---|
54 | }
|
---|
55 |
|
---|
56 |
|
---|
57 | mg_u_long
|
---|
58 | BIO_Delta_Length (mg_u_long val)
|
---|
59 | {
|
---|
60 | register mg_u_long num;
|
---|
61 | DELTA_LENGTH (val, num);
|
---|
62 | return (num);
|
---|
63 | }
|
---|
64 |
|
---|
65 |
|
---|
66 | mg_u_long
|
---|
67 | BIO_Elias_Length (mg_u_long val, mg_u_long b, double s)
|
---|
68 | {
|
---|
69 | register mg_u_long num;
|
---|
70 | ELIAS_LENGTH (val, b, s, num);
|
---|
71 | return (num);
|
---|
72 | }
|
---|
73 |
|
---|
74 | mg_u_long
|
---|
75 | BIO_Bblock_Length (mg_u_long val, mg_u_long b)
|
---|
76 | {
|
---|
77 | register mg_u_long num;
|
---|
78 | BBLOCK_LENGTH (val, b, num);
|
---|
79 | return (num);
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 | int
|
---|
84 | BIO_Bblock_Init (int N, int p)
|
---|
85 | {
|
---|
86 | int b;
|
---|
87 | b = (int) (0.5 + 0.6931471 * N / p);
|
---|
88 | return (b ? b : 1);
|
---|
89 | }
|
---|
90 |
|
---|
91 |
|
---|
92 | int
|
---|
93 | BIO_Bblock_Init_W (int N, int p)
|
---|
94 | {
|
---|
95 | int logb;
|
---|
96 | FLOORLOG_2 ((N - p) / p, logb);
|
---|
97 | return (logb < 0 ? 1 : (1 << logb));
|
---|
98 | }
|
---|
99 |
|
---|
100 | int
|
---|
101 | BIO_Bblock_Bound_b (int N, int p, int b)
|
---|
102 | {
|
---|
103 | int clogb;
|
---|
104 | CEILLOG_2 (b, clogb);
|
---|
105 | return (p * (1 + clogb) + (N - p * ((1 << clogb) - b + 1)) / b);
|
---|
106 | }
|
---|
107 |
|
---|
108 | int
|
---|
109 | BIO_Bblock_Bound (int N, int p)
|
---|
110 | {
|
---|
111 | int b;
|
---|
112 | b = BIO_Bblock_Init_W (N, p);
|
---|
113 | return (BIO_Bblock_Bound_b (N, p, b));
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 | /* adjustment is a hack to overcome inaccuracies in floating point,
|
---|
118 | where (int)(ln4/ln2) can equal 1, not 2. */
|
---|
119 | /* copied from mg-1.3f, 17-09-01 kjm18 */
|
---|
120 | int
|
---|
121 | BIO_Gamma_Bound (int N, int p)
|
---|
122 | {
|
---|
123 | /* return ((int) (p * (2 * log2 ((double) N / p) + 1))); */
|
---|
124 | return ((int) (p * (2 * log2 (((double) N * 1.0001) / p) + 1)));
|
---|
125 | }
|
---|
126 |
|
---|
127 | int
|
---|
128 | floorlog_2 (int b)
|
---|
129 | {
|
---|
130 | int logb;
|
---|
131 | FLOORLOG_2 (b, logb);
|
---|
132 | return logb;
|
---|
133 | }
|
---|
134 |
|
---|
135 | int
|
---|
136 | ceillog_2 (int b)
|
---|
137 | {
|
---|
138 | int logb;
|
---|
139 | CEILLOG_2 (b, logb);
|
---|
140 | return logb;
|
---|
141 | }
|
---|