home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 2.ddi / MATHSRC.ZIP / POLY.CAS < prev    next >
Encoding:
Text File  |  1992-06-10  |  2.5 KB  |  94 lines

  1. /*------------------------------------------------------------------------
  2.  * filename - poly.cas
  3.  *
  4.  * function(s)
  5.  *        poly - generates a polynomial from arguments
  6.  *-----------------------------------------------------------------------*/
  7.  
  8. /*
  9.  *      C/C++ Run Time Library - Version 5.0
  10.  *
  11.  *      Copyright (c) 1987, 1992 by Borland International
  12.  *      All Rights Reserved.
  13.  *
  14.  */
  15.  
  16.  
  17. #pragma inline
  18. #include <asmrules.h>
  19.  
  20. #include <_math.h>
  21. #include <math.h>
  22. #include <errno.h>
  23.  
  24. /*--------------------------------------------------------------------------*
  25.  
  26. Name            poly - generates a polynomial from arguments
  27.  
  28. Usage           double  poly(double x, int n, double c []);
  29.  
  30. Prototype in    math.h
  31.  
  32. Description     poly  generates  a  polynomial  in  x,  of  degree  n, with
  33.                 coefficients c[0],  c[1], ..., c[n].  For example, if  n=4,
  34.                 the generated polynomial is
  35.  
  36.                         c[4].x^4 + c[3].x^3 + c[2].x^2] + c[1].x + c[0]
  37.  
  38.                 The polynomial is calculated using Horner's method:
  39.  
  40.                         polynom = (..((x.c[n] + c[n-1]).x + c[n-2])..).x + c[0]
  41.  
  42. Return value    poly returns the  value of the polynomial as  evaluated for
  43.                 the given x.
  44.                 If n < 0  then the result is a domain error.
  45.                 A range error occurs if the result exceeds double range.
  46.  
  47. *---------------------------------------------------------------------------*/
  48. #pragma warn -rvl
  49. #pragma warn -use
  50. double  _FARFUNC poly (double x, int n, double c [])
  51. {
  52.         volatile unsigned    sw;
  53.  
  54. asm     FLD     DOUBLE (x)
  55. asm     mov     si, n
  56. asm     mov     cl, 3
  57. asm     shl     si, cl
  58. asm     or      si, si
  59. asm     jl      ply_domain
  60. asm     LES_    bx, c
  61. asm     FLD     DOUBLE (ES_ [bx+si])
  62. asm     jz      short   ply_end
  63.  
  64. ply_loop:
  65. asm     FMUL    ST, ST(1)
  66. asm     sub     si, 8
  67. asm     FADD    DOUBLE (ES_ [bx+si])
  68. asm     jg      ply_loop
  69.  
  70. ply_end:
  71. asm     FXAM
  72. asm     FSTSW   W0 (sw)
  73. asm     FSTP    ST(1)                   /* discard ST(1) */
  74. asm     mov     ax, sw
  75. asm     sahf
  76. asm     jc      ply_range
  77.         return;
  78.  
  79. ply_domain:
  80. asm     mov     si, DOMAIN
  81. asm     jmp     short   ply_err
  82.  
  83. ply_range:
  84. asm     mov     si, OVERFLOW
  85.  
  86. ply_err:
  87. asm     FSTP    ST(0)                   /* discard ST */
  88. #pragma warn -ret
  89.         return  _matherr (_SI, "poly", &x, c, HUGE_VAL);
  90. #pragma warn .ret
  91. }
  92. #pragma warn .rvl
  93. #pragma warn .use
  94.