SSE2初学指南

SIMD、SSE、AVX指令集 指令集 指令集是指CPU能执行的所有指令的集合,每一指令对应一种操作,任何程序最终要编译成一条条指令才能让CPU识别并执行。CPU依靠指令来计算和控制系统,所以指令强弱是衡量CPU性能的重要指标,指令集也成为提高CPU效率的有效工具。 CPU都有一个基本的指令集,比如说目前英特尔和AMD的绝大部分处理器都使用的是X86指令集,因为它们都源自于X86架构。但无论CPU有多快,X86指令也只能一次处理... 阅读详情

Intro

What is SSE2?

SSE2 is an extension of assembly language which allows programs to execute one operation on multiple pieces of data at a time. Because SSE2 is assembly however, it only works on processors that support it. If the commands are attempted to be executed on a machine which is not capable of doing so, a general protection fault will be encountered. Luckily there are easy ways to tell if the processor(s) you are running on supports SSE2.

Basic Structure of SSE2

SSE2 works just like any other set of assembly calls. There are registers in which data can be stored and operations that can execute on these registers. Each register is 16 bytes (2 doubles). The 8 registers are named xmm0 through xmm7.

Basics

Some Code

inline void Add(double *x, double *y, double *retval)
{
  asm
  {
    // Copy the first 16 bytes into xmm0, starting at the memory x points to
    movupd xmm0, [x]
    // Copy the first 16 bytes into xmm1, starting at the memory y points to
    movupd xmm1, [y]
    // Add the 2 doubles in xmm1 to the 2 doubles in xmm0, and put the
    // result in xmm0, overwriting the previous data stored there
    addpd xmm0, xmm1
    // Copy the 16 bytes of data in xmm0 to the memory ret points to
    movupd [retval], xmm0  
  }
}

Hopefully my comments before each line were enough to let you know what was going on. In case they weren't, I'll go into a little more detail about each line.

    asm{}

This keyword lets your compiler know that the code you are giving it will be in assembly and that it should compile it as such. It also, conveniently, tells the compiler to inline the code. This means that there is NO overhead for the asm block.

    movupd xmm0, [x]
    movupd xmm1, [y]

This command copies data from the second operand to the first; as always in Intel syntax, the asm is in dest, src order. By putting brackets around the x, we tell the mov command to copy the data that x points to the actual value of the pointer. The square brackets can be thought of as a method of dereferencing a pointer.

    addpd xmm0, xmm1

This is the line that does the actual arithmetic. It takes the value from the 2nd operand, src, and adds it to the 1st operand, dest, and stores the resulting value in the 1st operand, dest.

    movupd [retval], xmm0

Here, we copy the data that is in xmm0 to the memory that retval points to. Again, the square brackets dereference the variable retval in the same way that a '*' does in C/C++.

Some more operations

subpd dest, src  // subtract dest from src, store in dest
mulpd dest, src  // multiply dest and src, store in dest
divpd dest, src  // divide src by dest, store in dest
minpd dest, src  // store the smallest value, either dest or src, in dest
maxpd dest, src  // store the largest value, either dest or src, in dest
sqrtpd dest, src // take the square root of src and put the result in dest

A full list of SSE2 operations and a description of each can be found at HAYES Technologies.

Making the Most Of SSE2

The Faster Move Instruction

Up until now, we have been using movapd to move data to and from our registers. This is much slower than the instruction movapd which does the exact same thing, but assumes that the data is 16 byte aligned. This means that the pointer supplied must be divisible by 16. This becomes a rather large problem if you are compiling your code with gcc or the one supplied with Microsoft Visual C++. One solution to this problem is to use a different compiler such as one that Intel provides. The inherent problem with this is that the Intel compiler is not freeware like gcc. If you have already spent money on some other compiler, you probably do not want to spend more on this new compiler. One hack that I have come up with is the following:

#define AllignData(data) (void *)(((int)data + 15) &~ 0x0F)

//or an inline function if you prefer:

inline void *AllignData(void *data)
{
  return (void *)(((int)data + 15) &~ 0x0F);
}

