Home | History | Annotate | Download | only in util
      1 /**************************************************************************
      2  *
      3  * Copyright 2010 Luca Barbieri
      4  *
      5  * Permission is hereby granted, free of charge, to any person obtaining
      6  * a copy of this software and associated documentation files (the
      7  * "Software"), to deal in the Software without restriction, including
      8  * without limitation the rights to use, copy, modify, merge, publish,
      9  * distribute, sublicense, and/or sell copies of the Software, and to
     10  * permit persons to whom the Software is furnished to do so, subject to
     11  * the following conditions:
     12  *
     13  * The above copyright notice and this permission notice (including the
     14  * next paragraph) shall be included in all copies or substantial
     15  * portions of the Software.
     16  *
     17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
     20  * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
     21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
     22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
     23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     24  *
     25  **************************************************************************/
     26 
     27 
     28 #ifndef U_HALF_H
     29 #define U_HALF_H
     30 
     31 #include "pipe/p_compiler.h"
     32 #include "util/u_math.h"
     33 
     34 #ifdef __cplusplus
     35 extern "C" {
     36 #endif
     37 
     38 /*
     39  * References for float <-> half conversions
     40  *
     41  *  http://fgiesen.wordpress.com/2012/03/28/half-to-float-done-quic/
     42  *  https://gist.github.com/2156668
     43  *  https://gist.github.com/2144712
     44  */
     45 
     46 static inline uint16_t
     47 util_float_to_half(float f)
     48 {
     49    uint32_t sign_mask  = 0x80000000;
     50    uint32_t round_mask = ~0xfff;
     51    uint32_t f32inf = 0xff << 23;
     52    uint32_t f16inf = 0x1f << 23;
     53    uint32_t sign;
     54    union fi magic;
     55    union fi f32;
     56    uint16_t f16;
     57 
     58    magic.ui = 0xf << 23;
     59 
     60    f32.f = f;
     61 
     62    /* Sign */
     63    sign = f32.ui & sign_mask;
     64    f32.ui ^= sign;
     65 
     66    if (f32.ui == f32inf) {
     67       /* Inf */
     68       f16 = 0x7c00;
     69    } else if (f32.ui > f32inf) {
     70       /* NaN */
     71       f16 = 0x7e00;
     72    } else {
     73       /* Number */
     74       f32.ui &= round_mask;
     75       f32.f  *= magic.f;
     76       f32.ui -= round_mask;
     77       /*
     78        * XXX: The magic mul relies on denorms being available, otherwise
     79        * all f16 denorms get flushed to zero - hence when this is used
     80        * for tgsi_exec in softpipe we won't get f16 denorms.
     81        */
     82       /*
     83        * Clamp to max finite value if overflowed.
     84        * OpenGL has completely undefined rounding behavior for float to
     85        * half-float conversions, and this matches what is mandated for float
     86        * to fp11/fp10, which recommend round-to-nearest-finite too.
     87        * (d3d10 is deeply unhappy about flushing such values to infinity, and
     88        * while it also mandates round-to-zero it doesn't care nearly as much
     89        * about that.)
     90        */
     91       if (f32.ui > f16inf)
     92          f32.ui = f16inf - 1;
     93 
     94       f16 = f32.ui >> 13;
     95    }
     96 
     97    /* Sign */
     98    f16 |= sign >> 16;
     99 
    100    return f16;
    101 }
    102 
    103 static inline float
    104 util_half_to_float(uint16_t f16)
    105 {
    106    union fi infnan;
    107    union fi magic;
    108    union fi f32;
    109 
    110    infnan.ui = 0x8f << 23;
    111    infnan.f = 65536.0f;
    112    magic.ui  = 0xef << 23;
    113 
    114    /* Exponent / Mantissa */
    115    f32.ui = (f16 & 0x7fff) << 13;
    116 
    117    /* Adjust */
    118    f32.f *= magic.f;
    119    /* XXX: The magic mul relies on denorms being available */
    120 
    121    /* Inf / NaN */
    122    if (f32.f >= infnan.f)
    123       f32.ui |= 0xff << 23;
    124 
    125    /* Sign */
    126    f32.ui |= (f16 & 0x8000) << 16;
    127 
    128    return f32.f;
    129 }
    130 
    131 #ifdef __cplusplus
    132 }
    133 #endif
    134 
    135 #endif /* U_HALF_H */
    136 
    137