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