Path: blob/master/3rdparty/libjasper/jasper/jas_fix.h
16337 views
/*1* Copyright (c) 1999-2000 Image Power, Inc. and the University of2* British Columbia.3* Copyright (c) 2001-2002 Michael David Adams.4* All rights reserved.5*/67/* __START_OF_JASPER_LICENSE__8*9* JasPer License Version 2.010*11* Copyright (c) 2001-2006 Michael David Adams12* Copyright (c) 1999-2000 Image Power, Inc.13* Copyright (c) 1999-2000 The University of British Columbia14*15* All rights reserved.16*17* Permission is hereby granted, free of charge, to any person (the18* "User") obtaining a copy of this software and associated documentation19* files (the "Software"), to deal in the Software without restriction,20* including without limitation the rights to use, copy, modify, merge,21* publish, distribute, and/or sell copies of the Software, and to permit22* persons to whom the Software is furnished to do so, subject to the23* following conditions:24*25* 1. The above copyright notices and this permission notice (which26* includes the disclaimer below) shall be included in all copies or27* substantial portions of the Software.28*29* 2. The name of a copyright holder shall not be used to endorse or30* promote products derived from the Software without specific prior31* written permission.32*33* THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS34* LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER35* THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS36* "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING37* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A38* PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO39* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL40* INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING41* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,42* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION43* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE44* PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE45* THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.46* EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS47* BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL48* PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS49* GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE50* ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE51* IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL52* SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,53* AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL54* SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH55* THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,56* PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH57* RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY58* EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.59*60* __END_OF_JASPER_LICENSE__61*/6263/*64* Fixed-Point Number Class65*66* $Id: jas_fix.h,v 1.2 2008-05-26 09:41:51 vp153 Exp $67*/6869#ifndef JAS_FIX_H70#define JAS_FIX_H7172/******************************************************************************\73* Includes.74\******************************************************************************/7576#include <stdio.h>77#include <stdlib.h>78#include <math.h>7980#include <jasper/jas_config.h>81#include <jasper/jas_types.h>8283#ifdef __cplusplus84extern "C" {85#endif8687/******************************************************************************\88* Constants.89\******************************************************************************/9091/* The representation of the value zero. */92#define JAS_FIX_ZERO(fix_t, fracbits) \93JAS_CAST(fix_t, 0)9495/* The representation of the value one. */96#define JAS_FIX_ONE(fix_t, fracbits) \97(JAS_CAST(fix_t, 1) << (fracbits))9899/* The representation of the value one half. */100#define JAS_FIX_HALF(fix_t, fracbits) \101(JAS_CAST(fix_t, 1) << ((fracbits) - 1))102103/******************************************************************************\104* Conversion operations.105\******************************************************************************/106107/* Convert an int to a fixed-point number. */108#define JAS_INTTOFIX(fix_t, fracbits, x) \109JAS_CAST(fix_t, (x) << (fracbits))110111/* Convert a fixed-point number to an int. */112#define JAS_FIXTOINT(fix_t, fracbits, x) \113JAS_CAST(int, (x) >> (fracbits))114115/* Convert a fixed-point number to a double. */116#define JAS_FIXTODBL(fix_t, fracbits, x) \117(JAS_CAST(double, x) / (JAS_CAST(fix_t, 1) << (fracbits)))118119/* Convert a double to a fixed-point number. */120#define JAS_DBLTOFIX(fix_t, fracbits, x) \121JAS_CAST(fix_t, ((x) * JAS_CAST(double, JAS_CAST(fix_t, 1) << (fracbits))))122123/******************************************************************************\124* Basic arithmetic operations.125* All other arithmetic operations are synthesized from these basic operations.126* There are three macros for each type of arithmetic operation.127* One macro always performs overflow/underflow checking, one never performs128* overflow/underflow checking, and one is generic with its behavior129* depending on compile-time flags.130* Only the generic macros should be invoked directly by application code.131\******************************************************************************/132133/* Calculate the sum of two fixed-point numbers. */134#if !defined(DEBUG_OVERFLOW)135#define JAS_FIX_ADD JAS_FIX_ADD_FAST136#else137#define JAS_FIX_ADD JAS_FIX_ADD_OFLOW138#endif139140/* Calculate the sum of two fixed-point numbers without overflow checking. */141#define JAS_FIX_ADD_FAST(fix_t, fracbits, x, y) ((x) + (y))142143/* Calculate the sum of two fixed-point numbers with overflow checking. */144#define JAS_FIX_ADD_OFLOW(fix_t, fracbits, x, y) \145((x) >= 0) ? \146(((y) >= 0) ? ((x) + (y) >= 0 || JAS_FIX_OFLOW(), (x) + (y)) : \147((x) + (y))) : \148(((y) >= 0) ? ((x) + (y)) : ((x) + (y) < 0 || JAS_FIX_OFLOW(), \149(x) + (y)))150151/* Calculate the product of two fixed-point numbers. */152#if !defined(DEBUG_OVERFLOW)153#define JAS_FIX_MUL JAS_FIX_MUL_FAST154#else155#define JAS_FIX_MUL JAS_FIX_MUL_OFLOW156#endif157158/* Calculate the product of two fixed-point numbers without overflow159checking. */160#define JAS_FIX_MUL_FAST(fix_t, fracbits, bigfix_t, x, y) \161JAS_CAST(fix_t, (JAS_CAST(bigfix_t, x) * JAS_CAST(bigfix_t, y)) >> \162(fracbits))163164/* Calculate the product of two fixed-point numbers with overflow165checking. */166#define JAS_FIX_MUL_OFLOW(fix_t, fracbits, bigfix_t, x, y) \167((JAS_CAST(bigfix_t, x) * JAS_CAST(bigfix_t, y) >> (fracbits)) == \168JAS_CAST(fix_t, (JAS_CAST(bigfix_t, x) * JAS_CAST(bigfix_t, y) >> \169(fracbits))) ? \170JAS_CAST(fix_t, (JAS_CAST(bigfix_t, x) * JAS_CAST(bigfix_t, y) >> \171(fracbits))) : JAS_FIX_OFLOW())172173/* Calculate the product of a fixed-point number and an int. */174#if !defined(DEBUG_OVERFLOW)175#define JAS_FIX_MULBYINT JAS_FIX_MULBYINT_FAST176#else177#define JAS_FIX_MULBYINT JAS_FIX_MULBYINT_OFLOW178#endif179180/* Calculate the product of a fixed-point number and an int without overflow181checking. */182#define JAS_FIX_MULBYINT_FAST(fix_t, fracbits, x, y) \183JAS_CAST(fix_t, ((x) * (y)))184185/* Calculate the product of a fixed-point number and an int with overflow186checking. */187#define JAS_FIX_MULBYINT_OFLOW(fix_t, fracbits, x, y) \188JAS_FIX_MULBYINT_FAST(fix_t, fracbits, x, y)189190/* Calculate the quotient of two fixed-point numbers. */191#if !defined(DEBUG_OVERFLOW)192#define JAS_FIX_DIV JAS_FIX_DIV_FAST193#else194#define JAS_FIX_DIV JAS_FIX_DIV_UFLOW195#endif196197/* Calculate the quotient of two fixed-point numbers without underflow198checking. */199#define JAS_FIX_DIV_FAST(fix_t, fracbits, bigfix_t, x, y) \200JAS_CAST(fix_t, (JAS_CAST(bigfix_t, x) << (fracbits)) / (y))201202/* Calculate the quotient of two fixed-point numbers with underflow203checking. */204#define JAS_FIX_DIV_UFLOW(fix_t, fracbits, bigfix_t, x, y) \205JAS_FIX_DIV_FAST(fix_t, fracbits, bigfix_t, x, y)206207/* Negate a fixed-point number. */208#if !defined(DEBUG_OVERFLOW)209#define JAS_FIX_NEG JAS_FIX_NEG_FAST210#else211#define JAS_FIX_NEG JAS_FIX_NEG_OFLOW212#endif213214/* Negate a fixed-point number without overflow checking. */215#define JAS_FIX_NEG_FAST(fix_t, fracbits, x) \216(-(x))217218/* Negate a fixed-point number with overflow checking. */219/* Yes, overflow is actually possible for two's complement representations,220although highly unlikely to occur. */221#define JAS_FIX_NEG_OFLOW(fix_t, fracbits, x) \222(((x) < 0) ? (-(x) > 0 || JAS_FIX_OFLOW(), -(x)) : (-(x)))223224/* Perform an arithmetic shift left of a fixed-point number. */225#if !defined(DEBUG_OVERFLOW)226#define JAS_FIX_ASL JAS_FIX_ASL_FAST227#else228#define JAS_FIX_ASL JAS_FIX_ASL_OFLOW229#endif230231/* Perform an arithmetic shift left of a fixed-point number without overflow232checking. */233#define JAS_FIX_ASL_FAST(fix_t, fracbits, x, n) \234((x) << (n))235236/* Perform an arithmetic shift left of a fixed-point number with overflow237checking. */238#define JAS_FIX_ASL_OFLOW(fix_t, fracbits, x, n) \239((((x) << (n)) >> (n)) == (x) || JAS_FIX_OFLOW(), (x) << (n))240241/* Perform an arithmetic shift right of a fixed-point number. */242#if !defined(DEBUG_OVERFLOW)243#define JAS_FIX_ASR JAS_FIX_ASR_FAST244#else245#define JAS_FIX_ASR JAS_FIX_ASR_UFLOW246#endif247248/* Perform an arithmetic shift right of a fixed-point number without underflow249checking. */250#define JAS_FIX_ASR_FAST(fix_t, fracbits, x, n) \251((x) >> (n))252253/* Perform an arithmetic shift right of a fixed-point number with underflow254checking. */255#define JAS_FIX_ASR_UFLOW(fix_t, fracbits, x, n) \256JAS_FIX_ASR_FAST(fix_t, fracbits, x, n)257258/******************************************************************************\259* Other basic arithmetic operations.260\******************************************************************************/261262/* Calculate the difference between two fixed-point numbers. */263#define JAS_FIX_SUB(fix_t, fracbits, x, y) \264JAS_FIX_ADD(fix_t, fracbits, x, JAS_FIX_NEG(fix_t, fracbits, y))265266/* Add one fixed-point number to another. */267#define JAS_FIX_PLUSEQ(fix_t, fracbits, x, y) \268((x) = JAS_FIX_ADD(fix_t, fracbits, x, y))269270/* Subtract one fixed-point number from another. */271#define JAS_FIX_MINUSEQ(fix_t, fracbits, x, y) \272((x) = JAS_FIX_SUB(fix_t, fracbits, x, y))273274/* Multiply one fixed-point number by another. */275#define JAS_FIX_MULEQ(fix_t, fracbits, bigfix_t, x, y) \276((x) = JAS_FIX_MUL(fix_t, fracbits, bigfix_t, x, y))277278/******************************************************************************\279* Miscellaneous operations.280\******************************************************************************/281282/* Calculate the absolute value of a fixed-point number. */283#define JAS_FIX_ABS(fix_t, fracbits, x) \284(((x) >= 0) ? (x) : (JAS_FIX_NEG(fix_t, fracbits, x)))285286/* Is a fixed-point number an integer? */287#define JAS_FIX_ISINT(fix_t, fracbits, x) \288(JAS_FIX_FLOOR(fix_t, fracbits, x) == (x))289290/* Get the sign of a fixed-point number. */291#define JAS_FIX_SGN(fix_t, fracbits, x) \292((x) >= 0 ? 1 : (-1))293294/******************************************************************************\295* Relational operations.296\******************************************************************************/297298/* Compare two fixed-point numbers. */299#define JAS_FIX_CMP(fix_t, fracbits, x, y) \300((x) > (y) ? 1 : (((x) == (y)) ? 0 : (-1)))301302/* Less than. */303#define JAS_FIX_LT(fix_t, fracbits, x, y) \304((x) < (y))305306/* Less than or equal. */307#define JAS_FIX_LTE(fix_t, fracbits, x, y) \308((x) <= (y))309310/* Greater than. */311#define JAS_FIX_GT(fix_t, fracbits, x, y) \312((x) > (y))313314/* Greater than or equal. */315#define JAS_FIX_GTE(fix_t, fracbits, x, y) \316((x) >= (y))317318/******************************************************************************\319* Rounding functions.320\******************************************************************************/321322/* Round a fixed-point number to the nearest integer. */323#define JAS_FIX_ROUND(fix_t, fracbits, x) \324(((x) < 0) ? JAS_FIX_FLOOR(fix_t, fracbits, JAS_FIX_ADD(fix_t, fracbits, \325(x), JAS_FIX_HALF(fix_t, fracbits))) : \326JAS_FIX_NEG(fix_t, fracbits, JAS_FIX_FLOOR(fix_t, fracbits, \327JAS_FIX_ADD(fix_t, fracbits, (-(x)), JAS_FIX_HALF(fix_t, fracbits)))))328329/* Round a fixed-point number to the nearest integer in the direction of330negative infinity (i.e., the floor function). */331#define JAS_FIX_FLOOR(fix_t, fracbits, x) \332((x) & (~((JAS_CAST(fix_t, 1) << (fracbits)) - 1)))333334/* Round a fixed-point number to the nearest integer in the direction335of zero. */336#define JAS_FIX_TRUNC(fix_t, fracbits, x) \337(((x) >= 0) ? JAS_FIX_FLOOR(fix_t, fracbits, x) : \338JAS_FIX_CEIL(fix_t, fracbits, x))339340/******************************************************************************\341* The below macros are for internal library use only. Do not invoke them342* directly in application code.343\******************************************************************************/344345/* Handle overflow. */346#define JAS_FIX_OFLOW() \347jas_eprintf("overflow error: file %s, line %d\n", __FILE__, __LINE__)348349/* Handle underflow. */350#define JAS_FIX_UFLOW() \351jas_eprintf("underflow error: file %s, line %d\n", __FILE__, __LINE__)352353#ifdef __cplusplus354}355#endif356357#endif358359360