Changeset 873


Ignore:
Timestamp:
Aug 29, 2011, 2:07:56 AM (8 years ago)
Author:
sam
Message:

core: implement array versions of the float / half conversion routines.

Location:
trunk/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/half.cpp

    r872 r873  
    115115 * by Jeroen van der Zijp, November 2008. Tables are generated using
    116116 * the C++ preprocessor, thanks to a branchless implementation also
    117  * used in half_to_float_branch(). This code is actually almost always
    118  * slower than the branching one. */
     117 * used in half_to_float_branch(). This code is very fast when performing
     118 * conversions on arrays of values. */
    119119static inline uint32_t half_to_float_nobranch(uint16_t x)
    120120{
     
    158158
    159159/* This algorithm is similar to the OpenEXR implementation, except it
    160  * uses branchless code in the denormal path. */
     160 * uses branchless code in the denormal path. This is slower than the
     161 * table version, but will be more friendly to the cache for occasional
     162 * uses. */
    161163static inline uint32_t half_to_float_branch(uint16_t x)
    162164{
     
    214216}
    215217
     218size_t half::copy(half *dst, float const *src, size_t nelem)
     219{
     220    for (size_t i = 0; i < nelem; i++)
     221    {
     222        union { float f; uint32_t x; } u;
     223        u.f = *src++;
     224        *dst++ = makebits(float_to_half_nobranch(u.x));
     225    }
     226
     227    return nelem;
     228}
     229
     230size_t half::copy(float *dst, half const *src, size_t nelem)
     231{
     232    for (size_t i = 0; i < nelem; i++)
     233    {
     234        union { float f; uint32_t x; } u;
     235        u.x = half_to_float_nobranch((*src++).bits);
     236        *dst++ = u.f;
     237    }
     238
     239    return nelem;
     240}
     241
    216242} /* namespace lol */
    217243
  • trunk/src/half.h

    r872 r873  
    1717#define __LOL_HALF_H__
    1818
     19#include <cstdio>
    1920#include <stdint.h>
    2021
     
    5556    operator float() const;
    5657    inline operator int() const { return (int)(float)*this; }
     58
     59    /* Array conversions */
     60    static size_t copy(half *dst, float const *src, size_t nelem);
     61    static size_t copy(float *dst, half const *src, size_t nelem);
    5762
    5863    /* Operations */
Note: See TracChangeset for help on using the changeset viewer.