The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: ninjarider on August 04, 2005, 12:42:16 PM

Title: math coprocessor
Post by: ninjarider on August 04, 2005, 12:42:16 PM
i thought there was a fpu tutorial in the tutorials that came with masm. i need to know what i need to do to set up the fpu, and if i actually need to. how to move numbers to the fpu. how to use the sine and cosine functions. and how to get my number back. i think it was in the hla thing that i read. some stuff about it.
Title: Re: math coprocessor
Post by: pro3carp3 on August 04, 2005, 12:57:17 PM
You might want to take a look here...

http://www.ray.masmcode.com/fpu.html
(http://www.ray.masmcode.com/fpu.html)

Title: Re: math coprocessor
Post by: raymond on August 04, 2005, 01:45:10 PM
Although the following link seems to be the same as the one reported above, the html encoding of pro3carp3's message seems to have been messed up and his link does not work. This one should.

http://www.ray.masmcode.com/fpu.html

Raymond
Title: Re: math coprocessor
Post by: ninjarider on August 04, 2005, 02:20:44 PM
ray. -i've been reading your tutorial and plan to finish it. (may take days). which is y i asked the question in the first place
Title: Re: math coprocessor
Post by: Mark Jones on August 04, 2005, 02:32:09 PM
"Days" is not fast enough?

::)
Title: Re: math coprocessor
Post by: ninjarider on August 04, 2005, 07:02:06 PM
yeah. but when i learn something new i like to learn all of it and understand all of it.
Title: Re: math coprocessor
Post by: Mark Jones on August 04, 2005, 10:30:26 PM
"Patience Daniel-san, patience..."  :bg
Title: Re: math coprocessor
Post by: ninjarider on August 05, 2005, 03:29:51 PM
since the program that im writing does a lot of fpu functions like sin, cos, add, multi, and divide functions. would i be able to scatter these commands around in the code so that i dont have to wait. and would the following code be a good idea.

fmul st, st(1)
...some code...
fwait
fdiv st, st(2)
...some code...
fwait
fsin
...some code...
fwait
fmul st, st(3)


question on fpu clock cycles
im looking at a chart that says

fsin        | 486 257-354 *

does that mean that the fsin can take anywere from 257 to 354 clock cycles
Title: Re: math coprocessor
Post by: raymond on August 05, 2005, 05:12:50 PM
First, you rarely need to use the fwait instruction with modern processors, although I still tend to use it myself as a precaution when storing data from the FPU to memory before using that data with the CPU.

And YES you can intersperse CPU instructions with FPU instructions and they will "normally" run concurrently. However, multiplications and divisions by the CPU and the FPU may use the same hardware and would thus wait on each other.

Another trick to improve speed is to exchange FPU registers to possibly run two FPU instructions concurrently when they don't use the same hardware. For example:

fmul st,st(2)
fxch st,st(3)
fadd st,st(1)
fsub st,st(4)


While the current content of st(0) would get multiplied by st(2), the current contents of st(3) and st(1) would get added and st(4) subtracted from the result well before the multiplication would get completed. You only have to remember that the result of the multiplication would be in st(3).

Quotedoes that mean that the fsin can take anywere from 257 to 354 clock cycles

The hardware is probably hardcoded with something like Taylor series to compute the transcendental functions. The time required would thus vary to reach the error margin based on the input to the function.

Raymond
Title: Re: math coprocessor
Post by: ninjarider on August 05, 2005, 06:37:05 PM
with the code that u have. the fxch would move the value of st(3) to st. so how would it not be using the same hardware.
when u say that if it doesn't use the same hardware. does each instruction use a diffrent set of hardware or are u talking about the register in which the data resides

some of the code to be translated
x = sin(cameraangle.y * pi / 180
y = sin(cameraangle.x * pi / 180
z = cos(cameraangle.y * pi / 180

with cameraposition
    glulookat .x, .y, .z, .x + x, .y + y, .z + z, 0, 1, 0
end with
Title: Re: math coprocessor
Post by: raymond on August 06, 2005, 04:29:51 AM
The FPU is a complex processor. For example, multiplications are performed in a separate section while additions are performed in another section. Once the data has been sent to the multiplier, it doesn't matter anymore where that data was taken. The "TOP" register can then be loaded with different data and fed to the adding section which will run independently.

Obviously, there isn't a separate section for each instruction. The transcendental instructions are most probably using also the multiplying section. Some of the more modern processors may also have more than one section of each type and be able to perform parallel computations. In addition, processors could be different from one manufacturer to another (Intel, AMD, etc).

Hardware experts may be able to explain this much better than I can. :eek

FYI: The description of the fsin instruction in the FPU tutorial suggested earlier contains an example of coding to obtain the sine of an angle expressed in degrees.

Raymond
Title: Re: math coprocessor
Post by: ninjarider on August 08, 2005, 01:40:49 PM
believe it or not it was a good explanation.
Title: Re: math coprocessor
Post by: Mark Jones on August 09, 2005, 05:56:22 AM
Raymond is the house ALU genius around here. :) Have you seen his fractal renderer yet? :U
Title: Re: math coprocessor
Post by: ninjarider on August 09, 2005, 12:39:21 PM
dont think i have
Title: Re: math coprocessor
Post by: raymond on August 09, 2005, 01:51:39 PM
If you are interested in fractals, you can get it from the bottom of the following page:

http://www.ray.masmcode.com/complex.html

Raymond
Title: Re: math coprocessor
Post by: ninjarider on August 09, 2005, 04:56:42 PM
ok. my understanding just changed about fractals. i think i had it confused with something else. i thought i was a way of increasing the accurasing for measuring maps. but that is really cool. i dont think i have enough brain cells left for it today thou.
Title: Re: math coprocessor
Post by: ninjarider on August 09, 2005, 06:11:59 PM
how many clock cycles does the sin and cos function take