Coder Social home page Coder Social logo

Comments (2)

zwd-ms avatar zwd-ms commented on June 16, 2024

google benchmark should also show similar results, but it needs to prevent optimizations carefully.

however, in general we could just rely on compiler auto-vectorization to optimize this. Using -O3 -ffast-math -march=native can get much closer to the hand-written intrinsic performance.

#include <benchmark/benchmark.h>

static void BM_inv_sqrt(benchmark::State& state)
{
  volatile float x = 4.f;

  for (auto _ : state)
  {
    benchmark::DoNotOptimize(inv_sqrt(x));
    benchmark::ClobberMemory();
  }
}

static void BM_rsqrt(benchmark::State& state)
{
  volatile float x = 4.f;

  for (auto _ : state)
  {
    benchmark::DoNotOptimize(rsqrt(x));
    benchmark::ClobberMemory();
  }
}

BENCHMARK(BM_inv_sqrt);
BENCHMARK(BM_rsqrt);

BENCHMARK_MAIN();

from vowpal_wabbit.

zwd-ms avatar zwd-ms commented on June 16, 2024

from GCC doc: https://gcc.gnu.org/onlinedocs/gcc/x86-Options.html#x86-Options

-mrecip

This option enables use of RCPSS and RSQRTSS instructions (and their vectorized variants RCPPS and RSQRTPS) with an additional Newton-Raphson step to increase precision instead of DIVSS and SQRTSS (and their vectorized variants) for single-precision floating-point arguments. These instructions are generated only when -funsafe-math-optimizations is enabled together with -ffinite-math-only and -fno-trapping-math. Note that while the throughput of the sequence is higher than the throughput of the non-reciprocal instruction, the precision of the sequence can be decreased by up to 2 ulp (i.e. the inverse of 1.0 equals 0.99999994).

Note that GCC implements 1.0f/sqrtf(x) in terms of RSQRTSS (or RSQRTPS) already with -ffast-math (or the above option combination), and doesn’t need -mrecip.

Also note that GCC emits the above sequence with additional Newton-Raphson step for vectorized single-float division and vectorized sqrtf(x) already with -ffast-math (or the above option combination), and doesn’t need -mrecip.

from vowpal_wabbit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.