Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Assembly x86 > Re: Any more gr...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 25 of 39 Topic 4612 of 4822
Post > Topic >>

Re: Any more graceful way to saturate adds?

by Terje Mathisen <spamtrap@[EMAIL PROTECTED] > Apr 18, 2008 at 11:21 AM

Danjel McGougan wrote:
> On 17 Apr, 20:14, Terje Mathisen  <spamt...@[EMAIL PROTECTED]
> wrote:
>> pete wrote:
>>>> Was Terje's solution:
>>>>   add dl,al     ; Carry set if it overflows!
>>>>   sbb al,al     ; AL = 0xff if overflow
>>>>   or dl,al      ; Turns any overflow into 0xff
>>>> ...the right one?  I see overflow handling but not underflow...
> [...]
>> Anyway, unless clamping is very common, it is hard to beat the branch
>> predictors!
>>
> 
> I don't think the 808x CPU has a branch predictor. I guess one could

Ouch, I forgot about the 8088/8086 target!

> just add up the cycles used for each instruction on such an old CPU
> and come up with the best performing code for various statistics of
> the data. It should be pretty much deterministic (does it even use
> cache memory?).

The only cache at all is a 6/8 byte prefetch queue for instruction 
bytes, but on 8088 that queue is very often empty since it has to 
compete with all memory accesses.

The performance is easy to calculate though:

Without taken branches:
4 cycles per byte touched, code & data.

Taken branches were something like 4-8 cycles afair, might be wrong.

  ; Input: AL = signed byte, DL = unsigned byte
  ; Output: AL unsigned clamped byte


  xor dx,dx
next:			; bytes
  mov dl,[bx+si]		; 2+1
  lodsb			; 1+1
  cbw			; 1	-128 to 127
  add ax,dx		; 2	-128 to 383
   js underflow		; 2
  test ah,ah		; 2
   jnz overflow		; 2
store:
  stosb			; 1+1	Store clamped result
  jmp next		; 2+t

This looks like 14 bytes total, i.e. about 56 cycles for a nonclamped 
result.

underflow:
  xor ax,ax		; 2
  stosb			; 1+1
  jmp next		; 2+t

Underflow adds a taken branch but get rid of 2 code bytes, so we're 
talking about +/- zero extra cost.

overflow:
  mov al,255		; 3
  stosb			; 1+1
  jmp next		; 2+t

Overflow requires 3 extra code bytes and a taken branch, i.e. 
approximately 16+ cycles.

If most samples turn out to be in range, then we should simplify the 
code to optimize this path even further:

  xor dx,dx
next:			; bytes
  mov dl,[bx+si]		; 2+1
  lodsb			; 1+1
  cbw			; 1	-128 to 127
  add ax,dx		; 2	-128 to 383
  test ah,0ffh
   jnz clamp
  stosb
  jmp next

clamp:
   js underflow
  mov al,255
  stosb
  jmp next

underflow:
  xor ax,ax
  stosb
  jmp next

Terje
-- 
- <Terje.Mathisen@[EMAIL PROTECTED]
>
"almost all programming can be viewed as an exercise in caching"
 




 39 Posts in Topic:
Any more graceful way to saturate adds?
Jim Leonard <spamtrap  2008-04-10 14:59:55 
Re: Any more graceful way to saturate adds?
(Dick Wesseling) <spam  2008-04-11 02:32:55 
Re: Any more graceful way to saturate adds?
Terje Mathisen <spamt  2008-04-11 09:41:42 
Re: Any more graceful way to saturate adds?
Jim Leonard <spamtrap  2008-04-11 11:05:19 
Re: Any more graceful way to saturate adds?
Terence <spamtrap@[EM  2008-04-11 16:40:10 
Re: Any more graceful way to saturate adds?
Terence <spamtrap@[EM  2008-04-11 23:28:22 
Re: Any more graceful way to saturate adds?
"Wolfgang Kern"  2008-04-12 12:51:54 
Re: Any more graceful way to saturate adds?
Robert Redelmeier <red  2008-04-12 19:29:18 
Re: Any more graceful way to saturate adds?
Richard Russell <spam  2008-04-12 13:52:03 
Re: Any more graceful way to saturate adds?
Terje Mathisen <spamt  2008-04-13 15:31:59 
Re: Any more graceful way to saturate adds?
Terence <spamtrap@[EM  2008-04-12 17:03:07 
Re: Any more graceful way to saturate adds?
"Rod Pemberton"  2008-04-12 23:06:56 
Re: Any more graceful way to saturate adds?
"Rod Pemberton"  2008-04-12 23:10:57 
Re: Any more graceful way to saturate adds?
Terje Mathisen <spamt  2008-04-13 15:00:06 
Re: Any more graceful way to saturate adds?
"Rod Pemberton"  2008-04-13 11:27:29 
Re: Any more graceful way to saturate adds?
"Alexei A. Frounze&q  2008-04-13 17:19:25 
Re: Any more graceful way to saturate adds?
"Rod Pemberton"  2008-04-14 02:41:38 
Re: Any more graceful way to saturate adds?
Jim Leonard <spamtrap  2008-04-14 06:58:12 
Re: Any more graceful way to saturate adds?
"Alexei A. Frounze&q  2008-04-14 03:39:12 
Re: Any more graceful way to saturate adds?
pete <spamtrap@[EMAIL   2008-04-15 05:11:19 
Re: Any more graceful way to saturate adds?
Jim Leonard <spamtrap  2008-04-15 07:30:34 
Re: Any more graceful way to saturate adds?
pete <spamtrap@[EMAIL   2008-04-17 17:05:43 
Re: Any more graceful way to saturate adds?
Terje Mathisen <spamt  2008-04-17 13:14:08 
Re: Any more graceful way to saturate adds?
Danjel McGougan <spam  2008-04-18 02:31:30 
Re: Any more graceful way to saturate adds?
Terje Mathisen <spamt  2008-04-18 11:21:01 
Re: Any more graceful way to saturate adds?
dave <spamtrap@[EMAIL   2008-04-18 13:57:11 
Re: Any more graceful way to saturate adds?
Jim Leonard <spamtrap  2008-04-21 07:23:39 
Re: Any more graceful way to saturate adds?
Jim Leonard <spamtrap  2008-04-21 07:33:56 
Re: Any more graceful way to saturate adds?
Esra Sdrawkcab <spamt  2008-04-21 20:07:15 
Re: Any more graceful way to saturate adds?
Terje Mathisen <spamt  2008-04-22 11:01:06 
Re: Any more graceful way to saturate adds?
Esra Sdrawkcab <spamt  2008-04-23 08:20:41 
Re: Any more graceful way to saturate adds?
"H. Peter Anvin"  2008-06-02 09:43:53 
Re: Any more graceful way to saturate adds?
Terje Mathisen <spamt  2008-06-02 19:53:52 
Re: Any more graceful way to saturate adds?
aku ankka <spamtrap@[  2008-04-22 00:38:05 
Re: Any more graceful way to saturate adds?
Terje Mathisen <spamt  2008-04-22 11:03:00 
Re: Any more graceful way to saturate adds?
aku ankka <spamtrap@[  2008-04-22 23:43:47 
Re: Any more graceful way to saturate adds?
Jim Leonard <spamtrap  2008-05-01 13:13:06 
Re: Any more graceful way to saturate adds?
Terje Mathisen <spamt  2008-05-02 09:11:22 
Re: Any more graceful way to saturate adds?
pete <spamtrap@[EMAIL   2008-05-02 04:38:05 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue Oct 7 12:36:45 CDT 2008.