问如何累加一个128位寄存器中的四个float数?

news/2024/7/3 13:14:23 标签: float, arrays, function, 优化, basic, 编译器

问题(zlw):

一个_mm128型的寄存器包含  (xx3, xx2, xx1, xx0)这样四个float型浮点数.

想做 xx0 + xx1 + xx2 + xx3这样的计算

提问者自己觉得可以用:
xx = _mm_hadd_ps(xx, _mm_set_zero);    // to get (0, 0, xx3+ xx2, xx1+xx0)

 xx= _mm_add_ss(xx, _mm_shuffle_ps(xx,xx, _MM_SHUFFLE( 0, 0, 0, 1 )) ); 
_mm_store_ss(&temp,xx); 

亦可以用:

xx= _mm_add_ps(xx, _mm_movehl_ps(xx, xx));

xx= _mm_add_ss(xx, _mm_shuffle_ps(xx, xx, 1));

_mm_store_ss( &temp, xx );

问有没有更好的方法?

 

回答1(jimdempseyatthecove):

对同一寄存器使用两次hadd(源和目的寄存器为同一个)
// xx = { xx3, xx2, xx1, xx0 }
xx=_mm_hadd_ps(xx,xx);
// xx = { xx3+xx2, xx1+xx0, xx3+xx2, xx1+xx0}
xx=_mm_hadd_ps(xx,xx);
// xx = { xx2+xx3+xx1+xx0, xx3+xx2+xx1+xx0, xx3+xx2+xx1+xx0, xx3+xx2+xx1+xx0}

虽然最终每个32位里都是四个float型数的累加和,但是并不会带来额外开销
需要额外注意的是,如果在比较老的处理器上使用,需要在两次水平累加中间插入一个load或者store或者非sse操作(why?)

回答2(Brandon Hewitt):

“最好”的方法依赖实现的平台。

(这里用到了 Intel® Cilk™ Plus )

 

使用Intel C++ Composer XE Update 2 版本的编译器,产生的默认 Intel® SSE2代码如下 (i.e. icc -S -c test.c), :

movups (%rdi), %xmm0

movaps %xmm0, %xmm1

movhlps %xmm0, %xmm1

addps %xmm1, %xmm0

movaps %xmm0, %xmm2

shufps $245, %xmm0, %xmm2

addss %xmm2, %xmm0

ret


而支持 Intel SSE4.2 (icc -xSSE4.2 -S -c test.c)平台,则会得到
movups (%rdi), %xmm0

haddps %xmm0, %xmm0

haddps %xmm0, %xmm0

ret

这个查看Cilk汇编指令的方法很好。

Intel对自家产品的优化应该尽心尽力,一般手工优化能达到这样的水平应该说还算不错的。

 

P.S.//20110807

http://software.intel.com/sites/products/documentation/hpc/composerxe/en-us/cpp/mac/optaps/common/optaps_par_cean_prog.htm

C/C++ Extensions for Array Notations Programming Model

 

Reductions

A reduction combines array section elements to generate a scalar result. Intel® Cilk™ Plus supports reductions on array sections. It defines a generic reduction function that applies a user-defined dyadic function. It also has nine built-in common reduction functions. The built-in functions are polymorphic functions that accept int, float, and other C basic data type arguments. The names and descriptions of reduction functions are summarized in the table below.

Reduction Function Prototypes
Function PrototypesDescriptions
__sec_reduce(fun, identity, a[:]) Generic reduction function. Reduces fun across the array a[:] using identity as the initial value.
__sec_reduce_add(a[:]) Built-in reduction function. Adds values passed as arrays
__sec_reduce_mul(a[:]) Built-in reduction function. Multiplies values passed as arrays
__sec_reduce_all_zero(a[:]) Built-in reduction function. Tests that array elements are all zero
__sec_reduce_all_nonzero(a[:])Built-in reduction function. Tests that array elements are all non-zero
__sec_reduce_any_nonzero(a[:]) Built-in reduction function. Tests for any array element that is non-zero
__sec_reduce_min(a[:]) Built-in reduction function. Determines the minimum value of array elements
__sec_reduce_max(a[:]) Built-in reduction function. Determines the maximum value of array elements
__sec_reduce_min_ind(a[:]) Built-in reduction function. Determines the index of minimum value of array elements
__sec_reduce_max_ind(a[:])Built-in reduction function. Determines the index of maximum value of array elements

The reduction operation can reduce on multiple ranks. The number of ranks reduced depends on the execution context. For a given execution context of rank m and a reduction array section argument with rank n, where n>m, the last n-m ranks of the array section argument are reduced.

Example
sum = __sec_reduce_add(a[:][:]); // sum across the whole array a
sum_of_column[:] = __sec_reduce_add(a[:][:]); // sum across the column of a

 


http://www.niftyadmin.cn/n/1285537.html

相关文章

2016年系统架构师软考案例分析考点

1.软件的质量属性 质量属性包括:性能、可靠性、可用性、安全性、可修改性、易用性 2.用例和参与者 2.1 参与者 是指系统以外的,需要使用系统或与系统交互的事物,包括:人或组织、设备、外部系统等。在本题中,较为容易识…

2017年系统架构师软考案例分析考点

1.软件的质量属性 质量属性效用包括:性能、安全性、可用性、可修改性 2.系统架构风险、敏感点和权衡点的定义 2.1 系统架构风险:架构设计中潜在的、存在问题的架构决策所带来的隐患2.2 系统架构敏感点:为了实现某种特定的质量属性&#xff0…

2018年系统架构师软考案例分析考点

1.操作性需求、性能需求、 安全性需求和文化需求 统性能需求(Performance Requirements):指响应时间、吞吐量、准确性、有效性、资源 利用率等与系统完成任务效率相关的指标。可靠性、可用性等指标可归为此类。安全性需求(Security Requirements):系统向…

系统架构师论文-论混合软件架构设计

论混合软件架构设计 摘要 2007年3月,我所在的公司组织开发了一套完整的变电综合信息管理系统,在这个项 目中,我担任系统架构设计师职务,主要负责软件架构和网络安全体系架构设计的工作. 该系统包括变电运行所需的运行记录、图形开…

系统架构师论文-论系统的安全风险评估

论系统的安全风险评估 摘要 2005年3月,我参加了某石化公司的实验室信息管理系统项目的开发工作,该系统作为该石化公司产品质量信息管理平台, 将实验室的自动化分析仪器与计算机网络进行联结,实现自动采集样品分析数据以及对样品检…

又被自动升级阴了

今天电脑开机后,启动过程中弹出异常对话框“winlogon.exe 应用程序发生异常 未知软件异常(0xc0000409)位置为0x1009bc5d”无论点击“确定”还是“取消”后,进入界面便会蓝屏左上角会出现stop:c000021a unknovn hard error,接着自动重启。 但…

系统架构师论文-论信息系统的安全体系

论信息系统的安全体系 摘要 2005年2月,我參加了某水库管理信息系统项目的实施.通过系统的实施和运行,实现防汛、供水、发电、闸门监控、水文 等各种数据的采集、分析、存储,并通过网络及时地向有关部门汇报,以便相关领导进行调度…