Blame IlmImf/ImfCheckedArithmetic.h

Packit Service 6754ca
///////////////////////////////////////////////////////////////////////////
Packit Service 6754ca
//
Packit Service 6754ca
// Copyright (c) 2009, Industrial Light & Magic, a division of Lucas
Packit Service 6754ca
// Digital Ltd. LLC
Packit Service 6754ca
// 
Packit Service 6754ca
// All rights reserved.
Packit Service 6754ca
// 
Packit Service 6754ca
// Redistribution and use in source and binary forms, with or without
Packit Service 6754ca
// modification, are permitted provided that the following conditions are
Packit Service 6754ca
// met:
Packit Service 6754ca
// *       Redistributions of source code must retain the above copyright
Packit Service 6754ca
// notice, this list of conditions and the following disclaimer.
Packit Service 6754ca
// *       Redistributions in binary form must reproduce the above
Packit Service 6754ca
// copyright notice, this list of conditions and the following disclaimer
Packit Service 6754ca
// in the documentation and/or other materials provided with the
Packit Service 6754ca
// distribution.
Packit Service 6754ca
// *       Neither the name of Industrial Light & Magic nor the names of
Packit Service 6754ca
// its contributors may be used to endorse or promote products derived
Packit Service 6754ca
// from this software without specific prior written permission. 
Packit Service 6754ca
// 
Packit Service 6754ca
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit Service 6754ca
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit Service 6754ca
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit Service 6754ca
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit Service 6754ca
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit Service 6754ca
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit Service 6754ca
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit Service 6754ca
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit Service 6754ca
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit Service 6754ca
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit Service 6754ca
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit Service 6754ca
//
Packit Service 6754ca
///////////////////////////////////////////////////////////////////////////
Packit Service 6754ca
Packit Service 6754ca
#ifndef INCLUDED_IMF_CHECKED_ARITHMETIC_H
Packit Service 6754ca
#define INCLUDED_IMF_CHECKED_ARITHMETIC_H
Packit Service 6754ca
Packit Service 6754ca
//-----------------------------------------------------------------------------
Packit Service 6754ca
//
Packit Service 6754ca
//	Integer arithmetic operations that throw exceptions
Packit Service 6754ca
//      on overflow, underflow or division by zero.
Packit Service 6754ca
//
Packit Service 6754ca
//-----------------------------------------------------------------------------
Packit Service 6754ca
Packit Service 6754ca
#include <limits>
Packit Service 6754ca
#include "IexMathExc.h"
Packit Service 6754ca
#include "ImfNamespace.h"
Packit Service 6754ca
Packit Service 6754ca
OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER
Packit Service 6754ca
Packit Service 6754ca
template <bool b> struct StaticAssertionFailed;
Packit Service 6754ca
template <> struct StaticAssertionFailed <true> {};
Packit Service 6754ca
Packit Service 6754ca
#define IMF_STATIC_ASSERT(x) \
Packit Service 6754ca
    do {StaticAssertionFailed <x> staticAssertionFailed; ((void) staticAssertionFailed);} while (false)
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
template <class T>
Packit Service 6754ca
T
Packit Service 6754ca
uiMult (T a, T b)
Packit Service 6754ca
{
Packit Service 6754ca
    //
Packit Service 6754ca
    // Unsigned integer multiplication
Packit Service 6754ca
    //
Packit Service 6754ca
Packit Service 6754ca
    IMF_STATIC_ASSERT (!std::numeric_limits<T>::is_signed &&
Packit Service 6754ca
                        std::numeric_limits<T>::is_integer);
Packit Service 6754ca
Packit Service 6754ca
    if (a > 0 && b > std::numeric_limits<T>::max() / a)
Packit Service 6754ca
        throw IEX_NAMESPACE::OverflowExc ("Integer multiplication overflow.");
Packit Service 6754ca
Packit Service 6754ca
    return a * b;
Packit Service 6754ca
}
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
template <class T>
Packit Service 6754ca
T
Packit Service 6754ca
uiDiv (T a, T b)
Packit Service 6754ca
{
Packit Service 6754ca
    //
Packit Service 6754ca
    // Unsigned integer division
Packit Service 6754ca
    //
Packit Service 6754ca
Packit Service 6754ca
    IMF_STATIC_ASSERT (!std::numeric_limits<T>::is_signed &&
Packit Service 6754ca
                        std::numeric_limits<T>::is_integer);
Packit Service 6754ca
Packit Service 6754ca
    if (b == 0)
Packit Service 6754ca
        throw IEX_NAMESPACE::DivzeroExc ("Integer division by zero.");
Packit Service 6754ca
Packit Service 6754ca
    return a / b;
Packit Service 6754ca
}
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
template <class T>
Packit Service 6754ca
T
Packit Service 6754ca
uiAdd (T a, T b)
Packit Service 6754ca
{
Packit Service 6754ca
    //
Packit Service 6754ca
    // Unsigned integer addition
Packit Service 6754ca
    //
Packit Service 6754ca
Packit Service 6754ca
    IMF_STATIC_ASSERT (!std::numeric_limits<T>::is_signed &&
Packit Service 6754ca
                        std::numeric_limits<T>::is_integer);
Packit Service 6754ca
Packit Service 6754ca
    if (a > std::numeric_limits<T>::max() - b)
Packit Service 6754ca
        throw IEX_NAMESPACE::OverflowExc ("Integer addition overflow.");
Packit Service 6754ca
Packit Service 6754ca
    return a + b;
Packit Service 6754ca
}
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
template <class T>
Packit Service 6754ca
T
Packit Service 6754ca
uiSub (T a, T b)
Packit Service 6754ca
{
Packit Service 6754ca
    //
Packit Service 6754ca
    // Unsigned integer subtraction
Packit Service 6754ca
    //
Packit Service 6754ca
Packit Service 6754ca
    IMF_STATIC_ASSERT (!std::numeric_limits<T>::is_signed &&
Packit Service 6754ca
                        std::numeric_limits<T>::is_integer);
Packit Service 6754ca
Packit Service 6754ca
    if (a < b)
Packit Service 6754ca
        throw IEX_NAMESPACE::UnderflowExc ("Integer subtraction underflow.");
Packit Service 6754ca
Packit Service 6754ca
    return a - b;
Packit Service 6754ca
}
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
template <class T>
Packit Service 6754ca
size_t
Packit Service 6754ca
checkArraySize (T n, size_t s)
Packit Service 6754ca
{
Packit Service 6754ca
    //
Packit Service 6754ca
    // Verify that the size, in bytes, of an array with n elements
Packit Service 6754ca
    // of size s can be computed without overflowing:
Packit Service 6754ca
    //
Packit Service 6754ca
    // If computing
Packit Service 6754ca
    //
Packit Service 6754ca
    //      size_t (n) * s
Packit Service 6754ca
    //
Packit Service 6754ca
    // would overflow, then throw an IEX_NAMESPACE::OverflowExc exception.
Packit Service 6754ca
    // Otherwise return
Packit Service 6754ca
    //
Packit Service 6754ca
    //      size_t (n).
Packit Service 6754ca
    //
Packit Service 6754ca
Packit Service 6754ca
    IMF_STATIC_ASSERT (!std::numeric_limits<T>::is_signed &&
Packit Service 6754ca
                        std::numeric_limits<T>::is_integer);
Packit Service 6754ca
Packit Service 6754ca
    IMF_STATIC_ASSERT (sizeof (T) <= sizeof (size_t));
Packit Service 6754ca
Packit Service 6754ca
    if (size_t (n) > std::numeric_limits<size_t>::max() / s)
Packit Service 6754ca
        throw IEX_NAMESPACE::OverflowExc ("Integer multiplication overflow.");
Packit Service 6754ca
Packit Service 6754ca
    return size_t (n);
Packit Service 6754ca
}
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
#endif