hn-classics/_stories/1976/9516042.md

27 lines
17 KiB
Markdown
Raw Permalink Normal View History

---
created_at: '2015-05-09T10:56:13.000Z'
title: Floating Point Routines for the 6502 (1976)
url: http://www.6502.org/source/floats/wozfp1.txt
author: acqq
points: 56
story_text: ''
comment_text:
num_comments: 24
story_id:
story_title:
story_url:
parent_id:
created_at_i: 1431168973
_tags:
- story
- author_acqq
- story_9516042
objectID: '9516042'
2018-06-08 12:05:27 +00:00
year: 1976
---
2018-02-23 18:19:40 +00:00
[Source](http://www.6502.org/source/floats/wozfp1.txt "Permalink to ")
Dr. Dobb's Journal, August 1976, pages 17-19. Floating Point Routines for the 6502 by Roy Rankin, Department of Mechanical Engineering, Stanford University, Stanford, CA 94305 (415) 497-1822 and Steve Wozniak, Apple Computer Company 770 Welch Road, Suite 154 Palo Alto, CA 94304 (415) 326-4248 Editor's Note: Although these routines are for the 6502, it would appear that one could generate equivalent routines for most of the "traditional" microprocessors, relatively easily, by following the flow of the algorithms given in the excellent comments included in the program listing. This is particularly true of the transcendental functions, which were directly modeled after well-known and proven algorithms, and for which, the comments are relatively machine independent. These floating point routines allow 6502 users to perform most of the more popular and desired floating point and transcendental functions, namely: Natural Log - LOG Common Log - LOG10 Exponential - EXP Floating Add - FADD Floating Subtract - FSUB Floating Multiply - FMUL Floating Divide - FDIV Convert Floating to Fixed - FIX Convert Fixed to Floating - FLOAT They presume a four-byte floating point operand consisting of a one-byte exponent ranging from -128 to +127 and a 24-bit two's complement mantissa between 1.0 and 2.0. The floating point routines were done by Steve Wozniak, one of the principals in Apple Computer Company. The transcendental functions were patterned after those offered by Hewlett-Packard for their HP2100 minicomputer (with some modifications), and were done by Roy Rankin, a Ph.D. student at Stanford University. There are three error traps; two for overflow, and one for prohibited logarithm argument. ERROR (1D06) is the error exit used in the event of a non-positive log argument. OVFLW (1E3B) is the error exit for overflow occuring during calculation of e to some power. OVFL (1FE4) is the error exit for overflow in all of the floating point routines. There is no trap for underflow; in such cases, the result is set to 0.0. All routines are called and exited in a uniform manner: The arguments(s) are placed in the specified floating point storage locations (for specifics, see the documentation preceeding each routine in the listing), then a JSR is used to enter the desired routine. Upon normal completion, the called routine is exited via a subroutine return instruction (RTS). Note: The preceeding documentation was written by the Editor, based on phone conversations with Roy and studying the listing. There is a high probability that it is correct. However, since it was not written nor reviewed by the authors of these routines, the preceeding documentation may contain errors in concept or in detail. \-- JCW, Jr. In the Exponent: 00 Represents -128 ... 7F Represents -1 80 Represents 0 81 Represents +1 ... FF Represents +127 Exponent Two's Complement Mantissa SEEEEEEE SM.MMMMMM MMMMMMMM MMMMMMMM n n+1 n+2 n+3 * JULY 5, 1976 * BASIC FLOATING POINT ROUTINES * FOR 6502 MICROPROCESSOR * BY R. RANKIN AND S. WOZNIAK * * CONSISTING OF: * NATURAL LOG * COMMON LOG * EXPONENTIAL (E**X) * FLOAT FIX * FADD FSUB * FMUL FDIV * * * FLOATING POINT REPRESENTATION (4-BYTES) * EXPONENT BYTE 1 * MANTISSA BYTES 2-4 * * MANTISSA: TWO'S COMPLIMENT REPRESENTATION WITH SIGN IN * MSB OF HIGH-ORDER BYTE. MANTISSA IS NORMALIZED WITH AN * ASSUMED DECIMAL POINT BETWEEN BITS 5 AND 6 OF THE HIGH-ORDER * BYTE. THUS THE MANTISSA IS IN THE RANGE 1. TO 2. EXCEPT * WHEN THE NUMBER IS LESS THAN 2**(-128). * * EXPONENT: THE EXPONENT REPRESENTS POWERS OF TWO. THE * REPRESENTATION IS 2'S COMPLIMENT EXCEPT THAT THE SIGN * BIT (BIT 7) IS COMPLIMENTED. THIS ALLOWS DIRECT COMPARISON * OF EXPONENTS FOR SIZE SINCE THEY ARE STORED IN INCREASING * NUMERICAL SEQUENCE RANGING FROM $00 (-128) TO $FF (+127) * ($ MEANS NUMBER IS HEXADECIMAL). * * REPRESENTATION OF DECIMAL NUMBERS: THE PRESENT FLOATING * POINT REPRESENTATION ALLOWS DECIMAL NUMBERS IN THE APPROXIMATE * RANGE OF 10**(-38) THROUGH 10**(38) WITH 6 TO 7 SIGNIFICANT * DIGITS. * * 0003 ORG 3 SET BASE PAGE ADRESSES 0003 EA SIGN NOP 0004 EA X2 N