Index: Makefile.am Index: devtools/Architecture.am --- devtools/Architecture.am +++ devtools/Architecture.am @@ -36,7 +36,7 @@ AVR_TARGET_ASFLAGS = <> AVR_INSTALL_DIR = <> -VPATH = $(top_srcdir)/libc/stdlib:$(top_srcdir)/libc/pmstring:$(top_srcdir)/libc/string:$(top_srcdir)/libc/misc:$(top_srcdir)/libc/stdio:$(top_srcdir)/libm/fplib +VPATH = $(top_srcdir)/libc/stdlib:$(top_srcdir)/libc/pmstring:$(top_srcdir)/libc/string:$(top_srcdir)/libc/misc:$(top_srcdir)/libc/stdio:$(top_srcdir)/libm/fplib:$(top_srcdir)/libm/fixlib if HAS_<> @@ -69,6 +69,11 @@ include $(top_srcdir)/libc/stdio/Rules.am include $(top_srcdir)/libm/fplib/Rules.am +if HAS_fixed_point +include $(top_srcdir)/libm/fixlib/Rules.am +avr_LIBRARIES += libfixm.a +endif + nodist_libc_a_SOURCES = \ $(stdlib_a_c_sources) \ $(stdlib_a_asm_sources) \ @@ -81,12 +86,18 @@ $(stdio_a_c_sources) \ $(stdio_a_asm_sources) \ $(libm_a_c_sources) \ - $(libm_a_asm_sources) + $(libm_a_asm_sources) \ + $(libfixm_a_c_sources) \ + $(libfixm_a_asm_sources) nodist_libm_a_SOURCES = \ $(libm_a_c_sources) \ $(libm_a_asm_sources) +nodist_libfixm_a_SOURCES = \ + $(libfixm_a_c_sources) \ + $(libfixm_a_asm_sources) + libc_a_LIBADD = \ $(stdio_a_libadd) \ $(misc_a_libadd) @@ -99,4 +110,4 @@ echo all distdir install installdirs clean distclean uninstall check: -endif \ No newline at end of file +endif Index: doc/api/doxygen.config.in --- doc/api/doxygen.config.in +++ doc/api/doxygen.config.in @@ -553,6 +553,7 @@ @top_srcdir@/include/assert.h \ @top_srcdir@/include/ctype.h \ @top_srcdir@/include/errno.h \ + @top_srcdir@/include/fixmath.h \ @top_srcdir@/include/inttypes.h \ @top_srcdir@/include/math.h \ @top_srcdir@/include/setjmp.h \ Index: include/Makefile.am --- include/Makefile.am +++ include/Makefile.am @@ -35,6 +35,7 @@ assert.h \ ctype.h \ errno.h \ + fixmath.h \ inttypes.h \ math.h \ setjmp.h \ Index: libm/Makefile.am --- libm/Makefile.am +++ libm/Makefile.am @@ -29,6 +29,5 @@ # $Id: Makefile.am,v 1.2 2005/08/11 20:55:23 joerg_wunsch Exp $ # -SUBDIRS = fplib -DIST_SUBDIRS = fplib - +SUBDIRS = fplib fixlib +DIST_SUBDIRS = fplib fixlib Index: libm/fixlib/Files.am --- /dev/null +++ libm/fixlib/Files.am @@ -0,0 +1,68 @@ +# Copyright (c) 2009 Sean D'Epagnier +# 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: Files.am,v 1.11 2008/03/22 04:04:42 dmix Exp $ +# + +libfixm_a_c_sources = \ + asinr.c \ + countlsr.c \ + rintr.c + +libfixm_a_asm_sources = \ + absr.S \ + atan2r.S \ + atanhr.S \ + atanr.S \ + ceilr.S \ + coshr.S \ + cosr.S \ + expr.S \ + fix_cordicr.S \ + fix_divr.S \ + fix_multr.S \ + fix_sincosr.S \ + fix_sinhcoshr.S \ + hypotr.S \ + logr.S \ + modfr.S \ + roundr.S \ + sinhr.S \ + sinr.S \ + sqrtr.S \ + tanhr.S \ + tanr.S \ + truncr.S + +libfixm_a_extra_dist = \ + asmdef.h \ + fixdef.h \ + asinr-unused.S \ + sqrtr-unused.c + +# vim: set ft=make: Index: libm/fixlib/Makefile.am --- /dev/null +++ libm/fixlib/Makefile.am @@ -0,0 +1,36 @@ +# Copyright (c) 2009 Sean D'Epagnier +# 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: Makefile.am,v 1.5 2005/08/11 20:55:26 joerg_wunsch Exp $ +# + +include $(top_srcdir)/libm/fixlib/Files.am + +EXTRA_DIST = \ + $(libfixm_a_asm_sources) \ + $(libfixm_a_extra_dist) Index: libm/fixlib/Rules.am --- /dev/null +++ libm/fixlib/Rules.am @@ -0,0 +1,33 @@ +# Copyright (c) 2009 Sean D'Epagnier +# 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: Rules.am,v 1.2 2005/08/11 20:55:26 joerg_wunsch Exp $ + +include $(top_srcdir)/libm/fixlib/Files.am + +# vim: set ft=make: Index: libm/fixlib/absr.S --- /dev/null +++ libm/fixlib/absr.S @@ -0,0 +1,47 @@ +/* Copyright (c) 2009 Sean D'Epagnier + 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. */ + +#include "fixdef.h" +#include "asmdef.h" + +/* accum absr(accum x); + Returns the absolute value of x. + */ + +ENTRY absr + sbrs rA3, 7 + ret ; return if positive + com rA3 ; otherwise negate + com rA2 + com rA1 + neg rA0 + sbci rA1, -1 + sbci rA2, -1 + sbci rA3, -1 + ret +ENDFUNC Index: libm/fixlib/asinr-unused.S --- /dev/null +++ libm/fixlib/asinr-unused.S @@ -0,0 +1,83 @@ +/* Copyright (c) 2009 Sean D'Epagnier + 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. */ + +#include "fixdef.h" +#include "asmdef.h" + +/* unused because for whatever reason, this function + quite inaccurate (although much faster than c method) */ +ENTRY __asinr + ldi ZL, pm_lo8(__fix_cordic_asinr) + ldi ZH, pm_hi8(__fix_cordic_asinr) + rjmp __fix_cordicr_entry + +__fix_cordic_asinr: + push rAt0 + push rAt1 + push rAt2 + push rAt3 + X_movw rAt0, rA0 ; At = A + X_movw rAt2, rA2 + ldi rX0, lo8(CORDICC_X2) ; X = CORDICC + ldi rX1, hi8(CORDICC_X2) + ldi rX2, hlo8(CORDICC_X2) + ldi rX3, hhi8(CORDICC_X2) + clr rA0 ; A = 0 + clr rA1 + X_movw rA2, rA0 + X_movw rY0, rA0 ; Y = 0 + X_movw rY2, rY0 + ldi ZL, lo8(__atan_table) + ldi ZH, hi8(__atan_table) + inc __zero_reg__ +__fix_cordic_asinrcont: + set + cp rY0, rAt0 + cpc rY1, rAt1 + cpc rY2, rAt2 + cpc rY3, rAt3 + brge 1f + clt +1: rcall __fix_cordicr_update_AXY + rcall __fix_cordicr_update_circular + inc rcnt + cpi rcnt, 16 ; 17 iterations + brne __fix_cordic_asinrcont + pop rAt3 + pop rAt2 + pop rAt1 + pop rAt0 + com rA3 ; negate result + com rA2 + com rA1 + neg rA0 + sbci rA1,-1 + sbci rA2,-1 + sbci rA3,-1 + ret +ENDFUNC Index: libm/fixlib/asinr.c --- /dev/null +++ libm/fixlib/asinr.c @@ -0,0 +1,37 @@ +/* Copyright (c) 2009 Sean D'Epagnier + 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. */ + +#include + +#include "fixmath.h" + +/* this is a pretty inefficient identity, but it's pretty accurate */ +accum asinr(accum x) +{ + return atanr(x/sqrtr(1-x*x)); +} Index: libm/fixlib/asmdef.h --- /dev/null +++ libm/fixlib/asmdef.h @@ -0,0 +1,233 @@ +/* Copyright (c) 2006 Dmitry Xmelkov + 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: asmdef.h,v 1.2 2008/12/06 03:23:51 dmix Exp $ */ + +#ifndef _ASMDEF_H +#define _ASMDEF_H + +#ifndef __AVR_HAVE_MOVW__ +# if defined(__AVR_ENHANCED__) && __AVR_ENHANCED__ +# define __AVR_HAVE_MOVW__ 1 +# endif +#endif + +#ifndef __AVR_HAVE_LPMX__ +# if defined(__AVR_ENHANCED__) && __AVR_ENHANCED__ +# define __AVR_HAVE_LPMX__ 1 +# endif +#endif + +/* Historicaly, the _U() was intended to concatinate '_' prefix. + Let us save it, as one is usefull to find externals quickly. */ +#define _U(name) name + +#define XL r26 +#define XH r27 +#define YL r28 +#define YH r29 +#define ZL r30 +#define ZH r31 + +#define SPL_IO_ADDR 0x3D +#define SPH_IO_ADDR 0x3E + +.macro FUNCTION name + .ifdef .Lfunction + .err ; FUNCTION is defined already. + .endif + .Lfunction = 1 +#ifdef FUNC_SEGNAME + .section FUNC_SEGNAME, "ax", @progbits +#else + .text +#endif + .type _U(\name), "function" + .ifdef .LEND + .err ; .LEND is defined already. + .endif + .size _U(\name), .LEND - . +.endm +#define FUNCTION FUNCTION /* for CPP conditional expressions */ + +.macro ENTRY name + .ifndef .Lfunction +FUNCTION \name + .endif + .global _U(\name) +_U(\name): +.endm + +.macro ENDFUNC +.LEND: +.endm + + +/* Macro REGNO set the symbol name 'name' to integer which is a 'reg' + number (0..31). If 'reg' is not a valid register, compilation error. + Valid regs are: r0..r31, R0..R31 and 0..31 + */ +.macro REGNO name, reg + \name = -1 + + .L__regno = 0 + .irp sym, r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, \ + r10,r11,r12,r13,r14,r15,r16,r17,r18,r19, \ + r20,r21,r22,r23,r24,r25,r26,r27,r28,r29, \ + r30,r31 + .ifc \sym,\reg + \name = .L__regno + .endif + .L__regno = .L__regno + 1 + .endr + + .L__regno = 0 + .irp sym, R0, R1, R2, R3, R4, R5, R6, R7, R8, R9, \ + R10,R11,R12,R13,R14,R15,R16,R17,R18,R19, \ + R20,R21,R22,R23,R24,R25,R26,R27,R28,R29, \ + R30,R31 + .ifc \sym,\reg + \name = .L__regno + .endif + .L__regno = .L__regno + 1 + .endr + + .if \name < 0 + .L__regno = 0 + .rept 32 + .if \reg == .L__regno + \name = .L__regno + .endif + .L__regno = .L__regno + 1 + .endr + .endif + + .if \name < 0 + .err ; Invalid dst arg of 'X_lpm' macro. + .endif + +.endm /* REGNO */ + +/* Macro 'X_movw' extends enhanced movw instruction for classic chips. + */ +.macro X_movw dst,src +#if defined(__AVR_HAVE_MOVW__) && __AVR_HAVE_MOVW__ + movw \dst,\src +#else + REGNO .L__movw_dst, \dst + .if .L__movw_dst < 0 + .exitm + .endif + .if .L__movw_dst % 1 + .err ; Invalid dst arg in X_movw macro. + .exitm + .endif + + REGNO .L__movw_src, \src + .if .L__movw_src < 0 + .exitm + .endif + .if .L__movw_src % 1 + .err ; Invalid src arg in X_movw macro. + .exitm + .endif + mov .L__movw_dst, .L__movw_src + mov .L__movw_dst + 1, .L__movw_src + 1 +#endif +.endm /* X_movw */ + +/* Macro 'X_lpm' extends enhanced lpm instruction for classic chips. + Usage: + X_lpm reg, dst + where + reg is 0..31, r0..r31 or R0..R31 + dst is z, Z, z+ or Z+ + It is possible to omit both arguments. + + Possible results for classic chips: + lpm + lpm / mov Rd,r0 + lpm / adiw ZL,1 + lpm / mov Rd,r0 / adiw ZL,1 + + For enhanced chips it is one instruction always. + + ATTENTION: unlike enhanced chips SREG (S,V,N,Z,C) flags are + changed in case of 'Z+' dst. R0 is scratch. + */ +.macro X_lpm dst=r0, src=Z + + /* dst evaluation */ + REGNO .L__lpm_dst, \dst + .if .L__lpm_dst < 0 + .exitm ; do not multiply errors + .endif + + /* src evaluation */ + .L__lpm_src = -1 + .L__lpm_n = 0 + .irp reg, z,Z,z+,Z+ + .ifc \reg,\src + .L__lpm_src = .L__lpm_n + .endif + .L__lpm_n = .L__lpm_n + 1 + .endr + + .if (.L__lpm_src < 0) + .err ; Invalid src arg of 'X_lpm' macro. + .endif + + /* instruction(s) */ + .if .L__lpm_src < 2 + .if .L__lpm_dst == 0 + lpm + .else +#if defined(__AVR_HAVE_LPMX__) && __AVR_HAVE_LPMX__ + lpm .L__lpm_dst, Z +#else + lpm + mov .L__lpm_dst, r0 +#endif + .endif + .else + .if (.L__lpm_dst >= 30) + .err ; Registers 30 and 31 are inhibited as 'X_lpm *,Z+' dst. + .endif +#if defined(__AVR_HAVE_LPMX__) && __AVR_HAVE_LPMX__ + lpm .L__lpm_dst, Z+ +#else + lpm + .if .L__lpm_dst + mov .L__lpm_dst, r0 + .endif + adiw r30, 1 +#endif + .endif +.endm /* X_lpm */ + +#endif /* !_ASMDEF_H */ Index: libm/fixlib/atan2r.S --- /dev/null +++ libm/fixlib/atan2r.S @@ -0,0 +1,61 @@ +/* Copyright (c) 2009 Sean D'Epagnier + 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. */ + +#include "fixdef.h" +#include "asmdef.h" + +ENTRY __atan2r + sbrs rX3,7 ; test X for negative + rjmp __atanr_atan2entry ; if positive, regular atan2r + com rX3 ; X = -X + com rX2 + com rX1 + neg rX0 + sbci rX1, -1 + sbci rX2, -1 + sbci rX3, -1 + rcall __atanr_atan2entry + sbrc rA3, 7 ; + or - PI ? + rjmp 1f + ldi rX0, lo8(PIR) + ldi rX1, hi8(PIR) + ldi rX2, hlo8(PIR) + ldi rX3, hhi8(PIR) + rjmp 2f +1: ldi rX0, lo8(-PIR) + ldi rX1, hi8(-PIR) + ldi rX2, hlo8(-PIR) + ldi rX3, hhi8(-PIR) +2: sub rX0, rA0 ; subtract constant + sbc rX1, rA1 + sbc rX2, rA2 + sbc rX3, rA3 + X_movw rA0, rX0 ; move value back in place + X_movw rA2, rX2 + ret +ENDFUNC Index: libm/fixlib/atanhr.S --- /dev/null +++ libm/fixlib/atanhr.S @@ -0,0 +1,61 @@ +/* Copyright (c) 2009 Sean D'Epagnier + 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. */ + +#include "fixdef.h" +#include "asmdef.h" + +ENTRY __atanhr + ldi ZL, pm_lo8(__fix_cordic_atanhr) + ldi ZH, pm_hi8(__fix_cordic_atanhr) + rcall __fix_cordicr_entry + rjmp __fix_atan_realign1 + +__fix_cordic_atanhr: + X_movw rY0, rA0 ; move argument into Y + X_movw rY2, rA2 + clr rA0 ; A = 0 + clr rA1 + X_movw rA2, rA0 + X_movw rX0, rA0 ; X = 1 + ldi rX2,1 + clr rX3 +ENTRY __fix_cordic_atanhr_logentry + ldi ZL, lo8(__atanh_table) + ldi ZH, hi8(__atanh_table) + inc __zero_reg__ +__fix_cordic_atanhrcont: + inc rcnt ; start at first iteration for hyperbolic + mov __scratch_reg__, rY3 + com __scratch_reg__ + bst __scratch_reg__, 7 + rcall __fix_cordicr_update_AXY + rcall __fix_cordicr_update_hyperbolic + cpi rcnt, 16 ; 16 iterations + brne __fix_cordic_atanhrcont + ret +ENDFUNC Index: libm/fixlib/atanr.S --- /dev/null +++ libm/fixlib/atanr.S @@ -0,0 +1,61 @@ +/* Copyright (c) 2009 Sean D'Epagnier + 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. */ + +#include "fixdef.h" +#include "asmdef.h" + +ENTRY __atanr + clr rX0 ; X = 1 + clr rX1 + ldi rX2,1 + clr rX3 +ENTRY __atanr_atan2entry + ldi ZL, pm_lo8(__fix_cordic_atanr) + ldi ZH, pm_hi8(__fix_cordic_atanr) + rjmp __fix_cordicr_entry + +__fix_cordic_atanr: + X_movw rY0, rA0 ; move argument into Y + X_movw rY2, rA2 + clr rA0 ; A = 0 + clr rA1 + X_movw rA2, rA0 + ldi ZL, lo8(__atan_table) + ldi ZH, hi8(__atan_table) + inc __zero_reg__ +__fix_cordic_atanrcont: + mov __scratch_reg__, rY3 + com __scratch_reg__ + bst __scratch_reg__, 7 + rcall __fix_cordicr_update_AXY + rcall __fix_cordicr_update_circular + inc rcnt + cpi rcnt, 17 ; 17 iterations + brne __fix_cordic_atanrcont + rjmp __fix_atan_realign1 +ENDFUNC Index: libm/fixlib/ceilr.S --- /dev/null +++ libm/fixlib/ceilr.S @@ -0,0 +1,44 @@ +/* Copyright (c) 2009 Sean D'Epagnier + 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. */ + +#include "fixdef.h" +#include "asmdef.h" + +/* accum ceilr (accum x); + The ceilr() function rounds x up to the nearest integer. + */ + +ENTRY ceilr + cp rA0,__zero_reg__ + cpc rA1,__zero_reg__ + breq 0f + adiw rA2,1 + clr rA0 + clr rA1 +0: ret +ENDFUNC Index: libm/fixlib/coshr.S --- /dev/null +++ libm/fixlib/coshr.S @@ -0,0 +1,37 @@ +/* Copyright (c) 2009 Sean D'Epagnier + 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. */ + +#include "fixdef.h" +#include "asmdef.h" + +ENTRY __coshr + rcall __fix_sinhcoshr + X_movw rA0, rX0 + X_movw rA2, rX2 + rjmp __fix_sincos_realign2 +ENDFUNC Index: libm/fixlib/cosr.S --- /dev/null +++ libm/fixlib/cosr.S @@ -0,0 +1,37 @@ +/* Copyright (c) 2009 Sean D'Epagnier + 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. */ + +#include "fixdef.h" +#include "asmdef.h" + +ENTRY __cosr + rcall __fix_sincosr + X_movw rA0, rX0 + X_movw rA2, rX2 + rjmp __fix_sincos_realign2 +ENDFUNC Index: libm/fixlib/countlsr.c --- /dev/null +++ libm/fixlib/countlsr.c @@ -0,0 +1,49 @@ +/* Copyright (c) 2009 Sean D'Epagnier + 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. */ + +#include + +#include "fixmath.h" + +/* sanity check, should be 32bits minus sign */ +#if (ACCUM_IBIT + ACCUM_FBIT) != 31 +#error "ACCUM_IBIT + ACCUM_FBIT expected to be 31" +#endif + +int countlsr (accum x) +{ + unsigned long bits = bitsr(absr(x)), mask = 1L << 31; + int i = 1; + if(!bits) + return 32; + while (!(bits&mask)) { + i++; + mask>>=1; + } + return i; +} Index: libm/fixlib/expr.S --- /dev/null +++ libm/fixlib/expr.S @@ -0,0 +1,41 @@ +/* Copyright (c) 2009 Sean D'Epagnier + 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. */ + +#include "fixdef.h" +#include "asmdef.h" + +ENTRY __expr + rcall __fix_sinhcoshr + add rX0, rY0 + adc rX1, rY1 + adc rX2, rY2 + adc rX3, rY3 + X_movw rA0, rX0 + X_movw rA2, rX2 + rjmp __fix_sincos_realign2 +ENDFUNC Index: libm/fixlib/fix_cordicr.S --- /dev/null +++ libm/fixlib/fix_cordicr.S @@ -0,0 +1,177 @@ +/* Copyright (c) 2009 Sean D'Epagnier + 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. */ + +#include "fixdef.h" +#include "asmdef.h" + +/* __fix_cordicr_entry() save and restore registers used + by the cordic routines. This saves a bit of program memory + space since all of the routines get routed through here + to save the same registers. + + Z register contains the function for callback +*/ +ENTRY __fix_cordicr_entry + push rXh0 + push rXh1 + push rXh2 + push rXh3 + push rYh0 + push rYh1 + push rYh2 + push rYh3 + clr rcnt ; zero count + ldi rk, 3 ; set rk to 3 + icall ; call function specific routine + pop rYh3 ; restore clobbered registers + pop rYh2 + pop rYh1 + pop rYh0 + pop rXh3 + pop rXh2 + pop rXh1 + pop rXh0 + ret + +/* This routine updates the angle, in the A registers from the values in the + table pointed to by the Z register. It falls through to update XY + next since XY need to always be updated if A is. */ +ENTRY __fix_cordicr_update_AXY + X_lpm rXh1, Z+ ; load const from table + X_lpm rXh0, Z+ + brtc __fix_cordicr_updateApos + add rA0, rXh0 ; A += Ah + adc rA1, rXh1 + adc rA2, __zero_reg__ + clr __zero_reg__ + adc rA3, __zero_reg__ + rjmp __fix_cordicr_update_XY +__fix_cordicr_updateApos: + sub rA0, rXh0 ; A -= Ah + sbc rA1, rXh1 + sbc rA2, __zero_reg__ + clr __zero_reg__ + sbc rA3, __zero_reg__ + +/* This routine loads the values from X and Y into Xh and Yh, and shifts + them right by rcnt bits */ +ENTRY __fix_cordicr_update_XY ; Now load Xh and Yh with shifted values +#if 1 /* speed optimization (first byte shift) at the expense of size */ + cpi rcnt, 8 + brlt __fix_cordicr_shiftlessthan8 + clr rXh3 ; sign extend first byte + sbrc rX3, 7 + com rXh3 + mov rXh2, rX3 + mov rXh1, rX2 + mov rXh0, rX1 + clr rYh3 ; sign extend first byte + sbrc rY3, 7 + com rYh3 + mov rYh2, rY3 + mov rYh1, rY2 + mov rYh0, rY1 + subi rcnt, 8 + mov __zero_reg__, rcnt + subi rcnt, -8 + rjmp __fix_cordicr_shift +#endif +__fix_cordicr_shiftlessthan8: + X_movw rXh0, rX0 + X_movw rXh2, rX2 + X_movw rYh0, rY0 + X_movw rYh2, rY2 + mov __zero_reg__, rcnt ; zero after loop +__fix_cordicr_shift: + tst __zero_reg__ ; do we not shift at all? + breq __fix_cordicr_shiftdone +__fix_cordicr_shiftloop: + asr rXh3 + ror rXh2 + ror rXh1 + ror rXh0 + asr rYh3 + ror rYh2 + ror rYh1 + ror rYh0 + dec __zero_reg__ + brne __fix_cordicr_shiftloop +__fix_cordicr_shiftdone: + ret + +/* This routine checks the t bit in SREG and performs the correct operation + on X and Y registers for circular CORDIC method. */ +ENTRY __fix_cordicr_update_circular + brtc __fix_cordicr_update_XnegYpos + add rX0, rYh0 ; X += Yh + adc rX1, rYh1 + adc rX2, rYh2 + adc rX3, rYh3 +__fix_cordicr_update_Yneg: + sub rY0, rXh0 ; Y -= Xh + sbc rY1, rXh1 + sbc rY2, rXh2 + sbc rY3, rXh3 + ret +__fix_cordicr_update_XnegYpos: + sub rX0, rYh0 ; X -= Yh + sbc rX1, rYh1 + sbc rX2, rYh2 + sbc rX3, rYh3 +__fix_cordicr_update_Ypos: + add rY0, rXh0 ; Y += Xh + adc rY1, rXh1 + adc rY2, rXh2 + adc rY3, rXh3 + ret + +/* This routine checks the t bit in SREG and performs the correct operation + on X and Y registers for hyperbolic CORDIC method. */ +ENTRY __fix_cordicr_update_hyperbolic +#if 1 + tst rk ; repeat some iterations for hyperbolic + brne 0f + ldi rk, 3 + sbiw ZL, 2 + dec rcnt +0: dec rk +#endif +__fix_cordicr_update_hyperbolic_havek: + brtc __fix_cordicr_update_XposYpos + sub rX0, rYh0 ; X -= Yh + sbc rX1, rYh1 + sbc rX2, rYh2 + sbc rX3, rYh3 + rjmp __fix_cordicr_update_Yneg +__fix_cordicr_update_XposYpos: + add rX0, rYh0 ; X += Yh + adc rX1, rYh1 + adc rX2, rYh2 + adc rX3, rYh3 + rjmp __fix_cordicr_update_Ypos +ENDFUNC Index: libm/fixlib/fix_divr.S --- /dev/null +++ libm/fixlib/fix_divr.S @@ -0,0 +1,39 @@ +/* Copyright (c) 2009 Sean D'Epagnier + 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. */ + +#include "fixdef.h" +#include "asmdef.h" + +/* __fix_div() divides X by Y and puts + the result in rA0 for returning */ + +ENTRY __fix_div + X_movw r24, rY0 ; move y into place for division + X_movw r26, rY2 + rjmp __divsa3 ; maybe optimize by calling __divha3 instead? +ENDFUNC Index: libm/fixlib/fix_multr.S --- /dev/null +++ libm/fixlib/fix_multr.S @@ -0,0 +1,41 @@ +/* Copyright (c) 2009 Sean D'Epagnier + 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. */ + +#include "fixdef.h" +#include "asmdef.h" + +/* __fix_mult() multiplies X by rmul2 and puts + the result in rA0 for returning */ + +ENTRY __fix_multr + rcall __mulusa3 ; could optimize for speed, since CORDICH + ; is 0x13522, and CORICC is even smaller + X_movw rA0, rmulrL + X_movw rA2, rmulrHL + ret +ENDFUNC Index: libm/fixlib/fix_sincosr.S --- /dev/null +++ libm/fixlib/fix_sincosr.S @@ -0,0 +1,97 @@ +/* Copyright (c) 2009 Sean D'Epagnier + 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. */ + +#include "fixdef.h" +#include "asmdef.h" + +/* __fix_cordic_sincosr() computes sin and cosine for accum type */ +ENTRY __fix_sincosr + ldi ZL, pm_lo8(__fix_cordic_sincosr) + ldi ZH, pm_hi8(__fix_cordic_sincosr) + rjmp __fix_cordicr_entry + +__fix_cordic_sincosr: ; callback to run sin cos + ldi rX0, lo8(CORDICC_X4) + ldi rX1, hi8(CORDICC_X4) + ldi rX2, hlo8(CORDICC_X4) + ldi rX3, hhi8(CORDICC_X4) + clr rY0 ; set Y = 0 + clr rY1 + X_movw rY2, rY0 + lsl rA0 ; shift angle left 1 bit since table is 1 bit more + rol rA1 + rol rA2 + rol rA3 + ldi ZL, lo8(__atan_table) ; load atan table for lookup + ldi ZH, hi8(__atan_table) + inc __zero_reg__ +__fix_cordic_sincosrcont: + bst rA3, 7 + rcall __fix_cordicr_update_AXY + rcall __fix_cordicr_update_circular + inc rcnt + cpi rcnt, 16 ; 16 iterations + brne __fix_cordic_sincosrcont + X_movw rA0, rY0 ; put Y value in A for return + X_movw rA2, rY2 + ret + +ENTRY __fix_sincos_realign2 + asr rA3 ; algorithm uses data two bits shifted left for + ror rA2 ; increased precision, see CORDICC_X4 above + ror rA1 ; here, we shift the result back into place + ror rA0 +ENTRY __fix_atan_realign1 ; for atan, only shift out 1 bit + asr rA3 + ror rA2 + ror rA1 + ror rA0 + ret +ENDFUNC + + PGM_SECTION + .global __atan_table +__atan_table: + .byte 0x92, 0x20 ; 65536*2*atan(1/1) (1 in higher byte) + .byte 0xed, 0x63 ; 65536*2*atan(1/2) + .byte 0x7d, 0x6e ; 65536*2*atan(1/4) + .byte 0x3f, 0xab ; 65536*2*atan(1/8) + .byte 0x1f, 0xf5 ; 65536*2*atan(1/16) + .byte 0x0f, 0xff ; ... + .byte 0x08, 0x00 ; could optimize for size... + .byte 0x04, 0x00 + .byte 0x02, 0x00 + .byte 0x01, 0x00 + .byte 0x00, 0x80 + .byte 0x00, 0x40 + .byte 0x00, 0x20 + .byte 0x00, 0x10 + .byte 0x00, 0x08 + .byte 0x00, 0x04 + .byte 0x00, 0x02 + .end Index: libm/fixlib/fix_sinhcoshr.S --- /dev/null +++ libm/fixlib/fix_sinhcoshr.S @@ -0,0 +1,83 @@ +/* Copyright (c) 2009 Sean D'Epagnier + 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. */ + +#include "fixdef.h" +#include "asmdef.h" + +/* __fix_cordic_sinhcoshr() computes sin and cosine for accum type */ +ENTRY __fix_sinhcoshr + ldi ZL, pm_lo8(__fix_cordic_sinhcoshr) + ldi ZH, pm_hi8(__fix_cordic_sinhcoshr) + rjmp __fix_cordicr_entry + +__fix_cordic_sinhcoshr: ; callback to run sin cos + ldi rX0, lo8(CORDICH_X4) + ldi rX1, hi8(CORDICH_X4) + ldi rX2, hlo8(CORDICH_X4) + ldi rX3, hhi8(CORDICH_X4) + clr rY0 ; set Y=0 + clr rY1 + X_movw rY2, rY0 + lsl rA0 ; shift angle left 1 bit since table is 1 bit more + rol rA1 + rol rA2 + rol rA3 + ldi ZL, lo8(__atanh_table) ; load atan table for lookup + ldi ZH, hi8(__atanh_table) + inc __zero_reg__ +__fix_cordic_sinhcoshrcont: + inc rcnt ; start at first iteration for hyperbolic + bst rA3, 7 + rcall __fix_cordicr_update_AXY + rcall __fix_cordicr_update_hyperbolic + cpi rcnt, 14 ; 16 iterations + brne __fix_cordic_sinhcoshrcont + ret +ENDFUNC + + PGM_SECTION + .global __atanh_table +__atanh_table: + .byte 0x19, 0x3f ; 65536*2*atanh(1/2) (1 in higher byte) + .byte 0x82, 0xc5 ; 65536*2*atanh(1/4) + .byte 0x40, 0x56 ; 65536*2*atanh(1/8) + .byte 0x20, 0x0b ; 65536*2*atanh(1/16) + .byte 0x10, 0x01 ; 65536*2*atanh(1/32) + .byte 0x08, 0x00 ; ... + .byte 0x04, 0x00 ; could optimize for size... + .byte 0x02, 0x00 + .byte 0x01, 0x00 + .byte 0x00, 0x80 + .byte 0x00, 0x40 + .byte 0x00, 0x20 + .byte 0x00, 0x10 + .byte 0x00, 0x08 + .byte 0x00, 0x04 + .byte 0x00, 0x02 + .byte 0x00, 0x01 + .end Index: libm/fixlib/fixdef.h --- /dev/null +++ libm/fixlib/fixdef.h @@ -0,0 +1,98 @@ +/* Definitions used in fixed point math library assembly. + Author: Sean D'Epagnier +*/ + +#ifndef _FIXDEF_H +#define _FIXDEF_H + +#define rB0 r18 +#define rB1 r19 +#define rB2 r20 +#define rB3 r21 + +#define rA0 r22 +#define rA1 r23 +#define rA2 r24 +#define rA3 r25 + +#define __scratch_reg__ r0 +#define __zero_reg__ r1 + +/* defines for cordic routines */ +#define CORDICC 0x9b75 /* circular cordic gain constant */ +#define CORDICC_X2 0x136ea /* circular cordic gain constant times 2 */ +#define CORDICC_X4 0x26dd4 /* circular cordic gain constant times 4 */ +#define CORDICH 0x13522 /* hyperbolic cordic gain constant */ +#define CORDICH_X4 0x4d488 /* hyperbolic cordic gain constant times 4 */ + +#define PIR 0x3243f /* Pi */ +#define PI2R 0x19220 /* Pi/2 */ + +#define rXh0 r6 +#define rXh1 r7 +#define rXh2 r8 +#define rXh3 r9 +#define rYh0 r10 +#define rYh1 r11 +#define rYh2 r12 +#define rYh3 r13 +#define rY0 r14 +#define rY1 r15 +#define rY2 r16 +#define rY3 r17 +#define rX0 r18 +#define rX1 r19 +#define rX2 r20 +#define rX3 r21 + +#define rcnt r26 +#define rk r27 /* used for hyperbolic repeat count */ + +#define rAt0 r2 /* used for asin only */ +#define rAt1 r3 +#define rAt2 r4 +#define rAt3 r5 + +#if defined(__AVR_ENHANCED__) && __AVR_ENHANCED__ +#define rmul1L r16 /* multiplier Low */ +#define rmul1H r17 +#define rmul1HL r18 +#define rmul1HH r19 /* multiplier High */ + +#define rmul2L r20 /* multiplicand Low */ +#define rmul2H r21 +#define rmul2HL r22 +#define rmul2HH r23 /* multiplicand High */ + +#define rmulrL r24 /* result Low */ +#define rmulrH r25 +#define rmulrHL r26 +#define rmulrHH r27 /* result High */ +#else +#define rmul1L r18 /* multiplier Low */ +#define rmul1H r19 +#define rmul1HL r20 +#define rmul1HH r21 /* multiplier High */ + +#define rmul2L r24 /* multiplicand Low */ +#define rmul2H r25 +#define rmul2HL r26 +#define rmul2HH r27 /* multiplicand High */ + +#define rmulrL r14 /* result Low */ +#define rmulrH r15 +#define rmulrHL r16 +#define rmulrHH r17 /* result High */ +#endif + +/* Put functions at this section. */ +#ifdef FUNCTION +# error "The FUNCTION macro must be defined after FUNC_SEGNAME" +#endif +#define FUNC_SEGNAME .text.fixlib + +/* Put constant tables at low addresses in program memory, so they are + reachable for "lpm" without using RAMPZ on >64K devices. */ +#define PGM_SECTION .section .progmem.gcc_fixlib, "a", @progbits + +#endif /* !_FIXDEF_H */ Index: libm/fixlib/hypotr.S --- /dev/null +++ libm/fixlib/hypotr.S @@ -0,0 +1,62 @@ +/* Copyright (c) 2009 Sean D'Epagnier + 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. */ + +#include "fixdef.h" +#include "asmdef.h" + +ENTRY __hypotr + ldi ZL, pm_lo8(__fix_cordic_hypotr) + ldi ZH, pm_hi8(__fix_cordic_hypotr) + rcall __fix_cordicr_entry +#if defined(__AVR_ENHANCED__) && __AVR_ENHANCED__ + movw rmul1L, rX0 ; move X into place for scale multiplication + movw rmul1HL, rX2 +#endif + ldi rmul2L, lo8(CORDICC) + ldi rmul2H, hi8(CORDICC) + ldi rmul2HL, hlo8(CORDICC) + ldi rmul2HH, hhi8(CORDICC) + rjmp __fix_multr + +__fix_cordic_hypotr: ; hypot variation + X_movw rY0, rA0 ; move argument into Y (X already in place) + X_movw rY2, rA2 + clr rA0 ; A = 0 + clr rA1 + X_movw rA2, rA0 +__fix_cordic_hypotrcont: + mov __scratch_reg__, rY3 + com __scratch_reg__ + bst __scratch_reg__, 7 + rcall __fix_cordicr_update_XY + rcall __fix_cordicr_update_circular + inc rcnt + cpi rcnt, 14 ; 14 iterations + brne __fix_cordic_hypotrcont + ret +ENDFUNC Index: libm/fixlib/logr.S --- /dev/null +++ libm/fixlib/logr.S @@ -0,0 +1,52 @@ +/* Copyright (c) 2009 Sean D'Epagnier + 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. */ + +#include "fixdef.h" +#include "asmdef.h" + +ENTRY __logr + ldi ZL, pm_lo8(__fix_cordic_logr) + ldi ZH, pm_hi8(__fix_cordic_logr) + rjmp __fix_cordicr_entry + +__fix_cordic_logr: + X_movw rX0, rA0 ; X = A + 1 + X_movw rX2, rA2 + ldi ZL, 1 + adc rX2, ZL + adc rX3, __zero_reg__ + X_movw rY0, rA0 ; Y = A - 1 + X_movw rY2, rA2 + subi rY2, 1 + sbc rY3, __zero_reg__ + clr rA0 ; A = 0 + clr rA1 + X_movw rA2, rA0 + rcall __fix_cordic_atanhr_logentry ; same here.. save some space + ret +ENDFUNC Index: libm/fixlib/modfr.S --- /dev/null +++ libm/fixlib/modfr.S @@ -0,0 +1,58 @@ +/* Copyright (c) 2009 Sean D'Epagnier + 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. */ + +#include "fixdef.h" +#include "asmdef.h" + +/* accum modr (accum x, accum *iptr); + + The modr() function breaks the argument x into an integral part and a + fractional part, each of which has the same sign as x. The integral part + is stored in iptr. + This implementation skips writing by zero pointer. + */ + +#define iptr_lo r20 + +ENTRY modfr + clr __scratch_reg__ ; store sign extension + sbrs rA3, 7 + rjmp 1f + com __scratch_reg__ + adiw rA2, 1 ; increment needed if negative +1: X_movw ZL, iptr_lo ; save iptr + adiw ZL, 0 ; skip writing with NULL pointer + breq 2f + st Z, __zero_reg__ + std Z+1, __zero_reg__ + std Z+2, rA2 + std Z+3, rA3 +2: mov rA2, __scratch_reg__ + mov rA3, __scratch_reg__ + ret +ENDFUNC Index: libm/fixlib/rintr.c --- /dev/null +++ libm/fixlib/rintr.c @@ -0,0 +1,47 @@ +/* Copyright (c) 2009 Sean D'Epagnier + 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. */ + +#include + +#include "fixmath.h" + +/* sanity check */ +#if (ACCUM_FBIT) != 16 +#error "ACCUM_FBIT expected to be 16" +#endif + +int rintr (accum x) +{ + accum f = modfr(absr(x), 0); + if (f == .5 && !(bitsr(absr(x)) & 0x10000)) + if(x < 0) + x++; + else + x--; + return roundr(x); +} Index: libm/fixlib/roundr.S --- /dev/null +++ libm/fixlib/roundr.S @@ -0,0 +1,51 @@ +/* Copyright (c) 2009 Sean D'Epagnier + 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. */ + +#include "fixdef.h" +#include "asmdef.h" + +/* accum roundr (accum x); + The roundr() function rounds \a __x to the nearest integer, but rounds + halfway cases away from zero (instead of to the nearest even integer). + + Examples: + roundr(1.5) --> 2.0 + roundr(-1.4) --> -1.0 + */ + +ENTRY roundr + sbrs rA3, 7 ; negative? + rjmp 0f + subi rA0, 1 ; subtract 1 from least significant for the next test + sbci rA1, 0 +0: sbrc rA1, 7 ; test for .5 bit + adiw rA2, 1 ; bump to the next int + clr rA0 ; clear fraction + clr rA1 + ret +ENDFUNC Index: libm/fixlib/sinhr.S --- /dev/null +++ libm/fixlib/sinhr.S @@ -0,0 +1,37 @@ +/* Copyright (c) 2009 Sean D'Epagnier + 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. */ + +#include "fixdef.h" +#include "asmdef.h" + +ENTRY __sinhr + rcall __fix_sinhcoshr + X_movw rA0, rY0 + X_movw rA2, rY2 + rjmp __fix_sincos_realign2 +ENDFUNC Index: libm/fixlib/sinr.S --- /dev/null +++ libm/fixlib/sinr.S @@ -0,0 +1,37 @@ +/* Copyright (c) 2009 Sean D'Epagnier + 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. */ + +#include "fixdef.h" +#include "asmdef.h" + +ENTRY __sinr + rcall __fix_sincosr + X_movw rA0, rY0 + X_movw rA2, rY2 + rjmp __fix_sincos_realign2 +ENDFUNC Index: libm/fixlib/sqrtr-unused.c --- /dev/null +++ libm/fixlib/sqrtr-unused.c @@ -0,0 +1,54 @@ +/* Copyright (c) 2009 Sean D'Epagnier + 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. */ + +#include + +#include "fixmath.h" + +/* This function is pretty fast, but not as accurate as I would like. */ +accum sqrtr2(accum x) +{ + long i = bitsr(x); + long op = i, res = 0, mask = 1L << 30; + + while (mask > i) + mask >>= 2; + + while (mask) { + long j = i - res - mask; + if (j >= 0) { + res += mask << 1; + i = j; + } + mask >>= 2; + res >>= 1; + } + + res <<= (ACCUM_FBIT/2); /* shift because of decimal place */ + return rbits(res); +} Index: libm/fixlib/sqrtr.S --- /dev/null +++ libm/fixlib/sqrtr.S @@ -0,0 +1,70 @@ +/* Copyright (c) 2009 Sean D'Epagnier + 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. */ + +#include "fixdef.h" +#include "asmdef.h" + +ENTRY __sqrtr + ldi ZL, pm_lo8(__fix_cordic_sqrtr) + ldi ZH, pm_hi8(__fix_cordic_sqrtr) + rcall __fix_cordicr_entry +#if defined(__AVR_ENHANCED__) && __AVR_ENHANCED__ + movw rmul1L, rX0 ; move X into place for scale multiplication + movw rmul1HL, rX2 +#endif + ldi rmul2L, lo8(CORDICH) + ldi rmul2H, hi8(CORDICH) + ldi rmul2HL, hlo8(CORDICH) + ldi rmul2HH, hhi8(CORDICH) + rjmp __fix_multr + +_fix_cordic_sqrtr: + X_movw rX0, rA0 ; X = A + .25 + X_movw rX2, rA2 + ldi ZL, 0x40 + adc rX1, ZL + adc rX2, __zero_reg__ + adc rX3, __zero_reg__ + X_movw rY0, rA0 ; Y = A - .25 + ldi rY2, 0x40 + sub rY1, rY2 + X_movw rY2, rA2 + sbc rY2, __zero_reg__ + sbc rY3, __zero_reg__ + inc rcnt +__fix_cordic_sqrtrcont: + mov __scratch_reg__, rY3 + com __scratch_reg__ + bst __scratch_reg__, 7 + rcall __fix_cordicr_update_XY + rcall __fix_cordicr_update_hyperbolic + inc rcnt + cpi rcnt, 14 ; 14 iterations + brne __fix_cordic_sqrtrcont + ret +ENDFUNC Index: libm/fixlib/tanhr.S --- /dev/null +++ libm/fixlib/tanhr.S @@ -0,0 +1,37 @@ +/* Copyright (c) 2009 Sean D'Epagnier + 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. */ + +#include "fixdef.h" +#include "asmdef.h" + +ENTRY __tanhr + rcall __fix_sinhcoshr + X_movw rA0, rY0 + X_movw rA2, rY2 + rjmp __fix_div +ENDFUNC Index: libm/fixlib/tanr.S --- /dev/null +++ libm/fixlib/tanr.S @@ -0,0 +1,37 @@ +/* Copyright (c) 2009 Sean D'Epagnier + 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. */ + +#include "fixdef.h" +#include "asmdef.h" + +ENTRY __tanr + rcall __fix_sincosr + X_movw rA0, rY0 + X_movw rA2, rY2 + rjmp __fix_div +ENDFUNC Index: libm/fixlib/truncr.S --- /dev/null +++ libm/fixlib/truncr.S @@ -0,0 +1,50 @@ +/* Copyright (c) 2009 Sean D'Epagnier + 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. */ + +#include "fixdef.h" +#include "asmdef.h" + +/* accum truncr (accum x); + The truncr() function rounds x to the nearest integer not larger in + absolute value. + + Examples: + truncr(1.9) --> 1.0 + truncr(-1.9) --> -1.0 + */ + +ENTRY truncr + cp rA0,__zero_reg__ + cpc rA1,__zero_reg__ + breq 0f + clr rA0 + clr rA1 + sbrc rA3, 7 + adiw rA2, 1 +0: ret +ENDFUNC