template<typename _Ts, typename _Td> inline void cvt_32f( const _Ts* src, size_t sstep, _Td* dst, size_t dstep, Size size, float a, float b ) { #if CV_SIMD v_float32 va = vx_setall_f32(a), vb = vx_setall_f32(b); const int VECSZ = v_float32::nlanes*2; #endif sstep /= sizeof(src[0]); dstep /= sizeof(dst[0]); for( int i = 0; i < size.height; i++, src += sstep, dst += dstep ) { int j = 0; #if CV_SIMD for( ; j < size.width; j += VECSZ ) { if( j > size.width - VECSZ ) { if( j == 0 || src == (_Ts*)dst ) break; j = size.width - VECSZ; } v_float32 v0, v1; vx_load_pair_as(src + j, v0, v1); v0 = v_fma(v0, va, vb); v1 = v_fma(v1, va, vb); v_store_pair_as(dst + j, v0, v1); } #endif for( ; j < size.width; j++ ) dst[j] = saturate_cast<_Td>(src[j]*a + b); } }
void log32f( const float *_x, float *y, int n ) { CV_INSTRUMENT_REGION(); const float* const logTab_f = cv::details::getLogTab32f(); const int LOGTAB_MASK2_32F = (1 << (23 - LOGTAB_SCALE)) - 1; const float A0 = 0.3333333333333333333333333f, A1 = -0.5f, A2 = 1.f; int i = 0; const int* x = (const int*)_x; #if CV_SIMD const int VECSZ = v_float32::nlanes; const v_float32 vln2 = vx_setall_f32((float)ln_2); const v_float32 v1 = vx_setall_f32(1.f); const v_float32 vshift = vx_setall_f32(-1.f/512); const v_float32 vA0 = vx_setall_f32(A0); const v_float32 vA1 = vx_setall_f32(A1); const v_float32 vA2 = vx_setall_f32(A2); for( ; i < n; i += VECSZ ) { if( i + VECSZ > n ) { if( i == 0 || _x == y ) break; i = n - VECSZ; } v_int32 h0 = vx_load(x + i); v_int32 yi0 = (v_shr<23>(h0) & vx_setall_s32(255)) - vx_setall_s32(127); v_int32 xi0 = (h0 & vx_setall_s32(LOGTAB_MASK2_32F)) | vx_setall_s32(127 << 23); h0 = v_shr<23 - LOGTAB_SCALE - 1>(h0) & vx_setall_s32(LOGTAB_MASK*2); v_float32 yf0, xf0; v_lut_deinterleave(logTab_f, h0, yf0, xf0); yf0 = v_fma(v_cvt_f32(yi0), vln2, yf0); v_float32 delta = v_reinterpret_as_f32(h0 == vx_setall_s32(510)) & vshift; xf0 = v_fma((v_reinterpret_as_f32(xi0) - v1), xf0, delta); v_float32 zf0 = v_fma(xf0, vA0, vA1); zf0 = v_fma(zf0, xf0, vA2); zf0 = v_fma(zf0, xf0, yf0); v_store(y + i, zf0); } vx_cleanup(); #endif for( ; i < n; i++ ) { Cv32suf buf; int i0 = x[i]; buf.i = (i0 & LOGTAB_MASK2_32F) | (127 << 23); int idx = (i0 >> (23 - LOGTAB_SCALE - 1)) & (LOGTAB_MASK*2); float y0 = (((i0 >> 23) & 0xff) - 127) * (float)ln_2 + logTab_f[idx]; float x0 = (buf.f - 1.f)*logTab_f[idx + 1] + (idx == 510 ? -1.f/512 : 0.f); y[i] = ((A0*x0 + A1)*x0 + A2)*x0 + y0; } }
void exp32f( const float *_x, float *y, int n ) { CV_INSTRUMENT_REGION(); const float* const expTab_f = cv::details::getExpTab32f(); const float A4 = (float)(1.000000000000002438532970795181890933776 / EXPPOLY_32F_A0), A3 = (float)(.6931471805521448196800669615864773144641 / EXPPOLY_32F_A0), A2 = (float)(.2402265109513301490103372422686535526573 / EXPPOLY_32F_A0), A1 = (float)(.5550339366753125211915322047004666939128e-1 / EXPPOLY_32F_A0); int i = 0; const Cv32suf* x = (const Cv32suf*)_x; float minval = (float)(-exp_max_val/exp_prescale); float maxval = (float)(exp_max_val/exp_prescale); float postscale = (float)exp_postscale; #if CV_SIMD const int VECSZ = v_float32::nlanes; const v_float32 vprescale = vx_setall_f32((float)exp_prescale); const v_float32 vpostscale = vx_setall_f32((float)exp_postscale); const v_float32 vminval = vx_setall_f32(minval); const v_float32 vmaxval = vx_setall_f32(maxval); const v_float32 vA1 = vx_setall_f32((float)A1); const v_float32 vA2 = vx_setall_f32((float)A2); const v_float32 vA3 = vx_setall_f32((float)A3); const v_float32 vA4 = vx_setall_f32((float)A4); const v_int32 vidxmask = vx_setall_s32(EXPTAB_MASK); bool y_aligned = (size_t)(void*)y % 32 == 0; for( ; i < n; i += VECSZ*2 ) { if( i + VECSZ*2 > n ) { if( i == 0 || _x == y ) break; i = n - VECSZ*2; y_aligned = false; } v_float32 xf0 = vx_load(&x[i].f), xf1 = vx_load(&x[i + VECSZ].f); xf0 = v_min(v_max(xf0, vminval), vmaxval); xf1 = v_min(v_max(xf1, vminval), vmaxval); xf0 *= vprescale; xf1 *= vprescale; v_int32 xi0 = v_round(xf0); v_int32 xi1 = v_round(xf1); xf0 = (xf0 - v_cvt_f32(xi0))*vpostscale; xf1 = (xf1 - v_cvt_f32(xi1))*vpostscale; v_float32 yf0 = v_lut(expTab_f, xi0 & vidxmask); v_float32 yf1 = v_lut(expTab_f, xi1 & vidxmask); v_int32 v0 = vx_setzero_s32(), v127 = vx_setall_s32(127), v255 = vx_setall_s32(255); xi0 = v_min(v_max(v_shr<EXPTAB_SCALE>(xi0) + v127, v0), v255); xi1 = v_min(v_max(v_shr<EXPTAB_SCALE>(xi1) + v127, v0), v255); yf0 *= v_reinterpret_as_f32(v_shl<23>(xi0)); yf1 *= v_reinterpret_as_f32(v_shl<23>(xi1)); v_float32 zf0 = xf0 + vA1; v_float32 zf1 = xf1 + vA1; zf0 = v_fma(zf0, xf0, vA2); zf1 = v_fma(zf1, xf1, vA2); zf0 = v_fma(zf0, xf0, vA3); zf1 = v_fma(zf1, xf1, vA3); zf0 = v_fma(zf0, xf0, vA4); zf1 = v_fma(zf1, xf1, vA4); zf0 *= yf0; zf1 *= yf1; if( y_aligned ) { v_store_aligned(y + i, zf0); v_store_aligned(y + i + VECSZ, zf1); } else { v_store(y + i, zf0); v_store(y + i + VECSZ, zf1); } } vx_cleanup(); #endif for( ; i < n; i++ ) { float x0 = x[i].f; x0 = std::min(std::max(x0, minval), maxval); x0 *= (float)exp_prescale; Cv32suf buf; int xi = saturate_cast<int>(x0); x0 = (x0 - xi)*postscale; int t = (xi >> EXPTAB_SCALE) + 127; t = !(t & ~255) ? t : t < 0 ? 0 : 255; buf.i = t << 23; y[i] = buf.f * expTab_f[xi & EXPTAB_MASK] * ((((x0 + A1)*x0 + A2)*x0 + A3)*x0 + A4); } }