8个打包的32位浮点数的水平和

编程入门 行业动态 更新时间:2024-10-28 13:28:43
本文介绍了8个打包的32位浮点数的水平和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如果我有8个压缩的32位浮点数(__m256),提取所有8个元素的水平和的最快方法是什么?同样,如何获取水平的最大值和最小值?换句话说,以下C ++函数的最佳实现是什么?

If I have 8 packed 32-bit floating point numbers (__m256), what's the fastest way to extract the horizontal sum of all 8 elements? Similarly, how to obtain the horizontal maximum and minimum? In other words, what's the best implementation for the following C++ functions?

float sum(__m256 x); ///< returns sum of all 8 elements float max(__m256 x); ///< returns the maximum of all 8 elements float min(__m256 x); ///< returns the minimum of all 8 elements

推荐答案

在此处快速记入(因此未经测试):

Quickly jotted down here (and hence untested):

float sum(__m256 x) { __m128 hi = _mm256_extractf128_ps(x, 1); __m128 lo = _mm256_extractf128_ps(x, 0); lo = _mm_add_ps(hi, lo); hi = _mm_movehl_ps(hi, lo); lo = _mm_add_ps(hi, lo); hi = _mm_shuffle_ps(lo, lo, 1); lo = _mm_add_ss(hi, lo); return _mm_cvtss_f32(lo); }

对于最小/最大,将_mm_add_ps和_mm_add_ss替换为_mm_max_*或_mm_min_*.

For min/max, replace _mm_add_ps and _mm_add_ss with _mm_max_* or _mm_min_*.

请注意,这需要执行一些操作,因此需要进行大量工作; AVX并非真正旨在有效地进行水平操作.如果您可以将这项工作分批处理成多个向量,那么可能会有更有效的解决方案.

Note that this is a lot of work for a few operations; AVX isn't really intended to do horizontal operations efficiently. If you can batch up this work for multiple vectors, then more efficient solutions are possible.

更多推荐

8个打包的32位浮点数的水平和

本文发布于:2023-08-07 19:57:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1321447.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:浮点数   水平和

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!