* src/preproc/html/pre-html.cpp (usage): Don't describe options but
[s-roff.git] / tmac / 62bit.tmac
blobe35aabb996764de84c8a2c397369a70650e398da
1 .\" 62bit.tmac
2 .\"
3 .\" Copyright (C) 2003, 2006
4 .\"   Free Software Foundation, Inc.
5 .\"      Written by Werner Lemberg (wl@gnu.org)
6 .\"
7 .\" This file is part of groff.
8 .\"
9 .\" groff is free software; you can redistribute it and/or modify it under
10 .\" the terms of the GNU General Public License as published by the Free
11 .\" Software Foundation; either version 2, or (at your option) any later
12 .\" version.
13 .\"
14 .\" groff is distributed in the hope that it will be useful, but WITHOUT ANY
15 .\" WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 .\" FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17 .\" for more details.
18 .\"
19 .\" You should have received a copy of the GNU General Public License along
20 .\" with groff; see the file COPYING.  If not, write to the Free Software
21 .\" Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA.
22 .\"
23 .\"
24 .\"
25 .\" This file provides macros for addition, multiplication, and division of
26 .\" 62bit signed integers.  Its main application is to `scale' 31bit values,
27 .\" namely, to perform the operation `a * b / c' accurately.
28 .\"
29 .\" Note that it is the duty of the user to check whether the input values
30 .\" fit within 31 bits (this is the range [-1073741824,1073741823]).
31 .\"
33 .if d add31to62 \
34 .  nx
37 .\" .add31to62 <x> <y> <z>
38 .\"
39 .\" Add a 31bit signed integer to a signed 62bit integer.  Result is a
40 .\" signed 62bit integer:
41 .\"
42 .\"   <x> + (<y>h * 2^30 + <y>l) = <z>h * 2^30 + <z>l
43 .\"
44 .\"
45 .\" in: \n[<x>], \n[<y>h], \n[<y>l]
46 .\"
47 .\" out: \n[<z>h], \n[<z>l]
48 .\"
49 .\" Example: .add31to62 p q r
50 .\"
51 .\"          -> input registers: \n[p], \n[qh], \n[ql]
52 .\"             output registers: \n[rh], \n[rl]
53 .\"
54 .de1 add31to62
55 .  nr 62bit-lo2 (\\n[\\$2l])
56 .  nr 62bit-hi2 (\\n[\\$2h])
58 .  nr 62bit-i ((\\n[\\$1] + \\n[62bit-lo2]) / 1073741824)
59 .  nr \\$3l ((\\n[\\$1] + \\n[62bit-lo2]) % 1073741824)
61 .  ie ((\\n[62bit-lo2] > 0) & (\\n[\\$3l] < 0)) \{\
62 .    nr \\$3l +1073741824
63 .    nr 62bit-i -1
64 .  \}
65 .  el \
66 .    if ((\\n[62bit-lo2] < 0) & (\\n[\\$3l] > 0)) \{\
67 .      nr \\$3l -1073741824
68 .      nr 62bit-i +1
69 .    \}
71 .  nr \\$3h (\\n[62bit-hi2] + \\n[62bit-i])
75 .\" .mult31by31 <x> <y> <z>
76 .\"
77 .\" Multiply two 31bit signed integers.  Result is a 62bit signed
78 .\" integer:
79 .\"
80 .\"   <x> * <y> = <z>h * 2^30 + <z>l
81 .\"
82 .\"
83 .\" in: \n[<x>], \n[<y>]
84 .\"
85 .\" out: \n[<z>h], \n[<z>l]
86 .\"
87 .\" Example: .mult31by31 a b c
88 .\"
89 .\"          -> input registers: \n[a], \n[b]
90 .\"             output registers: \n[ch], \n[cl]
91 .\"
92 .de1 mult31by31
93 .  nr 62bit-1 (\\n[\\$1])
94 .  nr 62bit-2 (\\n[\\$2])
96 .  nr 62bit-sign 1
97 .  if !\\n[62bit-1] \{\
98 .    nr 62bit-sign -(\\n[62bit-sign])
99 .    nr 62bit-1 -(\\n[62bit-1])
100 .  \}
101 .  if !\\n[62bit-2] \{\
102 .    nr 62bit-sign -(\\n[62bit-sign])
103 .    nr 62bit-2 -(\\n[62bit-2])
104 .  \}
106 .  nr 62bit-lo1 (\\n[62bit-1] % 32768)
107 .  nr 62bit-hi1 (\\n[62bit-1] / 32768)
108 .  nr 62bit-lo2 (\\n[62bit-2] % 32768)
109 .  nr 62bit-hi2 (\\n[62bit-2] / 32768)
111 .  nr 62bit-lo3 (\\n[62bit-lo1] * \\n[62bit-lo2] % 1073741824)
112 .  nr 62bit-i1 (\\n[62bit-lo1] * \\n[62bit-hi2] % 1073741824)
113 .  nr 62bit-i2 (\\n[62bit-lo2] * \\n[62bit-hi1] % 1073741824)
114 .  nr 62bit-hi3 (\\n[62bit-hi1] * \\n[62bit-hi2] % 1073741824)
116 .  nr 62bit-i1 (\\n[62bit-i1] + \\n[62bit-i2] % 1073741824)
117 .  \" check carry overflow of 62bit-i1 + 62bit-i2
118 .  if (\\n[62bit-i1] < \\n[62bit-i2]) \
119 .    nr 62bit-hi3 +32768
121 .  nr 62bit-hi3 +(\\n[62bit-i1] / 32768)
122 .  \" multiply by 32768 in small steps to avoid overflow
123 .  nr 62bit-i 16 1
124 .  while \\n-[62bit-i] \
125 .    nr 62bit-i1 (\\n[62bit-i1] * 2 % 1073741824)
127 .  nr 62bit-lo3 (\\n[62bit-lo3] + \\n[62bit-i1] % 1073741824)
128 .  \" check carry overflow of 62bit-i1 + lo
129 .  if (\\n[62bit-lo3] < \\n[62bit-i1]) \
130 .    nr 62bit-hi3 +1
132 .  if !\\n[62bit-sign] \{\
133 .    nr 62bit-lo3 -(\\n[62bit-lo3])
134 .    nr 62bit-hi3 -(\\n[62bit-hi3])
135 .  \}
136 .  nr \\$3l \\n[62bit-lo3]
137 .  nr \\$3h \\n[62bit-hi3]
141 .\" .div62by31 <x> <y> <z>
143 .\" Divide a signed 62bit integer by a 31bit integer.  Result is a
144 .\" 31bit signed integer:
146 .\"   (<x>h * 2^30 + <x>l) / <y> = <z>
149 .\" in: \n[<x>h], \n[<x>l], \n[<y>]
151 .\" out: \n[<z>]
153 .\" Example: .div62by31 foo bar baz
155 .\"          -> input registers: \n[fooh] \n[fool] \n[bar]
156 .\"             output register: \n[baz]
158 .de1 div62by31
159 .  nr 62bit-lo1 \\n[\\$1l]
160 .  nr 62bit-hi1 \\n[\\$1h]
161 .  nr 62bit-2 \\n[\\$2]
162 .  nr 62bit-3 0
164 .  nr 62bit-sign 1
165 .  if ((\\n[62bit-lo1] < 0) : (\\n[62bit-hi1] < 0)) \{\
166 .    nr 62bit-sign -(\\n[62bit-sign])
167 .    nr 62bit-lo1 -(\\n[62bit-lo1])
168 .    nr 62bit-hi1 -(\\n[62bit-hi1])
169 .  \}
170 .  if !\\n[62bit-2] \{\
171 .    nr 62bit-sign -(\\n[62bit-sign])
172 .    nr 62bit-2 -(\\n[62bit-2])
173 .  \}
175 .  nr 62bit-i 31 1
176 .  while \\n-[62bit-i] \{\
177 .    nr 62bit-hi1 (\\n[62bit-hi1] * 2 % 1073741824)
178 .    nr 62bit-3 (\\n[62bit-3] * 2)
179 .    nr 62bit-hi1 +(\\n[62bit-lo1] / 536870912)
181 .    if (\\n[62bit-hi1] >= \\n[62bit-2]) \{\
182 .      nr 62bit-hi1 -\\n[62bit-2]
183 .      nr 62bit-3 +1
184 .    \}
185 .    nr 62bit-lo1 (\\n[62bit-lo1] * 2 % 1073741824)
186 .  \}
188 .  if !\\n[62bit-sign] \
189 .    nr 62bit-3 -(\\n[62bit-3])
190 .  nr \\$3 \\n[62bit-3]
193 .\" EOF