src/corelib/kernel/qmath.h
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtCore module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #ifndef QMATH_H
       
    43 #define QMATH_H
       
    44 
       
    45 #include <math.h>
       
    46 
       
    47 #include <QtCore/qglobal.h>
       
    48 
       
    49 QT_BEGIN_HEADER
       
    50 
       
    51 QT_BEGIN_NAMESPACE
       
    52 
       
    53 QT_MODULE(Core)
       
    54 
       
    55 #define QT_SINE_TABLE_SIZE 256
       
    56 
       
    57 extern Q_CORE_EXPORT const qreal qt_sine_table[QT_SINE_TABLE_SIZE];
       
    58 
       
    59 inline int qCeil(qreal v)
       
    60 {
       
    61 #ifdef QT_USE_MATH_H_FLOATS
       
    62     if (sizeof(qreal) == sizeof(float))
       
    63         return int(ceilf(float(v)));
       
    64     else
       
    65 #endif
       
    66         return int(ceil(v));
       
    67 }
       
    68 
       
    69 inline int qFloor(qreal v)
       
    70 {
       
    71 #ifdef QT_USE_MATH_H_FLOATS
       
    72     if (sizeof(qreal) == sizeof(float))
       
    73         return int(floorf(float(v)));
       
    74     else
       
    75 #endif
       
    76         return int(floor(v));
       
    77 }
       
    78 
       
    79 inline qreal qSin(qreal v)
       
    80 {
       
    81 #ifdef QT_USE_MATH_H_FLOATS
       
    82     if (sizeof(qreal) == sizeof(float))
       
    83         return sinf(float(v));
       
    84     else
       
    85 #endif
       
    86         return sin(v);
       
    87 }
       
    88 
       
    89 inline qreal qCos(qreal v)
       
    90 {
       
    91 #ifdef QT_USE_MATH_H_FLOATS
       
    92     if (sizeof(qreal) == sizeof(float))
       
    93         return cosf(float(v));
       
    94     else
       
    95 #endif
       
    96         return cos(v);
       
    97 }
       
    98 
       
    99 inline qreal qAcos(qreal v)
       
   100 {
       
   101 #ifdef QT_USE_MATH_H_FLOATS
       
   102     if (sizeof(qreal) == sizeof(float))
       
   103         return acosf(float(v));
       
   104     else
       
   105 #endif
       
   106         return acos(v);
       
   107 }
       
   108 
       
   109 inline qreal qSqrt(qreal v)
       
   110 {
       
   111 #ifdef QT_USE_MATH_H_FLOATS
       
   112     if (sizeof(qreal) == sizeof(float))
       
   113         return sqrtf(float(v));
       
   114     else
       
   115 #endif
       
   116         return sqrt(v);
       
   117 }
       
   118 
       
   119 inline qreal qLn(qreal v)
       
   120 {
       
   121 #ifdef QT_USE_MATH_H_FLOATS
       
   122     if (sizeof(qreal) == sizeof(float))
       
   123         return logf(float(v));
       
   124     else
       
   125 #endif
       
   126         return log(v);
       
   127 }
       
   128 
       
   129 inline qreal qPow(qreal x, qreal y)
       
   130 {
       
   131 #ifdef QT_USE_MATH_H_FLOATS
       
   132     if (sizeof(qreal) == sizeof(float))
       
   133         return powf(float(x), float(y));
       
   134     else
       
   135 #endif
       
   136         return pow(x, y);
       
   137 }
       
   138 
       
   139 #ifndef M_PI
       
   140 #define M_PI (3.14159265358979323846)
       
   141 #endif
       
   142 
       
   143 inline qreal qFastSin(qreal x)
       
   144 {
       
   145     int si = int(x * (0.5 * QT_SINE_TABLE_SIZE / M_PI)); // Would be more accurate with qRound, but slower.
       
   146     qreal d = x - si * (2.0 * M_PI / QT_SINE_TABLE_SIZE);
       
   147     int ci = si + QT_SINE_TABLE_SIZE / 4;
       
   148     si &= QT_SINE_TABLE_SIZE - 1;
       
   149     ci &= QT_SINE_TABLE_SIZE - 1;
       
   150     return qt_sine_table[si] + (qt_sine_table[ci] - 0.5 * qt_sine_table[si] * d) * d;
       
   151 }
       
   152 
       
   153 inline qreal qFastCos(qreal x)
       
   154 {
       
   155     int ci = int(x * (0.5 * QT_SINE_TABLE_SIZE / M_PI)); // Would be more accurate with qRound, but slower.
       
   156     qreal d = x - ci * (2.0 * M_PI / QT_SINE_TABLE_SIZE);
       
   157     int si = ci + QT_SINE_TABLE_SIZE / 4;
       
   158     si &= QT_SINE_TABLE_SIZE - 1;
       
   159     ci &= QT_SINE_TABLE_SIZE - 1;
       
   160     return qt_sine_table[si] - (qt_sine_table[ci] + 0.5 * qt_sine_table[si] * d) * d;
       
   161 }
       
   162 
       
   163 QT_END_NAMESPACE
       
   164 
       
   165 QT_END_HEADER
       
   166 
       
   167 #endif // QMATH_H