Re: Arithmetic rounding

From: [at]} <robert.d.campbell{>
Date: Wed, 19 Jun 2002 10:20:03 +0000 (GMT)

----boundary-LibPST-iamunique-327819255_-_-
Content-type: text/plain

>As far as I know, the GSL (Gnu
>Scientific Library) provides full support of the rounding
>options of the FPU.
>
>You might check their C code to get some clues.

Bernhard


Thanks for the clues. I have found some nice test examples
which (hopefully) will be useful.

The required control words are also clearly documented:

/* Here is the dirty part. Set up your 387 through the control word
* (cw) register.
*
* 15-13 12 11-10 9-8 7-6 5 4 3 2 1 0
* | reserved | IC | RC | PC | reserved | PM | UM | OM | ZM | DM | IM
*
* IM: Invalid operation mask
* DM: Denormalized operand mask
* ZM: Zero-divide mask
* OM: Overflow mask
* UM: Underflow mask
* PM: Precision (inexact result) mask
*
* Mask bit is 1 means no interrupt.
*
* PC: Precision control
* 11 - round to extended precision
* 10 - round to double precision
* 00 - round to single precision
*
* RC: Rounding control
* 00 - rounding to nearest
* 01 - rounding down (toward - infinity)
* 10 - rounding up (toward + infinity)
* 11 - rounding toward zero
*
* IC: Infinity control
* That is for 8087 and 80287 only.
*
* The hardware default is 0x037f which we use.
*/

I have also found the C code to do it:

/* Macros for accessing the hardware control word. */
#define _FPU_GETCW(cw) __asm__ ("fnstcw %0" : "=m" (*&cw))
#define _FPU_SETCW(cw) __asm__ ("fldcw %0" : : "m" (*&cw))

However I have never leaned to read (or write) C so I
do not know how to translate the two lines above. Any
ideas anyone ?


The example below is the kind of thing I am hoping to end up
with.

PROCEDURE [code] MultMod* (a, b, m : INTEGER) : INTEGER
  05AH, (* pop b -> edx *)
  059H, (* pop m -> ecx *)
  0F7H, 0EAH, (* edx:eax := eax * edx = a * b *)
  0F7H, 0F9H, (* eax := edx:eax / ecx = a * b DIV m *)
                (* edx = a * b MOD m *)
  08BH, 0C2H; (* move eax, edx edx -> eax *)

(*
PROCEDURE MultMod* (a, b, m : INTEGER) : INTEGER;
  BEGIN
    RETURN SHORT (LONG (a) * LONG (b) MOD LONG (m))
  END MultMod;
*)


Thanks

Robert Campbell.



--------------------------------------------

To unsubscribe from this mailing list, send a message containing the word "unsubscribe" to:
   blackbox-request{([at]})nowhere.xy

To get a list of valid e-mail commands and instructions on their usage, send a message containing the word "help" to the above address.

Send any problem reports or questions related to this email list to the list owner at

   owner-blackbox{([at]})nowhere.xy

Current posting policy:

a) To post you should use the same address by which you are subscribed to the mailing list. That way, the list server will recognize you as subscriber and forward your posting immediately, without creating any overhead.

b) If, for some reason, you cannot post from the address, by which you are subscribed, your message will be moderated to avoid spam. Please understand that moderation will often cause some delay, in particular over weekends or holydays.


----boundary-LibPST-iamunique-327819255_-_-
Content-type: application/rtf
Content-transfer-encoding: base64
Content-Disposition: attachment; filename="rtf-body.rtf"

e1xydGYxXGFuc2lcYW5zaWNwZzEyNTJcZnJvbXRleHQgXGRlZmYwe1xmb250dGJsDQp7XGYwXGZz
d2lzcyBBcmlhbDt9DQp7XGYxXGZtb2Rlcm4gQ291cmllciBOZXc7fQ0Ke1xmMlxmbmlsXGZjaGFy
c2V0MiBTeW1ib2w7fQ0Ke1xmM1xmbW9kZXJuXGZjaGFyc2V0MCBDb3VyaWVyIE5ldzt9fQ0Ke1xj
b2xvcnRibFxyZWQwXGdyZWVuMFxibHVlMDtccmVkMFxncmVlbjBcYmx1ZTI1NTt9DQpcdWMxXHBh
cmRccGxhaW5cZGVmdGFiMzYwIFxmMFxmczIwID5BcyBmYXIgYXMgSSBrbm93LCB0aGUgR1NMIChH
bnVccGFyDQo+U2NpZW50aWZpYyBMaWJyYXJ5KSBwcm92aWRlcyBmdWxsIHN1cHBvcnQgb2YgdGhl
IHJvdW5kaW5nXHBhcg0KPm9wdGlvbnMgb2YgdGhlIEZQVS5ccGFyDQo+XHBhcg0KPllvdSBtaWdo
dCBjaGVjayB0aGVpciBDIGNvZGUgdG8gZ2V0IHNvbWUgY2x1ZXMuXHBhcg0KXHBhcg0KQmVybmhh
cmRccGFyDQpccGFyDQpccGFyDQpUaGFua3MgZm9yIHRoZSBjbHVlc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HRoYXQgbW9kZXJhdGlvbiB3aWxsIG9mdGVuIGNhdXNlIHNv
bWUgZGVsYXksIGluIHBhcnRpY3VsYXIgb3ZlciB3ZWVrZW5kcyBvciBob2x5ZGF5fX0AcyBhbnlv



----boundary-LibPST-iamunique-327819255_-_---
Received on Wed Jun 19 2002 - 12:20:03 UTC

This archive was generated by hypermail 2.3.0 : Thu Sep 26 2013 - 06:29:42 UTC