Debian Codegraph @

converter/other/jpeg2000/libjasper/jpc/jpc_math.c

raw
1 #include <assert.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <math.h>
5 #include <stdlib.h>
6 #include <stdarg.h>
7
8 #include "jpc_math.h"
9
10
11
12 /* Calculate the integer quantity floor(log2(x)), where x is a positive
13 integer. */
14 int
15 jpc_floorlog2(int_fast32_t const arg) {
16
17 int y;
18 int x;
19
20 assert(arg > 0);
21
22 y = 0;
23 x = arg;
24 while (x > 1) {
25 x >>= 1;
26 ++y;
27 }
28 return y;
29 }
30
31
32
33 /*
34 jpc_floorlog2() and jpc_firstone() do the same thing.
35 The only difference is how input 0 is handled.
36
37 n : 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
38 ceil(log2(n)) : x 0 1 2 2 3 3 3 3 4 4 4 4 4 4 4 4 5 5 5
39 floor(log2(n)) : x 0 1 1 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4
40 31-__builtin_clz(n): x 0 1 1 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4
41 jpc_floorlog2(n) : x 0 1 1 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4
42 jpc_firstone(n) :-1 0 1 1 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4
43
44 */
45
46
47
48 int
49 jpc_firstone(int_fast32_t const arg) {
50 /*----------------------------------------------------------------------------
51 Calculate the bit position of the first leading one in a nonnegative
52 integer.
53
54 LSB is bit position 0. Iff there are no ones, return -1.
55 -----------------------------------------------------------------------------*/
56 int n;
57 int x;
58
59 assert(arg >= 0);
60
61 n = -1;
62 x = arg;
63 while (x > 0) {
64 x >>= 1;
65 ++n;
66 }
67 return n;
68 }
69
70
71
72 /*
73 * Copyright (c) 1999-2000 Image Power, Inc. and the University of
74 * British Columbia.
75 * Copyright (c) 2001-2002 Michael David Adams.
76 * All rights reserved.
77 */
78
79 /* __START_OF_JASPER_LICENSE__
80 *
81 * JasPer Software License
82 *
83 * IMAGE POWER JPEG-2000 PUBLIC LICENSE
84 * ************************************
85 *
86 * GRANT:
87 *
88 * Permission is hereby granted, free of charge, to any person (the "User")
89 * obtaining a copy of this software and associated documentation, to deal
90 * in the JasPer Software without restriction, including without limitation
91 * the right to use, copy, modify, merge, publish, distribute, sublicense,
92 * and/or sell copies of the JasPer Software (in source and binary forms),
93 * and to permit persons to whom the JasPer Software is furnished to do so,
94 * provided further that the License Conditions below are met.
95 *
96 * License Conditions
97 * ******************
98 *
99 * A. Redistributions of source code must retain the above copyright notice,
100 * and this list of conditions, and the following disclaimer.
101 *
102 * B. Redistributions in binary form must reproduce the above copyright
103 * notice, and this list of conditions, and the following disclaimer in
104 * the documentation and/or other materials provided with the distribution.
105 *
106 * C. Neither the name of Image Power, Inc. nor any other contributor
107 * (including, but not limited to, the University of British Columbia and
108 * Michael David Adams) may be used to endorse or promote products derived
109 * from this software without specific prior written permission.
110 *
111 * D. User agrees that it shall not commence any action against Image Power,
112 * Inc., the University of British Columbia, Michael David Adams, or any
113 * other contributors (collectively "Licensors") for infringement of any
114 * intellectual property rights ("IPR") held by the User in respect of any
115 * technology that User owns or has a right to license or sublicense and
116 * which is an element required in order to claim compliance with ISO/IEC
117 * 15444-1 (i.e., JPEG-2000 Part 1). "IPR" means all intellectual property
118 * rights worldwide arising under statutory or common law, and whether
119 * or not perfected, including, without limitation, all (i) patents and
120 * patent applications owned or licensable by User; (ii) rights associated
121 * with works of authorship including copyrights, copyright applications,
122 * copyright registrations, mask work rights, mask work applications,
123 * mask work registrations; (iii) rights relating to the protection of
124 * trade secrets and confidential information; (iv) any right analogous
125 * to those set forth in subsections (i), (ii), or (iii) and any other
126 * proprietary rights relating to intangible property (other than trademark,
127 * trade dress, or service mark rights); and (v) divisions, continuations,
128 * renewals, reissues and extensions of the foregoing (as and to the extent
129 * applicable) now existing, hereafter filed, issued or acquired.
130 *
131 * E. If User commences an infringement action against any Licensor(s) then
132 * such Licensor(s) shall have the right to terminate User's license and
133 * all sublicenses that have been granted hereunder by User to other parties.
134 *
135 * F. This software is for use only in hardware or software products that
136 * are compliant with ISO/IEC 15444-1 (i.e., JPEG-2000 Part 1). No license
137 * or right to this Software is granted for products that do not comply
138 * with ISO/IEC 15444-1. The JPEG-2000 Part 1 standard can be purchased
139 * from the ISO.
140 *
141 * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE.
142 * NO USE OF THE JASPER SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
143 * THIS DISCLAIMER. THE JASPER SOFTWARE IS PROVIDED BY THE LICENSORS AND
144 * CONTRIBUTORS UNDER THIS LICENSE ON AN ``AS-IS'' BASIS, WITHOUT WARRANTY
145 * OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION,
146 * WARRANTIES THAT THE JASPER SOFTWARE IS FREE OF DEFECTS, IS MERCHANTABLE,
147 * IS FIT FOR A PARTICULAR PURPOSE OR IS NON-INFRINGING. THOSE INTENDING
148 * TO USE THE JASPER SOFTWARE OR MODIFICATIONS THEREOF FOR USE IN HARDWARE
149 * OR SOFTWARE PRODUCTS ARE ADVISED THAT THEIR USE MAY INFRINGE EXISTING
150 * PATENTS, COPYRIGHTS, TRADEMARKS, OR OTHER INTELLECTUAL PROPERTY RIGHTS.
151 * THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE JASPER SOFTWARE
152 * IS WITH THE USER. SHOULD ANY PART OF THE JASPER SOFTWARE PROVE DEFECTIVE
153 * IN ANY RESPECT, THE USER (AND NOT THE INITIAL DEVELOPERS, THE UNIVERSITY
154 * OF BRITISH COLUMBIA, IMAGE POWER, INC., MICHAEL DAVID ADAMS, OR ANY
155 * OTHER CONTRIBUTOR) SHALL ASSUME THE COST OF ANY NECESSARY SERVICING,
156 * REPAIR OR CORRECTION. UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY,
157 * WHETHER TORT (INCLUDING NEGLIGENCE), CONTRACT, OR OTHERWISE, SHALL THE
158 * INITIAL DEVELOPER, THE UNIVERSITY OF BRITISH COLUMBIA, IMAGE POWER, INC.,
159 * MICHAEL DAVID ADAMS, ANY OTHER CONTRIBUTOR, OR ANY DISTRIBUTOR OF THE
160 * JASPER SOFTWARE, OR ANY SUPPLIER OF ANY OF SUCH PARTIES, BE LIABLE TO
161 * THE USER OR ANY OTHER PERSON FOR ANY INDIRECT, SPECIAL, INCIDENTAL, OR
162 * CONSEQUENTIAL DAMAGES OF ANY CHARACTER INCLUDING, WITHOUT LIMITATION,
163 * DAMAGES FOR LOSS OF GOODWILL, WORK STOPPAGE, COMPUTER FAILURE OR
164 * MALFUNCTION, OR ANY AND ALL OTHER COMMERCIAL DAMAGES OR LOSSES, EVEN IF
165 * SUCH PARTY HAD BEEN INFORMED, OR OUGHT TO HAVE KNOWN, OF THE POSSIBILITY
166 * OF SUCH DAMAGES. THE JASPER SOFTWARE AND UNDERLYING TECHNOLOGY ARE NOT
167 * FAULT-TOLERANT AND ARE NOT DESIGNED, MANUFACTURED OR INTENDED FOR USE OR
168 * RESALE AS ON-LINE CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING
169 * FAIL-SAFE PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES,
170 * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT
171 * LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
172 * JASPER SOFTWARE OR UNDERLYING TECHNOLOGY OR PRODUCT COULD LEAD DIRECTLY
173 * TO DEATH, PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE
174 * ("HIGH RISK ACTIVITIES"). LICENSOR SPECIFICALLY DISCLAIMS ANY EXPRESS
175 * OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. USER WILL NOT
176 * KNOWINGLY USE, DISTRIBUTE OR RESELL THE JASPER SOFTWARE OR UNDERLYING
177 * TECHNOLOGY OR PRODUCTS FOR HIGH RISK ACTIVITIES AND WILL ENSURE THAT ITS
178 * CUSTOMERS AND END-USERS OF ITS PRODUCTS ARE PROVIDED WITH A COPY OF THE
179 * NOTICE SPECIFIED IN THIS SECTION.
180 *
181 * __END_OF_JASPER_LICENSE__
182 */