/*  -*- Mode: Asm -*-  */

/* Copyright (c) 2002  Michael Stumpf  <mistumpf@de.pepperl-fuchs.com>
   All rights reserved.


   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions are met:

   * Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.
   
   * Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in
     the documentation and/or other materials provided with the
     distribution.
     
   * Neither the name of the copyright holders nor the names of
     contributors may be used to endorse or promote products derived
     from this software without specific prior written permission.

   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   POSSIBILITY OF SUCH DAMAGE. 
*/

/* $Id: log.S,v 1.7.2.2 2005/12/11 19:40:01 aesok Exp $ */

/*
    log.S is part of     FPlib V 0.3.0       ported to avr-as
    for details see readme.fplib

 *----------------------------------------------------------------------------------------
 *
 *	A = log(A)
 *
 * log( x ) = exponent(x)*ln(2) + log(matissae(x))
 *
 *
 * x = [0.5,1.0[
 * z = (x*sqrt(2)-1)/(x*sqrt(2)+1) = (x - 1/sqrt(2)/(x+1/sqrt(2))
 * log( x ) =  ((F2 * z*z + F1)*z*z + F0)*z - ln(sqrt(2))
 *             P0 + x (P1 + x * (P2 + x * (P3 + x * (P4 + x * P5))))
 *          =  -----------------------------------------------------
 *             Q0 + x (Q1 + x * (Q2 + x * (Q3 + x * (Q4 + x * Q5))))
 * P0 = -3.42819 [C0 5B 67 82]
 * P1 = -7.05769 [C0 E1 D8 A1]
 * P2 = -20.5693 [C1 A4 8D D9]
 * P3 = 9.48417  [41 17 BF 24]
 * P4 = 6.09923  [40 C3 2C E2]
 * P5 = 15.4718  [41 77 8C 4F]
 * Q0 = 1        [3F 80 00 00]
 * Q1 = 7.07107  [40 E2 46 30]
 * Q2 = 20       [41 A0 00 00]
 * Q3 = 28.2843  [41 E2 46 32]
 * Q4 = 20       [41 A0 00 01]
 * Q5 = 5.65685  [40 B5 04 F4]
 */

#if !defined(__DOXYGEN__)

#include "gasava.inc"
#include "macros.inc"
#include "fplib.inc"

          TEXT_SEG(fplib, log)
          FUNCTION(log)

GLOBAL(log)
   SBRC  rA3,7
   RJMP  _U(__fp_nanEDOM)	; A < 0 argument range error
1:
   RCALL _U(__fp_split_a)
   TST   rA3
   BRNE  2f
   RJMP  _U(__fp_nanEDOM)	; A == 0 argument range error

2:
   PUSH  rS0
   PUSH  rS1
   PUSH  rS2
   PUSH  rS3
   MOV   rS3,rA3
   LDI   rA3,0x3F		; load 0x7E as exponent =>
   ANDI  rA2,0x7F		; 2^-1 * 1.m = [0.5..1.0[

   LDI     ZL,LOW(table_log)
   LDI     ZH,HIGH(table_log)
   RCALL   _U(__fp_powerseries)

   MOV    rT0,rS3
   MOV    rS3,rA3
   MOV    rS2,rA2
   MOV    rS1,rA1
   MOV    rS0,rA0
   MOV    rA0,rT0
   SUBI   rA0,0x7E
   SBC    rA1,rA1
   MOV    rA2,rA1
   MOV    rA3,rA1
   RCALL  _U(__floatsisf)
   LDI    rB3,0x3F
   LDI    rB2,0x31
   LDI    rB1,0x72
   LDI    rB0,0x18		; ln(2)
   RCALL  _U(__mulsf3)
   MOV    rB3,rS3
   MOV    rB2,rS2
   MOV    rB1,rS1
   MOV    rB0,rS0
   POP    rS3
   POP    rS2
   POP    rS1
   POP    rS0
   RJMP   _U(__addsf3)

          ENDFUNC

	PGM_SECTION

table_log:
   DCB 5		; no of table entries - 1 (preload value)
   DCB 0x40,0xB5,0x04,0xF4 /* Q5 = 5.65685   */
   DCB 0x41,0xA0,0x00,0x01 /* Q4 = 20        */
   DCB 0x41,0xE2,0x46,0x32 /* Q3 = 28.2843   */
   DCB 0x41,0xA0,0x00,0x00 /* Q2 = 20        */
   DCB 0x40,0xE2,0x46,0x30 /* Q1 = 7.07107   */
   DCB 0x3F,0x80,0x00,0x00 /* Q0 = 1         */
   DCB 5
   DCB 0x41,0x77,0x8C,0x4F /* P5 = 15.4718   */
   DCB 0x40,0xC3,0x2C,0xE2 /* P4 = 6.09923   */
   DCB 0x41,0x17,0xBF,0x24 /* P3 = 9.48417   */
   DCB 0xC1,0xA4,0x8D,0xD9 /* P2 = -20.5693  */
   DCB 0xC0,0xE1,0xD8,0xA1 /* P1 = -7.05769  */
   DCB 0xC0,0x5B,0x67,0x82 /* P0 = -3.42819  */

#endif /* not __DOXYGEN__ */
