Jump to content
IGNORED

Awesome paper on computational math


solidcorp

Recommended Posts

This is an awesome paper on computational math http://www.jjj.de/fxt/fxtbook.pdf

 

Matters Computational

Ideas, Algorithms, Source Code

 

In depth explanations of various computational and math techniques, tricks, and gotchas. The first section deals with integers and binary and is totally relevant to 6502 programming. It's a great reference, getting into details of various math operations with an eye on pitfalls and optimization.

 

( Thinking of you [and DiMath] when I posted this Selgus! ;) )

  • Like 4
Link to comment
Share on other sites

Right off the bat (section 1.1.4) I found this 2's compliment signed integer gotcha - it's an oldie but a goodie!

 

if ( x<0 ) x = -x;
// assume x positive here (WRONG!)

 

This is because there are two numbers in the set of base two integers of a given size that are their own negative: 0 and 2^(bitSizeOfType). i.e. To negate a signed two's compliment integer, you toggle or flip all the bits and increment the number, considering the byte -128 (0x80): 10000000 toggled 01111111 incremented 10000000 => thus, it's its own negative.

 

The paper looks like it has all the bit and math tricks I know and much much more.

  • Like 1
Link to comment
Share on other sites

  • 3 weeks later...

I'm not sure where my copy of this book is right now. I think in a box but Hacker's Delight by Henry S. Warren was indeed a delight!

 

http://www.amazon.co...n/dp/0201914654

 

 

Looking at the Matters Computational book there is some overlap but the scope of Matters Computational is huge! nice.

 

Also saw that there was his home page http://www.jjj.de/

and a page on the book with more good stuff. This is really a gold mine and yes, I knew much of this already but it's pretty neat to see it collated and presented.

 

http://www.jjj.de/fx...ge.html#fxtbook

 

I have to admit I started to get overwhelmed because you referred to it as a paper. This BOOK is free but it's also available in print:

http://www.amazon.co...e/dp/3642147631

Edited by djmips
Link to comment
Share on other sites

Seems like a good book (the author is a German ;)). I liked the bit manipulation tricks he described, some are even new to me and could be very helpful eventually.

 

But I couldn't find the "double XOR trick" as I call it. It is useful when you only want to exchange some bits of a byte with the bits of another byte.

 

Instead of:

; replace the lower 4 bits of var1 with the lower 4 bits of var2 (without changing the upper 4 bits of var1)
lda var2
and #%1111
sta temp
lda var1
and #~%1111
ora temp
sta var1

you can do:

lda var1
eor var2
and #~%1111
eor var2
sta var1

Link to comment
Share on other sites

Seems like a good book (the author is a German ). I liked the bit manipulation tricks he described, some are even new to me and could be very helpful eventually.

 

But I couldn't find the "double XOR trick" as I call it. It is useful when you only want to exchange some bits of a byte with the bits of another byte.

 

 

The following is equivalent to your code

 

; replace the lower 4 bits of var1 with the lower 4 bits of var2 (without changing the upper 4 bits of var1)
lda var1
eor var2
and #%1111
eor var1
sta var1

 

 

and doing it that way means you could also

; swap the lower 4 bits of var1 with the lower 4 bits of var2 (without changing the upper 4 bits of var1 or var2)
lda var1
eor var2
and #%1111
tax
eor var1
sta var1
txa
eor var2
sta var2

 

 

And that technique is covered in Hacker's Delight with three variations. The variation I used was ©

post-4397-0-46735100-1359633470_thumb.png

 

The nomenclature is the + in a circle is XOR ; three vertical bars is ! XOR; a bar over the mask is a NOT

  • Like 1
Link to comment
Share on other sites

Right off the bat (section 1.1.4) I found this 2's compliment signed integer gotcha - it's an oldie but a goodie!

 

if ( x<0 ) x = -x;
// assume x positive here (WRONG!)

 

This is because there are two numbers in the set of base two integers of a given size that are their own negative: 0 and 2^(bitSizeOfType). i.e. To negate a signed two's compliment integer, you toggle or flip all the bits and increment the number, considering the byte -128 (0x80): 10000000 toggled 01111111 incremented 10000000 => thus, it's its own negative.

 

The paper looks like it has all the bit and math tricks I know and much much more.

 

I've had this as a bug in the past. A good reminder.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...