/*
                                 CMult.c

  An instructional example of a C FUNCTION that can be used by True BASIC.
  
  INPUT ARGUMENTS:
  
     Two strings containing IEEE format double precision numbers

     
  RETURN VALUE:
  
     Multiplied value returned as a string containing IEEE format

 Written using SAS/C, version 5.10B on an Amiga 3000
 Refer to lmkfile in this drawer for compiler options used.
 ====================================================================== */

#include <exec/types.h>
#include <proto/exec.h>
#include <proto/mathieeedoubbas.h>

#include "True BASIC:Assembly/TB.h"


/* ==================================================== */
/* === Declare math library pointer as SHARED value === */
/* === accessible from any function in this module, === */
/* ===     but not accessible from the outside.     === */
/* ==================================================== */
static struct Library *MathIeeeDoubBasBase;


/* ============================================ */
/* === ANSI function prototype declarations === */
/* ============================================ */
VOID CMult(LONG **arg, struct TBUserVector *uv);


VOID CMult(LONG **arg, struct TBUserVector *uv)
{

   /* ============================================== */
   /* === Pointers to hold addresses of argument === */
   /* ===    structures passed and returned.     === */
   /* ============================================== */
   struct TBString *in_one, *in_two, *out;
   

   /* =========================================== */
   /* === Pointers to numerical value members === */
   /* ===   of arguments structures passed.   === */
   /* =========================================== */
   DOUBLE numeric_one, numeric_two, *numeric_out;


   /* ==================================== */
   /* === Point to argument structures === */
   /* ===    passed from True BASIC    === */
   /* ==================================== */
   in_one = (struct TBString *) **arg--;
   in_two = (struct TBString *) **arg--;
   out = (struct TBString *) **arg;


   /* ============================================ */
   /* === Point to numeric members of argument === */
   /* ===  structures passed from True BASIC   === */
   /* ============================================ */
   numeric_one = *((double *)in_one->text);
   numeric_two = *((double *)in_two->text);
   numeric_out = (double *)out->text;
   
   
   /* ============================================= */
   /* === Test for proper length of ieee format === */
   /* ===  and get out if there is a problem    === */
   /* ============================================= */
   if(in_one->length != 8 || in_two->length != 8 || out->length != 8)
   {
      if(out->length == 8)
      {
         *numeric_out = (DOUBLE)0.0;
         return;
      }
      else
         return;
   }


   /* ============================================== */
   /* === Open the Amiga's IEEE double precision === */ 
   /* ===      math floating point library       === */
   /* ============================================== */
   if((MathIeeeDoubBasBase =
      (struct Library *)OpenLibrary("mathieeedoubbas.library",0)))
      {
         
         /* ===================================================== */
         /* === Perform actual multiplication by calling the  === */ 
         /* ===    multiply function in the Amiga's IEEE      === */
         /* === double precision floating point math library. === */
         /* ===================================================== */
         *numeric_out = IEEEDPMul(numeric_one, numeric_two);


         /* ===================================== */
         /* === Close the math library before === */
         /* ===   returning to True BASIC.    === */
         /* ===================================== */
         CloseLibrary(MathIeeeDoubBasBase);
                  
      }
   else
      {
         
         /* ========================================== */
         /* ===  The math library failed to open.  === */
         /* === Return a zero value to True BASIC. === */
         /* ========================================== */
         *numeric_out = 0.0;         
      }
      
   return;
       
}  /* End of MyMult() */
