Skip to content
qd-extra.lisp 54.5 KiB
Newer Older
;;;; -*- Mode: lisp -*-
;;;;
;;;; Copyright (c) 2007 Raymond Toy
;;;;
;;;; Permission is hereby granted, free of charge, to any person
;;;; obtaining a copy of this software and associated documentation
;;;; files (the "Software"), to deal in the Software without
;;;; restriction, including without limitation the rights to use,
;;;; copy, modify, merge, publish, distribute, sublicense, and/or sell
;;;; copies of the Software, and to permit persons to whom the
;;;; Software is furnished to do so, subject to the following
;;;; conditions:
;;;;
;;;; The above copyright notice and this permission notice shall be
;;;; included in all copies or substantial portions of the Software.
;;;;
;;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
;;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
;;;; OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
;;;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
;;;; HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
;;;; WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
;;;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
;;;; OTHER DEALINGS IN THE SOFTWARE.

;;; This file contains various possible implementations of some of the
;;; core routines.  These were experiments on faster and/or more
;;; accurate implementations.  The routines inf qd-fun.lisp are the
;;; default, but you can select a different implementation from here
;;; if you want.
;;;
;;; The end of the file also includes some tests of the different
;;; implementations for speed.

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479

;; This works but seems rather slow, so we don't even compile it.
#+(or)
(defun exp-qd/newton (a)
  (declare (type %quad-double a))
  ;; Newton iteration
  ;;
  ;; f(x) = log(x) - a
  ;;
  ;; x' = x - (log(x) - a)/(1/x)
  ;;    = x - x*(log(x) - a)
  ;;    = x*(1 + a - log(x))
  (let ((a1 (add-qd-d a 1d0))
	(x (make-qd-d (exp (qd-0 a)))))
    (setf x (mul-qd x (sub-qd a1 (log-qd/agm x))))
    (setf x (mul-qd x (sub-qd a1 (log-qd/agm x))))
    (setf x (mul-qd x (sub-qd a1 (log-qd/agm x))))
    x))

(defun expm1-qd/series (a)
  (declare (type %quad-double a))
  ;; Compute exp(x) - 1.
  ;;
  ;; D(x) = exp(x) - 1
  ;;
  ;; First, write x = s*log(2) + r*k where s is an integer and |r*k| <
  ;; log(2)/2.
  ;;
  ;; Then D(x) = D(s*log(2)+r*k) = 2^s*exp(r*k) - 1
  ;;           = 2^s*(exp(r*k)-1) - 1 + 2^s
  ;;           = 2^s*D(r*k)+2^s-1
  ;; But
  ;; exp(r*k) = exp(r)^k
  ;;          = (D(r) + 1)^k
  ;;
  ;; So
  ;; D(r*k) = (D(r) + 1)^k - 1
  ;;
  ;; For small r, D(r) can be computed using the Taylor series around
  ;; zero.  To compute D(r*k) = (D(r) + 1)^k - 1, we use the binomial
  ;; theorem to expand out the power and to exactly cancel out the -1
  ;; term, which is the source of inaccuracy.
  ;;
  ;; We want to have small r so the Taylor series converges quickly,
  ;; but that means k is large, which means the binomial expansion is
  ;; long.  We need to compromise.  Let use choose k = 8.  Then |r| <
  ;; log(2)/16 = 0.0433.  For this range, the Taylor series converges
  ;; to 212 bits of accuracy with about 28 terms.
  ;;
  ;;
  (flet ((taylor (x)
	   (declare (type %quad-double x))
	   ;; Taylor series for exp(x)-1
	   ;; = x+x^2/2!+x^3/3!+x^4/4!+...
	   ;; = x*(1+x/2!+x^2/3!+x^3/4!+...)
	   (let ((sum +qd-one+)
		 (term +qd-one+))
	     (dotimes (k 28)
	       (setf term (div-qd-d (mul-qd term x) (float (cl:+ k 2) 1d0)))
	       (setf sum (add-qd sum term)))
	     (mul-qd x sum)))
	 (binom (x)
	   (declare (type %quad-double x))
	   ;; (1+x)^8-1
	   ;; = x*(8 + 28*x + 56*x^2 + 70*x^3 + 56*x^4 + 28*x^5 + 8*x^6 + x^7)
	   ;; = x (x (x (x (x (x (x (x + 8) + 28) + 56) + 70) + 56) + 28) + 8)
	   (mul-qd
	    x
	    (add-qd-d
	     (mul-qd x
		     (add-qd-d
		      (mul-qd x
			      (add-qd-d
			       (mul-qd x
				       (add-qd-d
					(mul-qd x
						(add-qd-d
						 (mul-qd x
							 (add-qd-d
							  (mul-qd x
								  (add-qd-d x 8d0))
							  28d0))
						 56d0))
					70d0))
			       56d0))
		      28d0))
	     8d0)))
	 (arg-reduce (x)
	   (declare (type %quad-double x))
	   ;; Write x = s*log(2) + r*k where s is an integer and |r*k|
	   ;; < log(2)/2, and k = 8.
	   (let* ((s (truncate (qd-0 (nint-qd (div-qd a +qd-log2+)))))
		  (r*k (sub-qd x (mul-qd-d +qd-log2+ (float s 1d0))))
		  (r (div-qd-d r*k 8d0)))
	     (values s r))))
    (multiple-value-bind (s r)
	(arg-reduce a)
      (let* ((d (taylor r))
	     (dr (binom d)))
	(add-qd-d (scale-float-qd dr s)
		  (cl:- (scale-float 1d0 s) 1))))))
    
(defun log-qd/newton (a)
  (declare (type %quad-double a))
  ;; The Taylor series for log converges rather slowly.  Hence, this
  ;; routine tries to determine the root of the function
  ;;
  ;; f(x) = exp(x) - a
  ;;
  ;; using Newton iteration.  The iteration is
  ;;
  ;; x' = x - f(x) / f'(x)
  ;;    = x - (1 - a * exp(-x))
  ;;    = x + a * exp(-x) - 1
  ;;
  ;; Two iterations are needed.
  (let ((x (make-qd-d (log (qd-0 a)))))
    (dotimes (k 3)
      (setf x (sub-qd-d (add-qd x (mul-qd a (exp-qd (neg-qd x))))
			1d0)))
    x))


;;(declaim (inline agm-qd))

(defun agm-qd (x y)
  (declare (type %quad-double x y)
	   (optimize (speed 3)))
  (let ((diff (qd-0 (abs-qd (sub-qd x y)))))
    (cond ((< diff +qd-eps+)
	   x)
	  (t
	   (let ((a-mean (div-qd-d (add-qd x y) 2d0))
		 (g-mean (sqrt-qd (mul-qd x y))))
	     (agm-qd a-mean g-mean))))))

#+(or)
(defun agm-qd (x y)
  (declare (type %quad-double x y)
	   (optimize (speed 3) (space 0) (safety 0)))
  (let ((diff (qd-0 (abs-qd (sub-qd x y))))
	(x x)
	(y y))
    (declare (double-float diff))
    (loop while (> diff +qd-eps+)
      do
      (let ((a-mean (scale-float-qd (add-qd x y) -1))
	    (g-mean (sqrt-qd (mul-qd x y))))
	(setf x a-mean)
	(setf y g-mean)
	(setf diff (qd-0 (abs-qd (sub-qd x y))))))
    x))

(defun log-qd/agm (x)
  (declare (type %quad-double x))
  ;; log(x) ~ pi/2/agm(1,4/x)*(1+O(1/x^2))
  ;;
  ;; Need to make x >= 2^(d/2) to get d bits of precision.  We use
  ;;
  ;; log(2^k*x) = k*log(2)+log(x)
  ;;
  ;; to compute log(x).  log(2^k*x) is computed using AGM.
  ;;
  (multiple-value-bind (frac exp)
      (decode-float (qd-0 x))
    (declare (ignore frac))
    (cond ((>= exp 106)
	   ;; Big enough to use AGM
	   (div-qd +qd-pi/2+
		   (agm-qd +qd-one+
			   (div-qd (make-qd-d 4d0)
				   x))))
	  (t
	   ;; log(x) = log(2^k*x) - k * log(2)
	   (let* ((k (cl:- 107 exp))
		  (big-x (scale-float-qd x k)))
	     ;; Compute k*log(2) using extra precision by writing
	     ;; log(2) = a + b, where a is the quad-double
	     ;; approximation and b the rest.
	     (sub-qd (log-qd/agm big-x)
		     (add-qd (mul-qd-d +qd-log2+ (float k 1d0))
			     (mul-qd-d +qd-log2-extra+ (float k 1d0)))))))))

(defun log-qd/agm2 (x)
  (declare (type %quad-double x))
  ;; log(x) ~ pi/4/agm(theta2(q^4)^2,theta3(q^4)^2)
  ;;
  ;; where q = 1/x
  ;;
  ;; Need to make x >= 2^(d/36) to get d bits of precision.  We use
  ;;
  ;; log(2^k*x) = k*log(2)+log(x)
  ;;
  ;; to compute log(x).  log(2^k*x) is computed using AGM.
  ;;
  (multiple-value-bind (frac exp)
      (decode-float (qd-0 x))
    (declare (ignore frac))
    (cond ((>= exp 7)
	   ;; Big enough to use AGM (because d = 212 so x >= 2^5.8888)
	   (let* ((q (div-qd +qd-one+
			     x))
		  (q^4 (npow q 4))
		  (q^8 (sqr-qd q^4))
		  ;; theta2(q^4) = 2*q*(1+q^8+q^24)
		  ;;             = 2*q*(1+q^8+(q^8)^3)
		  (theta2 (mul-qd-d
			   (mul-qd
			    q
			    (add-qd-d
			     (add-qd q^8
				     (npow q^8 3))
			     1d0))
			   2d0))
		  ;; theta3(q^4) = 1+2*(q^4+q^16)
		  ;;             = 1+2*(q^4+(q^4)^4)
		  (theta3 (add-qd-d
			   (mul-qd-d
			    (add-qd q^4
				    (npow q^4 4))
			    2d0)
			   1d0)))
	     (div-qd +qd-pi/4+
		     (agm-qd (sqr-qd theta2)
			     (sqr-qd theta3)))))
	  (t
	   ;; log(x) = log(2^k*x) - k * log(2)
	   (let* ((k (cl:- 7 exp))
		  (big-x (scale-float-qd x k)))
	     (sub-qd (log-qd/agm2 big-x)
		     (add-qd (mul-qd-d +qd-log2+ (float k 1d0))
			     (mul-qd-d +qd-log2-extra+ (float k 1d0)))))))))

(defun log-qd/agm3 (x)
  (declare (type %quad-double x))
  ;; log(x) ~ pi/4/agm(theta2(q^4)^2,theta3(q^4)^2)
  ;;
  ;; where q = 1/x
  ;;
  ;; Need to make x >= 2^(d/36) to get d bits of precision.  We use
  ;;
  ;; log(2^k*x) = k*log(2)+log(x)
  ;;
  ;; to compute log(x).  log(2^k*x) is computed using AGM.
  ;;
  (multiple-value-bind (frac exp)
      (decode-float (qd-0 x))
    (declare (ignore frac))
    (cond ((>= exp 7)
	   ;; Big enough to use AGM (because d = 212 so x >= 2^5.8888)
	   (let* ((q (div-qd +qd-one+
			     x))
		  (q^4 (npow q 4))
		  (q^8 (sqr-qd q^4))
		  ;; theta2(q^4) = 2*q*(1+q^8+q^24)
		  ;;             = 2*q*(1+q^8+(q^8)^3)
		  (theta2 (mul-qd-d
			   (mul-qd
			    q
			    (add-qd-d
			     (add-qd q^8
				     (npow q^8 3))
			     1d0))
			   2d0))
		  ;; theta3(q^4) = 1+2*(q^4+q^16)
		  ;;             = 1+2*(q^4+(q^4)^4)
		  (theta3 (add-qd-d
			   (mul-qd-d
			    (add-qd q^4
				    (npow q^4 4))
			    2d0)
			   1d0)))
	     ;; Note that agm(theta2^2,theta3^2) = agm(2*theta2*theta3,theta2^2+theta3^2)/2
	     (div-qd +qd-pi/4+
		     (scale-float-qd
		      (agm-qd (scale-float-qd (mul-qd theta2 theta3) 1)
			      (add-qd (sqr-qd theta2)
				      (sqr-qd theta3)))
		      -1))))
	  (t
	   ;; log(x) = log(2^k*x) - k * log(2)
	   (let* ((k (cl:- 7 exp))
		  (big-x (scale-float-qd x k)))
	     (sub-qd (log-qd/agm3 big-x)
		     (add-qd
		      (mul-qd-d +qd-log2+ (float k 1d0))
		      (mul-qd-d +qd-log2-extra+ (float k 1d0)))))))))

