ljz@fxgrp.UUCP (Lloyd Zusman) (10/07/88)
Posting-number: Volume 4, Issue 116 Submitted-by: "Lloyd Zusman" <ljz@fxgrp.UUCP> Archive-name: apml/Part2 Enclosed you will find the Arbitrary Precision Math Library (2 of 5) Please post this to the comp.sources.misc newsgroup. I finally got this into good enough shape to send out to the net. To use, just unshar the 5 pieces, read the README file, possibly alter the makefiles to conform to your system's conventions, and then type 'make test'. Good luck! -- Lloyd Zusman Internet: ljz@fx.com Master Byte Software or ljz%fx.com@ames.arc.nasa.gov Los Gatos, California or fxgrp!ljz@ames.arc.nasa.gov "We take things well in hand." uucp: ...!ames!fxgrp!ljz [ our Internet connection is down: use uucp or mail to the entry above it ] #--------------------------Cut Here-------------------------- #! /bin/sh # This is a shell archive. Remove anything before the "#! /bin/sh" line, # then unpack it by saving it in a file and typing "sh file." # # Wrapped by Lloyd Zusman (ljz) at fxgrp on Wed Oct 5 12:41:51 1988 # # unpacks with default permissions # # Contents : LICENSE Makefile makefile.msc apm.arf addsub.c calc.c # if `test ! -s LICENSE` then echo "x - LICENSE" sed 's/^X//' > LICENSE << '@\END_OF_FILE_LICENSE' X Arbitrary Precision Math Library General Public License X (Written October 5, 1988) X X Copyright (C) 1988 Lloyd Zusman, Master Byte Software, Los X Gatos, California. Everyone is permitted to copy and distribute X verbatim copies of this license, but changing it is not allowed. X You can also use this wording to make the terms for other programs. X X The wording of this license is based on that of the X "GNU EMACS GENERAL PUBLIC LICENSE" by Richard Stallman, X Copyright (C) 1985, 1987, 1988, version of February 11, 1988, X but since some of the text has been changed, please be sure to X READ THIS CAREFULLY! X X This general public license is intended to give everyone the right Xto share the Arbitrary Precision Math Library (hereinafter referred to Xas the "APM Library"). To make sure that you get the rights we want Xyou to have, I need to make restrictions that forbid anyone to deny Xyou these rights or to ask you to surrender the rights. X X Specifically, we want to make sure that you have the right to give Xaway copies of the APM Library, that you receive source code or else Xcan get it if you want it, that you can change the APM Library or use Xpieces of it in new programs, and that you know you can do these Xthings. X X To make sure that everyone has such rights, we have to forbid you to Xdeprive anyone else of these rights. For example, if you distribute Xcopies of the APM Library, you must give the recipients all the Xrights that you have. You must make sure that they, too, receive or Xcan get the source code. And you must tell them their rights. X X Also, for our own protection, we must make certain that everyone Xfinds out that there is no warranty for the APM Library. If the APM XLibrary is modified by someone else and passed on, we want its Xrecipients to know that what they have is not what we distributed, so Xthat any problems introduced by others will not reflect on our Xreputation. X X Therefore we (Lloyd Zusman and Master Byte Software) make the Xfollowing terms which say what you must do to be allowed to Xdistribute or change the APM Library. X X COPYING POLICIES X X1. You may copy and distribute verbatim copies of the APM Library Xsource code as you receive it, in any medium, provided that you Xconspicuously and appropriately publish on each copy a valid copyright Xnotice "Copyright (C) 1988 Lloyd Zusman, Master Byte Software, Los XGatos, California" (or with whatever year is appropriate); keep intact Xthe notices on all files that refer to this License Agreement and to Xthe absence of any warranty; and give any other recipients of the the XAPM Library program a copy of this License Agreement along with the Xprogram. You may charge a distribution fee for the physical act of Xtransferring a copy. X X 2. You may modify your copy or copies of the APM Library source code or Xany portion of it, and copy and distribute such modifications under Xthe terms of Paragraph 1 above, provided that you also do the following: X X a) cause the modified files to carry prominent notices stating X that you changed the files and the date of any change; and X X b) cause the whole of any work that you distribute or publish, that in X whole or in part contains or is a derivative of the APM Library or any X part thereof, to be licensed to all third parties on terms identical X to those contained in this License Agreement (except that you may X choose to grant more extensive warranty protection to some or all X third parties, at your option). X X c) You may charge a distribution fee for the physical act of X transferring a copy, and you may at your option offer warranty X protection in exchange for a fee. X X d) You may not charge a license fee for the whole of any work that X you distribute or publish, that in whole or in part contains or is X a derivative of the APM library or any part thereof, without the X express written permission of Lloyd Zusman and Master Byte Software; X whether this permission is granted for free or in return for goods X services, royalties, or other compensation will be determined X solely by Lloyd Zusman and Master Byte Software. X XMere aggregation of another unrelated program with this program (or its Xderivative) on a volume of a storage or distribution medium does not bring Xthe other program under the scope of these terms. X X 3. You may copy and distribute the APM Library (or a portion or Xderivative of it, under Paragraph 2) in object code or executable form Xunder all the terms of Paragraphs 1 and 2 above provided that you also Xdo one of the following: X X a) accompany it with the complete corresponding machine-readable X source code, which must be distributed under the terms of X Paragraphs 1 and 2 above; or, X X b) accompany it with a written offer, valid for at least three X years, to give any third party free (except for a nominal X shipping charge) a complete machine-readable copy of the X corresponding source code, to be distributed under the terms of X Paragraphs 1 and 2 above; or, X X c) accompany it with the information you received as to where the X corresponding source code may be obtained. (This alternative is X allowed only for noncommercial distribution and only if you X received the program in object code or executable form alone.) X XFor an executable file, complete source code means all the source code Xfor all modules it contains; but, as a special exception, it need not Xinclude source code for modules which are standard libraries that Xaccompany the operating system on which the executable file runs. X X 4. You may not copy, sublicense, distribute or transfer the APM XLibrary except as expressly provided under this License Agreement. XAny attempt otherwise to copy, sublicense, distribute or transfer the XAPM Library is void and your rights to use the APM Library under this XLicense agreement shall be automatically terminated. However, parties Xwho have received computer software programs from you with this XLicense Agreement will not have their licenses terminated so long as Xsuch parties remain in full compliance. X X 5. If you wish to incorporate parts of the APM Library into other Xprograms whose distribution conditions are different, write to Lloyd XZusman at Master Byte Software. We have not yet worked out a simple Xrule that can be stated here, but we will often permit this. We will Xbe guided by the goals of (1) preserving the free status of all Xderivatives of our free software; of (2) promoting the sharing and Xreuse of software; and of (3) not allowing anyone to profit from the Xuse of our software without us also having the opportunity to share Xin these profits. X XYour comments and suggestions about our licensing policies and our Xsoftware are welcome! Please contact Lloyd Zusman, Master Byte XSoftware, 127 Wilder Ave., Los Gatos, California 95030, or call X(408) 395-5693. X X NO WARRANTY X X BECAUSE THE APM LIBRARY IS LICENSED FREE OF CHARGE, WE PROVIDE XABSOLUTELY NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE XLAW. EXCEPT WHEN OTHERWISE STATED IN WRITING, MASTER BYTE SOFTWARE, XLLOYD ZUSMAN AND/OR OTHER PARTIES PROVIDE THE APM LIBRARY "AS IS" XWITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, XBUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND XFITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY XAND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE THE APM XLIBRARY PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY XSERVICING, REPAIR OR CORRECTION. X X IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL MASTER BYTE XSOFTWARE, LLOYD ZUSMAN, AND/OR ANY OTHER PARTY WHO MAY MODIFY AND XREDISTRIBUTE THE APM LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU FOR XDAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR OTHER SPECIAL, XINCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR XINABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA XBEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR A XFAILURE OF THE PROGRAM TO OPERATE WITH PROGRAMS NOT DISTRIBUTED BY XMASTER BYTE SOFTWARE) THE PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF XTHE POSSIBILITY OF SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. @\END_OF_FILE_LICENSE else echo "shar: Will not over write LICENSE" fi if `test ! -s Makefile` then echo "x - Makefile" sed 's/^X//' > Makefile << '@\END_OF_FILE_Makefile' X# X# Makefile for building the APM library, "libapm.a" X# X XCFLAGS = -O X XLIBRARY = libapm.a X XOBJS = addsub.o calc.o memory.o misc.o muldiv.o utils.o X XSRCS = addsub.c calc.c memory.c misc.c muldiv.c utils.c X Xall: $(LIBRARY) X X$(LIBRARY): $(OBJS) X ar cru $(LIBRARY) $(OBJS) X ranlib $(LIBRARY) X Xtest: test/apmtest X Xtest/apmtest: $(LIBRARY) X cd test; make X Xclean: X rm -f $(OBJS) X cd test; make clean X X# The 'depend' production assumes the use of a program called 'makedepend'. X# Change your 'depend' line accordingly. X Xdepend: X makedepend $(SRCS) X cd test ; make depend X X# The 'shar' production assumes the use of a rather specific shar program X# called 'doshar'. Change your 'shar' line accordingly. X Xshar: X doshar README FUNCTIONS LICENSE Makefile makefile.msc apm.arf \ X *.c *.h test/Makefile test/makefile.msc test/apmtest.c X X### X# DO NOT DELETE THIS LINE -- makedepend uses it. X# DO NOT PUT ANYTHING AFTER THIS LINE, IT WILL GO AWAY. X Xaddsub.c: Xaddsub.o: addsub.c /usr/include/stdio.h apm.h apmlocal.h Xcalc.c: Xcalc.o: calc.c /usr/include/stdio.h /usr/include/varargs.h apm.h apmlocal.h Xmemory.c: Xmemory.o: memory.c /usr/include/stdio.h apm.h apmlocal.h Xmisc.c: Xmisc.o: misc.c /usr/include/stdio.h apm.h apmlocal.h Xmuldiv.c: Xmuldiv.o: muldiv.c /usr/include/stdio.h apm.h apmlocal.h Xutils.c: Xutils.o: utils.c /usr/include/stdio.h /usr/include/varargs.h apm.h apmlocal.h X X# IF YOU PUT ANYTHING HERE IT WILL GO AWAY @\END_OF_FILE_Makefile else echo "shar: Will not over write Makefile" fi if `test ! -s makefile.msc` then echo "x - makefile.msc" sed 's/^X//' > makefile.msc << '@\END_OF_FILE_makefile.msc' XM = S X XCFLAGS = -A$(M) -DMSC -Ox X XMAKEFILE = makefile X XLIBRARY = apm.lib X XRESPFILE = apm.arf X XSRCS = addsub.c \ X calc.c \ X memory.c \ X misc.c \ X muldiv.c \ X utils.c X XOBJS = addsub.obj \ X calc.obj \ X memory.obj \ X misc.obj \ X muldiv.obj \ X utils.obj X X.c.obj: X cl $(CFLAGS) -c $*.c X Xall: $(LIBRARY) X X$(LIBRARY): $(OBJS) X @-rm -f $(LIBRARY) X lib @$(RESPFILE) X Xclean: X rm -f $(OBJS) X X### Xaddsub.obj: c:/ms/include/stdio.h apm.h c:/ms/include/malloc.h apmlocal.h Xcalc.obj: c:/ms/include/stdio.h c:/ms/include/varargs.h apm.h \ X c:/ms/include/malloc.h apmlocal.h Xmemory.obj: c:/ms/include/stdio.h apm.h c:/ms/include/malloc.h apmlocal.h Xmisc.obj: c:/ms/include/stdio.h apm.h c:/ms/include/malloc.h apmlocal.h Xmuldiv.obj: c:/ms/include/stdio.h apm.h c:/ms/include/malloc.h apmlocal.h Xutils.obj: c:/ms/include/stdio.h c:/ms/include/varargs.h apm.h \ X c:/ms/include/malloc.h apmlocal.h @\END_OF_FILE_makefile.msc else echo "shar: Will not over write makefile.msc" fi if `test ! -s apm.arf` then echo "x - apm.arf" sed 's/^X//' > apm.arf << '@\END_OF_FILE_apm.arf' Xapm.lib Xy X+addsub.obj& X+calc.obj& X+memory.obj& X+misc.obj& X+muldiv.obj& X+utils.obj Xnul @\END_OF_FILE_apm.arf else echo "shar: Will not over write apm.arf" fi if `test ! -s addsub.c` then echo "x - addsub.c" sed 's/^X//' > addsub.c << '@\END_OF_FILE_addsub.c' X/****************************************************************************** X X Arbitrary Precision Math Library General Public License X (Written October 5, 1988) X X Copyright (C) 1988 Lloyd Zusman, Master Byte Software, Los X Gatos, California. Everyone is permitted to copy and distribute X verbatim copies of this license, but changing it is not allowed. X You can also use this wording to make the terms for other programs. X X The wording of this license is based on that of the X "GNU EMACS GENERAL PUBLIC LICENSE" by Richard Stallman, X Copyright (C) 1985, 1987, 1988, version of February 11, 1988, X but since some of the text has been changed, please be sure to X READ THIS CAREFULLY! X X This general public license is intended to give everyone the right Xto share the Arbitrary Precision Math Library (hereinafter referred to Xas the "APM Library"). To make sure that you get the rights we want Xyou to have, I need to make restrictions that forbid anyone to deny Xyou these rights or to ask you to surrender the rights. X X Specifically, we want to make sure that you have the right to give Xaway copies of the APM Library, that you receive source code or else Xcan get it if you want it, that you can change the APM Library or use Xpieces of it in new programs, and that you know you can do these Xthings. X X To make sure that everyone has such rights, we have to forbid you to Xdeprive anyone else of these rights. For example, if you distribute Xcopies of the APM Library, you must give the recipients all the Xrights that you have. You must make sure that they, too, receive or Xcan get the source code. And you must tell them their rights. X X Also, for our own protection, we must make certain that everyone Xfinds out that there is no warranty for the APM Library. If the APM XLibrary is modified by someone else and passed on, we want its Xrecipients to know that what they have is not what we distributed, so Xthat any problems introduced by others will not reflect on our Xreputation. X X Therefore we (Lloyd Zusman and Master Byte Software) make the Xfollowing terms which say what you must do to be allowed to Xdistribute or change the APM Library. X X COPYING POLICIES X X1. You may copy and distribute verbatim copies of the APM Library Xsource code as you receive it, in any medium, provided that you Xconspicuously and appropriately publish on each copy a valid copyright Xnotice "Copyright (C) 1988 Lloyd Zusman, Master Byte Software, Los XGatos, California" (or with whatever year is appropriate); keep intact Xthe notices on all files that refer to this License Agreement and to Xthe absence of any warranty; and give any other recipients of the the XAPM Library program a copy of this License Agreement along with the Xprogram. You may charge a distribution fee for the physical act of Xtransferring a copy. X X 2. You may modify your copy or copies of the APM Library source code or Xany portion of it, and copy and distribute such modifications under Xthe terms of Paragraph 1 above, provided that you also do the following: X X a) cause the modified files to carry prominent notices stating X that you changed the files and the date of any change; and X X b) cause the whole of any work that you distribute or publish, that in X whole or in part contains or is a derivative of the APM Library or any X part thereof, to be licensed to all third parties on terms identical X to those contained in this License Agreement (except that you may X choose to grant more extensive warranty protection to some or all X third parties, at your option). X X c) You may charge a distribution fee for the physical act of X transferring a copy, and you may at your option offer warranty X protection in exchange for a fee. X X d) You may not charge a license fee for the whole of any work that X you distribute or publish, that in whole or in part contains or is X a derivative of the APM library or any part thereof, without the X express written permission of Lloyd Zusman and Master Byte Software; X whether this permission is granted for free or in return for goods X services, royalties, or other compensation will be determined X solely by Lloyd Zusman and Master Byte Software. X XMere aggregation of another unrelated program with this program (or its Xderivative) on a volume of a storage or distribution medium does not bring Xthe other program under the scope of these terms. X X 3. You may copy and distribute the APM Library (or a portion or Xderivative of it, under Paragraph 2) in object code or executable form Xunder all the terms of Paragraphs 1 and 2 above provided that you also Xdo one of the following: X X a) accompany it with the complete corresponding machine-readable X source code, which must be distributed under the terms of X Paragraphs 1 and 2 above; or, X X b) accompany it with a written offer, valid for at least three X years, to give any third party free (except for a nominal X shipping charge) a complete machine-readable copy of the X corresponding source code, to be distributed under the terms of X Paragraphs 1 and 2 above; or, X X c) accompany it with the information you received as to where the X corresponding source code may be obtained. (This alternative is X allowed only for noncommercial distribution and only if you X received the program in object code or executable form alone.) X XFor an executable file, complete source code means all the source code Xfor all modules it contains; but, as a special exception, it need not Xinclude source code for modules which are standard libraries that Xaccompany the operating system on which the executable file runs. X X 4. You may not copy, sublicense, distribute or transfer the APM XLibrary except as expressly provided under this License Agreement. XAny attempt otherwise to copy, sublicense, distribute or transfer the XAPM Library is void and your rights to use the APM Library under this XLicense agreement shall be automatically terminated. However, parties Xwho have received computer software programs from you with this XLicense Agreement will not have their licenses terminated so long as Xsuch parties remain in full compliance. X X 5. If you wish to incorporate parts of the APM Library into other Xprograms whose distribution conditions are different, write to Lloyd XZusman at Master Byte Software. We have not yet worked out a simple Xrule that can be stated here, but we will often permit this. We will Xbe guided by the goals of (1) preserving the free status of all Xderivatives of our free software; of (2) promoting the sharing and Xreuse of software; and of (3) not allowing anyone to profit from the Xuse of our software without us also having the opportunity to share Xin these profits. X XYour comments and suggestions about our licensing policies and our Xsoftware are welcome! Please contact Lloyd Zusman, Master Byte XSoftware, 127 Wilder Ave., Los Gatos, California 95030, or call X(408) 395-5693. X X NO WARRANTY X X BECAUSE THE APM LIBRARY IS LICENSED FREE OF CHARGE, WE PROVIDE XABSOLUTELY NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE XLAW. EXCEPT WHEN OTHERWISE STATED IN WRITING, MASTER BYTE SOFTWARE, XLLOYD ZUSMAN AND/OR OTHER PARTIES PROVIDE THE APM LIBRARY "AS IS" XWITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, XBUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND XFITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY XAND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE THE APM XLIBRARY PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY XSERVICING, REPAIR OR CORRECTION. X X IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL MASTER BYTE XSOFTWARE, LLOYD ZUSMAN, AND/OR ANY OTHER PARTY WHO MAY MODIFY AND XREDISTRIBUTE THE APM LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU FOR XDAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR OTHER SPECIAL, XINCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR XINABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA XBEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR A XFAILURE OF THE PROGRAM TO OPERATE WITH PROGRAMS NOT DISTRIBUTED BY XMASTER BYTE SOFTWARE) THE PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF XTHE POSSIBILITY OF SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. X X******************************************************************************/ X X X/* X * Addition and subtraction routines for the APM library. X * X * $Log: addsub.c,v $ X * Revision 1.0 88/10/05 12:38:08 ljz X * Initial release. X * X */ X#ifndef lint Xstatic char rcsid[] = "$Header: addsub.c,v 1.0 88/10/05 12:38:08 ljz Exp $"; X#endif /* ! lint */ X X#include <stdio.h> X#include "apm.h" X#include "apmlocal.h" X Xint Xapm_add(result, num1, num2) XAPM result; XAPM num1; XAPM num2; X{ X int length1; X int length2; X int leftofdp1; X int leftofdp2; X int len; X int dp; X int n; X int n1; X int n2; X int ercode; X short sign; X short base; X short tempval; X short carry; X short c1; X short c2; X X apm_errno = APM_OK; X X ERR_RETURN(APM_val_format(result)); X ERR_RETURN(apm_validate(num1)); X ERR_RETURN(apm_validate(num2)); X X if (result == num1 || result == num2) { X return (APM_error(APM_EOVERLAP)); X } X if (num1->base != num2->base) { X return (APM_error(APM_EBASE)); X } X base = num1->base; X X if (num1->sign < 0 && num2->sign >= 0) { X num1->sign = 1; X ercode = apm_subtract(result, num2, num1); X num1->sign = -1; X return (APM_error(ercode)); X } X else if (num1->sign >= 0 && num2->sign < 0) { X num2->sign = 1; X ercode = apm_subtract(result, num1, num2); X num2->sign = -1; X return (APM_error(ercode)); X } X X sign = SIGNOF(num1->sign); X X ERR_RETURN(APM_trim(num1, 1, 1)); X ERR_RETURN(APM_trim(num2, 1, 1)); X X length1 = num1->length; X length2 = num2->length; X X if (num1->dp >= num2->dp) { X dp = num1->dp; X n1 = 0; X n2 = num2->dp - num1->dp; X } X else { X dp = num2->dp; X n1 = num1->dp - num2->dp; X n2 = 0; X } X X leftofdp1 = length1 - num1->dp; X leftofdp2 = length2 - num2->dp; X X if (leftofdp1 >= leftofdp2) { X len = leftofdp1 + dp + 1; X } X else { X len = leftofdp2 + dp + 1; X } X X ERR_RETURN(APM_size(result, len)); X X carry = 0; X for (n = 0; n < len - 1; ++n, ++n1, ++n2) { X if (n1 < 0 || n1 >= length1) { X c1 = 0; X } X else { X c1 = num1->data[n1]; X } X if (n2 < 0 || n2 >= length2) { X c2 = 0; X } X else { X c2 = num2->data[n2]; X } X tempval = c1 + c2 + carry; X result->data[n] = tempval % base; X carry = tempval / base; X } X X result->data[n] = carry; X result->base = base; X result->length = len; X result->sign = sign; X result->dp = dp; X X return (APM_error(APM_trim(result, 1, 1))); X} X Xint Xapm_subtract(result, num1, num2) XAPM result; XAPM num1; XAPM num2; X{ X int length1; X int length2; X int leftofdp1; X int leftofdp2; X int len; X int dp; X int n; X int n1; X int n2; X int ercode; X short base; X short tempval; X short borrow; X short c1; X short c2; X X apm_errno = APM_OK; X X ERR_RETURN(APM_val_format(result)); X ERR_RETURN(apm_validate(num1)); X ERR_RETURN(apm_validate(num2)); X X if (result == num1 || result == num2) { X return (APM_error(APM_EOVERLAP)); X } X if (num1->base != num2->base) { X return (APM_error(APM_EBASE)); X } X X base = num1->base; X X if (num1->sign < 0 && num2->sign >= 0) { X num1->sign = 1; X ercode = apm_add(result, num2, num1); X num1->sign = -1; X if (ercode >= APM_OK) { X result->sign = -(result->sign); X } X return (APM_error(ercode)); X } X else if (num1->sign >= 0 && num2->sign < 0) { X num2->sign = 1; X ercode = apm_add(result, num1, num2); X num2->sign = -1; X return (APM_error(ercode)); X } X X ERR_RETURN(APM_trim(num1, 1, 1)); X ERR_RETURN(APM_trim(num2, 1, 1)); X X length1 = num1->length; X length2 = num2->length; X X if (num1->dp >= num2->dp) { X dp = num1->dp; X n1 = 0; X n2 = num2->dp - num1->dp; X } X else { X dp = num2->dp; X n1 = num1->dp - num2->dp; X n2 = 0; X } X X leftofdp1 = length1 - num1->dp; X leftofdp2 = length2 - num2->dp; X X if (leftofdp1 >= leftofdp2) { X len = leftofdp1 + dp + 1; X } X else { X len = leftofdp2 + dp + 1; X } X X ERR_RETURN(APM_size(result, len)); X X borrow = 0; X for (n = 0; n < len - 1; ++n, ++n1, ++n2) { X if (n1 < 0 || n1 >= length1) { X c1 = 0; X } X else { X c1 = num1->data[n1]; X } X if (n2 < 0 || n2 >= length2) { X c2 = 0; X } X else { X c2 = num2->data[n2]; X } X tempval = (c1 + borrow) - c2; X if (tempval < 0) { X borrow = -1; X tempval += base; X } X else { X borrow = 0; X } X result->data[n] = tempval; X } X X if (borrow == 0) { X result->data[n] = 0; X result->length = len; X result->sign = 1; X } X else { X int carry = 1; X int basem1 = base - 1; X result->data[n] = basem1; X for (n = 0; n < len; ++n) { X int value = result->data[n]; X value = (basem1 - value) + carry; X result->data[n] = value % base; X carry = value / base; X } X result->sign = -1; X } X X if (num1->sign < 0) { X result->sign = -(result->sign); X } X X result->length = len; X result->base = base; X result->dp = dp; X X return (APM_error(APM_trim(result, 1, 1))); X} @\END_OF_FILE_addsub.c else echo "shar: Will not over write addsub.c" fi if `test ! -s calc.c` then echo "x - calc.c" sed 's/^X//' > calc.c << '@\END_OF_FILE_calc.c' X/****************************************************************************** X X Arbitrary Precision Math Library General Public License X (Written October 5, 1988) X X Copyright (C) 1988 Lloyd Zusman, Master Byte Software, Los X Gatos, California. Everyone is permitted to copy and distribute X verbatim copies of this license, but changing it is not allowed. X You can also use this wording to make the terms for other programs. X X The wording of this license is based on that of the X "GNU EMACS GENERAL PUBLIC LICENSE" by Richard Stallman, X Copyright (C) 1985, 1987, 1988, version of February 11, 1988, X but since some of the text has been changed, please be sure to X READ THIS CAREFULLY! X X This general public license is intended to give everyone the right Xto share the Arbitrary Precision Math Library (hereinafter referred to Xas the "APM Library"). To make sure that you get the rights we want Xyou to have, I need to make restrictions that forbid anyone to deny Xyou these rights or to ask you to surrender the rights. X X Specifically, we want to make sure that you have the right to give Xaway copies of the APM Library, that you receive source code or else Xcan get it if you want it, that you can change the APM Library or use Xpieces of it in new programs, and that you know you can do these Xthings. X X To make sure that everyone has such rights, we have to forbid you to Xdeprive anyone else of these rights. For example, if you distribute Xcopies of the APM Library, you must give the recipients all the Xrights that you have. You must make sure that they, too, receive or Xcan get the source code. And you must tell them their rights. X X Also, for our own protection, we must make certain that everyone Xfinds out that there is no warranty for the APM Library. If the APM XLibrary is modified by someone else and passed on, we want its Xrecipients to know that what they have is not what we distributed, so Xthat any problems introduced by others will not reflect on our Xreputation. X X Therefore we (Lloyd Zusman and Master Byte Software) make the Xfollowing terms which say what you must do to be allowed to Xdistribute or change the APM Library. X X COPYING POLICIES X X1. You may copy and distribute verbatim copies of the APM Library Xsource code as you receive it, in any medium, provided that you Xconspicuously and appropriately publish on each copy a valid copyright Xnotice "Copyright (C) 1988 Lloyd Zusman, Master Byte Software, Los XGatos, California" (or with whatever year is appropriate); keep intact Xthe notices on all files that refer to this License Agreement and to Xthe absence of any warranty; and give any other recipients of the the XAPM Library program a copy of this License Agreement along with the Xprogram. You may charge a distribution fee for the physical act of Xtransferring a copy. X X 2. You may modify your copy or copies of the APM Library source code or Xany portion of it, and copy and distribute such modifications under Xthe terms of Paragraph 1 above, provided that you also do the following: X X a) cause the modified files to carry prominent notices stating X that you changed the files and the date of any change; and X X b) cause the whole of any work that you distribute or publish, that in X whole or in part contains or is a derivative of the APM Library or any X part thereof, to be licensed to all third parties on terms identical X to those contained in this License Agreement (except that you may X choose to grant more extensive warranty protection to some or all X third parties, at your option). X X c) You may charge a distribution fee for the physical act of X transferring a copy, and you may at your option offer warranty X protection in exchange for a fee. X X d) You may not charge a license fee for the whole of any work that X you distribute or publish, that in whole or in part contains or is X a derivative of the APM library or any part thereof, without the X express written permission of Lloyd Zusman and Master Byte Software; X whether this permission is granted for free or in return for goods X services, royalties, or other compensation will be determined X solely by Lloyd Zusman and Master Byte Software. X XMere aggregation of another unrelated program with this program (or its Xderivative) on a volume of a storage or distribution medium does not bring Xthe other program under the scope of these terms. X X 3. You may copy and distribute the APM Library (or a portion or Xderivative of it, under Paragraph 2) in object code or executable form Xunder all the terms of Paragraphs 1 and 2 above provided that you also Xdo one of the following: X X a) accompany it with the complete corresponding machine-readable X source code, which must be distributed under the terms of X Paragraphs 1 and 2 above; or, X X b) accompany it with a written offer, valid for at least three X years, to give any third party free (except for a nominal X shipping charge) a complete machine-readable copy of the X corresponding source code, to be distributed under the terms of X Paragraphs 1 and 2 above; or, X X c) accompany it with the information you received as to where the X corresponding source code may be obtained. (This alternative is X allowed only for noncommercial distribution and only if you X received the program in object code or executable form alone.) X XFor an executable file, complete source code means all the source code Xfor all modules it contains; but, as a special exception, it need not Xinclude source code for modules which are standard libraries that Xaccompany the operating system on which the executable file runs. X X 4. You may not copy, sublicense, distribute or transfer the APM XLibrary except as expressly provided under this License Agreement. XAny attempt otherwise to copy, sublicense, distribute or transfer the XAPM Library is void and your rights to use the APM Library under this XLicense agreement shall be automatically terminated. However, parties Xwho have received computer software programs from you with this XLicense Agreement will not have their licenses terminated so long as Xsuch parties remain in full compliance. X X 5. If you wish to incorporate parts of the APM Library into other Xprograms whose distribution conditions are different, write to Lloyd XZusman at Master Byte Software. We have not yet worked out a simple Xrule that can be stated here, but we will often permit this. We will Xbe guided by the goals of (1) preserving the free status of all Xderivatives of our free software; of (2) promoting the sharing and Xreuse of software; and of (3) not allowing anyone to profit from the Xuse of our software without us also having the opportunity to share Xin these profits. X XYour comments and suggestions about our licensing policies and our Xsoftware are welcome! Please contact Lloyd Zusman, Master Byte XSoftware, 127 Wilder Ave., Los Gatos, California 95030, or call X(408) 395-5693. X X NO WARRANTY X X BECAUSE THE APM LIBRARY IS LICENSED FREE OF CHARGE, WE PROVIDE XABSOLUTELY NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE XLAW. EXCEPT WHEN OTHERWISE STATED IN WRITING, MASTER BYTE SOFTWARE, XLLOYD ZUSMAN AND/OR OTHER PARTIES PROVIDE THE APM LIBRARY "AS IS" XWITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, XBUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND XFITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY XAND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE THE APM XLIBRARY PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY XSERVICING, REPAIR OR CORRECTION. X X IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL MASTER BYTE XSOFTWARE, LLOYD ZUSMAN, AND/OR ANY OTHER PARTY WHO MAY MODIFY AND XREDISTRIBUTE THE APM LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU FOR XDAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR OTHER SPECIAL, XINCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR XINABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA XBEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR A XFAILURE OF THE PROGRAM TO OPERATE WITH PROGRAMS NOT DISTRIBUTED BY XMASTER BYTE SOFTWARE) THE PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF XTHE POSSIBILITY OF SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. X X******************************************************************************/ X X X/* X * Calculator mode for the APM library. X * X * $Log: calc.c,v $ X * Revision 1.0 88/10/05 12:38:11 ljz X * Initial release. X * X */ X#ifndef lint Xstatic char rcsid[] = "$Header: calc.c,v 1.0 88/10/05 12:38:11 ljz Exp $"; X#endif /* ! lint */ X X#include <stdio.h> X#include <varargs.h> X#include "apm.h" X#include "apmlocal.h" X X/* X * The stack is declared to contain one more value than is used so that X * the calculation of &APM_stack[APM_STACK_SIZE] doesn't generate an X * error (I have been led to believe that under certain machine X * architectures, calculating an invalid pointer will cause an error X * even if the pointer is not dereferenced). X */ Xstatic APM APM_stack[APM_STACK_SIZE + 1]; Xstatic APM *APM_stack_ptr = (APM *)NULL; X X/* X * We need a few scratch registers. The user can't get to them, but X * I use them in some of these routines. X */ X#define NUM_SCRATCH 3 X#define TOT_REGISTERS (APM_NUM_REGISTERS + NUM_SCRATCH) Xstatic APM APM_register[TOT_REGISTERS]; X X/* X * This macro is used to access the scratch registers. X */ X#define XREG(N) APM_register[APM_NUM_REGISTERS + (N)] X Xstatic int XAPM_get_reg(value, regnum) XAPM value; Xint regnum; X{ X APM reg; X int ercode = APM_OK; X X APM_calc_init(); X X apm_errno = APM_OK; X X if (regnum < 0 || regnum >= APM_NUM_REGISTERS) { X return (APM_set_errno(APM_EPARM)); X } X X reg = APM_register[regnum]; X ercode = APM_size(value, reg->length); X if (ercode < APM_OK) { X return (APM_set_errno(ercode)); X } X X APM_copy_bytes(value->data, reg->data, reg->length * sizeof(short)); X value->sign = SIGNOF(reg->sign); X value->base = reg->base; X value->length = reg->length; X value->dp = reg->dp; X return (APM_OK); X} X Xstatic int XAPM_set_reg(previous, regnum, value) XAPM previous; Xint regnum; XAPM value; X{ X APM reg; X int ercode = APM_OK; X X APM_calc_init(); X X apm_errno = APM_OK; X X if (regnum < 0 || regnum >= APM_NUM_REGISTERS) { X return (APM_set_errno(APM_EPARM)); X } X X X ercode = apm_validate(value); X if (ercode < APM_OK) { X return (APM_set_errno(ercode)); X } X X if (previous == value) { X return (APM_set_errno(APM_EOVERLAP)); X } X X ercode = APM_get_reg(previous, regnum); X if (ercode < APM_OK) { X return (APM_set_errno(ercode)); X } X X reg = APM_register[regnum]; X ercode = APM_size(reg, value->length); X if (ercode < APM_OK) { X return (APM_set_errno(ercode)); X } X X APM_copy_bytes(reg->data, value->data, value->length * sizeof(short)); X reg->sign = SIGNOF(value->sign); X reg->base = value->base; X reg->length = value->length; X reg->dp = value->dp; X return (APM_OK); X} X Xstatic int XAPM_calc_op(value) XAPM value; X{ X int ercode = APM_OK; X X APM_calc_init(); X X if (value == (APM)NULL) { X return (APM_ENULL); X } X else if (value->magic != OP_MAGIC) { X return (APM_EBADVAL); X } X X switch (value->length) { /* length = command, dp = operand */ X case APM_CALC_ABS: /* unary operations */ X case APM_CALC_NEG: X case APM_CALC_RECIP: X case APM_CALC_SCALE: X ercode = APM_calc_unop(value->length, value->dp); X break; X case APM_CALC_ADD: /* binary operations */ X case APM_CALC_SUB: X case APM_CALC_MUL: X case APM_CALC_DIV: X ercode = APM_calc_binop(value->length, value->dp); X break; X case APM_CALC_CLEAR: /* miscellaneous operations */ X case APM_CALC_DUP: X case APM_CALC_SWAP: X case APM_CALC_PUSH: X case APM_CALC_POP: X ercode = APM_calc_misc(value->length, value->dp); X break; X default: X ercode = APM_EBADVAL; X break; X } X X return (ercode); X} X Xstatic int XAPM_calc_unop(operation, operand) Xint operation; Xint operand; X{ X int ercode = APM_OK; X X APM_calc_init(); X X switch (operation) { X case APM_CALC_ABS: X ercode = APM_pop(XREG(0)); X if (ercode != APM_OK) { X break; X } X ercode = apm_absolute_value(XREG(1), XREG(0)); X if (ercode < APM_OK) { X APM_push(XREG(0)); X } X else { X ercode = APM_push(XREG(1)); X } X break; X case APM_CALC_NEG: X ercode = APM_pop(XREG(0)); X if (ercode != APM_OK) { X break; X } X ercode = apm_negate(XREG(1), XREG(0)); X if (ercode < APM_OK) { X APM_push(XREG(0)); X } X else { X ercode = APM_push(XREG(1)); X } X break; X case APM_CALC_RECIP: X ercode = APM_pop(XREG(0)); X if (ercode != APM_OK) { X break; X } X ercode = apm_reciprocal(XREG(1), operand, XREG(0)); X if (ercode < APM_OK) { X APM_push(XREG(0)); X } X else { X ercode = APM_push(XREG(1)); X } X break; X case APM_CALC_SCALE: X ercode = APM_pop(XREG(0)); X if (ercode != APM_OK) { X break; X } X ercode = apm_scale(XREG(1), XREG(0), operand); X if (ercode < APM_OK) { X APM_push(XREG(0)); X } X else { X ercode = APM_push(XREG(1)); X } X break; X default: X ercode = APM_EBADVAL; X break; X } X X return (APM_set_errno(ercode)); X} X Xstatic int XAPM_calc_binop(operation, operand) Xint operation; Xint operand; X{ X int ercode = APM_OK; X X APM_calc_init(); X X switch (operation) { X case APM_CALC_ADD: X APM_pop(XREG(0)); X APM_pop(XREG(1)); X apm_assign_long(XREG(2), 0L, 0, 0); X ercode = apm_add(XREG(2), XREG(1), XREG(0)); X APM_push(XREG(2)); X break; X case APM_CALC_SUB: X APM_pop(XREG(0)); X APM_pop(XREG(1)); X apm_assign_long(XREG(2), 0L, 0, 0); X ercode = apm_subtract(XREG(2), XREG(1), XREG(0)); X APM_push(XREG(2)); X break; X case APM_CALC_MUL: X APM_pop(XREG(0)); X APM_pop(XREG(1)); X apm_assign_long(XREG(2), 0L, 0, 0); X ercode = apm_multiply(XREG(2), XREG(1), XREG(0)); X APM_push(XREG(2)); X break; X case APM_CALC_DIV: X APM_pop(XREG(0)); X APM_pop(XREG(1)); X apm_assign_long(XREG(2), 0L, 0, 0); X ercode = apm_divide(XREG(2), operand, APM_register[0], X XREG(1), XREG(0)); X APM_push(XREG(2)); X break; X default: X ercode = APM_EBADVAL; X break; X } X X return (APM_set_errno(ercode)); X} X Xstatic int XAPM_calc_misc(operation, operand) Xint operation; Xint operand; X{ X int ercode = APM_OK; X X APM_calc_init(); X X switch (operation) { X case APM_CALC_CLEAR: X APM_stack_ptr = &APM_stack[0]; X break; X case APM_CALC_DUP: X if ((ercode = APM_pop(XREG(0))) == APM_OK) { X if ((ercode = APM_push(XREG(0))) == APM_OK) { X ercode = APM_push(XREG(0)); X } X } X break; X case APM_CALC_SWAP: X if ((ercode = APM_pop(XREG(0))) == APM_OK) { X if ((ercode = APM_pop(XREG(1))) == APM_OK) { X if ((ercode = APM_push(XREG(0))) == APM_OK) { X ercode = APM_push(XREG(1)); X } X } X } X break; X case APM_CALC_PUSH: X if (operand < 0 || operand >= APM_NUM_REGISTERS) { X ercode = APM_EPARM; X break; X } X ercode = APM_push(APM_register[operand]); X break; X case APM_CALC_POP: X if (operand < 0 || operand >= APM_NUM_REGISTERS) { X ercode = APM_EPARM; X break; X } X ercode = APM_pop(APM_register[operand]); X break; X default: X ercode = APM_EBADVAL; X break; X } X X return (APM_set_errno(ercode)); X} X Xstatic int XAPM_push(value) XAPM value; X{ X int i; X int ercode = APM_OK; X X APM_calc_init(); X X /* X * If stack is full, shift everything down by one, thereby losing X * the value at the bottom of the stack. X */ X if (APM_stack_ptr >= &APM_stack[APM_STACK_SIZE]) { X for (i = 0; i < APM_STACK_SIZE - 1; ++i) { X (void)apm_assign(APM_stack[i], APM_stack[i + 1]); X } X APM_stack_ptr = &APM_stack[APM_STACK_SIZE - 1]; X } X X ercode = apm_assign(*APM_stack_ptr, value); X if (ercode >= APM_OK) { X ++APM_stack_ptr; X } X X return (ercode); X} X Xstatic int XAPM_pop(value) XAPM value; X{ X int ercode = APM_OK; X X APM_calc_init(); X X if (APM_stack_ptr <= &APM_stack[0]) { /* at bottom of stack */ X ercode = apm_assign_long(value, 0L, 0, 0); X if (ercode >= APM_OK) { X APM_stack_ptr = &APM_stack[0]; /* just in case */ X } X } X else { X ercode = apm_assign(value, APM_stack_ptr[-1]); X if (ercode >= APM_OK) { X --APM_stack_ptr; X } X } X X return (ercode); X} X Xint Xapm_get_register(value, regnum) XAPM value; Xint regnum; X{ X APM reg; X X apm_errno = APM_OK; X X APM_calc_init(); X X if (regnum < 0 || regnum >= APM_NUM_REGISTERS) { X return (APM_error(APM_EPARM)); X } X X ERR_RETURN(APM_get_reg(value, regnum)); X X return (APM_OK); X} X Xint Xapm_set_register(previous, regnum, value) XAPM previous; Xint regnum; XAPM value; X{ X APM reg; X X apm_errno = APM_OK; X X APM_calc_init(); X X if (regnum < 0 || regnum >= APM_NUM_REGISTERS) { X return (APM_error(APM_EPARM)); X } X X ERR_RETURN(APM_set_reg(previous, regnum, value)); X X return (APM_OK); X} X Xint Xapm_calc(va_alist) Xva_dcl X{ X va_list ap; X APM result; X APM value; X X va_start(ap); X X apm_errno = APM_OK; X X result = va_arg(ap, APM); X ERR_RETURN(APM_val_format(result)); X X APM_calc_init(); X X APM_calc_misc(APM_CALC_CLEAR, 0); /* start with a clear stack */ X X while ((value = va_arg(ap, APM)) != NULL) { X if (value->magic == APM_MAGIC) { X ERR_RETURN(APM_push(value)); X } X else if (value->magic == OP_MAGIC) { X int ercode = APM_calc_op(value); X (void)APM_free(value); /* we must free operations */ X ERR_RETURN(ercode); X } X else { X return (APM_set_errno(APM_EBADVAL)); X } X } X X#ifdef MSDOS X va_end(ap); X#else X va_end(); X#endif /* MSDOS */ X X return (APM_pop(result)); X} X XAPM Xapm_op_alloc(type, operand) Xint type; Xint operand; X{ X APM op; X X apm_errno = APM_OK; X X op = APM_alloc(); X if (op == (APM)NULL) { X APM_set_errno(APM_ENOMEM); X return ((APM)NULL); X } X op->magic = OP_MAGIC; X op->length = type; X op->dp = operand; X X return (op); X} X Xvoid XAPM_calc_init() X{ X if (APM_stack_ptr == (APM *)NULL) { X APM local; X int ercode = APM_OK; X int i; X X for (i = 0; i < TOT_REGISTERS; ++i) { X local = APM_alloc(); X if (local == (APM)NULL) { X APM_set_errno(APM_ENOMEM); X break; X } X ercode = APM_parse_long(local, 0L, 0); X if (ercode < APM_OK) { X APM_set_errno(ercode); X break; X } X APM_register[i] = local; X } X for (i = 0; i < APM_STACK_SIZE; ++i) { X local = APM_alloc(); X if (local == (APM)NULL) { X APM_set_errno(APM_ENOMEM); X break; X } X ercode = APM_parse_long(local, 0L, 0); X if (ercode < APM_OK) { X APM_set_errno(ercode); X break; X } X APM_stack[i] = local; X } X APM_stack_ptr = &APM_stack[0]; X } X} @\END_OF_FILE_calc.c else echo "shar: Will not over write calc.c" fi echo "Finished archive 2 of 5" # to concatenate archives, remove anything after this line exit 0