Edit

kc3-lang/angle/src/common/mathutil.cpp

Branch :

  • Show log

    Commit

  • Author : Jamie Madill
    Date : 2020-03-20 14:54:11
    Hash : 26f8f1a9
    Message : Revert "Refactor BitCount" This reverts commit baecb7d55c692a93a29a46939029b8262174ca62. Reason for revert: Broke Skia Win/ARM builder. See issue for details. Original change's description: > Refactor BitCount > > POPCNT intrinsics cannot be used without hardware support, so a CPUID > check and polyfills are required for some CPUs when using MSVC to > avoid runtime failure. > > Other changes include: > - Clang: use builtins on all platforms to provide exact intent to the > compiler; > - MSVC on ARM: use dedicated intrinsics; > - x86/x64 fallback is now branchless and works in constant time. > > Bug: angleproject:4462 > Change-Id: I00fcabda1c842677d8cb4bfd280d932d0d10c0a5 > Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2102811 > Reviewed-by: Jamie Madill <jmadill@chromium.org> > Reviewed-by: Geoff Lang <geofflang@chromium.org> > Commit-Queue: Geoff Lang <geofflang@chromium.org> TBR=geofflang@chromium.org,jmadill@chromium.org,lexa.knyazev@gmail.com Change-Id: Ia1756abdf7da2aa9574149eb388915f97758bba0 No-Presubmit: true No-Tree-Checks: true No-Try: true Bug: angleproject:4462 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2112276 Reviewed-by: Jamie Madill <jmadill@chromium.org> Commit-Queue: Jamie Madill <jmadill@chromium.org>

  • src/common/mathutil.cpp
  • //
    // Copyright 2013 The ANGLE Project Authors. All rights reserved.
    // Use of this source code is governed by a BSD-style license that can be
    // found in the LICENSE file.
    //
    
    // mathutil.cpp: Math and bit manipulation functions.
    
    #include "common/mathutil.h"
    
    #include <math.h>
    #include <algorithm>
    
    namespace gl
    {
    
    namespace
    {
    
    struct RGB9E5Data
    {
        unsigned int R : 9;
        unsigned int G : 9;
        unsigned int B : 9;
        unsigned int E : 5;
    };
    
    // B is the exponent bias (15)
    constexpr int g_sharedexp_bias = 15;
    
    // N is the number of mantissa bits per component (9)
    constexpr int g_sharedexp_mantissabits = 9;
    
    // Emax is the maximum allowed biased exponent value (31)
    constexpr int g_sharedexp_maxexponent = 31;
    
    constexpr float g_sharedexp_max =
        ((static_cast<float>(1 << g_sharedexp_mantissabits) - 1) /
         static_cast<float>(1 << g_sharedexp_mantissabits)) *
        static_cast<float>(1 << (g_sharedexp_maxexponent - g_sharedexp_bias));
    
    }  // anonymous namespace
    
    unsigned int convertRGBFloatsTo999E5(float red, float green, float blue)
    {
        const float red_c   = std::max<float>(0, std::min(g_sharedexp_max, red));
        const float green_c = std::max<float>(0, std::min(g_sharedexp_max, green));
        const float blue_c  = std::max<float>(0, std::min(g_sharedexp_max, blue));
    
        const float max_c = std::max<float>(std::max<float>(red_c, green_c), blue_c);
        const float exp_p =
            std::max<float>(-g_sharedexp_bias - 1, floor(log(max_c))) + 1 + g_sharedexp_bias;
        const int max_s = static_cast<int>(
            floor((max_c / (pow(2.0f, exp_p - g_sharedexp_bias - g_sharedexp_mantissabits))) + 0.5f));
        const int exp_s =
            static_cast<int>((max_s < pow(2.0f, g_sharedexp_mantissabits)) ? exp_p : exp_p + 1);
    
        RGB9E5Data output;
        output.R = static_cast<unsigned int>(
            floor((red_c / (pow(2.0f, exp_s - g_sharedexp_bias - g_sharedexp_mantissabits))) + 0.5f));
        output.G = static_cast<unsigned int>(
            floor((green_c / (pow(2.0f, exp_s - g_sharedexp_bias - g_sharedexp_mantissabits))) + 0.5f));
        output.B = static_cast<unsigned int>(
            floor((blue_c / (pow(2.0f, exp_s - g_sharedexp_bias - g_sharedexp_mantissabits))) + 0.5f));
        output.E = exp_s;
    
        return bitCast<unsigned int>(output);
    }
    
    void convert999E5toRGBFloats(unsigned int input, float *red, float *green, float *blue)
    {
        const RGB9E5Data *inputData = reinterpret_cast<const RGB9E5Data *>(&input);
    
        *red =
            inputData->R * pow(2.0f, (int)inputData->E - g_sharedexp_bias - g_sharedexp_mantissabits);
        *green =
            inputData->G * pow(2.0f, (int)inputData->E - g_sharedexp_bias - g_sharedexp_mantissabits);
        *blue =
            inputData->B * pow(2.0f, (int)inputData->E - g_sharedexp_bias - g_sharedexp_mantissabits);
    }
    
    int BitCountPolyfill(uint32_t bits)
    {
        int ones = 0;
        while (bits)
        {
            ones += bool(bits & 1);
            bits >>= 1;
        }
        return ones;
    }
    
    }  // namespace gl