Path: blob/master/3rdparty/openexr/Imath/ImathFun.cpp
16337 views
///////////////////////////////////////////////////////////////////////////1//2// Copyright (c) 2002, Industrial Light & Magic, a division of Lucas3// Digital Ltd. LLC4//5// All rights reserved.6//7// Redistribution and use in source and binary forms, with or without8// modification, are permitted provided that the following conditions are9// met:10// * Redistributions of source code must retain the above copyright11// notice, this list of conditions and the following disclaimer.12// * Redistributions in binary form must reproduce the above13// copyright notice, this list of conditions and the following disclaimer14// in the documentation and/or other materials provided with the15// distribution.16// * Neither the name of Industrial Light & Magic nor the names of17// its contributors may be used to endorse or promote products derived18// from this software without specific prior written permission.19//20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.31//32///////////////////////////////////////////////////////////////////////////333435#include "ImathFun.h"3637namespace Imath {383940float41succf (float f)42{43union {float f; int i;} u;44u.f = f;4546if ((u.i & 0x7f800000) == 0x7f800000)47{48// Nan or infinity; don't change value.49}50else if (u.i == 0x00000000 || u.i == 0x80000000)51{52// Plus or minus zero.5354u.i = 0x00000001;55}56else if (u.i > 0)57{58// Positive float, normalized or denormalized.59// Incrementing the largest positive float60// produces +infinity.6162++u.i;63}64else65{66// Negative normalized or denormalized float.6768--u.i;69}7071return u.f;72}737475float76predf (float f)77{78union {float f; int i;} u;79u.f = f;8081if ((u.i & 0x7f800000) == 0x7f800000)82{83// Nan or infinity; don't change value.84}85else if (u.i == 0x00000000 || u.i == 0x80000000)86{87// Plus or minus zero.8889u.i = 0x80000001;90}91else if (u.i > 0)92{93// Positive float, normalized or denormalized.9495--u.i;96}97else98{99// Negative normalized or denormalized float.100// Decrementing the largest negative float101// produces -infinity.102103++u.i;104}105106return u.f;107}108109110double111succd (double d)112{113union {double d; Int64 i;} u;114u.d = d;115116if ((u.i & 0x7ff0000000000000LL) == 0x7ff0000000000000LL)117{118// Nan or infinity; don't change value.119}120else if (u.i == 0x0000000000000000LL || u.i == 0x8000000000000000LL)121{122// Plus or minus zero.123124u.i = 0x0000000000000001LL;125}126else if (u.i > 0)127{128// Positive double, normalized or denormalized.129// Incrementing the largest positive double130// produces +infinity.131132++u.i;133}134else135{136// Negative normalized or denormalized double.137138--u.i;139}140141return u.d;142}143144145double146predd (double d)147{148union {double d; Int64 i;} u;149u.d = d;150151if ((u.i & 0x7ff0000000000000LL) == 0x7ff0000000000000LL)152{153// Nan or infinity; don't change value.154}155else if (u.i == 0x0000000000000000LL || u.i == 0x8000000000000000LL)156{157// Plus or minus zero.158159u.i = 0x8000000000000001LL;160}161else if (u.i > 0)162{163// Positive double, normalized or denormalized.164165--u.i;166}167else168{169// Negative normalized or denormalized double.170// Decrementing the largest negative double171// produces -infinity.172173++u.i;174}175176return u.d;177}178179180} // namespace Imath181182183