VBGood网站全文搜索 Google

搜索VBGood全站网页(全文搜索)

VB爱好者乐园(VBGood)

 找回密码
 立即注册
搜索
楼主: littlekevin

还能优化吗?

[复制链接]
 楼主| 发表于 2009-2-5 20:11:32 | 显示全部楼层
intel instruction set.pdf,34页啊

  1. --  Branch hints:
  2. •    2EH--Branch not taken (used only with Jcc instructions)
  3. •    3EH--Branch taken (used only with Jcc instructions)

  4. Branch hint prefixes (2EH, 3EH) allow a program to give a hint to the processor about
  5. the most likely code path for a branch. Use these prefixes only with conditional
  6. branch instructions (Jcc). Other use of branch hint prefixes and/or other undefined
  7. opcodes with Intel 64 or IA-32 instructions is reserved; such use may cause unpre-
  8. dictable behavior.
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2009-2-5 20:27:17 | 显示全部楼层
原帖由 PctGL 于 2009-2-5 20:00 发表
》add确实比inc快,因为inc不改变carry flag,可能会导致对前一个指令的错误依赖(false dependent)
inc 就是因为不影响标志位,所以对他本身的执行效率来讲也是有所增强的,而且很多时候我们需要的就是不影响标志位的操作,能被你解释成错误依赖。。。。

》add一个时钟周期可以执行4个,inc只能有2个
完全错误的说法
你对指令占用cpu时钟周期的理解正好颠倒了...而且怎么感觉你对80486的cpu不熟悉的样子


不让上传附件了..说超过什么大小

Intel Pentium 4 Processor Optimization Reference Manual

  1. 3.5.1.1       Use of the INC and DEC Instructions
  2. The INC and DEC instructions modify only a subset of the bits in the flag register. This
  3. creates a dependence on all previous writes of the flag register. This is especially
  4. problematic when these instructions are on the critical path because they are used to
  5. change an address for a load on which many other instructions depend.
  6. Assembly/Compiler Coding Rule 32. (M impact, H generality) INC and DEC
  7. instructions should be replaced with ADD or SUB instructions, because ADD and
  8. SUB overwrite all flags, whereas INC and DEC do not, therefore creating false
  9. dependencies on earlier instructions that set the flags.  
复制代码
回复 支持 反对

使用道具 举报

发表于 2009-2-5 20:52:59 | 显示全部楼层
inc 和 add 的区别除了速度,灵活性,指令长度,之外恐怕就是影响标志位的问题了,这个就不用说了
我看了下你的代码,虽然不知道你的程序想做什么,但 add 的指令是无需考虑标志位的问题,sub 的指令利用了标志位,都知道标志位会因 add,sub 指令而改变,在这个基础上,只剩下 inc 和 add 的执行速度的讨论,那么你是从哪份资料上看到的:
》add一个时钟周期可以执行4个,inc只能有2个 ?


2e,3e的问题了解了
其实,通过学习分支预测可以知道,跳转是跳好还是不跳好,跳与不跳存在着很大的速度差别,在宏观上来讲,几乎感受不到,但对于cpu来说是一个很大的问题。至于到底该跳不该跳,有没人发表意见,排除强制分支预测方案,而用预定的cpu分支预测方案,
则应该是: 跳,效率更高,还是不跳高?
综合上面我说的,答案其实很清楚是 。。。。  

没有跳转最好。。。
如果有条件跳转,则尽量安排代码为有效跳转,让跳转执行,别让跳转只是路过


那电子书的页码到底怎么标的,  看不明白,我认为的第34页怎么是 adc 指令介绍那页

[ 本帖最后由 PctGL 于 2009-2-5 23:43 编辑 ]
回复 支持 反对

使用道具 举报

 楼主| 发表于 2009-2-5 20:59:59 | 显示全部楼层
》add一个时钟周期可以执行4个,inc只能有2个
fog.pdf最后有个指令表。注意Reciprocal throughput,add是0.25,即4个,inc是0.5,2个

至于电子书的页码..我用的PDF-XChange Viewer..不知道页码对不对..
回复 支持 反对

使用道具 举报

发表于 2009-2-5 21:29:40 | 显示全部楼层
。。。
写了一堆点了下清空内容。。。晕了。。。

我也就不写了,贴两个资料给你看吧

INC - Increment
        Usage:  INC     dest
        Modifies flags: AF OF PF SF ZF
        Adds one to destination unsigned binary operand.
                                 Clocks                 Size
        Operands         808x  286   386   486          Bytes
        reg8              3     2     2     1             2
        reg16             3     2     2     1             1

        reg32             3     2     2     1             1
        mem            15+EA    7     6     3            2-4  (W88=23+EA)

        FE /0 INC r/m8 Increment r/m byte by 1
        FF /0 INC r/m16 Increment r/m word by 1
        FF /0 INC r/m32 Increment r/m doubleword by 1

        40+ rw INC r16 Increment word register by 1
        40+ rd INC r32 Increment doubleword register by 1




