Tags: , | Posted by usagi on 2010/07/14 14:43 | コメント (0)

libwrpmath.hxxを加え、max_power_of関数を定義しました。
この関数は「N以下で最大のBのべき乗Rを得る」関数です。

<使用例>

R = wrp::math::max_power_of<B>(N);

C++, using GeSHi 1.0.8.8
  1. #include <iostream>
  2. #include <wrp/math.hxx>
  3.  
  4. int main()
  5. {
  6. std::cout
  7. <<wrp::math::max_powert_of<2>(9000)<<std::endl
  8. <<wrp::math::max_powert_of<3>(9000)<<std::endl
  9. <<wrp::math::max_powert_of<4>(9000)<<std::endl
  10. <<wrp::math::max_powert_of<5>(9000)<<std::endl
  11. <<wrp::math::max_powert_of<6>(9000)<<std::endl
  12. <<wrp::math::max_powert_of<7>(9000)<<std::endl
  13. ;
  14. }
Parsed in 0.019 seconds at 19.57 KB/s

実行結果は以下の通り。

8192
6561
4096
3125
7776
2401

<主な用途>

  • GPUの効率的なテクスチャサイズへのリサイズ
  • 高速フーリエ変換に対する標本数の正規化

<実装>

C++, using GeSHi 1.0.8.8
  1. template<size_t B, typename T>
  2. inline const T max_power_of(const T n)
  3. {
  4. assert(B > 1);
  5. assert(n > 0);
  6.  
  7. std::function<const T(const T)> f_ = [&](const T c)->const T
  8. {
  9. const T cb = c * static_cast<const T>(B);
  10. return cb > n ? c : f_(cb);
  11. };
  12.  
  13. return f_(1);
  14. }
  15.  
Parsed in 0.018 seconds at 15.66 KB/s

<参考>

コメント可能な期間を過ぎました