- Timestamp:
- Oct 12, 2007, 2:27:23 AM (17 years ago)
- File:
-
- 1 edited
-
branches/working-0710/ccl/level-0/l0-numbers.lisp (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/working-0710/ccl/level-0/l0-numbers.lisp
r7354 r7395 1715 1715 1716 1716 (defun init-random-state-seeds () 1717 (let* ((ticks (ldb (byte 32 0) (+ (mixup-hash-code (%current-tcr)) 1718 (primary-ip-interface-address) 1719 (mixup-hash-code 1720 (logand (get-internal-real-time) 1721 (1- most-positive-fixnum)))))) 1722 (high (ldb (byte 16 16) (if (zerop ticks) #x10000 ticks))) 1717 (let* ((ticks (ldb (byte 32 0) (get-internal-real-time))) 1718 (high (ldb (byte 16 16) ticks)) 1723 1719 (low (ldb (byte 16 0) ticks))) 1724 1720 (declare (fixnum high low)) … … 1726 1722 1727 1723 1724 #+32-bit-target 1728 1725 (defun random (number &optional (state *random-state*)) 1729 1726 (if (not (typep state 'random-state)) (report-bad-arg state 'random-state)) … … 1733 1730 (if (< number 65536) 1734 1731 (fast-mod (%next-random-seed state) number) 1735 (%bignum-random number state)))) 1732 (let* ((n 0) 1733 (nhalf (ash (+ 15 (integer-length number)) -4))) 1734 (declare (fixnum n nhalf)) 1735 (dotimes (i nhalf (fast-mod n number)) 1736 (setq n (logior (the fixnum (ash n 16)) 1737 (the fixnum (%next-random-seed state))))))))) 1736 1738 ((and (typep number 'double-float) (> (the double-float number) 0.0)) 1737 1739 (%float-random number state)) … … 1742 1744 (t (report-bad-arg number '(or (integer (0)) (float (0.0))))))) 1743 1745 1746 #+64-bit-target 1747 (defun random (number &optional (state *random-state*)) 1748 (if (not (typep state 'random-state)) (report-bad-arg state 'random-state)) 1749 (cond 1750 ((and (fixnump number) (> (the fixnum number) 0)) 1751 (locally (declare (fixnum number)) 1752 (let* ((n 0) 1753 (n32 (ash (+ 31 (integer-length number)) -5))) 1754 (declare (fixnum n n32)) 1755 (dotimes (i n32 (fast-mod n number)) 1756 (setq n (logior (the fixnum (ash n 16)) 1757 (the fixnum (%next-random-seed state)))))))) 1758 ((and (typep number 'double-float) (> (the double-float number) 0.0)) 1759 (%float-random number state)) 1760 ((and (typep number 'short-float) (> (the short-float number) 0.0s0)) 1761 (%float-random number state)) 1762 ((and (bignump number) (> number 0)) 1763 (%bignum-random number state)) 1764 (t (report-bad-arg number '(or (integer (0)) (float (0.0))))))) 1765 1744 1766 1745 1767 #| … … 1784 1806 1785 1807 #+64-bit-target 1786 (defun %next-random-pair (high low) 1787 (declare (type (unsigned-byte 16) high low)) 1788 (let* ((n0 1789 (%i* 48271 1790 (the (unsigned-byte 31) 1791 (logior (the (unsigned-byte 31) 1792 (ash (ldb (byte 15 0) high) 16)) 1793 (the (unsigned-byte 16) 1794 (ldb (byte 16 0) low)))))) 1795 (n (fast-mod n0 (1- (expt 2 31))))) 1808 (defun %next-random-seed (state) 1809 (let* ((n (the fixnum (* (the fixnum (random.seed-1 state)) 48271)))) 1796 1810 (declare (fixnum n)) 1797 (values (ldb (byte 15 16) n) 1798 (ldb (byte 16 0) n)))) 1799 1811 (setf (random.seed-1 state) (fast-mod n (1- (expt 2 31)))) 1812 (logand n (1- (ash 1 32))))) 1813 1814 #+32-bit-target 1800 1815 (defun %next-random-seed (state) 1801 1816 (multiple-value-bind (high low) (%next-random-pair (%svref state 1) … … 1807 1822 (logior high (the fixnum (logand low (ash 1 15)))))) 1808 1823 1809 1824 #+32-bit-target 1810 1825 (defun %bignum-random (number state) 1811 1826 (let* ((bits (+ (integer-length number) 8)) … … 1836 1851 1837 1852 (defun %float-random (number state) 1838 (if (zerop number) 1839 number 1840 (let ((ratio (gvector :ratio (random most-positive-fixnum state) most-positive-fixnum))) 1841 (declare (dynamic-extent ratio)) 1842 (* number ratio)))) 1853 (let ((ratio (gvector :ratio (random most-positive-fixnum state) most-positive-fixnum))) 1854 (declare (dynamic-extent ratio)) 1855 (* number ratio))) 1843 1856 1844 1857 (eval-when (:compile-toplevel :execute)
Note:
See TracChangeset
for help on using the changeset viewer.