#+(or)
(defun atan-d (y x)
  (let* ((r (abs (complex x y)))
	 (xx (cl:/ x r))
	 (yy (cl:/ y r)))
    (let ((z (atan (float y 1f0) (float x 1f0)))
	  (sinz 0d0)
	  (cosz 0d0))
      (format t "z = ~A~%" z)
      (cond ((> xx yy)
	     (format t "xx > yy~%")
	     (dotimes (k 5)
	       (let* ((sinz (sin z))
		      (cosz (cos z))
		      (delta (cl:/ (cl:- yy sinz)
				   cosz)))
		 (format t "sz, dz = ~A ~A~%" sinz cosz)
		 (format t "delta  = ~A~%" delta)
		 (setf z (cl:+ z delta))
		 (format t "z = ~A~%" z))))
	    (t
	     (dotimes (k 20)
	       (let ((sinz (sin z))
		     (cosz (cos z)))
		 (format t "sz, dz = ~A ~A~%" sinz cosz)
		 
		 (setf z (cl:- z (cl:/ (cl:- xx cosz)
				       sinz)))
		 (format t "z = ~A~%" z)))))
      z)))

#||
(defvar *table*)
(defvar *ttable*)
(defvar *cordic-scale*)

#+nil
(defun setup-cordic ()
  (let ((table (make-array 34))
	(ttable (make-array 34)))
    (setf (aref table 0) 1d0)
    (setf (aref table 1) 1d0)
    (setf (aref table 2) 1d0)
    (setf (aref ttable 0) (cl:/ pi 4))
    (setf (aref ttable 1) (cl:/ pi 4))
    (setf (aref ttable 2) (cl:/ pi 4))
    (loop for k from 3 below 34 do
	 (setf (aref table k) (cl:* 0.5d0 (aref table (cl:1- k))))
	 (setf (aref ttable k) (atan (aref table k))))
    (setf *table* table)
    (setf *ttable* ttable)))

(defun setup-cordic ()
  (let ((table (make-array 34))
	(ttable (make-array 34)))
    (setf (aref table 0) 4d0)
    (setf (aref table 1) 2d0)
    (setf (aref table 2) 1d0)
    (setf (aref ttable 0) (atan 4d0))
    (setf (aref ttable 1) (atan 2d0))
    (setf (aref ttable 2) (cl:/ pi 4))
    (loop for k from 3 below 34 do
	 (setf (aref table k) (cl:* 0.5d0 (aref table (cl:1- k))))
	 (setf (aref ttable k) (atan (aref table k))))
    (setf *table* table)
    (setf *ttable* ttable)))

(defun setup-cordic ()
  (let ((table (make-array 34))
	(ttable (make-array 34))
	(scale 1d0))
    (loop for k from 0 below 34 do
	 (setf (aref table k) (scale-float 1d0 (cl:- 2 k)))
	 (setf (aref ttable k) (atan (aref table k)))
	 (setf scale (cl:* scale (cos (aref ttable k)))))
    (setf *table* table)
    (setf *ttable* ttable)
    (setf *cordic-scale* scale)))


(defun cordic-rot (x y)
  (let ((z 0))
    (dotimes (k (length *table*))
      (cond ((plusp y)
	     (psetq x (cl:+ x (cl:* y (aref *table* k)))
		    y (cl:- y (cl:* x (aref *table* k))))
	     (incf z (aref *ttable* k)))
	    (t
	     (psetq x (cl:- x (cl:* y (aref *table* k)))
		    y (cl:+ y (cl:* x (aref *table* k))))
	     (decf z (aref *ttable* k)))
	    ))
    (values z x y)))

(defun cordic-vec (z)
  (let ((x 1d0)
	(y 0d0)
	(scale 1d0))
    (dotimes (k 12 (length *table*))
      (setf scale (cl:* scale (cos (aref *ttable* k))))
      (cond ((minusp z)
	     (psetq x (cl:+ x (cl:* y (aref *table* k)))
		    y (cl:- y (cl:* x (aref *table* k))))
	     (incf z (aref *ttable* k)))
	    (t
	     (psetq x (cl:- x (cl:* y (aref *table* k)))
		    y (cl:+ y (cl:* x (aref *table* k))))
	     (decf z (aref *ttable* k)))
	    ))
    (values x y z scale)))

(defun atan2-d (y x)
  (multiple-value-bind (z dx dy)
      (cordic-rot x y)
    (let ((theta (cl:/ dy dx)))
      (format t "theta = ~A~%" theta)
      (let ((corr (cl:+ theta
		     (cl:- (cl:/ (expt theta 3)
			   3))
		     (cl:/ (expt theta 5)
			5))))
	(format t "corr = ~A~%" corr)
	(cl:+ z corr)))))

(defun tan-d (r)
  (multiple-value-bind (x y z)
      (cordic-vec r)
    (setf x (cl:* x *cordic-scale*))
    (setf y (cl:* y *cordic-scale*))
    (format t "x = ~A~%" x)
    (format t "y = ~A~%" y)
    (format t "z = ~A~%" z)
    ;; Need to finish of the rotation
    (let ((st (sin z))
	  (ct (cos z)))
      (format t "st, ct = ~A ~A~%" st ct)
      (psetq x (cl:- (cl:* x ct) (cl:* y st))
	     y (cl:+ (cl:* y ct) (cl:* x st)))
      (format t "x = ~A~%" x)
      (format t "y = ~A~%" y)
      (cl:/ y x)
      )))

(defun sin-d (r)
  (declare (type double-float r))
  (multiple-value-bind (x y z s)
      (cordic-vec r)
    
    ;; Need to finish the rotation
    (let ((st (sin z))
	  (ct (cos z)))
      (psetq x (cl:- (cl:* x ct) (cl:* y st))
	     y (cl:+ (cl:* y ct) (cl:* x st)))
      (cl:* s y))))
||#

Raymond Toy's avatar
Raymond Toy committed
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
#||
;; Here is a function for clisp that can be used to create the atan2 table
;; that we need.

