Skip to content

Instantly share code, notes, and snippets.

@dnbaker
Last active December 6, 2021 19:29
Show Gist options
  • Save dnbaker/049c708bceaf65310951febf6a16784b to your computer and use it in GitHub Desktop.
Save dnbaker/049c708bceaf65310951febf6a16784b to your computer and use it in GitHub Desktop.
Approximate log/log2
#pragma once
static inline float flog2_float(float x) {return (float)*(uint32_t *)&x * 0x1p-23f - 127.f;}
static inline double fastlog2_double(double x) {return (double)*(uint64_t *)&x * 0x1p-52 - 1023;}
// To convert this to approximate log-base-e, divide these by log(2)
#define LIBKL_ALOG_PD_MUL 1.539095918623324e-16
#define LIBKL_ALOG_PD_INC -709.0895657128241
#define LIBKL_ALOG_PS_MUL 8.2629582881927490e-8f
#define LIBKL_ALOG_PS_INC -88.02969186f
#if __AVX512F__
static inline __attribute__((always_inline)) __m512d _mm512_ss_alog_pd(__m512d x) {
return _mm512_fmadd_pd(_mm512_cvtepi64_pd(_mm512_castpd_si512(x)),
_mm512_set1_pd(LIBKL_ALOG_PD_MUL),
_mm512_set1_pd(LIBKL_ALOG_PD_INC));
}
static inline __attribute__((always_inline)) __m512 _mm512_ss_alog_ps(__m512 x) {
return _mm512_fmadd_ps(_mm512_cvtepi32_ps(_mm512_castps_si512(x)),
_mm512_set1_ps(LIBKL_ALOG_PS_MUL),
_mm512_set1_ps(LIBKL_ALOG_PS_INC));
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment