Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #18917

Re: ANN: All FIPS 180-4 Secure Hash Algorithms in ANS Forth

From mhx@iae.nl (Marcel Hendrix)
Subject Re: ANN: All FIPS 180-4 Secure Hash Algorithms in ANS Forth
Newsgroups comp.lang.forth
Message-ID <88799515028434@frunobulax.edu> (permalink)
Date 2013-01-20 08:56 +0200
References <6944e941-c6bc-4c82-9d8c-44dd7beda849@c14g2000vbd.googlegroups.com>
Organization Wanadoo

Show all headers | View raw


Alex McDonald <blog@rivadpm.com> writes Re: ANN: All FIPS 180-4 Secure Hash Algorithms in ANS Forth

> On Jan 19, 7:14am, m...@iae.nl (Marcel Hendrix) wrote:
>> I have never understood why push and pop are slower than memory
>> references on Intel chips. It must be something with changing the
>> stack pointer, because other languages use the stack all of the
>> time for their locals. However, if it were that, it would suffice
>> to use e.g. rbp for the Forth stack. I have never seen reports
>> that that is fundamentally faster then using rsp.

> I use EBP for the stack in my Forth. It's marginally faster (in a
>micro benchmark on an i7) than using ESP; and a lot faster than using
>ESI and STOS/LODS. The code is, however, a lot larger.

To make this more concrete, I wrote a small program and ran the tests:

FORTH> bench ( i7 2,66 GHz)
direct : 1.800 seconds elapsed.
null   : 0.601 seconds elapsed.
inc    : 1.726 seconds elapsed.
xchg   : 4.533 seconds elapsed.
rsp    : 1.804 seconds elapsed.
rbp    : 1.807 seconds elapsed. ok

Here "direct" copies 12 memory locations 1 cell up (stack without a 
stack pointer), "rsp" tests stack pushes on the user return stack,
"rbp" tests stack pushes on a stack indexed by RBP, "null" is a 
reference for how long it takes to execute 5,120,000,000 nop opcodes,
and "xchg" executes 5,120,000,000 xchg opcodes.

Conclusions: 
  1) it doesn't matter (for speed) if rsp or rbp is 
     used to build a stack.
  2) Memory R/W is exactly the same as stack R/W
  3) A push is as fast as a simple register opcode (inc)
  4) A nop is very fast (I believe it is optimized away in the CPU)
  5) A simple xchg of two registers (in 64 bit mode) is very slow.

It might even follow that there is no advantage in using registers
over directly adressed memory locations...

Conclusion 5) is not relevant to the discussion, I just mention it 
as an interesting experimental finding. (An assembler should provide 
a bit-trick replacement.)

-marcel

-- ------------
NEEDS -assemble

ANEW -testreg

#512 =: /ss
CREATE stack /ss CELLS ALLOT stack /ss CELLS ERASE
stack /ss CELLS + =: start

ALSO ASSEMBLER
: many_nop,  ( -- ) /ss 0 ?DO   nop,             LOOP ;
: many_ex,   ( -- ) /ss 0 ?DO   rax -> rax xchg, LOOP ;
: many_inc,  ( -- ) /ss 0 ?DO   rax inc,         LOOP ;
: rax_push0, ( -- ) /ss 0 ?DO   [rcx   I 1+ cells +] qword -> rax mov,  rax -> [rcx I CELLS +] qword mov,  LOOP ;
: rax_push1, ( -- ) /ss 0 ?DO   rax -> push,  LOOP ;
: rax_push2, ( -- ) /ss 0 ?DO   rax -> [rbp] mov,   [rbp -1 cells +] -> rbp lea,  LOOP ;
PREVIOUS

CODE test_direct ( a -- )
	stack q# -> rcx mov,
	  rax_push0, 
	rax pop, rax -> [rcx /ss 1- cells +] qword mov,
	rbx jmp,
END-CODE

CODE test_null ( a -- )
	rax pop,
	rsp -> rcx mov, start q# -> rsp mov,
	  many_nop, 
	rcx -> rsp mov,
	rbx jmp,
END-CODE

CODE test_ex ( a -- )
	rax pop,
	rsp -> rcx mov, start q# -> rsp mov,
	  many_ex, 
	rcx -> rsp mov,
	rbx jmp,
END-CODE

CODE test_inc ( a -- )
	rax pop,
	  many_inc, 
	rbx jmp,
END-CODE

CODE test_rsp ( a -- )
	rax pop,
	rsp -> rcx mov, start q# -> rsp mov,
	  rax_push1, 
	rcx -> rsp mov,
	rbx jmp,
END-CODE

CODE test_rbp ( a -- )
	rax pop,
	rbp -> rcx mov, start q# -> rbp mov,
	  rax_push2,
	rcx -> rbp mov,
	rbx jmp,
END-CODE