(defun make-atan-table-data ()
  (let ((scale 1l0))
    (dotimes (k 67)
      (let* ((x (scale-float 1L0 (- 2 k)))
	     (p (atan x)))
	(setf scale (* scale (cos p)))
	(multiple-value-bind (int exp sign)
	    (integer-decode-float p)
	  (let* ((len (integer-length int))
		 (wanted (ldb (byte 212 (- len 212)) int))
		 (bit (ldb (byte 1 (- len (* 4 53) 1)) int))
		 (roundp (not (zerop (ldb (byte (- len (* 4 53) 2) 0) int)))))
	    ;;(format t "~&~v,'0b~%" len int)
	    ;;(format t "~b~a~%" wanted (make-string (- len 212) :initial-element #\-))
	    ;;(format t "~v,'-b~%" len (ash bit (- len 212 1)))
	    ;;(format t "~v,'-b~%" len (ldb (byte (- len (* 4 53) 2) 0) int))
	    ;; See if we need to round up the answer.  
	    (when (= bit 1)
	      ;; Round to even
	      (cond (roundp
		     (incf wanted))
		    (t
		     ;; Round to even
		     (when (oddp wanted)
		       (incf wanted)))))
	    ;;(format t "~b~a~%" wanted (make-string (- len 212) :initial-element #\-))
	    
	    (let* ((i0 (ldb (byte 53 (* 3 53)) wanted))
		   (i1 (ldb (byte 53 (* 2 53)) wanted))
		   (i2 (ldb (byte 53 (* 1 53)) wanted))
		   (i3 (ldb (byte 53 0) wanted)))
	      (write `(make-qd-d
		       (scale-float (float ,i0 1d0) ,(+ exp (- len (* 1 53))))
		       (scale-float (float ,i1 1d0) ,(+ exp (- len (* 2 53))))
		       (scale-float (float ,i2 1d0) ,(+ exp (- len (* 3 53))))
		       (scale-float (float ,i3 1d0) ,(+ exp (- len (* 4 53)))))
		     :case :downcase))))))
    scale))
||#
	       
	
#+nil
(defconstant +atan-table+
  (make-array 66
	      :initial-contents
	      (list
	       +qd-pi/4+
	       +qd-pi/4+
	       +qd-pi/4+
	       ;; Do we need to make these values more accurate?  (The
	       ;; reader has quite a bit of roundoff.)
	       #.(qd-from-string "0.46364760900080611621425623146121440202853705428612026381093308872018q0") 
	       #.(qd-from-string "0.24497866312686415417208248121127581091414409838118406712737591466738q0")
	       #.(qd-from-string "0.12435499454676143503135484916387102557317019176980408991511411911572q0")
	       #.(qd-from-string "0.062418809995957348473979112985505113606273887797499194607527816898697q0")
	       #.(qd-from-string "0.031239833430268276253711744892490977032495663725400040255315586255793q0")
	       #.(qd-from-string "0.0156237286204768308028015212565703189111141398009054178814105073966645q0")
	       #.(qd-from-string "0.0078123410601011112964633918421992816212228117250147235574539022483893q0")
	       #.(qd-from-string "0.003906230131966971827628665311424387140357490115202856215213095149011q0")
	       #.(qd-from-string "0.00195312251647881868512148262507671393161074677723351033905753396043094q0")
	       #.(qd-from-string "9.7656218955931943040343019971729085163419701581008759004900725226773q-4")
	       #.(qd-from-string "4.8828121119489827546923962564484866619236113313500303710940335348752q-4")
	       #.(qd-from-string "2.4414062014936176401672294325965998621241779097061761180790046091019q-4") 
	       #.(qd-from-string "1.22070311893670204239058646117956300930829409015787498451939837846645q-4") 
	       #.(qd-from-string "6.1035156174208775021662569173829153785143536833346179337671134316588q-5") 
	       #.(qd-from-string "3.0517578115526096861825953438536019750949675119437837531021156883611q-5") 
	       #.(qd-from-string "1.5258789061315762107231935812697885137429238144575874846241186407446q-5") 
	       #.(qd-from-string "7.6293945311019702633884823401050905863507439184680771577638306965336q-6") 
	       #.(qd-from-string "3.8146972656064962829230756163729937228052573039688663101874392503939q-6") 
	       #.(qd-from-string "1.9073486328101870353653693059172441687143421654501533666700577234671q-6") 
	       #.(qd-from-string "9.53674316405960879420670689923112390019634124498790160133611802076q-7") 
	       #.(qd-from-string "4.7683715820308885992758382144924707587049404378664196740053215887142q-7") 
	       #.(qd-from-string "2.3841857910155798249094797721893269783096898769063155913766911372218q-7") 
	       #.(qd-from-string "1.19209289550780685311368497137922112645967587664586735576738225215437q-7") 
	       #.(qd-from-string "5.9604644775390554413921062141788874250030195782366297314294565710003q-8") 
	       #.(qd-from-string "2.9802322387695303676740132767709503349043907067445107249258477840843q-8") 
	       #.(qd-from-string "1.4901161193847655147092516595963247108248930025964720012170057805491q-8") 
	       #.(qd-from-string "7.4505805969238279871365645744953921132066925545665870075947601416172q-9") 
	       #.(qd-from-string "3.725290298461914045267070571811923583671948328737040524231998269239q-9") 
	       #.(qd-from-string "1.8626451492309570290958838214764904345065282835738863513491050124951q-9") 
	       #.(qd-from-string "9.3132257461547851535573547768456130389292649614929067394376854242196q-10") 
	       #.(qd-from-string "4.6566128730773925777884193471057016297347863891561617421323492554414q-10") 
	       #.(qd-from-string "2.32830643653869628902042741838821270371274293204981860525486662280605q-10") 
	       #.(qd-from-string "1.16415321826934814452599092729852658796396457380014290026584979170883q-10") 
	       #.(qd-from-string "5.8207660913467407226496761591231582349549156257795272423976206167147q-11") 
	       #.(qd-from-string "2.9103830456733703613273032698903947793693632003639830495829934525029q-11") 
	       #.(qd-from-string "1.4551915228366851806639597837362993474211703608936710732067270213307q-11") 
	       #.(qd-from-string "7.2759576141834259033201841046703741842764629388821429640111752890838q-12") 
	       #.(qd-from-string "3.6379788070917129516601402005837967730345578669779258118296083646486q-12") 
	       #.(qd-from-string "1.81898940354585647583007611882297459662931973336029253714520765350336q-12") 
	       #.(qd-from-string "9.094947017729282379150388117278718245786649666696631862264792881855q-13") 
	       #.(qd-from-string "4.5474735088646411895751949990348397807233312083369623012466392138249q-13") 
	       #.(qd-from-string "2.2737367544323205947875976170668549725904164010421166413578155299654q-13") 
	       #.(qd-from-string "1.1368683772161602973937988232271068715738020501302644662229139921281q-13") 
	       #.(qd-from-string "5.6843418860808014869689941345026335894672525626628305471702634435609q-14") 
	       #.(qd-from-string "2.8421709430404007434844970695472041986834065703328538172835210852389q-14") 
	       #.(qd-from-string "1.42108547152020037174224853506058802483542582129160672712566632799217q-14") 
	       #.(qd-from-string "7.1054273576010018587112426756616725310442822766145084088962160950957q-15") 
	       #.(qd-from-string "3.5527136788005009293556213378756778163805352845768135511116874239215q-15") 
	       #.(qd-from-string "1.7763568394002504646778106689434441020475669105721016938889503158663q-15") 
	       #.(qd-from-string "8.881784197001252323389053344724227002559458638215127117361184578544q-16") 
	       #.(qd-from-string "4.440892098500626161694526672362989312819932329776890889670147968684q-16") 
	       #.(qd-from-string "2.22044604925031308084726333618160413285249154122211136120876849284695q-16") 
	       #.(qd-from-string "1.11022302462515654042363166809081575098156144265276392015109606150467q-16") 
	       #.(qd-from-string "5.5511151231257827021181583404540958606019518033159549001888700768492q-17") 
	       #.(qd-from-string "2.7755575615628913510590791702270500685127439754144943625236087596052q-17") 
	       #.(qd-from-string "1.3877787807814456755295395851135253015328429969268117953154510949506q-17") 
	       #.(qd-from-string "6.9388939039072283776476979255676268417598037461585147441443138686883q-18") 
	       #.(qd-from-string "3.4694469519536141888238489627838134626418504682698143430180392335861q-18") 
	       #.(qd-from-string "1.7347234759768070944119244813919067365411688085337267928772549041983q-18") 
	       #.(qd-from-string "8.673617379884035472059622406959533689231148510667158491096568630248q-19") 
	       #.(qd-from-string "4.336808689942017736029811203479766845431237313833394811387071078781q-19") 
	       #.(qd-from-string "2.16840434497100886801490560173988342281757653922917435142338388484765q-19") 
	       #.(qd-from-string "1.08420217248550443400745280086994171142153300490364679392792298560597q-19") 

	       ))
  "Table of atan(2^(-k)) for k = 1 to 64.  But the first three entries are 1")

(defconstant +atan-table+
  (make-array 67
	      :initial-contents
	      (list
	       (%make-qd-d (scale-float (float 5970951936056572 1.0d0) -52)
			  (scale-float (float 5427585433121543 1.0d0) -105)
			  (scale-float (float 5608515294538868 1.0d0) -158)
			  (scale-float (float 445395631680583 1.0d0) -211))
	       (%make-qd-d (scale-float (float 4986154552901188 1.0d0) -52)
			  (scale-float (float 3814906810089799 1.0d0) -105)
			  (scale-float (float 1896417689773139 1.0d0) -158)
			  (scale-float (float 3393132800284032 1.0d0) -211))
	       (%make-qd-d (scale-float (float 7074237752028440 1.0d0) -53)
			  (scale-float (float 2483878800010755 1.0d0) -106)
			  (scale-float (float 3956492004828932 1.0d0) -159)
			  (scale-float (float 2434854662709436 1.0d0) -212))
	       (%make-qd-d (scale-float (float 8352332796509007 1.0d0) -54)
			  (scale-float (float 3683087214424816 1.0d0) -107)
			  (scale-float (float 8240297260223171 1.0d0) -160)
			  (scale-float (float 5174086704442609 1.0d0) -213))
	       (%make-qd-d (scale-float (float 8826286527774941 1.0d0) -55)
			  (scale-float (float 3471944699336670 1.0d0) -108)
			  (scale-float (float 4798212191802497 1.0d0) -161)
			  (scale-float (float 6908472993489831 1.0d0) -214))
	       (%make-qd-d (scale-float (float 8960721713639277 1.0d0) -56)
			  (scale-float (float 6978747913895162 1.0d0) -109)
			  (scale-float (float 1204496828771308 1.0d0) -162)
			  (scale-float (float 6150314016033077 1.0d0) -215))
	       (%make-qd-d (scale-float (float 8995498542038505 1.0d0) -57)
			  (scale-float (float 6996384121843768 1.0d0) -110)
			  (scale-float (float 6481245652257127 1.0d0) -163)
			  (scale-float (float 6083920726820778 1.0d0) -216))
	       (%make-qd-d (scale-float (float 9004268940523044 1.0d0) -58)
			  (scale-float (float 5921825575778154 1.0d0) -111)
			  (scale-float (float 1742767809528138 1.0d0) -164)
			  (scale-float (float 3392785816514584 1.0d0) -217))
	       (%make-qd-d (scale-float (float 9006466354344602 1.0d0) -59)
			  (scale-float (float 6455912199422039 1.0d0) -112)
			  (scale-float (float 7793493312778976 1.0d0) -165)
			  (scale-float (float 4748718880757240 1.0d0) -218))
	       (%make-qd-d (scale-float (float 9007016009513623 1.0d0) -60)
			  (scale-float (float 1583402193514233 1.0d0) -113)
			  (scale-float (float 4599960241393675 1.0d0) -166)
			  (scale-float (float 4964226307734805 1.0d0) -219))
	       (%make-qd-d (scale-float (float 9007153442175927 1.0d0) -61)
			  (scale-float (float 1458797116501429 1.0d0) -114)
			  (scale-float (float 2180379843517813 1.0d0) -167)
			  (scale-float (float 7244224576758923 1.0d0) -220))
	       (%make-qd-d (scale-float (float 9007187801521083 1.0d0) -62)
			  (scale-float (float 5961909987006481 1.0d0) -115)
			  (scale-float (float 1439161705865198 1.0d0) -168)
			  (scale-float (float 1250151122136839 1.0d0) -221))
	       (%make-qd-d (scale-float (float 9007196391431099 1.0d0) -63)
			  (scale-float (float 6595226783193595 1.0d0) -116)
			  (scale-float (float 7270788700276565 1.0d0) -169)
			  (scale-float (float 5212528258452836 1.0d0) -222))
	       (%make-qd-d (scale-float (float 9007198538913211 1.0d0) -64)
			  (scale-float (float 6605122380416172 1.0d0) -117)
			  (scale-float (float 2579496809882929 1.0d0) -170)
			  (scale-float (float 2545695100421145 1.0d0) -223))
	       (%make-qd-d (scale-float (float 9007199075784027 1.0d0) -65)
			  (scale-float (float 6605276999209814 1.0d0) -118)
			  (scale-float (float 8635423593413256 1.0d0) -171)
			  (scale-float (float 6747877897971029 1.0d0) -224))
	       (%make-qd-d (scale-float (float 9007199210001749 1.0d0) -66)
			  (scale-float (float 6605279415128805 1.0d0) -119)
			  (scale-float (float 5633073770825222 1.0d0) -172)
			  (scale-float (float 744251135568860 1.0d0) -225))
	       (%make-qd-d (scale-float (float 9007199243556181 1.0d0) -67)
			  (scale-float (float 3227579732349669 1.0d0) -120)
			  (scale-float (float 1645511649516378 1.0d0) -173)
			  (scale-float (float 7212311609477561 1.0d0) -226))
	       (%make-qd-d (scale-float (float 9007199251944789 1.0d0) -68)
			  (scale-float (float 3016473500406501 1.0d0) -121)
			  (scale-float (float 1629935234837168 1.0d0) -174)
			  (scale-float (float 1206159191623029 1.0d0) -227))
	       (%make-qd-d (scale-float (float 9007199254041941 1.0d0) -69)
			  (scale-float (float 3003279360882405 1.0d0) -122)
			  (scale-float (float 1629874389467187 1.0d0) -175)
			  (scale-float (float 8712158240272416 1.0d0) -228))
	       (%make-qd-d (scale-float (float 9007199254566229 1.0d0) -70)
			  (scale-float (float 3002454727161717 1.0d0) -123)
			  (scale-float (float 1629874151789961 1.0d0) -176)
			  (scale-float (float 3116377062563786 1.0d0) -229))
	       (%make-qd-d (scale-float (float 9007199254697301 1.0d0) -71)
			  (scale-float (float 3002403187554167 1.0d0) -124)
			  (scale-float (float 3881673964546782 1.0d0) -177)
			  (scale-float (float 6119176246102625 1.0d0) -230))
	       (%make-qd-d (scale-float (float 9007199254730069 1.0d0) -72)
			  (scale-float (float 3002399966328695 1.0d0) -125)
			  (scale-float (float 4198333313342644 1.0d0) -178)
			  (scale-float (float 114377133012236 1.0d0) -231))
	       (%make-qd-d (scale-float (float 9007199254738261 1.0d0) -73)
			  (scale-float (float 3002399765002103 1.0d0) -126)
			  (scale-float (float 4203281115667621 1.0d0) -179)
			  (scale-float (float 7620376512343991 1.0d0) -232))
	       (%make-qd-d (scale-float (float 9007199254740309 1.0d0) -74)
			  (scale-float (float 3002399752419191 1.0d0) -127)
			  (scale-float (float 4203358425078949 1.0d0) -180)
			  (scale-float (float 7121931241085909 1.0d0) -233))
	       (%make-qd-d (scale-float (float 9007199254740821 1.0d0) -75)
			  (scale-float (float 3002399751632759 1.0d0) -128)
			  (scale-float (float 4203359633038501 1.0d0) -181)
			  (scale-float (float 7119984189245056 1.0d0) -234))
	       (%make-qd-d (scale-float (float 9007199254740949 1.0d0) -76)
			  (scale-float (float 3002399751583607 1.0d0) -129)
			  (scale-float (float 4203359651912869 1.0d0) -182)
			  (scale-float (float 7119976583573803 1.0d0) -235))
	       (%make-qd-d (scale-float (float 9007199254740981 1.0d0) -77)
			  (scale-float (float 3002399751580535 1.0d0) -130)
			  (scale-float (float 4203359652207781 1.0d0) -183)
			  (scale-float (float 7119976553864150 1.0d0) -236))
	       (%make-qd-d (scale-float (float 9007199254740989 1.0d0) -78)
			  (scale-float (float 3002399751580343 1.0d0) -131)
			  (scale-float (float 4203359652212389 1.0d0) -184)
			  (scale-float (float 7119976553748096 1.0d0) -237))
	       (%make-qd-d (scale-float (float 9007199254740991 1.0d0) -79)
			  (scale-float (float 3002399751580331 1.0d0) -132)
			  (scale-float (float 4203359652212461 1.0d0) -185)
			  (scale-float (float 7119976553747643 1.0d0) -238))
	       (%make-qd-d (scale-float (float 9007199254740991 1.0d0) -80)
			  (scale-float (float 7505999378950826 1.0d0) -133)
			  (scale-float (float 6455159465897710 1.0d0) -186)
			  (scale-float (float 8245876460590265 1.0d0) -239))
	       (%make-qd-d (scale-float (float 9007199254740991 1.0d0) -81)
			  (scale-float (float 8631899285793450 1.0d0) -134)
			  (scale-float (float 6032947000831726 1.0d0) -187)
			  (scale-float (float 8404206134990009 1.0d0) -240))
	       (%make-qd-d (scale-float (float 9007199254740991 1.0d0) -82)
			  (scale-float (float 8913374262504106 1.0d0) -135)
			  (scale-float (float 6006558721765102 1.0d0) -188)
			  (scale-float (float 8406680036152505 1.0d0) -241))
	       (%make-qd-d (scale-float (float 9007199254740991 1.0d0) -83)
			  (scale-float (float 8983743006681770 1.0d0) -136)
			  (scale-float (float 6004909454323438 1.0d0) -189)
			  (scale-float (float 8406718690858169 1.0d0) -242))
	       (%make-qd-d (scale-float (float 9007199254740991 1.0d0) -84)
			  (scale-float (float 9001335192726186 1.0d0) -137)
			  (scale-float (float 6004806375108334 1.0d0) -190)
			  (scale-float (float 8406719294837945 1.0d0) -243))
	       (%make-qd-d (scale-float (float 9007199254740991 1.0d0) -85)
			  (scale-float (float 9005733239237290 1.0d0) -138)
			  (scale-float (float 6004799932657390 1.0d0) -191)
			  (scale-float (float 8406719304275129 1.0d0) -244))
	       (%make-qd-d (scale-float (float 9007199254740991 1.0d0) -86)
			  (scale-float (float 9006832750865066 1.0d0) -139)
			  (scale-float (float 6004799530004206 1.0d0) -192)
			  (scale-float (float 8406719304422585 1.0d0) -245))
	       (%make-qd-d (scale-float (float 9007199254740991 1.0d0) -87)
			  (scale-float (float 9007107628772010 1.0d0) -140)
			  (scale-float (float 6004799504838382 1.0d0) -193)
			  (scale-float (float 8406719304424889 1.0d0) -246))
	       (%make-qd-d (scale-float (float 9007199254740991 1.0d0) -88)
			  (scale-float (float 9007176348248746 1.0d0) -141)
			  (scale-float (float 6004799503265518 1.0d0) -194)
			  (scale-float (float 8406719304424925 1.0d0) -247))
	       (%make-qd-d (scale-float (float 9007199254740991 1.0d0) -89)
			  (scale-float (float 9007193528117930 1.0d0) -142)
			  (scale-float (float 6004799503167214 1.0d0) -195)
			  (scale-float (float 8406719304424926 1.0d0) -248))
	       (%make-qd-d (scale-float (float 9007199254740991 1.0d0) -90)
			  (scale-float (float 9007197823085226 1.0d0) -143)
			  (scale-float (float 6004799503161070 1.0d0) -196)
			  (scale-float (float 8406719304424926 1.0d0) -249))
	       (%make-qd-d (scale-float (float 9007199254740991 1.0d0) -91)
			  (scale-float (float 9007198896827050 1.0d0) -144)
			  (scale-float (float 6004799503160686 1.0d0) -197)
			  (scale-float (float 8406719304424926 1.0d0) -250))
	       (%make-qd-d (scale-float (float 9007199254740991 1.0d0) -92)
			  (scale-float (float 9007199165262506 1.0d0) -145)
			  (scale-float (float 6004799503160662 1.0d0) -198)
			  (scale-float (float 8406719304424926 1.0d0) -251))
	       (%make-qd-d (scale-float (float 9007199254740991 1.0d0) -93)
			  (scale-float (float 9007199232371370 1.0d0) -146)
			  (scale-float (float 6004799503160661 1.0d0) -199)
			  (scale-float (float 3903119677054430 1.0d0) -252))
	       (%make-qd-d (scale-float (float 9007199254740991 1.0d0) -94)
			  (scale-float (float 9007199249148586 1.0d0) -147)
			  (scale-float (float 6004799503160661 1.0d0) -200)
			  (scale-float (float 3058694746922462 1.0d0) -253))
	       (%make-qd-d (scale-float (float 9007199254740991 1.0d0) -95)
			  (scale-float (float 9007199253342890 1.0d0) -148)
			  (scale-float (float 6004799503160661 1.0d0) -201)
			  (scale-float (float 3005918188789214 1.0d0) -254))
	       (%make-qd-d (scale-float (float 9007199254740991 1.0d0) -96)
			  (scale-float (float 9007199254391466 1.0d0) -149)
			  (scale-float (float 6004799503160661 1.0d0) -202)
			  (scale-float (float 3002619653905886 1.0d0) -255))
	       (%make-qd-d (scale-float (float 9007199254740991 1.0d0) -97)
			  (scale-float (float 9007199254653610 1.0d0) -150)
			  (scale-float (float 6004799503160661 1.0d0) -203)
			  (scale-float (float 3002413495475678 1.0d0) -256))
	       (%make-qd-d (scale-float (float 9007199254740991 1.0d0) -98)
			  (scale-float (float 9007199254719146 1.0d0) -151)
			  (scale-float (float 6004799503160661 1.0d0) -204)
			  (scale-float (float 3002400610573790 1.0d0) -257))
	       (%make-qd-d (scale-float (float 9007199254740991 1.0d0) -99)
			  (scale-float (float 9007199254735530 1.0d0) -152)
			  (scale-float (float 6004799503160661 1.0d0) -205)
			  (scale-float (float 3002399805267422 1.0d0) -258))
	       (%make-qd-d (scale-float (float 9007199254740991 1.0d0) -100)
			  (scale-float (float 9007199254739626 1.0d0) -153)
			  (scale-float (float 6004799503160661 1.0d0) -206)
			  (scale-float (float 3002399754935774 1.0d0) -259))
	       (%make-qd-d (scale-float (float 9007199254740991 1.0d0) -101)
			  (scale-float (float 9007199254740650 1.0d0) -154)
			  (scale-float (float 6004799503160661 1.0d0) -207)
			  (scale-float (float 3002399751790046 1.0d0) -260))
	       (%make-qd-d (scale-float (float 9007199254740991 1.0d0) -102)
			  (scale-float (float 9007199254740906 1.0d0) -155)
			  (scale-float (float 6004799503160661 1.0d0) -208)
			  (scale-float (float 3002399751593438 1.0d0) -261))
	       (%make-qd-d (scale-float (float 9007199254740991 1.0d0) -103)
			  (scale-float (float 9007199254740970 1.0d0) -156)
			  (scale-float (float 6004799503160661 1.0d0) -209)
			  (scale-float (float 3002399751581150 1.0d0) -262))
	       (%make-qd-d (scale-float (float 9007199254740991 1.0d0) -104)
			  (scale-float (float 9007199254740986 1.0d0) -157)
			  (scale-float (float 6004799503160661 1.0d0) -210)
			  (scale-float (float 3002399751580382 1.0d0) -263))
	       (%make-qd-d (scale-float (float 9007199254740991 1.0d0) -105)
			  (scale-float (float 9007199254740990 1.0d0) -158)
			  (scale-float (float 6004799503160661 1.0d0) -211)
			  (scale-float (float 3002399751580334 1.0d0) -264))
	       (%make-qd-d (scale-float (float 9007199254740991 1.0d0) -106)
			  (scale-float (float 9007199254740991 1.0d0) -159)
			  (scale-float (float 6004799503160661 1.0d0) -212)
			  (scale-float (float 3002399751580331 1.0d0) -265))
	       (%make-qd-d (scale-float (float 9007199254740991 1.0d0) -107)
			  (scale-float (float 9007199254740991 1.0d0) -160)
			  (scale-float (float 8256599316845909 1.0d0) -213)
			  (scale-float (float 3002399751580331 1.0d0) -266))
	       (%make-qd-d (scale-float (float 9007199254740991 1.0d0) -108)
			  (scale-float (float 9007199254740991 1.0d0) -161)
			  (scale-float (float 8819549270267221 1.0d0) -214)
			  (scale-float (float 3002399751580331 1.0d0) -267))
	       (%make-qd-d (scale-float (float 9007199254740991 1.0d0) -109)
			  (scale-float (float 9007199254740991 1.0d0) -162)
			  (scale-float (float 8960286758622549 1.0d0) -215)
			  (scale-float (float 3002399751580331 1.0d0) -268))
	       (%make-qd-d (scale-float (float 9007199254740991 1.0d0) -110)
			  (scale-float (float 9007199254740991 1.0d0) -163)
			  (scale-float (float 8995471130711381 1.0d0) -216)
			  (scale-float (float 3002399751580331 1.0d0) -269))
	       (%make-qd-d (scale-float (float 9007199254740991 1.0d0) -111)
			  (scale-float (float 9007199254740991 1.0d0) -164)
			  (scale-float (float 9004267223733589 1.0d0) -217)
			  (scale-float (float 3002399751580331 1.0d0) -270))
	       (%make-qd-d (scale-float (float 9007199254740991 1.0d0) -112)
			  (scale-float (float 9007199254740991 1.0d0) -165)
			  (scale-float (float 9006466246989141 1.0d0) -218)
			  (scale-float (float 3002399751580331 1.0d0) -271))
	       (%make-qd-d (scale-float (float 9007199254740991 1.0d0) -113)
			  (scale-float (float 9007199254740991 1.0d0) -166)
			  (scale-float (float 9007016002803029 1.0d0) -219)
			  (scale-float (float 3002399751580331 1.0d0) -272))
	       (%make-qd-d (scale-float (float 9007199254740991 1.0d0) -114)
			  (scale-float (float 9007199254740991 1.0d0) -167)
			  (scale-float (float 9007153441756501 1.0d0) -220)
			  (scale-float (float 3002399751580331 1.0d0) -273))
	       (%make-qd-d (scale-float (float 9007199254740991 1.0d0) -115)
			  (scale-float (float 9007199254740991 1.0d0) -168)
			  (scale-float (float 9007187801494869 1.0d0) -221)
			  (scale-float (float 3002399751580331 1.0d0) -274))
	       (%make-qd-d (scale-float (float 9007199254740991 1.0d0) -116)
			  (scale-float (float 9007199254740991 1.0d0) -169)
			  (scale-float (float 9007196391429461 1.0d0) -222)
			  (scale-float (float 3002399751580331 1.0d0) -275))
	       (%make-qd-d (scale-float (float 9007199254740991 1.0d0) -117)
			  (scale-float (float 9007199254740991 1.0d0) -170)
			  (scale-float (float 9007198538913109 1.0d0) -223)
			  (scale-float (float 3002399751580331 1.0d0) -276))
	       ))
  "Table of atan(2^(-k)) for k = -2 to 64.  But the first three entries are 1")

(defconstant +atan-power-table+
  (make-array 67
	      :element-type 'double-float
	      :initial-contents
	       (loop for k from 0 below 67
		     collect (scale-float 1d0 (- 2 k)))
	       )
"Table of (2^(-k)) for k = -2 to 64.  But the first three entries are 1")

(defconstant +cordic-scale+
  #.(qd-from-string "0.065865828601599636584870082133151126045971796871364763285694473524426q0"))


;; This is the basic CORDIC rotation.  Based on code from
;; http://www.voidware.com/cordic.htm and
;; http://www.dspcsp.com/progs/cordic.c.txt.
;;
;; The only difference between this version and the typical CORDIC
;; implementation is that the first 3 rotations are all by pi/4.  This
;; makes sense.  If the angle is greater than pi/4, the rotations will
;; reduce it to at most pi/4.  If the angle is less than pi/4, the 3
;; rotations by pi/4 will cause us to end back at the same place.
;; (Should we try to be smarter?)
(defun cordic-rot-qd (x y)
  (declare (type %quad-double y x)
	   (optimize (speed 3)))
  (let* ((zero +qd-zero+)
	 (z zero))
    (declare (type %quad-double zero z))
    (dotimes (k (length +atan-table+))
      (declare (fixnum k))
      (cond ((qd-> y zero)
	     (psetq x (add-qd x (mul-qd-d y (aref +atan-power-table+ k)))
		    y (sub-qd y (mul-qd-d x (aref +atan-power-table+ k))))
	     (setf z (add-qd z (aref +atan-table+ k))))
	    (t
	     (psetq x (sub-qd x (mul-qd-d y (aref +atan-power-table+ k)))
		    y (add-qd y (mul-qd-d x (aref +atan-power-table+ k))))
	     (setf z (sub-qd z (aref +atan-table+ k))))))
    (values z x y)))

(defun atan2-qd/cordic (y x)
  (declare (type %quad-double y x))
  ;; Use the CORDIC rotation to get us to a small angle.  Then use the
  ;; Taylor series for atan to finish the computation.
  (multiple-value-bind (z dx dy)
      (cordic-rot-qd x y)
    ;; Use Taylor series to finish off the computation
    (let* ((arg (div-qd dy dx))
	   (sq (neg-qd (sqr-qd arg)))
	   (sum +qd-one+))
      ;; atan(x) = x - x^3/3 + x^5/5 - ...
      ;;         = x*(1-x^2/3+x^4/5-x^6/7+...)
      (do ((k 3d0 (cl:+ k 2d0))
	   (term sq))
	  ((< (abs (qd-0 term)) +qd-eps+))
	(setf sum (add-qd sum (div-qd-d term k)))
	(setf term (mul-qd term sq)))
      (setf sum (mul-qd arg sum))
      (add-qd z sum))))

(defun atan-qd/cordic (y)
  (declare (type %quad-double y))
  (atan2-qd/cordic y +qd-one+))

(defun atan-qd/duplication (y)
  (declare (type %quad-double y)
	   (optimize (speed 3) (space 0)))
  (cond ((< (abs (qd-0 y)) 1d-4)
	 ;; Series
	 (let* ((arg y)
		(sq (neg-qd (sqr-qd arg)))
		(sum +qd-one+))
	   ;; atan(x) = x - x^3/3 + x^5/5 - ...
	   ;;         = x*(1-x^2/3+x^4/5-x^6/7+...)
	   (do ((k 3d0 (cl:+ k 2d0))
		(term sq))
	       ((< (abs (qd-0 term)) +qd-eps+))
	     (setf sum (add-qd sum (div-qd-d term k)))
	     (setf term (mul-qd term sq)))
	   (mul-qd arg sum)))
	(t
	 ;; atan(x) = 2*atan(x/(1 + sqrt(1 + x^2)))
	 (let ((x (div-qd y
			  (add-qd-d (sqrt-qd (add-qd-d (sqr-qd y) 1d0))
				    1d0))))
	   (scale-float-qd (atan-qd/duplication x) 1)))))

(defun cordic-vec-qd (z)
  (declare (type %quad-double z)
	   (optimize (speed 3)))
  (let* ((x +qd-one+)
	 (y +qd-zero+)
	 (zero +qd-zero+))
    (declare (type %quad-double zero x y))
    (dotimes (k 30 (length +atan-table+))
      (declare (fixnum k)
	       (inline mul-qd-d sub-qd add-qd))
      (cond ((qd-> z zero)
	     (psetq x (sub-qd x (mul-qd-d y (aref +atan-power-table+ k)))
		    y (add-qd y (mul-qd-d x (aref +atan-power-table+ k))))
	     (setf z (sub-qd z (aref +atan-table+ k))))
	    (t
	     (psetq x (add-qd x (mul-qd-d y (aref +atan-power-table+ k)))
		    y (sub-qd y (mul-qd-d x (aref +atan-power-table+ k))))
	     (setf z (add-qd z (aref +atan-table+ k))))))
    (values z x y)))

(defun tan-qd/cordic (r)
  (declare (type %quad-double r))
  (multiple-value-bind (z x y)
      (cordic-vec-qd r)
    ;; Need to finish the rotation
    (multiple-value-bind (st ct)
	(sincos-taylor z)
      (psetq x (sub-qd (mul-qd x ct) (mul-qd y st))
	     y (add-qd (mul-qd y ct) (mul-qd x st)))
      (div-qd y x))))


(defun sin-qd/cordic (r)
  (declare (type %quad-double r))
  (multiple-value-bind (z x y)
      (cordic-vec-qd r)