ADD - Arithmetic Addition
        Usage:  ADD     dest,src
        Modifies flags: AF CF OF PF SF ZF
        Adds "src" to "dest" and replacing the original contents of "dest".
        Both operands are binary.
                                 Clocks                 Size
        Operands         808x  286   386   486          Bytes
        reg,reg           3     2     2     1             2

        mem,reg         16+EA   7     7     3            2-4  (W88=24+EA)
        reg,mem          9+EA   7     6     2            2-4  (W88=13+EA)
        reg,immed         4     3     2     1            3-4
        mem,immed       17+EA   7     7     3            3-6  (W88=23+EA)

        accum,immed       4     3     2     1            2-3

        04 ib ADD AL, imm8 Add imm8 to AL
        05 iw ADD AX, imm16 Add imm16 to AX
        05 id ADD EAX, imm32 Add imm32 to EAX
        80 /0 ib ADD r/m8,imm8 Add imm8 to r/m8
        81 /0 iw ADD r/m16,imm16 Add imm16 to r/m16

        81 /0 id ADD r/m32,imm32 Add imm32 to r/m32
        83 /0 ib ADD r/m16,imm8 Add sign-extended imm8 to r/m16
        83 /0 ib ADD r/m32,imm8 Add sign-extended imm8 to r/m32
        00 / r ADD r/m8,r8 Add r8 to r/m8
        01 / r ADD r/m16,r16 Add r16 to r/m16

        01 / r ADD r/m32,r32 Add r32 to r/m32
        02 / r ADD r8,r/m8 Add r/m8 to r8
        03 / r ADD r16,r/m16 Add r/m16 to r16
        03 / r ADD r32,r/m32 Add r/m32 to r32
回复 支持 反对

使用道具 举报

发表于 2009-2-5 21:36:43 | 显示全部楼层
现在不流行优化了吧.
我就用VB,慢慢的运行多爽
回复 支持 反对

使用道具 举报

 楼主| 发表于 2009-2-5 21:37:31 | 显示全部楼层
..这个我也有..

还是要听官方的..
回复 支持 反对

使用道具 举报

发表于 2009-2-5 22:05:05 | 显示全部楼层
不是听不听官方的,你看的不对
inc 是执行速度最快的指令之一了,他快到,在一个时钟周期内,就能完成,把时钟周期比做 1 ,inc 可能只用了 0.7 的时间,但剩余的 0.3 的时间是无法继续使用的,只能浪费掉。
也就是说1个时钟周期只能完成一个指令,当然这是理论上的。。。 具体cpu是不是那么干的,看什么书也不管用,也不好测试,因为太快了。
add 只用不到1个时钟周期不是不可能,因为到了80486的时候,intel对各种整数部运算指令的优化已经可以说是很完美了,只能这样说,add 快,inc 会更快,而且他们对不同的操作器操作也会有不同的时间消耗,对寄存器和对内存的时间消耗就肯定不一样。你还可以参考一下微指令那部分的说明,我也跟不上时代了,那个电子书上说的是最新的cpu架构,而我还停留在486阶段。。。 也许intel真就做到了add 比 inc 快
你所说的那部分不是cpu时钟周期,那部分说明的有可能是某个指令的效率,应该多参照下 uops

[ 本帖最后由 PctGL 于 2009-2-5 22:08 编辑 ]
回复 支持 反对

使用道具 举报

 楼主| 发表于 2009-2-5 22:11:16 | 显示全部楼层
至于uops,add是一个,inc是2个

但是在add和inc的问题上就别争了.............
帮我看看代码吧..........
回复 支持 反对

使用道具 举报

发表于 2009-2-5 23:39:14 | 显示全部楼层
并非微指令少就快

代码我就不写了,胡说两句我的意见

lea reg,reg 速度没问题,但如果带运算,速度就差远了建议改成 add
push ebp,mov ebp,esp , 后可用 esp 做暂存,可以少 push mem , pop mem 这两项是体力活
add esi,1 建议改成 inc
32位汇编 mov dl,[esi+1] 这种代码 和 xor edx,edx mov edx,[esi+1] 比起来: 有时可能会更慢
注意操作的数据类型是有符号还是无符号整形,调整跳转指令
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

文字版|手机版|小黑屋|VBGood  

GMT+8, 2023-3-22 04:39

VB爱好者乐园(VBGood)
快速回复 返回顶部 返回列表