#10000000 VALUE #times
: bench	CR ." direct : " TIMER-RESET #times 0 DO  I test_direct  LOOP .ELAPSED 
	CR ." null   : " TIMER-RESET #times 0 DO  I test_null    LOOP .ELAPSED 
	CR ." inc    : " TIMER-RESET #times 0 DO  I test_inc     LOOP .ELAPSED 
	CR ." xchg   : " TIMER-RESET #times 0 DO  I test_ex      LOOP .ELAPSED 
	CR ." rsp    : " TIMER-RESET #times 0 DO  I test_rsp     LOOP .ELAPSED 
	CR ." rbp    : " TIMER-RESET #times 0 DO  I test_rbp     LOOP .ELAPSED ;

Back to comp.lang.forth | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

ANN: All FIPS 180-4 Secure Hash Algorithms in ANS Forth jzakiya@gmail.com - 2013-01-15 21:31 -0800
  Re: ANN: All FIPS 180-4 Secure Hash Algorithms in ANS Forth jzakiya@gmail.com - 2013-01-15 21:57 -0800
    Re: ANN: All FIPS 180-4 Secure Hash Algorithms in ANS Forth jzakiya@gmail.com - 2013-01-26 10:01 -0800
  Re: ANN: All FIPS 180-4 Secure Hash Algorithms in ANS Forth Bernd Paysan <bernd.paysan@gmx.de> - 2013-01-16 15:25 +0100
    Re: ANN: All FIPS 180-4 Secure Hash Algorithms in ANS Forth jzakiya@gmail.com - 2013-01-16 09:45 -0800
      Re: ANN: All FIPS 180-4 Secure Hash Algorithms in ANS Forth Bernd Paysan <bernd.paysan@gmx.de> - 2013-01-16 19:08 +0100
        Re: ANN: All FIPS 180-4 Secure Hash Algorithms in ANS Forth jzakiya@gmail.com - 2013-01-16 10:32 -0800
          Re: ANN: All FIPS 180-4 Secure Hash Algorithms in ANS Forth Bernd Paysan <bernd.paysan@gmx.de> - 2013-01-16 23:33 +0100
            Re: ANN: All FIPS 180-4 Secure Hash Algorithms in ANS Forth Alex McDonald <blog@rivadpm.com> - 2013-01-16 14:53 -0800
              Re: ANN: All FIPS 180-4 Secure Hash Algorithms in ANS Forth jzakiya@gmail.com - 2013-01-16 20:40 -0800
                Re: ANN: All FIPS 180-4 Secure Hash Algorithms in ANS Forth mhx@iae.nl (Marcel Hendrix) - 2013-01-17 22:36 +0200
                Re: ANN: All FIPS 180-4 Secure Hash Algorithms in ANS Forth jzakiya@gmail.com - 2013-01-17 22:24 -0800
                Re: ANN: All FIPS 180-4 Secure Hash Algorithms in ANS Forth jzakiya@gmail.com - 2013-01-17 22:46 -0800
                Re: ANN: All FIPS 180-4 Secure Hash Algorithms in ANS Forth mhx@iae.nl (Marcel Hendrix) - 2013-01-19 09:14 +0200
                Re: ANN: All FIPS 180-4 Secure Hash Algorithms in ANS Forth Alex McDonald <blog@rivadpm.com> - 2013-01-19 08:24 -0800
                Re: ANN: All FIPS 180-4 Secure Hash Algorithms in ANS Forth jzakiya@gmail.com - 2013-01-19 18:10 -0800
                Re: ANN: All FIPS 180-4 Secure Hash Algorithms in ANS Forth mhx@iae.nl (Marcel Hendrix) - 2013-01-20 08:56 +0200
                Re: ANN: All FIPS 180-4 Secure Hash Algorithms in ANS Forth Bernd Paysan <bernd.paysan@gmx.de> - 2013-01-20 14:38 +0100
                Re: ANN: All FIPS 180-4 Secure Hash Algorithms in ANS Forth mhx@iae.nl (Marcel Hendrix) - 2013-01-20 16:25 +0200
                Re: ANN: All FIPS 180-4 Secure Hash Algorithms in ANS Forth Alex McDonald <blog@rivadpm.com> - 2013-01-20 11:44 -0800
                Re: ANN: All FIPS 180-4 Secure Hash Algorithms in ANS Forth anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2013-01-21 17:22 +0000
                Re: ANN: All FIPS 180-4 Secure Hash Algorithms in ANS Forth mhx@iae.nl (Marcel Hendrix) - 2013-01-21 22:51 +0200
                Re: ANN: All FIPS 180-4 Secure Hash Algorithms in ANS Forth anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2013-01-22 17:26 +0000
        Re: ANN: All FIPS 180-4 Secure Hash Algorithms in ANS Forth anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2013-01-18 16:50 +0000
    Re: ANN: All FIPS 180-4 Secure Hash Algorithms in ANS Forth mhx@iae.nl (Marcel Hendrix) - 2013-01-16 19:53 +0200
      Re: ANN: All FIPS 180-4 Secure Hash Algorithms in ANS Forth Bernd Paysan <bernd.paysan@gmx.de> - 2013-01-16 23:29 +0100

csiph-web