-
Notifications
You must be signed in to change notification settings - Fork 16
/
usenetPost.txt
138 lines (112 loc) · 3.05 KB
/
usenetPost.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
Newsgroups: comp.lang.c
Path: sparky!uunet!ferkel.ucsb.edu!taco!gatech!rpi!ghost.dsi.unimi.it!univ-lyon1.fr!scsing.switch.ch!dxcern!marmac2.in2p3.fr!user
From: [email protected] (Meessen Christophe)
Subject: Fixed point sqrt
Message-ID: <[email protected]>
Followup-To: comp.lang.c
Sender: [email protected] (USENET News System)
Organization: Centre de Physique des Particules de Marseille
Date: Thu, 28 Jan 1993 08:35:23 GMT
Lines: 127
In article <[email protected]>, [email protected] (Dave
Hines) wrote:
>
> > Well? Does anyone know of a quick and dirty way to get a reasonably accurate
> > square root of a fixed point integer number?
>
> Hmmm - I have a long integer square root routine.
Dave Hines proposed a sqrt function receiving an unsigned long (X) and
returning
an unsigned long. The remainder, an unsigned long, is accessible as
'result2'.
The algorithm uses 4 unsigned long variables.
Thus it's a long -> long function.
I suggest two algorithms using the same variables. The first one will
compute a sqrt of a long, returning a fixed point value. The second will
compute the sqrt of a fixed point value, returning a fixed point value.
Thus it's a long -> fixed or fixed -> fixed function.
We will suppose a fixed is equivalent to a long, and longs are 32 bits
wide.
/*
* fixed sqrtL2F( long X );
*
* Long to fixed point square roots.
* RETURNS the fixed point square root of X (long).
* REQUIRES X is positive.
*
* Christophe MEESSEN, 1993.
*/
fixed sqrtL2F( long X ) {
unsigned long t, q, b, r;
r = X;
b = 0x40000000;
q = 0;
while( b > 0 ) {
t = q + b;
if( r >= t ) {
r = r - t;
q = t + b;
}
r = r * 2; /* shift left 1 bit */
b = b / 2; /* shift right 1 bit */
}
if( r >= q )
q = q + 1;
return( q );
}
/*
* fixed sqrtF2F( fixed X );
*
* Fixed to fixed point square roots.
* RETURNS the fixed point square root of X (fixed).
* REQUIRES X is positive.
*
* Christophe MEESSEN, 1993.
*/
fixed sqrtF2F ( fixed X ) {
unsigned long t, q, b, r;
r = X;
b = 0x40000000;
q = 0;
while( b >= 256 ) {
t = q + b;
if( r >= t ) {
r = r - t;
q = t + b;
}
r = r * 2; /* shift left 1 bit */
b = b / 2; /* shift right 1 bit */
}
q = q / 256; /* shift right 8 bits */
return( q );
}
for memory I just rewrote the long to long sqrt function.
/*
* long sqrtL2L( long X );
*
* Long to long point square roots.
* RETURNS the integer square root of X (long).
* REMAINDER is in the local variable r of type long on return.
* REQUIRES X is positive.
*
* Christophe MEESSEN, 1993.
*/
long sqrtL2L( long X ) {
unsigned long t, q, b, r;
r = X;
b = 0x40000000;
q = 0;
while( b >= 256 ) {
t = q + b;
q = q / 2; /* shift right 1 bit */
if( r >= t ) {
r = r - t;
q = q + b;
}
b = b / 4; /* shift right 2 bits */
}
return( q );
}
This functions have been tested. But I'll be pleased to receive any
comments.
Please respond through mail: [email protected]