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

Packit 78deda
#include <assert.h>
Packit 78deda
#include <stdio.h>
Packit 78deda
#include <string.h>
Packit 78deda
#include <math.h>
Packit 78deda
#include <stdlib.h>
Packit 78deda
#include <stdarg.h>
Packit 78deda
Packit 78deda
#include "jpc_math.h"
Packit 78deda
Packit 78deda
Packit 78deda
Packit 78deda
/* Calculate the integer quantity floor(log2(x)), where x is a positive
Packit 78deda
  integer. */
Packit 78deda
int
Packit 78deda
jpc_floorlog2(int const arg) {
Packit 78deda
Packit 78deda
	int y;
Packit 78deda
    int x;
Packit 78deda
Packit 78deda
	assert(arg > 0);
Packit 78deda
Packit 78deda
	y = 0;
Packit 78deda
    x = arg;
Packit 78deda
	while (x > 1) {
Packit 78deda
		x >>= 1;
Packit 78deda
		++y;
Packit 78deda
	}
Packit 78deda
	return y;
Packit 78deda
}
Packit 78deda
Packit 78deda
Packit 78deda
Packit 78deda
/*
Packit 78deda
  jpc_floorlog2() and jpc_firstone() do the same thing.
Packit 78deda
  The only difference is how input 0 is handled.
Packit 78deda
Packit 78deda
n                  : 0 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 
Packit 78deda
ceil(log2(n))      : x 0  1  2  2  3  3  3  3  4  4  4  4  4  4  4  4  5  5  5 
Packit 78deda
floor(log2(n))     : x 0  1  1  2  2  2  2  3  3  3  3  3  3  3  3  4  4  4  4 
Packit 78deda
31-__builtin_clz(n): x 0  1  1  2  2  2  2  3  3  3  3  3  3  3  3  4  4  4  4 
Packit 78deda
jpc_floorlog2(n)   : x 0  1  1  2  2  2  2  3  3  3  3  3  3  3  3  4  4  4  4 
Packit 78deda
jpc_firstone(n)    :-1 0  1  1  2  2  2  2  3  3  3  3  3  3  3  3  4  4  4  4 
Packit 78deda
Packit 78deda
*/
Packit 78deda
Packit 78deda
Packit 78deda
Packit 78deda
int
Packit 78deda
jpc_firstone(int const arg) {
Packit 78deda
/*---------------------------------------------------------------------------- 
Packit 78deda
  Calculate the bit position of the first leading one in a nonnegative
Packit 78deda
  integer.
Packit 78deda
-----------------------------------------------------------------------------*/
Packit 78deda
	int n;
Packit 78deda
    int x;
Packit 78deda
Packit 78deda
	assert(arg >= 0);
Packit 78deda
Packit 78deda
	n = -1;
Packit 78deda
    x = arg;
Packit 78deda
	while (x > 0) {
Packit 78deda
		x >>= 1;
Packit 78deda
		++n;
Packit 78deda
	}
Packit 78deda
	return n;
Packit 78deda
}
Packit 78deda
Packit 78deda
Packit 78deda
Packit 78deda
/*
Packit 78deda
 * Copyright (c) 1999-2000 Image Power, Inc. and the University of
Packit 78deda
 *   British Columbia.
Packit 78deda
 * Copyright (c) 2001-2002 Michael David Adams.
Packit 78deda
 * All rights reserved.
Packit 78deda
 */
