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


Groups > comp.lang.postscript > #1151

Re: Calculate a 16-bit Carry Flag?

From "luser.droog" <luser.droog@gmail.com>
Newsgroups comp.lang.postscript
Subject Re: Calculate a 16-bit Carry Flag?
Date 2012-12-05 23:34 -0600
Organization unorganized
Message-ID <k9paps$nkv$1@dont-email.me> (permalink)
References <k9a31h$ngc$1@dont-email.me> <yNGdnRpPq9tZeyXNnZ2dnUVZ_tSdnZ2d@posted.nuvoxcommunications> <k9bbcb$l24$1@dont-email.me> <JcCdnf5qr67SbCfNnZ2dnUVZ_sSdnZ2d@posted.nuvoxcommunications> <k9md6l$m41$1@dont-email.me>

Show all headers | View raw


Paul Vojta wrote:

> In article <JcCdnf5qr67SbCfNnZ2dnUVZ_sSdnZ2d@posted.nuvoxcommunications>,
> Robert Bonomi <bonomi@host122.r-bonomi.com> wrote:
>>define what *you* mean by 'carry' and 'overflow' :)
>>
>>from my _distant_ memories, overflow is set on mul/div -- you can't get
>>meaningful (even partial) results there.
>>'carry' is set for add/sub  -- only one excess/'lost' bit is possible,
>>and you do multiple-precision is done by adding the 'carry' bit as a LSB
>>to the 'next more significant' chunk of th operand.
>>
>>*dim* recollection of implementation details:
>> a) 'carry' set on =signed= result that carries into sign bit (only).
>>    Or unsigned carry into next higher bit.
>> b) 'overflow' set if anything propagates -past- the carry position
>> c) 'overflow' set implies 'carry' set.
> 
> Dim, indeed.
> 
> On the 8086 family, arithmetic is two's complement, and there are no
> separate "signed" or "unsigned" addition instructions.
> 
> Carry is set if the operation carries past the most significant bit.
> (In other words, interpreting the bits as unsigned integers, carry for
> 16-bit addition is set if and only if the sum is >= 2^{16}.  For 16-bit
> subtraction a-b, carry is set if and only if b>a as unsigned quantities.)
> This way, you can do addition of (say) 32-bit integers on a 16-bit machine
> by doing a 16-bit add on the lowest significant word, and then a 16-bit
> ADC (add with carry) on the corresponding higher-significant words.
> 
> Overflow is set if the operation leads to an overflow when considering the
> operands as signed quantities.  So, for addition, if you add two words
> with the same sign bit and the sum has a different sign bit, then you get
> overflow;
> otherwise you don't.  For subtraction, it's a bit more complicated:
> for a-b, if a>0, b<0, and the sign bit of the result is set, then you get
> overflow; if a<0, b>0, and the sign bit of the result is not set, then you
> get overflow; otherwise you don't.
> 
> Overflow does not imply carry set.  For example, 0x4000+0x4000 gives
> 0x8000 with the overflow bit set but no carry bit.
> 

Now that's a very nice explanation, thank you. Examples, reasoning.

>>Aside -- beware of gotcha on 'complement and add'. can't complement a
>>'maximum negative' operand.  If 1st operand not 'maximum negative', then
>>complement it, else if 2nd operand not 'maximum negative', then complement
>>it, else (both are 'maximum negative'), SHORTCUT: result is =zero=.
> 
> Complement and add only works in one's complement arithmetic.  For two's
> complement (as in most modern processors, including the 8086), you need
> to complement and increment to turn a (signed) word w into -w.  That will
> not simplify things if you are emulating a processor.
> 
> --Paul

Actually this part seems irrelevant for me. I had already
interpreted "complement" as 2's-complement. And since ps integers are
32-bits, I can go ahead and negate the "maximum negative" 16-bit quantity.
Cause it still fits in 32bits!

Stealing from the C code led me to this set of procs for setting the flags.
And that part appears to run the test program. (But it's stuck in a loop
somewhere. So flags could still ba at issue. )

/setZF { dup 0 eq {/ZF 1 def}{/ZF 0 def} ifelse } def
/setSF { dup {-7 -15}W get bitshift 1 and /SF exch def } def
/setF { setZF setSF } def
/logF { setF /CF 0 def /OF 0 def } def %logical operators clear OF+CF

/nz1 { 0 ne{1}{0}ifelse } def %nonzero to 1
/arithF {
    /CF c {16#ff00 16#ffff0000}W get and nz1 def
    /OF c a xor c b xor and {16#80 16#8000}W get and nz1 def
    /AF a b xor c xor 16#10 and nz1 def
} def
/ADD {
    debug {()= pstack} if
    {b a}{exch def}forall
    /c a b add def
    c arithF setF
} def
/SUB {
    D 1 xor {exch}repeat
    {b a}{exch def}forall
    /c a b sub def
    c arithF setF
} def
/SBB {
    D 1 xor {exch}repeat
    {b a}{exch def}forall
    /c a b CF add sub def
    c arithF setF
} def

Back to comp.lang.postscript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Calculate a 16-bit Carry Flag? "luser.droog" <luser.droog@gmail.com> - 2012-11-30 04:50 -0600
  Re: Calculate a 16-bit Carry Flag? bonomi@host122.r-bonomi.com (Robert Bonomi) - 2012-11-30 11:03 -0600
    Re: Calculate a 16-bit Carry Flag? "luser.droog" <luser.droog@gmail.com> - 2012-11-30 16:18 -0600
      Re: Calculate a 16-bit Carry Flag? bonomi@host122.r-bonomi.com (Robert Bonomi) - 2012-12-02 00:12 -0600
        Re: Calculate a 16-bit Carry Flag? "luser.droog" <luser.droog@gmail.com> - 2012-12-03 00:06 -0600
        Re: Calculate a 16-bit Carry Flag? vojta@math.berkeley.edu (Paul Vojta) - 2012-12-05 02:57 +0000
          Re: Calculate a 16-bit Carry Flag? "luser.droog" <luser.droog@gmail.com> - 2012-12-05 23:34 -0600
            Re: Calculate a 16-bit Carry Flag? Bogus@Embarq.com (Steve) - 2012-12-07 15:59 +0000

csiph-web