Maybe its just the fact my head feels like mush at the moment, but i cant think of a nice algorithm that will calculate fractions for GDI displaying.

Im writing a Plot routine (x,y) to plot data points. If my Drawing Box is say 200x200 pixels, and i want to display UNSIGNED BYTES (max 255). I hit a limit at 200, where the below ratio == 1.

I *was* using this equation To determing the Data Height For the Plot:

Plot_y = DataPoint * ( Delta_Pixels_Y / MAX_Y_DATA )

The Problem is when MAX_Y_DATA gets larger than Delta_Pixels (Physical # of y pixels to use). When it does, it becomes ZERO (integer math), and a i get nothing displayed... :rolleyes:

I can not use the FPU or MMX to solve this. All i have to work with is the standard registers (including esi, edi, and ebx). As well, it should work for SIGNED numbers as well as UNSIGNED.

However, I *could* use the FPU on one condition: The FPU contents (what ever is in it) must be fully saved and restored.

This is something im not familiar with for PROTECTED mode. My FPU knowledge came from a 16 bit AoA.. :rolleyes:. I would imagine it would be easier to solve if i used the above equation on the FPU and casted back to an integer when done, but then again im unsure of the correct way of doing this as well.....

So in recap,
1) Anyone know of a good Integer method to handle fractions * integers that result > 0.
or
2) Anyone know of the Proper way of fully storing and restoring the FPU (including any status, and control bits)?


Thanx for any advice you can provide!
:alright:
NaN
:stupid:
Posted on 2002-05-31 02:47:53 by NaN
Here is a sample of what im talking about: The max in this case is 190. Here you see it as column 3. Every data point is being represented at a 1:1 ratio. If I add a data point 191 in the array, it will all go zero, as discussed above in my first post.
Posted on 2002-05-31 02:58:06 by NaN
You could use a log scale.

1000
100
10
1
Posted on 2002-05-31 03:10:47 by bdjames
Hi NaN, one trick to remember with computer maths is to leave division towards the end of a caluclation as this reduces precision errors.

This is espicially true of integer maths, so try the following simple change:

Plot_y = ( DataPoint * Delta_Pixels_Y ) / MAX_Y_DATA

As for 2) I don't know but would be interested to hear how its done.
Posted on 2002-05-31 05:15:55 by Eóin
Plot_y = ( DataPoint * Delta_Pixels_Y ) / MAX_Y_DATA

This works fine.
You can avoid the division loop with one of the two following tricks:

1) Use a precomputed table of division by MAX_Y_DATA.
In your case, if Delta_Pixels_Y and MAX_Y_DATA are given, you can use a precomputed table of Delta_Pixels_Y*MAX_Y_DATA.
Ex:
DataPoint is between 0 and 255.
DataTable[0] = 0*Delta_Pixels_Y / MAX_Y_DATA
DataTable[...]
DataTable[255] = 255*Delta_Pixels_Y / MAX_Y_DATA

Now, you can have Plot_Y in one lookup table:
DataTable

This is especially efficient if you have to display a HUGE WAVE sample.

2) Use a multiplication by the inverse.
Simply compute INTCOEF = 65536/MAX_Y_DATA.
Now, if you want to have the result of A/MAX_Y_DATA, simply:
(A *INTCOEF) / 65536

This method is efficient when you need to keep the fraction.

JC
Posted on 2002-05-31 06:45:04 by MCoder
Thanx for the suggestions so far...

Eoin, Yup, I changes the order of op's so as you suggested, and it now works to 255. Thanx. My head must have been mush not to have seen this.

bdjames, Sorry it wont protray the data fairly, as well, i would need to use the FPU to determine the log if i wanted to.. Thanx anyways.

MCoder, Your suggestions are good ones, but I failed to say that that all three terms can be changing, so its impractical to make static arrays.


While i got things padded out for this simple case.. there will be more complex cases coming.. It would be nice to know how to properly save and restore the FPU, if anywone knows how to? In the future i plan to suport Real4 and Real8 data sets, so weather i like it or not, i will eventually need to use the FPU...

Thanx again for your help everyone.
:alright:
NaN
Posted on 2002-05-31 10:21:20 by NaN
No one know how to safely store and restore the FPU's contents in Masm32??

Is it safe to use the same instructions as 16 bit FPU commands?

Thanx... NaN
Posted on 2002-05-31 16:32:46 by NaN

Is it safe to use the same instructions as 16 bit FPU commands?
Yes, 14.4.10.6 The FSAVE, FNSAVE, and FRSTOR Instructions.
Posted on 2002-05-31 16:59:13 by bitRAKE
Thanx for the Link bitRAKE... this is way better than the pdf's...

:alright:
NaN
Posted on 2002-05-31 18:21:16 by NaN
The FXSAVE and FXRSTORE instructions seem very fast for 512 bytes!
Posted on 2002-06-07 22:14:20 by bitRAKE
Im not familiar with these commands?? They are not FPU are they?
Posted on 2002-06-07 22:27:48 by NaN
It's actually like an FPU-SSE instruction. You can also use FSAVE/FNSAVE and FRSTOR. :)
Posted on 2002-06-07 23:20:21 by stryker