Packit 78deda
Packit 78deda
/* __START_OF_JASPER_LICENSE__
Packit 78deda
 * 
Packit 78deda
 * JasPer Software License
Packit 78deda
 * 
Packit 78deda
 * IMAGE POWER JPEG-2000 PUBLIC LICENSE
Packit 78deda
 * ************************************
Packit 78deda
 * 
Packit 78deda
 * GRANT:
Packit 78deda
 * 
Packit 78deda
 * Permission is hereby granted, free of charge, to any person (the "User")
Packit 78deda
 * obtaining a copy of this software and associated documentation, to deal
Packit 78deda
 * in the JasPer Software without restriction, including without limitation
Packit 78deda
 * the right to use, copy, modify, merge, publish, distribute, sublicense,
Packit 78deda
 * and/or sell copies of the JasPer Software (in source and binary forms),
Packit 78deda
 * and to permit persons to whom the JasPer Software is furnished to do so,
Packit 78deda
 * provided further that the License Conditions below are met.
Packit 78deda
 * 
Packit 78deda
 * License Conditions
Packit 78deda
 * ******************
Packit 78deda
 * 
Packit 78deda
 * A.  Redistributions of source code must retain the above copyright notice,
Packit 78deda
 * and this list of conditions, and the following disclaimer.
Packit 78deda
 * 
Packit 78deda
 * B.  Redistributions in binary form must reproduce the above copyright
Packit 78deda
 * notice, and this list of conditions, and the following disclaimer in
Packit 78deda
 * the documentation and/or other materials provided with the distribution.
Packit 78deda
 * 
Packit 78deda
 * C.  Neither the name of Image Power, Inc. nor any other contributor
Packit 78deda
 * (including, but not limited to, the University of British Columbia and
Packit 78deda
 * Michael David Adams) may be used to endorse or promote products derived
Packit 78deda
 * from this software without specific prior written permission.
Packit 78deda
 * 
Packit 78deda
 * D.  User agrees that it shall not commence any action against Image Power,
Packit 78deda
 * Inc., the University of British Columbia, Michael David Adams, or any
Packit 78deda
 * other contributors (collectively "Licensors") for infringement of any
Packit 78deda
 * intellectual property rights ("IPR") held by the User in respect of any
Packit 78deda
 * technology that User owns or has a right to license or sublicense and
Packit 78deda
 * which is an element required in order to claim compliance with ISO/IEC
Packit 78deda
 * 15444-1 (i.e., JPEG-2000 Part 1).  "IPR" means all intellectual property
Packit 78deda
 * rights worldwide arising under statutory or common law, and whether
Packit 78deda
 * or not perfected, including, without limitation, all (i) patents and
Packit 78deda
 * patent applications owned or licensable by User; (ii) rights associated
Packit 78deda
 * with works of authorship including copyrights, copyright applications,
Packit 78deda
 * copyright registrations, mask work rights, mask work applications,
Packit 78deda
 * mask work registrations; (iii) rights relating to the protection of
Packit 78deda
 * trade secrets and confidential information; (iv) any right analogous
Packit 78deda
 * to those set forth in subsections (i), (ii), or (iii) and any other
Packit 78deda
 * proprietary rights relating to intangible property (other than trademark,
Packit 78deda
 * trade dress, or service mark rights); and (v) divisions, continuations,
Packit 78deda
 * renewals, reissues and extensions of the foregoing (as and to the extent
Packit 78deda
 * applicable) now existing, hereafter filed, issued or acquired.
Packit 78deda
 * 
Packit 78deda
 * E.  If User commences an infringement action against any Licensor(s) then
Packit 78deda
 * such Licensor(s) shall have the right to terminate User's license and
Packit 78deda
 * all sublicenses that have been granted hereunder by User to other parties.
Packit 78deda
 * 
Packit 78deda
 * F.  This software is for use only in hardware or software products that
Packit 78deda
 * are compliant with ISO/IEC 15444-1 (i.e., JPEG-2000 Part 1).  No license
Packit 78deda
 * or right to this Software is granted for products that do not comply
Packit 78deda
 * with ISO/IEC 15444-1.  The JPEG-2000 Part 1 standard can be purchased
Packit 78deda
 * from the ISO.
Packit 78deda
 * 
Packit 78deda
 * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE.
Packit 78deda
 * NO USE OF THE JASPER SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
Packit 78deda
 * THIS DISCLAIMER.  THE JASPER SOFTWARE IS PROVIDED BY THE LICENSORS AND
Packit 78deda
 * CONTRIBUTORS UNDER THIS LICENSE ON AN ``AS-IS'' BASIS, WITHOUT WARRANTY
Packit 78deda
 * OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION,
Packit 78deda
 * WARRANTIES THAT THE JASPER SOFTWARE IS FREE OF DEFECTS, IS MERCHANTABLE,
Packit 78deda
 * IS FIT FOR A PARTICULAR PURPOSE OR IS NON-INFRINGING.  THOSE INTENDING
Packit 78deda
 * TO USE THE JASPER SOFTWARE OR MODIFICATIONS THEREOF FOR USE IN HARDWARE
Packit 78deda
 * OR SOFTWARE PRODUCTS ARE ADVISED THAT THEIR USE MAY INFRINGE EXISTING
Packit 78deda
 * PATENTS, COPYRIGHTS, TRADEMARKS, OR OTHER INTELLECTUAL PROPERTY RIGHTS.
Packit 78deda
 * THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE JASPER SOFTWARE
Packit 78deda
 * IS WITH THE USER.  SHOULD ANY PART OF THE JASPER SOFTWARE PROVE DEFECTIVE
Packit 78deda
 * IN ANY RESPECT, THE USER (AND NOT THE INITIAL DEVELOPERS, THE UNIVERSITY
Packit 78deda
 * OF BRITISH COLUMBIA, IMAGE POWER, INC., MICHAEL DAVID ADAMS, OR ANY
Packit 78deda
 * OTHER CONTRIBUTOR) SHALL ASSUME THE COST OF ANY NECESSARY SERVICING,
Packit 78deda
 * REPAIR OR CORRECTION.  UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY,
Packit 78deda
 * WHETHER TORT (INCLUDING NEGLIGENCE), CONTRACT, OR OTHERWISE, SHALL THE
Packit 78deda
 * INITIAL DEVELOPER, THE UNIVERSITY OF BRITISH COLUMBIA, IMAGE POWER, INC.,
Packit 78deda
 * MICHAEL DAVID ADAMS, ANY OTHER CONTRIBUTOR, OR ANY DISTRIBUTOR OF THE
Packit 78deda
 * JASPER SOFTWARE, OR ANY SUPPLIER OF ANY OF SUCH PARTIES, BE LIABLE TO
Packit 78deda
 * THE USER OR ANY OTHER PERSON FOR ANY INDIRECT, SPECIAL, INCIDENTAL, OR
Packit 78deda
 * CONSEQUENTIAL DAMAGES OF ANY CHARACTER INCLUDING, WITHOUT LIMITATION,
Packit 78deda
 * DAMAGES FOR LOSS OF GOODWILL, WORK STOPPAGE, COMPUTER FAILURE OR
Packit 78deda
 * MALFUNCTION, OR ANY AND ALL OTHER COMMERCIAL DAMAGES OR LOSSES, EVEN IF
Packit 78deda
 * SUCH PARTY HAD BEEN INFORMED, OR OUGHT TO HAVE KNOWN, OF THE POSSIBILITY
Packit 78deda
 * OF SUCH DAMAGES.  THE JASPER SOFTWARE AND UNDERLYING TECHNOLOGY ARE NOT
Packit 78deda
 * FAULT-TOLERANT AND ARE NOT DESIGNED, MANUFACTURED OR INTENDED FOR USE OR
Packit 78deda
 * RESALE AS ON-LINE CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING
Packit 78deda
 * FAIL-SAFE PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES,
Packit 78deda
 * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT
Packit 78deda
 * LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
Packit 78deda
 * JASPER SOFTWARE OR UNDERLYING TECHNOLOGY OR PRODUCT COULD LEAD DIRECTLY
Packit 78deda
 * TO DEATH, PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE
Packit 78deda
 * ("HIGH RISK ACTIVITIES").  LICENSOR SPECIFICALLY DISCLAIMS ANY EXPRESS
Packit 78deda
 * OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.  USER WILL NOT
Packit 78deda
 * KNOWINGLY USE, DISTRIBUTE OR RESELL THE JASPER SOFTWARE OR UNDERLYING
Packit 78deda
 * TECHNOLOGY OR PRODUCTS FOR HIGH RISK ACTIVITIES AND WILL ENSURE THAT ITS
Packit 78deda
 * CUSTOMERS AND END-USERS OF ITS PRODUCTS ARE PROVIDED WITH A COPY OF THE
Packit 78deda
 * NOTICE SPECIFIED IN THIS SECTION.
Packit 78deda
 * 
Packit 78deda
 * __END_OF_JASPER_LICENSE__
Packit 78deda
 */