void main
{
  const int sizeofdata = 512;

  double *lotsofdata;
  double *tempptr;

  tempptr = new double[sizeofdata + 2];
  lotsofdata = AllignData(tempptr);

  asm
  {
      movapd xmm0, [lotsofdata];
      // do lots of CPU intensive SSE2 operations on lotsofdata here
  }

  delete [] tempptr;
  lotsofdata = 0;
  // do not delete lotsofdata, just set it to 0, the memory it
  // points to is no longer valid
}

What we need for this instruction to work is memory that has a 16-byte alligned memory address.

Memory that is 16byte alligned:

When we use the new command in C/C++ to allocate memory, we are given memory that may or may not be 16byte alligned. So, what we do, is instead of using the very first bit of our memory block, we start using it at the first place that is 16byte alligned.
-- yellow is unusbale, green is what we actually use --

By doing this, we waste the memory that comes before the first 16-byte alligned memory address. Normally this is not too big of a problem as the overhead is only once per memory block and if we are allocating large amounts of memory at once, 1 or even 12 bytes will hardly make a difference. The only problem with this is that by not using the beginning of our memory, we end up having a smaller amount of usable memory than we asked for. In the worst case, 15 bytes are not usable:

Therefore, to make sure that we get a specific number of usable bytes, we allocate at least 15 extra bytes. In the case of allocating double values, this means we must allocate an extra 2 doubles, giving us 16 extra bytes:

If you are not using doubles, just make sure that at least 15 extra bytes are allocated. Just determine the size of the data type and compute how many are needed to give you the needed padding.

Another important thing to notice is that we delete the variable that holds the original pointer to all of our memory. If you try to delete the memory starting in the middle of our memory block, you cause a general protection fault and have a memory leak.

Ordering Your Operations

One thing that is often over looked is the order that registers are used. When an operation is performed, there is a delay while the result is bieng moved to its destination. if the next operation requires this value, it must wait for it to be stored into the register. If however, the next operation does not need this data, it does not need to wait for it to be stored, it can go ahead and execute at the same time that the previous result is getting stored.

For instance, there will be a speed difference between the following two code segments:

#1:

asm
{
  movupd xmm0, [x]   // xmm0 = x
  movupd xmm1, [y]   // xmm1 = y
  movupd xmm2, [z]   // xmm2 = z
  movupd xmm3, [w]   // xmm3 = w

  movapd xmm4, xmm2  // xmm4 = z
  movapd xmm5, xmm1  // xmm5 = y
  movapd xmm6, xmm0  // xmm6 = x
    
  addpd xmm2, xmm3   // xmm2 = z + w
  addpd xmm1, xmm2   // xmm1 = y + z + w
  addpd xmm0, xmm1   // xmm0 = x + y + z + w

  mulpd xmm4, xmm3   // xmm4 = z * w
  mulpd xmm5, xmm4   // xmm5 = y * z * w
  mulpd xmm6, xmm5   // xmm6 = x * y * z * w

  divpd xmm0, xmm6   // xmm0 = (x * y * z * w) / (x + y + z + w)

  movupd [ret], xmm0 // ret = (x * y * z * w) / (x + y + z + w)
}

#2:

asm
{
  movupd xmm0, [x]   // xmm0 = x
  movupd xmm1, [y]   // xmm1 = y
  movupd xmm2, [z]   // xmm2 = z
  movupd xmm3, [w]   // xmm3 = w

  movapd xmm4, xmm2  // xmm4 = z
  movapd xmm5, xmm1  // xmm5 = y
  movapd xmm6, xmm0  // xmm6 = x
    
  addpd xmm2, xmm3   // xmm2 = z + w
  mulpd xmm4, xmm3   // xmm4 = z * w
  addpd xmm1, xmm2   // xmm1 = y + z + w
  mulpd xmm5, xmm4   // xmm5 = y * z * w
  addpd xmm0, xmm1   // xmm0 = x + y + z + w
  mulpd xmm6, xmm5   // xmm6 = x * y * z * w

  divpd xmm0, xmm6   // xmm0 = (x * y * z * w) / (x + y + z + w)

  movupd [ret], xmm0 // ret = (x * y * z * w) / (x + y + z + w)
}

The second piece of code will run faster. This is because in the second case, there are only 2 cases where one instruction relies on the data from the previous one to perform its computations. Because of this, instructions can be executed immediately after the previous one finsihes instead of waiting for it to store its result in the registers.

Don't get carried away

A common mistake made by people new to SSE2 is to convert a lot of their old and future code into SSE2. This can actually result in slower code. The reason for this is the very large overhead for the CPU to copy memory to the registers. If you have an application that is doing a small number of operations on a large data set, you can expect to be less efficient than if you are doing a lot of operations on a small amount of data.

Compiling SSE2 with gcc/g++

The first thing that you need to remember to do when you want to compile SSE2 embedded C/C++ code with gcc/g++, is to throw in the -masm=intel switch during compile. You must also put ".intel_syntax noprefix" in front of your asm code and surround it with quotes like this:

    asm(".intel_syntax noprefix/n");
    asm("    mov eax, x/n");
    asm("    movupd xmm0, [eax+0x00]/n");
    asm("    movupd xmm1, [eax+0x10]/n");
    asm("    addpd xmm0, xmm1/n");
    asm("    movupd [eax+0x20], xmm0/n");

    or

    asm(".intel_syntax noprefix
         mov eax, x
         movupd xmm0, [eax+0x00]
         movupd xmm1, [eax+0x10]
         addpd xmm0, xmm1
         movupd [eax+0x20], xmm0/n");

Note that the asm block is inside "()" not "{}". Also, if you want to use a variable declared in your C/C++ code, you must define it publicly. Any variables defined locally, whether inside your main function, inside a for loop, etc, will not be seen by the linker and will be considered an "undefined reference".

End

Questions? Comments? Suggestions? mis-spellings? Grammatical Errors? Email me at shilindalian@msn.com

SSE指令集学习使用 本篇介绍SSE指令集,SSE是流化SIMD扩展(Streaming SIMD Extension, SSE),提供全新的一组寄存器,处理128位打包数据。SSE提供了xmm寄存器,xmm一组8个128位的寄存器,分别名为xmm0-xmm7,SSE构架提供对打包单精度浮点数的SIMD支持。 阅读详情

相关推荐

SSE/SSE2编程与优化技巧实战指南

SIMD技术简介SIMD,即单指令流多数据流(Single Instruction, Multiple Data),是一种计算机架构中的数据处理模式,它允许一条指令同时对多个数据执行相同的操作。通过并行化数据处理过程,SIMD技术显著提升了计算机系统的数据处理能力,尤其是在处理多媒体数据如图形、音频、视频等领域。SSE技术的演进与特点。

weixin_42323064的博客 831

sse转neon

sse转neon

jacke121的专栏 8810

sse2neon-master.zip

intel 指令转 arm neon指令实现,里面包含所有源码,以及测试验证代码,是arm国产化开发必备资料

SSE2 小记-1

整数加法:paddb/w/d/q 浮点型加法:paddpd,paddsd,两者的差别是后者只改变低64位,高64位不变,如下: 饱和型加法:paddsb/w,

奋斗的小麻雀的博客 296

neon-0.28.4-1.i386

svn服务器配置中相关的文件,neon-0.28.4-1.i386.rpm

sse连续枚举法拉丁超立方实验设计matlab代码.zip

sse连续枚举法拉丁超立方实验设计matlab代码 sseLHDPLUS为main,设置其中的m、n就可以生成对应尺寸的LHD,且该代码支持多cpu并行(实时显示计算进度),相比原方法有很大提升。最后给出与matlab自带lhsdesign对比结果,包括maximin、phip。

SSE2Neon:跨平台指令集转换工具的创新实践

是一个开源项目,它主要致力于将Intel的SSE(Streaming SIMD Extensions)指令集翻译为ARM架构的NEON向量扩展指令集。作为一个编译时转换库,SSE2Neon帮助开发者在x86和ARM平台上实现高性能的计算代码,特别是在移植优化的多媒体处理、数值计算和机器学习算法时提供极大便利。 ## 技术分析 SSE2Neon 使用C++编写,基于源码级别的转换。其工作原理是通...

gitblog_00078的博客 794

utf8:使用范围算法(NEON + SSE4 + AVX2)的快速UTF-8验证

utf8:使用范围算法(NEON + SSE4 + AVX2)的快速UTF-8验证

x86-64汇编浮点开发避坑指南:SSE2、ABI与内存对齐

浮点运算是现代系统编程的基础能力,其底层实现远不止指令语法层面——它深度耦合CPU架构、调用约定与内存模型。x86-64平台已全面转向SSE2浮点体系,取代老旧的x87栈式指令;System V AMD64 ABI明确规定浮点参数与返回值必须通过xmm0-xmm1传递,整数寄存器无法直接读取浮点结果。同时,SSE2指令要求16字节内存对齐,未对齐访问可能引发段错误或性能陡降;数据类型转换(如cvttsd2si)存在溢出饱和行为,需主动检测。本文聚焦Linux环境下x86-64汇编浮点开发的真实约束,覆盖寄存

weixin_33971977的博客 193

11n、11ac、11ax和11be MCS表(全网最全)

1、无线协商速率介绍及计算 2、附11n、11ac、11ax 完整MCS速率表

weixin_41500064的博客 2万+

#Linux内存管理# 用一个案例详细介绍ARMv7-A架构 缺页中断处理的原理

跳转到异常向量:根据 异常向量表基地址(通常配置在 VBAR 寄存器中,例如 0xFFFF0000) + 数据中止异常的偏移量 0x10,跳转到数据中止异常的处理程序入口点 0xFFFF0010(实践中,这个入口通常是一条跳转到更复杂处理函数的指令)。malloc 通常不会立即分配物理内存页,它只是在内核为该进程维护的虚拟地址空间中找到一段足够大的空闲虚拟地址区域(比如 0x00008000 - 0x00008FFF),标记为已用,并返回起始地址(例如 0x00008000)给程序。

sinat_37817094的博客 994

Linux x86-64下SSE2汇编实战:内存对齐与性能优化

SIMD(单指令多数据)是现代CPU实现并行计算的基础技术,其核心原理是利用宽寄存器(如XMM)一次性处理多个同类型数据,显著提升数值密集型任务的吞吐量。在x86-64 Linux环境中,SSE2作为架构强制支持的最小公分母指令集,兼具高兼容性与确定性性能优势,广泛应用于图像处理、实时信号分析和嵌入式数值计算等场景。相比高级语言自动向量化,手写SSE2可精准控制内存对齐、避免缓存污染、调度非临时存储,并绕过编译器抽象层限制。本文聚焦SSE2在Linux下的工程落地,深入解析movaps内存对齐要求、posi

weixin_34101229的博客 323

STM32G474片内运放实战:从独立模式到PGA与ADC采样链路

在嵌入式控制系统中,运放是连接模拟信号与数字处理的关键环节。传统方案依赖外部分立运放搭建放大电路,存在成本高、PCB布局敏感、一致性差等痛点。STM32G474在MCU内部集成多路可编程运放(OPAMP),支持独立模式、跟随器、PGA以及比较器模式,配合内部ADC和DMA可构成完整模拟采集链路。通过片内互联,信号路径大幅缩短,抗干扰性和通道一致性显著提升,尤其适用于电机电流采样、数字电源等场景。本文以STM32G474为例,详解从独立模式到PGA增益配置,再到ADC与DMA联动调试的完整过程,并给出采样时间

weixin_29194255的博客 256

SIMD(单道指令多道数据流)指令(MMX/SSE1/SSE2)详解(中文).

http://www.aogosoft.com/bbs/view.asp?id=74373 作者:ham 向作者致敬! SIMD(单道指令多道数据流)指令(MMX/SSE1/SSE2)详解(中文).还有更多的多媒体指令集,SSE3,SSE4

daoqinglin的专栏 9308

Jetson Nano系统重装的艺术:从格式化到烧录的深度解析

本文详细解析了Jetson Nano系统重装的完整流程,从SD卡格式化到系统镜像烧录的进阶技巧,包括硬件选择、软件准备、常见问题处理及深度学习环境配置。特别推荐使用三星EVO Plus SD卡和官方电源以确保稳定性,适合开发者快速掌握嵌入式设备系统重装的核心技术。

weixin_28273593的博客 612

【VSCode Python无法格式化代码】

提示:需要Install Formatter…安装插件autopep8,点击install。出现Format Document。

AIFRZ的博客 1万+

初学汇编之CPU类型识别

http://www.cppblog.com/Jedimaster/archive/2006/08/18/11423.html 为了以后优化算法,还是硬着头皮学了一下汇编,也就是C\C++与汇编的混合编程,帖一个类的代码,非常简单的东西,觉得还可以优化。识别CPU的型号,使用了Intel的CPUID指令,还没有来得及看AMD的资料,以后有空了我也做一个类似于CPUID一样的小工具呵呵。 cla

skyell的专栏 653

硬件工程师入门基础知识(一)基础元器件认识(二)

我们的电路输入一般是只有两种状态,0V 或者是其它的高电平(1.8V,3.3V,5V 等),截止状态一般不用怎么考虑,因为如果让三极管的 Vbe=0,自然就截止了,重要的是饱和状态如何保证。也就是说只要 Rb

小幽余生不加糖 1万+

<<BASM 初学者入门>> 第 1 课 A

> 第 1 课 A http://www.cnpack.org 翻译:SkyJacker 时间:2007 Introduction to BASM for Beginners BASM 入门教程简介 The series of articles named “BASM for beginners” currently consists of 7 articles and no. 8

gencheng的专栏 1078

AVDepthData、LiDAR 深度数据与人像模式全解析:iOS 真实景深图采集与处理实战

在 iOS 高端影像系统中,真实景深信息的采集与利用已成为区分旗舰设备与基础设备的重要能力。Apple 自 iPhone 7 Plus 引入双摄虚化以来,逐步发展出基于 AVDepthData、TrueDepth 与 LiDAR 的多维深度感知技术,并开放接口供开发者获取高精度深度图,构建背景虚化、人像分割、3D 重建等多种功能。本文将深入解析 AVDepthData 的结构与使用方式,系统讲解 LiDAR 采集架构、人像模式的景深融合逻辑,以及如何在实际项目中采集、预览并处理深度图数据,实现具备工程可用性

努力分享一些人工智能、计算机视觉、影像等相关的知识干货! 1865

C# 上位机 YOLO 实战:从数据采集到目标识别的全流程拆解

本文以工业螺栓缺陷检测为例,完整拆解了C#上位机+YOLO从数据采集到目标识别的全流程。数据采集:保证图像质量,线程安全采集,必要时归档数据;预处理:适配YOLO模型输入格式,复用对象减少内存开销;推理解析:正确解析张量结果,过滤低置信度目标;落地环节:可视化展示结果,实现工业交互和日志记录。

这里只有干货:从 Modbus 通信到 MES 对接,从 YOLO 训练到工控机部署,带你搞定工业软件开发全流程。 1462

<<BASM 初学者入门>> 第 3 课

> 第 3 课 http://www.cnpack.org 翻译:SkyJacker 时间:2007 In this third lesson topic such as MMX and SSE2 will be introduced together with Int64 arithmetic’s. This is the first time we will see process

gencheng的专栏 843

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

SIMD初学

SIMD初学 在学习SIMD之前,我们首先需要了解什么是标量与矢量。 浮点运算指令分为两大类:Packed(矢量) 和Scalar(标量)。 Packed指令是一次对寄存器中的四个浮点数(即DATA0 ~ DATA3)均进行计算,而Scalar一次则只对寄存器中的DATA0进行计算。如下图所示: *1.SIMD的历史与分类 SIMD(Single Instruction Multiple Dat...

qq_40406891的博客 2995

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

上一篇: SSE所需头文件
下一篇: MMX及SSE优化--MMX篇
dbgnu
博客等级 码龄25年 3粉丝 3原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值