Вы находитесь на странице: 1из 282

Hacker's Delight

Henry S. Warren, Jr.

ADDISON-WESLEY
Boston * San Francisco * New York 4 Toronto 4 Montreal
London 4 Munich * Paris 4 Madrid
Capetown 4 Sydney 4 Tokyo * Singapore Mexico City


, .

""
* - *
2004

32.973.26-018.2.75
64
681.3.07

""
. ..
. .. , ..
. ..
"" :
info@williamspublishing.com, http://www.williamspublishing.com

. , .
64
, . . : . . .:
"", 2004. 288. : . . . .
ISBN 5-8459-0572-9 (.)
""
, , . .
, , ;
.

; ,
. ,
,
.
32.973.26-018.2.75

.

, ,
,
Addison-Wesley Publishing Company, Inc.
Authorized translation from the English language edition published by Addison-Wesley Publishing Company, Inc,
Copyright 2002
All rights reserved. No part of this book may be reproduced or transmitted in any form or by any means, electronic
or mechanical, including photocopying, recording or by any information storage retrieval system, without permission
from the Publisher.
Russian language edition published by Williams Publishing House according to the Agreement with R&I
Enterprises International, Copyright 2004
ISBN 5-8459-0572-9 (.)
ISBN 0-201-91465-4 (.)

"", 2004
Addison-Wesley Publishing Company, Inc, 2002

10

12

1.

15

2.

25

3. 2

57

4.

63

5.

75

6.

99

7.

107

8.

131

9.

139

10.

155

11.

197

12.

215

13.

227

14.

233

15.

251

16.

261

. 4-

273

277

279

283

10

12

13

1.

15

1.1.
1.2.

18

2.

25

2.1.
2.2.
2.3.
2.4.
2.5.
2.6.
2.7. sign
2.8.
2.9.
2.10. " 2**"
2.11.
2.12.
2.13. ,
2.14.
2.15. /
2.16.
2.17. ,
2.18. Doz, Max, Min
2.19.
2.20.

3. 2
3.1. 2
3.2. 2
3.3. 2

4.
4.1.
4.2.
4.3.

5.
5.1.
5.2.
5.3.
5.4.

6.
6.1.
6.2.

28
30
31
31
32
32
33
33
34
34
39
45
46
47
47
48
50
51
53

57
57
58
60

63
63
65
68

75
75
83
86
92

99
99
104

7.
7.1.
7.2.
7.3.
7.4. ,
7.5.
7.6.

107
107
111
113
121
126
129

8.
8.1.
8.2. 64-
8.3.
8.4.

131
131
133
134
135

9.
9.1.
9.2.
9.3.
9.4.

139
139
142
146
149

10.
10.1. 2
10.2. 2
103.
10.4. , 2
10.5. , -2
10.6.
10.7.
10.8.
10.9. , 1
10.10.
10.11. ( )
10.12.

10.13.
10.14.
10.15.
10.16.

155
155
156
157
160
166
169
171
175
177
180
181

11.
11.1.
11.2.
113.
11.4.

197

12.

215

184
184
186
186
193

204
205
207

12.1. -2
12.2. -1+/

215

123.
12.4.

223
224

13.
13.1.
13.2.
133. -
13.4.

227

230

14.
14.1.
14.2.
143.
14.4.
14.5.
14.6. ,
14.7.

233
235
237
243
245
248
248
249

15.
15.1. IEEE
15.2.
15.3.
15.4.

251
251
254
255
257

16.
16.1.
16.2.
163.
16.4.

261
261
263
266
267

. 4-

273

277

279

283

. (Joseph W. Gaud),
,


30 , , MAC .
DEC PDP-10,
, . , , , .
PDP-10 , , .
; , ,
Web-, PDP-10. (
" ".)
1972 ,

1.
: ; , .
( )
(,
), , , ,
, ,

.
, .
. ,
, .
", , .
- , ".
, , . , , ,
. , ,
. . , Java, .

, - ,
. ,
1
? hacks memo; 36- PDP-10
6- , PDP-10, ,
. ,
, .

10

. , , .
, ,
, .
. ,
. ,
, ,
,
, , ,
. .
. , . (Guy L. Steele, Jr.)
,
2002


Caveat empto?:
.
(
,
. (Robert D. Bliss), 1992)
, . ,
. 32- , .

.
, . .
, , , . , Fortran Java , Pascal ADA !
.
, , . , ,
.,
.
, ,
. , ;
, .
.
, .
, RISC-.
. ,
. ,
.
, .
, 0 ( ) .
2

12

Caveat emptor (.) - .

,
. , , ( ).
3.
, - , , . , ,
.
, .
, . ,
,
4. , ,
, , - .

(Bruce Shriver) (Dennis Allison), .


, .
(Martin E. Hopkins) IBM,
" ". , .
Addison-Wesley, . , 50- , . , . (Guy L. Steele, Jr.), , , , , ().
. . , , 2- 3- 17- 257- 65537;
, 3 5.
. , ,
" ", .
Web-
www.HackersDelight.org.
.. . (H.S. Warren, Jr.)
, -
2002

"" , , Web-
, "".
. . .
4
, , 64 :
m a i n ( a ) { p r i n t f ( , 3 4 , = " m a i n ( a ) { p r i n t f ( , 3 4 , a = % c % s % c , 34) ; } " , 3 4 ) ;}

13

1.1.
, . " " ( )
. , . - ; . , , . ,
, .

, ,
2", .
, .
,
: (and), , , .
, , 32 , 2 .
, .

, , , , ( , ). ,
. ,
(
, ). (,
), .
, "+", , (" "). , ( ). ; (
, ). ,
= 0x80000000, . = 0x80000000, = = -231, + = -232
+ = 0. ( 0x80000000 ,
31 .)
, ()
0. , , , , 1,4,8, 16, 32 64 .

"abs", "rem" , , .
< < z
< , ( 1, , 0, ),
z. x<y<z "<"
(x<y)&(y<z).
: while, do for. while :

while ()
. (
), . . while , ( ).
do while, , :

do while ()
, .
, , ,
.
for
f ( 6 ] ; 2; })
\ , (
-). ( ). ( ), , ( ), . ^ (, , ), ei.
, "do i=l to n" for (1=1,- i<=n; i++) (
).

1.2.

, , , RISC ( Compaq Alpha, SGI MIPS IBM RS/6000).
, 16. , 32-.
0 ,
.
, " ", , , "".
, .
RISC: " RISC",
. 1.2, "RISC ", RISC-
, . 1.3.

18

1.

1.2. RISC-

add, sub, mul,

RT, RA, RB

RT<-RA op RB

div, divu, rem,


remu

addi, muli

op (add), (sub), (mul), (div), (divu),


() ()
RT, RA, I

RT<RA op

op (addi)
(muli), I 16-

addis

RT, RA, I

RT<-RA+(I

and, or, xor

RT, RA, RB

RT-RA op RB

0X0000)

op (and), (or) ()
andi, ori, xori

RT, RA, lu

, 16-

beq, bne, bit,


ble, bgt, bge

RT, target

target, , .. RT=0, RT#0, RT<0, RTSO,


RT>0 RT>0 (RT )

bt, bf

RT, target

target, (true/false); bne /beq

cmpeq, cmpne,

RT, RA, RB

RT RA RB; RT 0, , 1, . : , , , .,
. ""

RT, RA, I

cmpeq,
16-

RT, RA, lu

cmpltu,
16-

RT, d(RA)

, , RT
RA + d, d
16-

cmplt, cmple,
cmpgt, cmpge,
cmpltu,

cmpleu,

cmpgtu, cmpgeu
cmpieq, cmpine,
cmpilt, cmpile,
cmpigt, cmpige
cmpiequ,

cmpineu,

cmpiltu, cmpileu,
cmpigtu, cmpigeu
Idbu, Idh, Idhu,

Idw

1.2.

19

. 1.2

mulhs, mulhu

RT,RA,RB

RT 32
RA RB ( )

not

RT,RA

RT RA

shl, shr, shrs

RT, RA, RB

RT RA,
;
(RB).
shrs
,
. ( 64)

shli,shri,shrsi

RT, RA, lu

RT RA,
,

I

stb,sth,stw

RS, d(RA)

, RS RA + d,
d 16-

RA RB .

, , (
switch), ,
, . ,
.
, .
, RISC, . 1.3.

1.3. RISC-

abs , nabs

RT, RA

RT ( ) RA

andc , eqv , nand ,

RT, RA, RB

, ,
,

extr

RT, RA, I,
L

I I+L- 1 RA RT;

extrs

RT, RA, I,
L

extr,

nor, ore

20

1.

. 1.3

ins

RT, RA, I,
L

RA 0 L- 1
I I+L-1 RT

nlz

RT, RA

RT
RA ( 0 32)

pop

RT, RA

RT RA (
0 32)

Idb

RT, d(RA)

RA + d RT, d 16-

moveq, movne,

RT, RA, RB

RT RB, RA=0 (RA^O


..)- ,
RT

shir, shrr

RT, RA, RB

RT RA
;
RB

shlri, shrri

RT,RA, lu

RT RA ;

trpeq, trpne,

RA, RB

(trap), RA=RB (RA*RB ..)

RA, I

trpeq
,
16-

RA, lu

trpltu
,
16-

movlt, movie,
movgt , movge

trplt, trple,
trpgt, trpge,
trpltu, trpleu,
trpgtu, trpgeu
trpieq, trpine,
trpilt, trpile,
trpigt, trpige
trpiequ, trpineu,
trpiltu,

trpileu,

trplgtu, trpigeu

" ", , .
. 1.4.

1.2.

21

1.4.

beq RO, target


, -2311<232

li

target
RT, I

mov

RT, RA

ori RT, RA, 0

RA RT

neg

RT , RA

sub RT, RO, RA

(
)

addi RT, RA, -I


15
(1*>-2 )

subi RT, RA, I


16
, I. , 0<1 <2 ,
15
ori RO. 2 <1 <0,
addi RO. 16 I ,
addis. I : ,
ori addis. ( , , , ,
, .)
, RISC-
.

RISC-. ,
SPEC. , , , , (int),
. ,
2 ; , 1,
.
RISC- , .
,
. .
:
, , (
). ,
. , -.
, (moveq)
, . RT, RA RB,
RT , -

22

1.

. , , RT,
. ,
, () . , .
. RISC, ,
32-
32 (5- ).
, , 14 , 64 ( 6- ).


, , , , . , , .
,
, .
, , ,
(
, ).
,
, . , . ,
( ).
,
RISC-,
.
, ( ,
, ).
, . , ,
,
. :
, .

1.2.

23

,
. , RS/6000, 1992 , ,
(,
). , -
. , . ,
, -. , , .. .

24

1.

2.1.
.
(, 01011000 =>
01010000),
*&(*-!).
,
2 ( 0).
, 2-1, :
*&(*+!).
(, 01011000 =>
00001000, , 0),
*&(-*).
(, 10100111 => 00001000,
, 0),
-*&(* + !).

, , . (, 01011000 ==> 000001 ,


0, 1.)

&(-1),
-!(*!-*),

(*&-*)-!

.
, (, 01011000 =>
00001111, 0, 1).
(-1)
(,
01011000 => 01011111, 0, 1).

|(*-1)
(,
01011000 => 01000000)
((*|(*-!)) + !)&*.

, 2'-2*,
j > k > 0 ( ).
:
( ), -1
+ 1 , - -.(: + 1) , & | | &, ,
. ,
,
.
(, 101001 1 1 => 10101 111,
0, , ),
:
, , , , [59]. , ,
, , , , ,
( ) . ,
, , . .
. , , , , , ,
, .
, , , . , ,
.. ,
, ..
( ) ,
.
, ,
, , ,
, .. .
,
. , , :
, =*,!(*&*).

(1)

( 0 31.) / ( 2
), .
, , , ,
, . , .

26

2.

X31

30

J-30

34

0
0

1*

...

'

-2

>0

''

...
...

2 3 , , (1),
1,
( 4) . , , . 32
. .
,
,
.
, , , ,
, ( ,
). , , ,
, (
1, , 0, ;
, ).
, , . ?
. ,
, / 1, i'- .
, ..

, , , , ( ).
.. (R.W. Gosper) [25,
item 175]1. , .
, , ,
, . ,
11110000, ,
100000111. . , .. s = &- (
000000010000). , r=xxxl00000000,
1

[32], .7.6.7.

2.1.

27

. ,
-1 1,
. : ,
000 11111 0000.
, , . s,
(s 2), , , .
.
s < & -

*-\

((

\* \

(*$) 2 Us

2.1 ,
RISC-, . (
=0, 0.)
2.1.

unsigned snoob(unsigned x) {
unsigned smallest, ripple, ones;
// x = xxxO 1111 0000
smallest = x & -x,//
0000 0001 0000
ripple
= x + smallest;
//
xxxl 0000 0000
A
ones
= x ripple;
//
0001 1111 0000
ones
= (ones 2)/smallest; //
0000 0000 0111
return
ripple | ones;
//
xxxl 0000 0111
, ntz(r), \() () ( ), (2)
( 32).
<-\

<-\ (jrr)(33-nlz(s))l
*-\ (1(()-2))-1)

2.2.
, .
.

28

)
)

- = - + 1
= -,(*-!)

= - -1
2.

= + 1

-,-* = -1

+ = -1
= () + 2(

= ( \ ) + (&)

)
)

2(\)-(9)

- = +-. + 1

= (&-,)-(-&)

(9)-2(-&)
2(&-)-(9)
(\)-(&)

&- = (*1.)-.

= *-(*&>)

)
)
)

-|(*-JO = --1
= - + .

)
X)

(&)-(\)-1

= (&) + -,(\)
*|j

= (&-) +

*&> = (-|>)

() , : . = + 2 ..
, () : ->\- = -2. , , .
() (), , .
() () [25, item 23]. () :
: : ( ),
. ()
, 0+1 1 +0.
, 0.5. ,
,
(), 0.25.
; ,
, ()
.
() () () () . () (), . , () ,
1-1 0-0.
2.2.

29

() , , RISC-. -
(( | ) & -*( &)). , () ()
( (DeMorgan) .

2.3.

/
, , .
:

()<(\)

. 2.1, .
2.1. 16

=^

0
1
1

f 1

1
1
1

-S;

? 5

1
1

1
1

1
1

1
1
1
1

f ( x , y ) g(x,y) . 2.1. , f ( x , y ) 1, g(x,y) 1, (,)


f ( x , y ) < g ( x , y ) . ,
.
( ), : (&)<<(\-<) ..
f ( x , y ) g(x,y) 0 1, 1 0 , . , , - / g

f ( x , y ) < g ( x , y ) .
. , ,
+< z<x, z + ; y < a . , "+" .
, , ,
. .

30

2.

(\)>.(,)

(&)<(,)

(\)< +

> +

( \)

\-\<(()

, , ,
(). \-\ -,
max.(x,y)-min(x,y).
( ,
* , ).

2.4.
, ( ).
,<-;31, (, 2
+ , 1).
abs
nabs
(<)-
-()
( + )

(-)

-(2&)

(2&)-

,
1, :

2.5.
" " (sign extension) , ,
. ,
. , , .
.
(( + 0x00000080) & OxOOOOOOFF) - 0x00000080
((& OxOOOOOOFF) 0 0x00000080) - 0x00000080

2.4.

31

"+" "-" "". , , ,


.

2.6.

(shift right signed),
. [21],
, . 64-
0<<31, 0<<63. ,
"" " ,
".
RISC-.
( + 0x80000000) ! - 800000001
0x80000000 ;
-(jc&0x80000000)ii; ( *:t>it j-U+f

f * \ f f

lxn\\ - :31 31-

0x80000000 1 < 31- .


,
. =31, " \.
: -I( 31

2.7. sign
sign, signum,

[29].

, 2.6. ( ).
32

2.

.
(*>0)-(<0)
>0)-(*<0)

" \- ( 31
" \ ;
: (-31
= -231 .

2.8.
sign :

.
, , , .
, (3).
(>)-(<)
(>)-(<)
PowerPC [10].
"" " ".
subf
subfc
subfe
subfe

R5,
R6,
R7,
R8,

Ry,
Rx,
Ry,
R7,

Rx
Ry
Rx
R5

#
#
#
#

R5
R6
R7
R8

<-- Rx-Ry
<-- Ry- Rx,
<-- Rx-Ry+,
<-- R5-R7+nepeHoc, ( )

RISC-, . < , <


(. 2.11), , 12 ( < <).
RISC-
( ).

2.9.
( Fortran ISIGN),
:
-abs(jt), y<0.

2.8.

33

( 32)
.

= (abs(*)+/)e/

2.10. " 2**"


0 . n- , 2",
. , PowerPC
(Iswi) 5 . ,
, 32 . 0 31, ,
, 1 32, " 32" , ( ) ,
(, Iswx PowerPC).
1 2" , 2", :
2"-1. - .
. , .
((*-1)&7) + 1

(( _1)|_8)+9

((* + 7)|8)-7

(( + 7)&7) + 1

((*+7)|-8) + 9

((-1)&8) + *

8-(-*7)
~(-*|8)

2.11.
, , 1, , 0, . (
). 1 0, (, ), 31 .
-1/0, (, Basic),
3 1 .
, MIPS, Compaq Alpha, RISC
,
0/1 , .
"nabs", .
, . 34

2.

"nabs", "abs", nabs(x)


-abs () . : ,
, (, ). "abs", "nabs",
, .
"nlz"
. doz ( ) 2.18.
= :

abs(x-y)-!
abs(x-y + 0x80000000)
n\z(x-y)<K26

#:

nabs(x-y)
nlz(x-y)-32
-\-

<:

(-)[(>)&((-))]
(&-)\(()&(-))
nabs(doz(.y,x))

<:

[24]

(\
[24]

<:

(-*&)\((.)&(-))
(-&)\((-\)&(-))

<:

1^*1

> , > : . 0x80000000


, .
, < /2 - / 2 , .
, ,
.

<:
<:

2.11.

35

(
-), , ( ).
, "nlz", [56]. = 1 0 .

0 , . , . , , .
=0:

abs(x)-l
abs(x + 0x80000000)

x*0:

nabs(x)
nlz(*)-32

>0:

\(-1)
\-<-
xnabs(jc)

x>0:

-x

<0:
*<0:

[]

-X&-X

, 231. .

< =
= -2"<-2" .

<, < .. 2 ' , .

36

2.

,
, < = < , , < = > [42].
; ,

3, ., . <, < .

( = *)
. , N
< ,
( - ). :
< =

^ = + 231<+2"

> =
> =

<

= ~[-\

> =
>

- <


,
.
. carry ()
, . , _ - ,
+ + 1 , "".
=:

(0-(->)),
carry (( + J) + l),
carry((x ->-!) + !)

*:

carry (( - )- 1) = carry ((x-y ) + (-!))

<:

2.11.

37

x<y:
<:
.

carry ((j +2" )-( + 231))


-^cairy(x-y)
carry(>-jc)

jc=0:

carry(O-jc), carry( + l)

*0:

(-1) = carry (* + (-!))

.r<0:

(* + )

xZO:

carry (z" -(* + 2"))

> S ;
"".
IBM RS/6000 PowerPC
GNU Superoptimizer [17]. RS/6000
abs(x), nabs(x), doz(*,.y) . , RS/6000
( )
, . " " , , ( 1/0 -1/0). Power PC, abs(jr), nabs(oc) doz(x,y),
.


,
" ", (, , RISC-) . ,
.
. - + +1,
:
- ;
, - ;
N - ;
Z - , 1, ( )
, 0 .
,
( , "+" ).

38

2.

V:

, 0 ( )

=: Z
#: z
<: NV
<:
>:
>:

N=V

<:

<:

Q+Z

>:

C0Z

>:

2.12.
"" , , , . , , , ,
. (, MIPS),
( , , ).


, ,
,
. , , . , ,
, .. * + > + !.
, ,

, 0 +1.
, , , 0 1.
, 4- . , -8 S S -1 0 > ^ 7 (
). -8< + +<7.
, 4- ;
, .
, . .
()

()

-8 < < -1

0<<7

2.12.

39

()

()

, 4 , ..

()
-16<* + .+<-9

()
S<x + y+c<l5

() 4- 0, .. .
() 4- 1, .. .
, ,
-- , 0 1, 1 , . ,
, ,
-- , :
(, , , j).
( ). 31
1/0 -1/0.
+ +
--
()&((--))
((--))&((--) = )

( ), RISC , ( ). .
, , (
). ( ).
++
--
<- ( s )& 0x80000000
z<-(xy)& 0x80000000
((x + y+c)x)&((xz) + y+c) = y

(xy)&((xz)-y-c)y

z 0x80000000, , 0 . , . , 1

, ( - 2 ) + . + 0 , .. + + 23' ,
+ + . , 1 ,
JI
31
(* + 2 )+.y+c<0, .. + .+<-2 ,
40

2.

+ +. =
( 0 ), * . ( ) .
RISC.
,
,
. .
, + 2" , . , + >2" ,
.. ( + 2 3 1 )+( + 231) S 3 23'. , ( 232)
1. , ,
0.
.
(* 2") + ( 2") s .
,
s.
, .
, ,
. , - + +1.
.
, , (,
(x&y)&(sx) (xy)&(dx) , s d ).


: " ". ,
, , , -
+ J+1. , ,
. , , , , .
/
( 0 1).
:
( + + )@

:
(--)

i / i.

2.12.

41

/
/
, . , ,
, , = . , .
+ +:

&1
--:
(-*&)\((*)&(--))
(-&)\((-\)&(--))

&1
[46]
, .
() , ( ) . . , , . ,
, , 0 1,
( ).
+
++1
-
--1

-<

+ <

x + y+lx

<

x-y>x

/ , , ,
. /, .
/ ( ) , , .

, 32
( , 32-
64- ). 32 , , , . hi(*x.y)
1(.) 64- . [46].
42

2.

.
, 0, ,
.
, .
:

z<-x*y

z*-x*y

= -2" = -1 .

, -2 . , ( ). , ( < 0 & = -2" ) . 0, " " ( &).



(.. ).
32
32
, >2 -1, *>((2 -1)/.) , ,

, *>| (2 -l) / I . *' - * * / '


\
: * 0& >\ OxFFFFFFFF+.y .
!
. ,
, > 2JI -1 . , , < -231 .
(. 2.2).
2.2.

>0
*>0
*<0

> Ox7FFFFFFF -
< 0x80000000 +

0
< 0x80000000 +
* 0 & < OxTFFFFFFF +

, .
+231 .
. ,
, , .
2" -1, : , 2 .

2.12.

43

j x+y]

, * ,
, . ,
,
. . ,
32- , ,
64- + + + \ ( 64,
= 0 = ). , 64- 32. :
nlz( jc) + nlz( ) 2 32 , ;
nlz(jr) + nlz(.y) < 30 , .
nlz(*)+nlz(.y) = 31 , , . , , / = *[;/2] ( ). 2/ ( 2t + x ,
31
), , 1 2 2 . , 2.2,
( overflow).

2.2.
unsigned , , z, m, n, t;

m = nlz (x) ,n = nlz (y) ;

if (m + n <= 30) goto overflow;

t = x* (y 1) ;

if ((int)t < 0) goto overflow;

z = t * 2;
if (y & 1) {
Z = z + X;

if (z < x) goto overflow;

// z .

. m = nlz(jc) + nlz(*) n = nlz(>) + nlz(7) .:
+ > 34 , ;
+ < 31 , .
32 33. + = 33 , 231
( -231), (.. , 1(*)<0).
+ =32 .

44

2.

; , nlz(abs(je)) + nlz(abs(>)),
, 31 32.

+ ,
= 01 ( = 0x80000000 & = -1).
( )
0+0.
, , ( ). , ,
.
[abs(> 0x80000000) | (abs(x) & abs(.y = 0x80000000))] <
, , 0. , ( 0),
.

z <-(* 0x80000000) | ( +1)
( ),
= 01 z = 0 .

(nabs(>)&nabs(z))a:0
f(nlz(j)|nlz(z))i5l*0
, ( ).
PowerPC .
, = 0.

2.13. ,
, . , ( ). , :

( );
;
32- ,
,
, .

2.13. ,

45

, ( 33-)
: , . ,
.
9 12 .
: " , , > 0", " ,
, = 0" ", , < 0". ,
.
, :
" , , = 0" 0 0; ", , = 0"
.
" ", .. + +1.
, - + +1 , , ( "" : , 1, , , , 0, ,
). ,
. ,
, , : " , , = 0" ", , = ".
, : , . ( 4-
FxF = 01, FxF = El ( )). , , .
() , , , , . , , , . ,
. " , , > ",
" , , = 0" ", , = ".
, , ,
10 12 .

2.14.
.
32 , 32.

46

<-[ * ]| (32-)

<- | ( <(32- )

2.

2.15. /
" /" 2.12
, ,
. (z,,z0) (*,,0)
(>,,>) . 1 , 0 . , 32 . .

>,+ .
. - f z0 < * ] ,
, ,
(0 1) , , , SLTU (Set on Less
Then Unsigned ", ") MIPS [46].
.

z, <-*,-.* -*
( "
. &<-! *<> , ,
SLTU, .
, 31 (
0, ).

2.16.
(*,,*) 32- ,
64- , \ ,
(>,, ) .
, 0 63. , 64 .
, 32 63 -32 -1,
, . , 32 . (
Intel x86, 32.)

2.15. /

47

2.18. Doz, Max, Min


doz :

< .

doz " ", ,


, 0.
(, ) (:,.) . ,
doz(x, ) , . Fortran IDIM,
.
, doz, max min
, , . , , , doz(jc, )
< ,
max min.
d<r-x-y

max (:, ) = + doz(x,y)


min(x,y) = x-doz(x,y)
doz(jt,.y) ,
, .
max min .
.

d < -

(, ) = + dozu(x,y)
(,) = x-dozu(x,y)
IBM RS/6000 ( 801)
doz(x,y), (,.) min(x,y)
,
( ).
, , 2 max min. , RISC *-.(,) :
cmplt z, ,
movne , z,

; z = 1, < , z =
; z 0, =

' ,
.

50

2.

2.19.
,
[34].

-
1-

.
=. .
^- +
*--
+--

*~-
*~ +
*--

^-
yt-y-x
* +

, , (, , " ").
,
, . , (
).
:
:

t -
< t


, i'- ,= 1,
, ,=0. "" .
. .

'<-(&)\(&)
*-(&)\(&)
*<-*'
, m -. , .
( , )
, (). .

()
*-
+-(&)

()
*-

()
t*-(xy)&m

, (), ,
,
m , .
2. 19.

51

() [19]. (
, ),
.


, jc ( ),
..
D , ,
. k .

D .
tl=(x&m)-Kk

( ^
< 2 = \xk l&ifi
D ( ),
' , .
, .
[19], . , " " (). , , , D.

, ti D D ( ), a t-i D . , , , D , , .. .


, , - ,
. , , ,
, .
, , , 52

2.

.
,
.
i

2.20.
, . , , . ,

, .
:

if ( == ) = ;
else = ;
:
= ( == ) ? : ,-

( ) : ^-+- < @ . ft ,
RISC-. , + .
:
? , ,
, . /,

f(a) = b;

f(b)=c;

f(c)=a.

, , .
,

-- ,
(-)(-)

(~)(-)

- - ,
(-)(-) '

, = ..
14 ,
,
. 3, ( ). (4) :
3

. ,
' +**' + 2 +dx + e x(x(x(ax+b)+c)+d)+e .
; .
2.20. ...

53

+[(-) +(-)
.
(4) , :
/(*)-((-(* -)))+((-(*.))*)+(*.*))*).
,
, 1 1 , .
,
.
, -
- [19]:

+
.
, , , , .

if ( == ) = ,else if ( == ) = ;
else = , [19]
, , . .
, . , HI 2 ,
a, b , , . 21,31 20.
1 0 1 0
1 1 1 1
1 0 1 0
,

1
1
0
,

a
*

, ,
, .
HI, (^.,^) = (0,0,1) (1,0,0). , 2

: (,,> , ) = (0,1,0) (1,0,1). , .


!.

( , ) = (0,1,1), ,. = (():
/(*) = * *(-*) + *., *(-)+*

54

2.

2.

(a/iA,cJ= (0,1,1), (!,*.!,!) = (1>0,1):


f ( x ) = x,,i*(a-b) + x,!*(a-c) + (b+c-a)

3.

(> >> )= (1,0,0), (, . ,; ) = (0,1,0) :


/(*) = *., *(*-) + *3 *(-)+

4.

((1|,*1,,) = (1,0,0), (,*,, = (1,0,1):


/(*) = * *(*-) + *.,. *(-) +


. 0 1 0 , 1. , ,
:

, , , RISC-. ,
.

. \, 2, ..., .
. ( ) s, t, v
(.. , f ( x ) , , d, / 0 1):
,
:

2.20. ...

55

3
2
3.1. 2
* , , 8, : & -8
*3 <*: 3 . , " " , : (-37) & (-8) = -40.
. ,
8 : ( + 7) & -8 + (- & 7) .
, " " .
,
8 , , [22].
8 0 (..
-37 -32) :

: -(jr2j29,
, .
,
log, (, 3 , 8,
Iog28 = 3). ,
k .
:

& ((-1) : k )
1<

t <-(1<*)-1; (x+t)&-t
-(-!)*;
(x-t-i)&t

3.2. 2
flp2( ) clp2( ) , 2, :
, < 0,
| , < ,
,
= 0,
clp2(*) = |
0,
=0,
2[|01!!
>0;
|
2() 2, , !2() 2, ,
. (, flp2(0. 1) = 0.0625 ).
,
() L*J ceil(x) s ["*] , .
|_*J = [] ,

2( ) = clp2(*) , 2

, . , ,
232 ( ,
!2(:) = 0 >231). ;.
X

2()

!2()

231

31

2 -1

231

31

2 +1

231

232 -1

231

31

231

2() clp2(*) , ( ).
! 2()=

58

22-1, *1,
/
I
Iflp2(2*-l], 1<52",

clp2[x+2 + l],
*<2.

3. 2

, , 31
. , =0 >2 , 0 -1, 32
63. (, PowerPC) " 64", .
(.. -1 1).
= l<K(31-nlz(*)) =

= 0x80000000nlz(x)
) = l(32-nlz(*-l)) =
= 0x80000000;*.(nlz(* -1) -l)


3.1 , , .
12 .

3.1. 2,
unsigned f Ip2 (unsigned x)
=

( 1) ;

X =

X =

(X 2) ;
(X 4) ;
(X 8} ;
(X 16) ;

X = X
X =

return - ( 1) ;
3.2 , .

. ,
0, .

3.2.
= 0x80000000;

do {

while ( > )

= ;

X = X & (X - 1) ;

= 1;

return ;

} while(x != 0) ;
return ;

4nlz(jc)+3 ;
*0 4() 1, 0 .

() .

3.2. 2

59



2. 3.3
12 .
3.3. 2,
unsigned clp2(unsigned )
= X - 1;
= X
(X
= X
(X
= X
(X
= X
(X
= X
(X
return x + 1;

X
X
X
X
X
X

l);
2);
4);
);
16)

,
.

= 1;

while ( < )
= 2*,return ;

//

;=0 1, , ; >231 . 4 + 3 , 2
. ,
>3 (.. >8).

3.3. 2
, , 2. 0. ,
, .. /.
, a + 1-l, /2,
( / , ).
/=0 1, , , . / , , ,
. / ( )
, .
IBM System/370 , ,
[7]. , 4096
( ).

RA, =(-4096)
ALR
RA,RL

CROSSES
RA ( )
OxFFFFFOOO .
60

3. 2

, .
1, ,
, 32- .
, 1 .
RA ,
( , ).
, , =0 /=4096, , RA , CROSSES .
, RISC-, .
8. [7] CROSSES
, (.. ( | -8) + 1 > 232 )
(.. ( \ -8) + / * 232 ), ( \ -8) + / > 232 . , , (( | -8) - 1) + 1 . , ;
-8.
, , + , -~< ,

->( - 1) = - " ".

RISC- .
, , 8- , (&7)+/-1>8. ( /). 8-(&7)</, , .

8-(&7)</,
RISC- (
, ).
/ -(8 -( &7)) , , .

3.3. 2

61

4

4.1.
" " (bounds checking) ,
* , ..
<<.
, .
. , , 1 10. A(i)
1 S i < 10 . i
, . ,
[52]:
, , ; , i - 1 .
:

( )? , <.
,
,
.
.
.
. b , < b ,
- , -.
. (, 32- .)
a S f c , - 0
(231 - 1) - (-231 ) = 232 - 1 . 0
231-1, (
). ,
,
.

31

32

2 2 -1, 32
2 (
32 ). , 32
, -2 1. 32
2 ,
1.
, (
31
32
2 ) 2 . ,
.
" ".
. b < ,

<:< = -<- .

. , . , <, - , ( (1)), b .
1: <. - , ,

- + 2 . b ( 32- ), 32
32
+ 2 > b - + 2 >-. ,
-> - (1) .
2: < < b. - < b - . < ,
- - , . , -< - (1) .
3: > b. - > b - . b > , > , - : - , . , ->- (1) .
,
, , .
,
, a, b .
U

< , <:< = -<- = -<-.


U

,-.
\AJ

<6, << = -<-.


u
<:, << = -<-.
u

<, a<x<b = --1<--1=--1<--1.


- -1 + -.
-2"'1 < < 2""1 -1
. ,
- . =8.

64

4.

-128<*<127

+128<255

7] + 1<1

)
)
)
)

() ()
( () , 7 ).
()-() (, , ()), , ,
() ()
.

"\

<< + 2" -I ** I ( -) I = 0.

4.2.

, .
, ,
switch .
,
( ):

<<,
c<y<d.

/.

+ , - -?
, a+c<x+y<b + d. , .
.

4.2.

65

. a, b, , d, xuy ,

<<
c<y<d,

a+cx+y<b+d

32

0S*-.y<2 -l, -</<-20,

a-d<x-y<A-c

<-< .

()

(4) , +
+ * +d . b+d ,
+ , + 0 . (5) , , 0, ,
( ).
. .
+ b+d , +
( (4)). + , +d , : + . ,
,
. ,

+ , b+d ,
+ <,212-\ b + d>2n.
+ + b + d ,
232-1 232, .. +
232-1 0 ( ).
, + , b+d
, , < c<d .
(4) . (5) ,
, "" , 0.
(6) (5), = = 0 , . ( - ,
, 232 - - + 1 .)
66

4.

(. 2. 12), , 4.1,
( s ) ( t ) .
4.1.
S = + ;
s=a-d;
t = b + d;

if (s >= a && t < b)


{
S = 0;
t = OxPFPFFFPF;

t = b - ;

if (s > a && t <= b)


{
s = 0;
t = OxFFFFFFFF;


. ,
, ( ):

< < ,
< < d.
+ , - - .
, .
a + c<-23>,b + d<-2": a+c<x+y<b+d
-2": ~2il < x + y <2SI -1
2 31 : a +c < x + <b+d

(8)

2 31 : -2 < x + y <2 -1
3>
a + c>2 ,b + d>23>: a+c<x+y<b+d
, + b + d
, + + b +d , (232). ,
+ ,
b + d , , + ( ). .
-d < - < -
, ,
.
a-d<-23t,b-c<-23<:
3l
a-d<-2 ,b-c>-2":
3>
3t
31
-2 <,a-d<2 ,b-c<2 :
3i
3t
3t
-2 <a-d<2 ,b-ct>2 :

a-d<x-y<b-c
31

-2 < - <2 -1
a-d<x
J1
-2 <x-y

4.2.

67


= = , , .
31

-2 <-

, ,
,
. -,
(8)
. ,
, (. 2.12). ,
31
+ <-2 s = + ,-, if (< && < && s>=0) ....
1
, , . : if ( ( & & -s) < ) .... , , 4.2.

4.2.
s = + ;
t = b + d;

u = a & c & ~ s & ~ ( b & d & ~t);


A
v = ((a
) | -(a * s & (~b & -d & t) ;
if ( ( u | v) < 0}
S = 0x80000000;
t = OX7FFFFFFF;
u true (.. 1), +
, b + d . v true, + , b + d . " ,
s ". if ( ( u | v ) < 0 )
if (u<o | | v<o), .. , v ( ).

4.3.
, , ( ):
<,<,
c<,y<d.

,.

, , . , ,
, , .
68

4.

\, &, -:.
(9) 2.3 ,

-jc = 2 -1 - ,
(, ) <( \y)<b+d,
0<(x + y)<mm(b,d),
Q<(xy)<b+d,
, b+d .
,
. . , ,
:
00010 < < 00100,
01001 < . < 10100.

](

( 36 ) ,
01010 ( | .) < 10111, .. (,), \ ; b+d , b\d .
,
, , d (9)?
\ . ,
, \ . , (10), .
= =
, \ .
. an , ,
, \ .
. 0, 0. 1,
1 (,
).
. , , , , 0 1
0 \.
\ ,
1 . ,
, ,
. , . ,
.
, , .
. , , 4.3.

69

\. , 0 1,
\,
.
4.3. , -&
&- . , , m :
m = 0x80000000 n l z ( a * c ) ;
,
0 1. " 0 (.. = );
64. nlz , flp2 (. 3.2) .
4.3. \

unsigned minOR(unsigned a, unsigned b,


unsigned , unsigned d)
{
unsigned m, temp,m = 0X80000000;
while (m != 0)

if (-a & & m)


{
temp = (a | m) & -m;
if (temp <= b) {a = temp; break;}
else if (a & -c & m)
{
temp = (c | m) & -m,if (temp <= d) {c = temp; break;}

m = m 1;
}
return a | c;
\ ,
, , (9). . , b
rf . , b\d , 1 0 . (..
), . , . , . , 70

4.

, 4.4.
b & d, ,
:
m = 0x80000000 nlz(b & d) ;
4.4. \
unsigned maxOR (unsigned a, unsigned b,
unsigned , unsigned d)
unsigned m,

temp,-

m = 0x80000000;
while (m != 0)
{

if (b & d & m)

temp = (b if (temp >=


temp = (d if (temp >=

m)
(m - 1) ;
a) {b = temp; break;}
m)
(m - 1) ;
c) {d = temp; break;}

m = m 1;
}

return b | d;
& , (9), :
. (DeMorgan):
, \ , &

(<< <=> <<- ):

, 4.5 4.6,
.
4.5. &
unsigned minAND (unsigned , unsigned b,
unsigned , unsigned d)
{

unsigned m, temp;
m = 0x80000000;
while (m != 0)
{

if (-a & -c & m)

temp = (a | m) & -m;


if (temp <= b) {a = temp; break;}
4.3.

71

temp = ( | m) & -m;


if (temp <= d) {c = temp; break;}

m = m 1;
}

return a & c;

4.6. &
unsigned maxAND( unsigned a, unsigned b,
unsigned c, unsigned d)

unsigned m, temp;
m = 0x80000000;
while (m != 0)

if (b & -d & m)
temp = (b & -m) | (m - 1) ;
if (temp >= a) {b = temp,- break;}

else if (-b & d & m)


temp = (d & ~tn) | (m - 1) ;
if (temp >= c) {d = temp; break;}

}
m = m 1;
}

return b & d;

}
, . ,
, , . ,
()
= ((&-,)|(-&.)).
> -:*sV\
''

sx<v
c-:,y<d

lv

c<y<d

(
, ), , .
:
minXOR (a,*,c,d) = (,,-,-) | minAND(-A,-e,c,rf),
maxXOR(e,A,c,rf) = maxOR(0,maxAND(a,ft,-rf,-ic),0>maxAND(-ift,-1a,c,d)).
minXOR maxOR .
minXOR , minOR (. 4.3),
break, * .
maxXOR , maxOR (. 4.4),
, if

72

4.

temp = (b - m) | (m - 1) ;
if (temp >= a) b = temp;
else
temp = (d - m) | (m - 1) ;
if (temp >= c) d = temp,-

, b * d.


. 0
( b d),
. . 4.1
\. 0,
"", 0 "".
minOR , \
, maxOR . \ : a,b,cnd 4- ,
0 15, switch.
, 0 15, , > b d .

<< 4=> <-<,
. 4.1
( )
. , , .

b
-

+
+

4.1. minOR
maxOR
d ()
maxOR ()
- minOR(o,*,c,d)
maxOR(a,*,c,d)
+
-1
+ minOR(a,ft,c,rf)
maxOR(a,*,c,d)

+
+

min(a,c)

-1
maxOR(0 ) ft > 0,rf)

mmOR(a,OxFFFFFFFF,c,d)

maxOR(OAc,rf)

+
+

imnOR(o,*,c,d)

(,<*)

minOR (a,b,c, OxFFFFFFFF)

maxOR(o,ft,0,rf)

minOR(o,ft,c,d)

maxOR(a,ft,c,d)

4.3.

73

5

5.1.
IBM Stretch ( 1960 )
(
!). (population count), Stretch SPARCv9.
,
.

. , ,
..
[54]. . 5.1. , , ,
( 23 ).

10

1 1 11

0001 1 0 00 1 1 0 1

1111

1011

111111

0 1 1 0 1 0 000 1 0 1 00 1 0 0 1 1 0 1 0 0 1 1 0 1 0 1 0 1 0
00 1

1 00 1 0 00 1 0 00 1 0 00 1 1 00 1 1 0 1 000 1 00

00 0 00 1 0 1 00000 1 0000000 1 1 0 0 0 00 1 0 00
0 0 0 0 0 0 0000 0 0 1 0 0 1 0 0 0 0 0 0 0000 0 0 1

1 10

0 0 0 0 0 0 0000 0 0 0 0 0000 0 0 0 0 0000 0 1 0 1

11

. 5.1. , " "


" ":
( 32- ) ( 16 ), .
( ). :
16- 8- ..

(
) , ,
. log, (32) = 5 .
" " , ,
, 7.1.
, . 5.1, :
(
(X
(X
(
(X

& 0x55555555) + ((
& 0x33333333) + ((X
& OxOFOFOFOF) - ( (X
& OxOOFFOOFF) + ((X
& OxOOOOFFFF) + ((

1)
2)
4)
8)
16)

&
&
&
&
&

0x55555555);
0x33333333);
OxOFOFOFOF);
OxOOFFOOFF);
OxOOOOFFFF);

, ,
( & ) 1,
( 1) & 0x55555555,
, - . .
, ; ,
, , . ,
,
. 5.1; 2 1 .
5.1.
int pop (unsigned x)
{
X = X - ( (X 1) & 0x55555555);
X =

(X & 0 x 3 3 3 3 3 3 3 3 )

{ (X 2)

& 0x33333333);

X = ( + (X 4 ) ) & OxOFOFOFOF;
X = X + (X 8) ;
X = X + (X 16) ;

return & ;

:
,|
(1)

(1) 0.
, (1) 31
31 . 5.1
.
(1),
4- . b3b2btba , \ 0 1.

76

5.

(1) : , , i-

i 0 31. , < 2 1 2 .
(1) . ,
10 :
sum_digit

=,-9UUU|

, .
.
4,
(), 5.1,
= - 3 * ( ( 2) & 0X33333333);
, , 3.
,
[25, item 169], (1)
, . , , ,
. , , 63.
( , ):
int pop(unsigned x)
unsigned n;
n = (x 1) & 033333333333;
x = x-n,n = (n 1) & 033333333333;
= - n;
= ( + ( 3)) & 030707070707;
return % 63;

5.1.

//
/ / 3-
//
//
// 6-
// 6-

77

63 ( , , 3).
, 6- , , , 64.
b b -1 b > 3 - \ , , . 32, modu(;t,63)
, .. .
DEC PDP-10 , ,
, 10 . RISC
13 , ,
, , ,
. 64- ,
62 .
[26] . (1) 4-
. 4- 8- , 0x01010101.

int pop(unsigned x)

unsigned n,-

( 1) & 0x77777777;

X - ;
( 1) & 0x77777777;

X - ;

//
// 4-
//

( 1) & 0X77777777;

X - ;
( + ( 4)) & OxOFOFOFOF;
*001010101;

return 24;

//
//

19 RISC-. , . 0x77777777
,
. , .
[54, 60] ( 5.2). ,
0. ,
2 + 5pop(*) , , .
5.2.

int pop(unsigned x)
int n, = 0;

78

5.

while (x != 0)
n = n + 1;
X = X & (x - 1) ;

return n;
.
( = | (+1))
, (..
-1). 32- . ( ,
32 , .)
[47].
32 , . () !
:
J1 ( \

() = -!)[*'']

(2)

, , 2. , : 31 , ..
63 , .
, (2), , . , 32 , 31 ,
. -1.
6- : = 001001.
001001
010010

ml

100100
001001
010010
100100

, .
(1) , , (1)

()
(2) . -,
, ,
, ; -,
, . , . 5.3.
5.1.

79

5.3.
int pop (unsigned x)

int i, sum;

// //

sum = ;

forfi = 1; i <= 31; i++)


x = rotatel (x, 1) ;

// sum = ;

// while(x != 0)
//

sum = sum + x;

//
// }

return -sum,-

x = x 1;

sum = sum - x;

// return sum,-

}
, pop(jr) , ,
. , 0 255 table
() .
table .
( ):
int pop (unsigned x)

//

static char table [256] = {


0, \, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
4, 5, 5, 6, 5, 6, , 7, 5, 6, 6, 7, 6, 7, 7, 8};

return table
table
table
table

[x
& OxFF] +
[(x 8) & OxFF] +
[(x 16) & OxFF] +
[(x 2 4 ) ] ;

[25, item 167]


9- , . 36 .
32- ( 8- ).
X

=
=
=
=
=

* 0x08040201;
3;
& 0x11111111;
* 0x11111111;
28;

//
//
//
//
//

4

4-
(0 1)

7- :

=
=
=
=

* 0x02040810;
& 0x11111111;
* 0x11111111;
28;

// 4
// 4-
// (0 1)
//

* 15.
80

5.

. . , ,
64- 64- ,
. 15-
( , 16- , , , 16 ).
long long GNU [57]
, int ( long long
64 ). ULL unsigned long long.
int pop (unsigned x)

unsigned long long y;


= x * Ox0002000400080010ULL;
= & OxIlllllllllllllllULL;
= * OxIlllllllllllllllULL;

= 60;
return ;


pop(jc)
, 5.1, .
, ,
.
4- 4, 4- 12, .
8-
, 24,
= (X & OXOFOFOFOF) + ( (X 4) & OxOFOFOFOF) ;
, \ 255; ( [255/24 J ). 10
16- 240
:
X = (X & OxOOFFOOFF) + ( (X 8) & OxOOFFOOFF);
, 32-
, 273 ([65535/240]).
:
X =

(X & OXOOOOFFFF)

( (X 16);

5.1.

81

, , , . , 5.4 . 8- .
, . 8- ,
, [25 5/8 J = 31 .
5.4.
int pop_array (unsigned A[], int n)
{

int i, j, lim;
unsigned s, sS, x;
S = 0;

for (i = 0; i < n; i = i + 31)

lira = tnin(n, i + 31);


S8 = 0;

for (j = i; j < lim,- j++)


{
X = A[j];

X = X - ((X 1) & 0x55555555);


x = (X & 0x33333333) + ((x 2) & 0x33333333);
x = (x + (x 4)) & OxOFOFOFOF;
s8 = s8 + X;

}
X = (s8 OxOOFFOOFF) + ( (s8 8) & OxOOFFOOFF) ;
x = (x & OxOOOOFFFF) + (x 16);
s = s + x;
}

return s;

}
. GCC , RISC . 22 17.6 S.4, ..
20%.

() , , " "
(Hamming distance) . (
) , , ..
(, , [12].)
, .
, .
82

5.

bits ( 32- ),
i, A[i] .
bitsum, , bitsum[j] bits , j-. , 0, 2, 32,
47, 48 95- .
bits
0x00000005
0x00018001
0x80000000

bitsum

2
5

[0]
[2]
[32]
[47]

(95]
/, 0 < / < 95 , sparse_i bits, , i. :
j = i 5;

k = i & 31;

// j = 1/32

// k = r e m ( i , 3 2 )

mask = 1 k,// "l" k


if ( ( b i t s t j ] & mask) == 0)
goto no_such_element;
mask = mask - 1;
// k
sparse_i =
bitsum [j] + pop ( b i t s t j ] & mask);

.
(-)
(. 5.4).

5.2.
"" (parity) ,
. "",
; 1.


1, ,
0, .
2, .. .

"" "" .
, , ""
, "" . . .
5.2.

83

(*)
, . () .
, () .

,
. ( ,
.)

( = 32 ; , ).

=
=
=
=

X * (X 1) ;
* ( 2) ;
( 4) ;
( 8) ;

(3)

= * ( 16) ;

10 , , (62 ),
.
.. i'-
, /- . ,
,
( i) ( j) , i - 1 ( i S ).
" ",
"", [30, 41]. ,
() O(Iogn) . , , (3)
, , , .
( ) ,
, , , .
O(logn) , () ( ).
(3)
(. 13).
(3), , , /- , /- .

, : , ,
.
84

5.

, 0 1 ( ).
X = X

(X 1) ;

X = ( (X 2)) & 0x11111111;


X = *11111111;
= (X 28) & 1;


1, .
, . , 8.
,
15, () , ,
, .

7-
[25, item 167] , 7- ( ). ,
.
36- , 32- .
modu (( * 0x10204081) & Ox888888FF,1920).
modu(a,ft) ,
, "* " 232,
1920 15-27. . ,
OxOOOOOOTF 0x00000055 0x00000255.
[25] 7-
( 8- ):
tnodu((x *0x00204081)| Ox3DB6DBOO,1152),
1152 = 9-27. , , , 8
9 1. Ox3DB6DBOO OxBDB6DBOO,
.
, ,
, . .

GF(2) (
).

5.2.

85

5.3.
, . , . RISC-
20-29 . .
if ( == 0) return(32);

0;

if (x
if (X
if (x
if (x
if (X

<=

<=
<=
<=
<=

OxOOOOFFFF) {n = n + 16;
OxOOFFFFFF) n = n + 8;
OxOFFFFFFF) n = n + 4;
OX3FFFFFFF) n = n + 2;
OX7FFFFFFF)

return n;

n = n

X = X 16;}
X = X << 8;)
X = X << 4;}
X = X << 2;}

+ 1;

:
if ( ( & OxFFFFOOOO) == 0} {n = n +16; x = X 16;
if ((x & OxFFOOOOOO) ==0) {n = n + 8; X = X 8;
,
, .
if , ,

= + 1 - (X 31);

"+1" , 0, 1. , 12 20
RISC- ( 5.5). , ; :
if ( ( i n t ) x <= 0) return (-x 2 6 ) & 32;

5.5.
int nlz(unsigned x)
{
int n;

if (x === 0) return(32) ,n = 1;
if ( ( X :.> 16) ==0) {n = + 16;
if X : > 24) ==0) {n = n 8;
if ( ( X ; > 28) ==0) {n = ^ 4 ;
if X 5 30) ==0) {n = 2;
n = n - (x 3 D ;
return n,-

X
X
X
X

=
=
=
=

X 16 /
X << 8/
X << 4 7
X <: < 2 i

5.6 ,
. ,
, . 12 20 RISC-.
86

5.

5.6.

int nlz(unsigned x)
unsigned ;

int n;

= 32;
= X 16;

= 8;
= X 4;

= X 2;

if

if

if
if

*
( ! = 0) { = -16;
= - 8;
( ! = 0)
= - 4;
( ! = 0)
( 1 _ 0) = - 2;

X
X =
X =
X =

X 1; if ( != 0) return n - 2;
return n - x;

;
;
;
;

,

static char table[256] = { 0 , 1 , 2 , 2 , 3 , 3 , 3 , 3 , 4 , 4 , . . . , 8 ) ;
return n - table[x];
,
.

. , 5.7 5.6 . 23-33
RISC-, 10 .
5.7.

int nlz(unsigned x)

unsigned ,int n, c,-

n = 32;
= 16;

do {

= X ;

if ( != 0) {n = n - ; x = ;}
= 1;

} while ( != 0);

= - (x 31) ;
return n - x;

}
,
1, ,
1;
, . , ( )
. , 5.8.
5.3.

87

,
. , ,
VLIW- ( )
(
).
5.8.
int nlz(int x)

{
int , ;

L:

= 0;
= X;
if (x < 0) return n;
if ( == 0) return 32 - , = + 1;
X = X 1;
= 1;

goto L;

RISC-

min(3 + 6nlz(*),5 + 6(32-nlz(*))) , 99 . VLIW-, ( ; ),


.
5.5 5.6 ,
. 5.9
nlz 28 RISC-.
5.9.

int nlz(unsigned x)

int , m, n,
m

=
=
=
=

-(x 16);
// = ,
( 16) & 16; // = 16.
16 - ;
// , = 0
;
// 16 .
//
= - 0x100;
// 8-15 ,
m = ( 16) & 8;
// 8
n = n + ,// 8

= ;

= - 0x1000;
88

// 12-15 ,
5.

m = ( 16) & 4;
n = n + m;

// 4
// 4

X = X ;

= - 0x4000;
= ( 16) & 2;
n = n + ,-

// 14-15 ,
// n 2
// 2

= 14;
m & ~( 1);
return n + 2 - m,-

// = , 1, 2 3
// m = , 1, 2 2

= ;

pop(x),
, 5.10.
.
11 .
, () .
5.1, 21
, 32 RISC-, .
5.10.
()
int nlz(unsigned x)
{
int pop(unsigned x) ;
X = X

X = X

x = x
x = x
x = x

(X 1) ;
(X 2 ) ;
(X 4) ;
(X 8) ;
(X

return p o p ( ~ x ) ;



, IEEE-.

, . 5.11.
5.11.
IEEE-
int nlz(unsigned k)
{
union {
unsigned a s l n t [ 2 ] ;
double asDouble;
};
int n;
5.3.

89

asDouble = (double)k + 0.5;


n = 1054 - (aslnt [LE] 20);
return n,-

() C++ . LE
1, , , 0 . 0.5 ( ) k = 0.
,
-. ,
, , . , ,

.
5.11 ANSI- C++, . , ,
. XLC IBM AIX GCC AIX Windows 2000 ( ).
, .
= (double)k + 0.5;
n = 1054 - (*((unsigned *)&xx + LE) 20);
, ANSI, ,
[8]. ,
, .
2,
.
asDouble = (double)k;
n = 1054 - (aslnt [LE] 2 0 ) ;
n = (n & 31) + (n 9) ;

k = k & ~(k 1);


asFloat = (float)k + 0.5f;
n = 158 - (aslnt 23);
k = k & ~ (k 1) ;
asFloat = (float)k;
n = 158 - (aslnt 23);
n = (n & 31) + (n 6) ;

. .
90

5.

k = 0.5,
(
1054 (41)).

, . , : , ( ), . k (
) FFFFFF80 hhhWhbh, 7FFFFFCO 7FFFFFFF,
3FFFFFEO 3FFFFFFF ..
, .
, .
GNU C/C++
, [57]. , . . (
.)
#define NLZ(kp) \
({union {unsigned _aslnt; float _asFloat ; } ;
unsigned _k = (kp) , _kk = _k & ~ (_k 1) ;
_asFloat = (float) _kk + 0.5f;
158 - (_aslnt 23) ; })

\
\
\

,
kp (
).


, nlz " 2".
* 0 (. 1 1 .4):

bitsize , . :

bitsize(jc) =

1,
2,
3,
4,

: = -1 ,
= -2 1
-4<:<-32<;<3,
-8 < < -5 4 < < 7,

32, -2 3 1 <^<-2 3 0 +12 3 0 <:<2 3 | -1.

5.3.

91

, bitsize(jc)=bitsize(-jc-l). , --1 = -,
bitsize (
):
X = X * ( 31);
return 33 - n l z ( x ) ;

// ( < 0 ) , X = - - 1;


(. 9.1 9.3). , nlz(jt) .
: = (.
2.11) (. 11.1-11.4).

. nlz [19].
0 1/2, 1
1/4, 2 1/8 .. ,
, (
) (,
). ,
.

5.4.
, :
32 -nlz(-ct &(*-!)).

, [27], :
(-1*&(:-1))
32-(|-*).
, 2.1. ,
. ,
5.1, 3 + 21 = 24 , .
5.12 , , 12 20 RISC- ( * 0).

92

5.

5.12.
int ntz(unsigned x)

int n;
if (x == 0) return(32);

n = 1;
if ( (X
if ((X
if ( (X
if ((X
return

&
&
&
&
n

OxOOOOFFFF)
OxOOOOOOFF)
OxOOOOOOOF)
0x00000003)
- (x & 1);

==
==
==
==

0)
0)
0)
0)

= +16; x = X 16;}
= + 8; X =
= + 4; X =
= + 2; X = X 2;

n + 16 17, , ( ).
5.13. . 12 21 RISC-. ,
(,
, , ).

5.13. ,

int ntz(unsigned x)

unsigned ;

int n,if (x == 0) return 32;


n = 31;
= X 16; if (y != 0) {n = n -16;
8; if (y != 0) {n = n - 8;

= x 4; if
= x 2; if
= x 1; if
return n ;

(y
(y
(y

!= 0) {n = n - 4;
!= 0) {n = n - 2;

!= 0) {n = n

X = y;}
X = y; }
X
X

= ;
=

- i;}

return ,
( ):
= - ( ( X 1) 3 1 ) ;

[4].
5.14 8- .
(
return 7 return 8, ). 32- 11 13 . ,
. , 8- 12 ( 41 );
32- 48 164 , 64 .
5.4.

93

5.14. ,

int ntz(char x)

if (x & 15) {
if (x & 13) {
if (x & 1) return 0;
else return 1;
}
else if (x & 4) return 2;
else return 3;
else if (x & 0x30) {
if (x & 0x10) return 4;
else return 5;
}
else if (x & 0x40) return 6;
else if (x) return 7;
else return 8;

}
, , ,
( 5.15). 5 + 3ntz(x) , 3 + 3(32-ntz(x))
RISC-.
5.15.

int ntz(unsigned x)
{
int n;
X = -X & (X - 1) ;

n = 0;
while(x != 0) {
n = n + 1;
X = X 1;
}

// = 32;
// while (x != 0)
//
n n - 1;

return n,-

X = X + X;

//
//

//

return n;

}
, * 1.0. , ,,, pi , \
.
S

=I. 0+ I.l + i.2 + --3+-4 + .5 + ...SY-A-.


2
4
8
16
32
64
^2"+1

, :

94

5.

1/4 1/8 1/16 1/32


1/8 1/16 1/32
1/16 1/32
1/32

1/64
1/64
1/64
1/64
1/64

...
...
...
...
...

S. , S
. .
1 1 1 1
1
-+-+ + + ... = 4 8 16 32
2
1 1 1 1
1
- + + + + ... =
8 16 32 64
4
1
1
1
1
1
+ + + - + ... = 16 32 64 128
8

, , + + + . . . = 1 .
.
, ntz(or), , 0, (, ),
"" . , "
2 "

31 - nlz(* & -) .

[19] ,
. " " (ruler function), , , ,
..
ntz (R.W. Gosper), , .
XQ, X\, X2,...,
+1 =/(). / , . 0, \, 2, ..., ^\, ^, X^+i, ..., ^+
(Xf,=X^, ^+1=^+^.1 .., ).
.
, .
,
. -

5.4.

95

. , .
, (R.W. Floyd) [39, . 3.1,
6]. :
*-/(*)

y=f(f(y}}
( 0). - = =,. . , , , , , .. 2- = .
/ 0 ,
Xi Xn+i , ^, Xn+fl. ,

Xfi+i, XfHi...... , /
.
[25, item 132; 39, 3.1, 7]
, . , /,
. - ; Iog2 () +1,
. : , ,
< 232, 33 .
5.16 , .
/ 0. // . (
/
/// , , - //, +1 < max ( -1,1).) ( = 1, 2,...) ( [log2/ij + l)
.
X/, , i + l (.. /
, ), i +1 , i +1 ..
5.16.
void ld_Gosper(int ( * f ) ( i n t ) , int X O , int *mu_l,
int *mu u, int *lambda)
{

int Xn, k, m, kmax, n, Igl;


int [33] ;
T[0]

= XO;

Xn = XO;
for (n = 1; ; ++)
{
= f (Xn) ;

kmax = 31 - n l z ( n ) ;
for (k = 0,- k <= kmax,- k++)
{
if (Xn == T [ k ] ) goto L;

96

// Floor(Iog2 n)

5.

T[ntz(n+l)] = Xn,-

//

L:
// = raax{i | i < n ntz(i+l) = k}
m = ((((n k) - 1) | 1) k) - l;
*lambda = n - m,Igl = 31 - nlz(*lambda - 1);
// Ceil(log2 lambda)-!
*mu_u = m;
// mu
*mu 1 = m - max(l, 1 Igl) + 1 ; // mu
}

, :
X,:
Y Y

2'01

Y -Y
Yi
.
2*
3

Y4 ' 2F 1Y Y

A5:A4,A,,A3
Y 6 . Y
Y Y

4 , 3 , 3

1:6,},
Y Y

5Y Y
3

Y
7

Ag * 6' 5 ' 7
Y . Y

Y Y
-F10 .
8Y
,^ Y
9 ,/ 3 , 7

,,: , 9 , 3 , 7

Y, ,
Y ,
Y7
AY, , .Y1 0 ,

, : 1 2 ,,, , 1

14 ' 12'

Y15 ' 14'


Y

13'

Y
13'

Y
II'

Y
7

Y Y Y
^* Y|6 .
I4Y, ^v
t3 , , 7 , 15

1 7 : 1 6 , | 3 , ,,, 1 5
Y, s
Y
Y Y Y
A
. Y
16,,7,11,7,|5

, , ..
n < ft + 2. [39].
. 0 -1. k, k 1 2"-1,
ntz(Jt) .

(. 13.1). - k (
k 1 2"-1), ntz(;t).

5.4.

97

6.1.
- .
; . strlen.
, , .
strlen
. , ,
. 0 3, ; , 4. . , , ,
. zbytel(jt) zbyter(jt) ("00" , ""
, "" , ).
0, - = 00,
1,

zbytel (

0, ;c=xxxxxxOO,

= ,

1, * = xxxxOOnn,

2, * = nnnn 00 ,
3, Jt = nnnnnn00,
4, jt = nnnnnnnn.

zbyter (jc) = 2, ;c = xxOOnnnn,


3, jr = 00nnnnnn,
4, = .

6.1 zbytel(x),
. , ,
( ).
6.1.
int zbytel (unsigned x)
{
if
( ( x 2 4 ) == 0) return 0;
else if ((x & OxOOFFOOOO) == 0) return 1;
else if ( (x & OxOOOOFFOO) == 0) return 2;
else if ((x & OxOOOOOOFF) == 0) return 3;
else return 4;
}
zbytel (x) 2 11 RISC (11 , ,
strlen). zbyter(;c) zbytel ( x ) .

6.2 zbytel(x) .
, 0x80,
, . -, . [42].
6.2.
int zbytel (unsigned x)
{
unsigned ,int n;
// : 0080
= (X & OX7F7F7F7F) + OX7F7F7F7F; // 7F 7F Ixxxxxxx
= -( | X | OX7F7F7F7F);
// 80 00 00000000
n = nlz(y) 3;
// n = 0 ... 4; 4
return n;
//
}
, 8 ( ). nlz (. 5.4), , ,
= (32 - n l z ( - y & ( - 1 ) ) ) 3;

, - -, 12 .
, PowerPC
;
Iwbrx.
6.2 64- . ,
64- ( ) (
), 32-.
( 6.1) 23 .
, , (, , ).
nlz , . 6.3 nlz ( ).

6.3.
nlz
// : 00 80
= ( & OX7F7F7F7F) + Ox7F7F7F7F; //7F 7F Ixxxxxxx
= -( | X | OX7F7F7F7F);
//80 00 00000000
// :
if ( == 0} return 4;
//00000000 ==> 4,
else if ( > OxOOOOFFFF)
//SOxxxxxx ==> ,

100

6.

return ( 31) * 1;
else
return ( 15)

3;

//OOSOxxxx -> 1,
//000080XX > 2,
//00000080 ==> 3,

10 13 RISC- (10 ,
). , , , 6.1, . ,
64- .
nlz. ,
6.3, ,
0x00, 0x80. Ox7F
, ( )
. , 0 15
, :
remu (0x80808080, 127) = 15,
remu (0x80000000, 127) = 8,
remu (0x00008080, 127) = 3.
16 . , ,
if ( == ), ( ):

static char table[16] = {4, 3, 2, 2, 1, 1, 1, 1,


, , , , , , , 0 } ;
return table [y%127] ;
127 31, , , table.
, 127 31, , 20 . 6.3
, , if ( == ) ,
:
return table[hopu(y, 0x02040810) & 15];
return table [y*0x00204081 28];
hopu (a, b) 32 . , , , 32 .
, , , --, 0x00204081

4-
13 (7 , 4 --,
2 28 table); ,
.
64-
.
return table [%511] ,6.1.

101

table 256 8, , 1, 0, 2, , 1, 0, 3, , 1, 0, 2, , 1,
0, 4, ... (.. table [i] i).
:
return table [hopu (, 0x0204081020408100) & 255] ;
return table [y*0x000204081020408156] ;

table 256 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 3, ....


0x2040810204081 13 :

, .
32- -,
6.3
return table [i] = {, , , , , , , , 1,
1, 1, 1, 2, 2, 3, 4}. 64- .
6.2,
nlz. , , d , " ", " : " ..
zbytel(jr) =a +ab +abc +abcd .
, ,
6.4 ( ).
6.4.
= ( & OX7F7F7F7F) + OX7F7F7F7F;

=|;

tl = 31;
t2 = ( 23) & tl;
t3 = ( 15) & t2;

t4 = (y 7) & t3;
return tl + t2 + t3 + t4;

// 1
// tl =
// t2 = ab
// t3 = abc

// t4 = abed

15 RISC-.
, . ,
,
10 .
:
zbyter(oc) -abed +bcd +cd +d .
( , 6.4)


zbytel zbyter , . ,
, , 102

6.

. , ASCII (0x20) , 0x20202020 .


, .
6.2
, . , ( ) , 12 16. 6.2 Ox77FF7FFF [51] (
1, 0).


6.2 , 0 , 128.
, 0 9.
= (X & OX7F7F7F7F) + 0x76767676;
=
OX7F7F7F7F;
// > 9 = OxFF
=
= -;
// > 9 = 0x00
// <= 9 = 0x80
n = nlz(y) 3;
. ,
, ,
128. , ASCII
0x41 0x5 . ,
0x41414141 , . , 0 0x19 (
0x5 -41), , .
2.17 = 0x41414141,
d
d

=
=
=
=
=
=

( | 0x80808080) - 0x41414141;
~(( | OX7F7F7F7F) * d) ;
(d & OX7F7F7F7F) + 0x66666666;
I d;
| OX7F7F7F7F; // , 41-5, FF
-;
// , 41-5, 00
// ,
41-5, 80
n = nlz() 3;
. ,
, 0x30 0x39 ( ASCII),
0x30303030,
0 9 ( ,
8 - ).
. , , -

6.1.

103

0 137 (0x89), 0 9 - | & .


, - | d
0x41 0x5 - & d,
, 0x41 OxDA.

6.2.
, , , .
; , . ( ) , . , (
, , , , ).
(Albert Chang), , .
, ( ) nlz, .
:
. (
). , , .
,
. .
, 0 31,
. 32,
, .
int ffstrl(unsigned x, int n)
{
int k, p;
p = 0;
//
while ( != 0)
{
k = nlz(x); //
= k;
// ( )
= + k;
k = nlz(-x); //
if (k >= n)
// ,
return p; //
= k; // ,
p=p+k;
//
}
return 32;

104

6.

, ,
. , .
: , =055555555 2 2 178
RISC-.
, , . , ,
32- .
, .
*<-*&(*<1)

<-&(<2)

2. 4 ( 2,
2). 8. nlz , 8.
, 32.
, 1 32, . ,
.
. = 10.
&(*<*: 5)
*,&(*, 2)
4 <-: 3 &(: 3 <:1)
/2.
\.
[5/2 J = 2 ,
(3=5-2). 3 ^. -1.
6.5. 1 32 3 36 RISC-.
, ( 32-
) > 1. 20 (
).
. , 1, , ,
, .
> 5.

6.2.

105

6.5.

int ffstrl(unsigned x, int n)

int s;
while (n > 1) {

s = n 1;

X = X & (X S) ;
= - S;

return nlz(x);
, , (
, -). 6.5
, . ,

*& , , .. .
,
. , , , 0x80808080.
.
,
,
nlz. ( )
( 6.1).
+-\(<4)
<- ; | ( 2)
*-\(<1)
*<-Ox7F7F7F7F|*
12 RISC- (..
, 6.2,
).

106

6.

7

7.1.
, .. :
rev (0x01234567) = 62480
rev (00000001001000110100010101100111) = 11100110101000101100010010000000

. , (little-endian), ,
, DEC Intel, , (big-endian), .
[3]: , 2- .. .

=
=
=
=
=

(
(
(
(
(X

&
&
&
&
&

0x55555555)
0x33333333)
OxOFOFOFOF)
OxOOFFOOFF)
OxOOOOFFFF)

1
2
4
8
16

(X
(X
(X
(
(

&
&
&
&
&

)
)
OXFOFOFOFO)
OxFFOOFFOO)
OxFFFFOOOO)

1;
2;
4;
8;
16;

, 7.1, , . 30 RISC- .
7.1.
unsigned rev(unsigned x)
= ( & 0x55555555)
= ( & 0x33333333)
= ( & OxOFOFOFOF)

1
2
4

(
(
(

1) & 0x55555555;
2) & 0X33333333;
4) & OxOFOFOFOF;

= ( 24)
(( & OxFFOO) 8)
(( 8) & OxFFOO)
(X 24);
return ;

RISC. ,
:
((

& OxOOFFOOFF)8 1 1|1 1\ x 8 I & OxOOFFOOFF I .

Ha PowerPC [26]:
8 , rlwirai (rotate left
word immediate then mask insert
).


[19] :
if
if
if
if
if

(k
(k
(k
(k
(k

& 1)
& 2)
& 4)
& 8)
&16)

x
x
x

=
=
=
=
=

(
(X
(x
(X
(x

&
&
&
&
&

0x55555555)
0x33333333)
OxOFOFOFOF)
OxOOFFOOFF)
OxOOOOFFFF)

(x
(x
(x
(X
(X

1
2
4
8
16

&
&
&
&
&

OxAAAAAAAA)
OxCCCCCCCC)
OxFOFOFOFO)
OXFFOOFFOO)
OxFFFFOOOO)

1;
2;
4;
8;
16;

( .) k = 31,
. k = 24 . k = 7,
, .
k = 16 , ..
, , / .
( , ).


[25, item 167] 6-, 7- 8- . 36- , 6- 32- ,
7- 8- 64- . .
6 :

7 :
8 :

remu

(( * 0x00082082) & 0x01122408,255)

remu (( * 0x40100401) & 0x442211008,255)


remu((*

*0x202020202) &0x10884422010,1023)

"" , .. ,
, 0.
remu rem
mod, remu .

256 1024, 9
( 9, ). , . , 6- 32-
( 232):
t<-(x* 0x00082082) & 0x01122408
(/*001010101)24
,
( 20 ) /
108

7.

, . , , 10 RISC-,
, 20 RISC-.
, 7.1 6- 15 9 15 , . . 32- .
8- 9- 32-
[25].
8- :
s <- ( * 0x02020202) & 0x84422010
t <-(* 8) & 0x00000420
remu (s+f, 1023)
remu . (
, ,
, .)
8- , ,
:
s <- ( * 0x00020202) & 0x01044010
- ( * 0x00080808) & 0x02088020
remu (s+t, 4095)
,
, , . 14 RISC-, .
<-* 0x00020202
<- 0x01044010
s <u&m
(0x01001001 *(+()) 24
9- :
s <- ( * 0x01001001) & 0x84108010
t <- ( * 0x00040040) & 0x00841080
remu(s+M023)
, , 6 . , 8 .
12 RISC-,
. , .
, ,
. ( 7.1.

109

) . 8- ,
, , ..
:
0000 0000 0000 0000 0000 0000 abed efgh =>
0000 0000 0000 0000 0000 0000 0000 bdfh
:
t <- ( * 0x01010101)& 0x40100401
(*008040201)27
,
( 9-) .


() i
, 1, rev() [50].
, , i
rev(i). , i
rev(i) , i, , rev(i) 29 ?
, i ,
, .
: ,
? , 4- ( ):
,

8, 4, , 2, ,

6, ,

1,

9, 5, D, 3, , 7, F.

/,
, 32, . , i 32- .
1 32- ,
.

, . 1, , ( ), 0. :
unsigned , ,m = 0x80000000;

= * m;
if ((int)x >= 0)
do
{

m = m 1;
= m;
7.

} while (x < m) ;
RISC-, , . , , 1/2 , 10
( ) 1/4 ..,

...

2
4
8
16
= 4.1+8.1+12.1+16.^ + ...-!

+!+!++
4 8

16

( 1,
1/2 + 1/4+1/8 + .... ,
5.4.) 131.
,
, 1 :

s< nlz(-vr),
*<-jr(Ox80000000s J ,
<-( (x<*:s J + Ox80000000 jis .

RISC-, ,
OxFFFFFFFF 0 ,
64. (-
Intel x86, 32.)

7.2.
" " (perfect shuffle), . , "" "".
. 32 , , .
, 15
3 1 . 32- ( )
abed efgh ijkl mnop ABCD EFGH IJKL MNOP,

:
cCdD eEfF gGhH iljJ kKlL mMnN oOpP,

7.2.

111

:
AaBb CcDd EeFf GgHh l i J j KkLl MmNn OoPp.
W 2.
RISC- log,(W/2) ,
[19], .. 32- :
abed
abed
abed
abAB
aAbB

efgh
efgh
ABCD
cdCD
cCdD

ijkl
ABCD
efgh
efEF
eEfF

mnop
EFGH
EFGH
ghGH
gGhH

ABCD
ijkl
ijkl
ijIJ
iljJ

EFGH
mnop
IJKL
klKL
kKIL

IJKL
IJKL
mnop
mnMN
mMnN

MNOP
MNOP
MNOP
opOP
oOpP

,
42 RISC-.
X
X
X

=
=
=
=

(
(
(X
(

&
&
&
&

OxOOOOFFOO)
OxOOFOOOFO)
)
0x22222222)

8
4
2
1

(X
(X
(
(

8)
4)
2)
1)

&
&
&
&

OxOOOOFFOO
OxOOFOOOFO

0x22222222

&
&
&
&

OxFFOOOOFF;
OxFOOFFOOF;
;
0x99999999;

30 ( 17
21
),
( 2.19). .
t = ( (X 8)) & OxOOOOFFOO; = " t * (t ) ;
t = (X ( 4)) & OxOOFOOOFO; X = X t (t 4);
t = (X

(X 2)) & ; X = X

(t 2);

t = ( (X 1)) & 0x22222222; = X t " (t 1);


.
t
t
t
t

=
=
=
=

(
(
(X
(X

(
(
(X
(

1))
2))
4))
8))

&
&
&
&

0x22222222;
;
OxOOFOOOFO;
OXOOOOFFOO;

=
=
=
=

t
t
t
t

(t

1);

(t 2 ) ;
(t. 4 ) ;
(t 8) ;

; .. ; .. , ,
.
, , .
:
X = (X 16) | (X 16) ;

112

7.

( 16 ). , , , .
,

, , .
, , (
) ; ,
, , .. 32-
0000 0000 0000 0000 ABCD EFGH IJKL MNOP

OCOD OEOF OGOH OIOJ OKOL OMON .


,
22 RISC-. 19 RISC-

(12 ).
0.
X =
X =
X=
X =

((X & OxFFOO)


( (X 4)
X) &
( (X 2)
X) &
X 1) | X) &

8)
( & OxOOFF);
OxOFOFOFOF;
0x33333333;
0x55555555;

, ( ; . 7.4)
26 29 RISC-, ,
. , ,
18 21 RISC-,
, 12 15 .
= & 0x55555555 // ( )
= (( >
X) & 0x33333333 ;
((X > > 2)
) & OxOFOFOFOF }

) & OxOOFFOOFF i
X = ((X > > 4)
X = ((X > > 8)
X) & OxOOOOFFFF i

X
X

> 1)

7.3.
,
, .
, ,
8 , .

7.3.

113


.

, - , (
) . 8x8, 8x8 ,
. 8x8.
, :
. , 8x8 , (.. , , ,
). 8x8 , ..
, , (
, , , ). ,
, al, ..., 7 8- , . , , ..., 7 8- , ,
. , . . ,
7 0 () 0 7 (7). , , , , 0 0 7 7.

al
2

4
5

= 0123
= 89
ghij
= opqr
= wxyz
= EFGH
= MNOP
= UVWX

bO = 08 wEMU
= 19hp xFNV
2 = 2aiq yGOW
= 3bjr ZHPX
4 = 4cks AIQY
5 = 5dlt BJRZ
6 = 6 emu CKS$
7 = 7fnv DLT.

4567

cdef
klmn
stuv ==>
ABCD
IJKL
QRST
YZ$.


, . .
=( &128)
(4 &128}/16
= ( & 64) *2
(4 & 64) /8
2=( & 32) *4
(4 & 32) /4
=( & 16) *8

(4 & 16) /2

4=(
(4
5= (
(4
114

& 8)*16
& 8)
& 4)*32
& 4)*2

(al
(aS
(al
(5
(al
(5

&128)/2
&128)/32
& 64)
& 64) /16
& 32) *2
& 32)/8

(al & 16) *4


(5 & 16) /4
(al & 8)*8

(5 & 8) /2
(al & 4)*16
(5 & 4)

(2 &128J/4
( &128)/64
(2 & 64) /2
( & 64) /32

(2 & 32)

( & 32) /16

(2 & 16) *2
( & 16) /8
(2
(
(2
(

&
&
&
&

8) *4
8) /4
4)*8
4) /2

( &128J/8 |
(7
)/128
( & 64) /4 |
(7 & 64)/64;

( & 32) /2 |
(7 & 32) /32;

( & 16) |

(7 & 16) /16;


( & 8)*2 |
<7 & 8)/8;
( & 4)*4 |
<7 & 4) /4;

7.

6=( & 2)*64


(a4 & 2 ) * 4

7=(

)*128

(a4 & 1)*8

(al
(a5
(al
(a5

&
&
&
&

2)*32
2)*2
1)*64
1)*4

(a2
(
(a2
(

&
&
&
&

2)*16

(a3 &

2)

(a? &
(a3 &
(a7 &

1)*32
1)*2

2)*8
2) /2;
1)*16
1);

174 (62
, 56 56 ). .
PowerPC 63 (
56 ). , .

, ,
RISC- .
8x8 16 2x2 . 2x2 ( 2x2)
2x2. 2x2, 4x4,
2x2. .
0123
89ab
ghij
opqr
wxyz
EFGH
MNOP
UVWX

4567
cdef
klmn
stuv
ABCD
IJKL
QRST
YZ$.

082a
193b
goiq
=> hpjr
wEyG
xFzH
MUOW
NVPX

4c6e
5d7f
ksmu
Itnv
AICK
BJDL
QYS$
RZT.

08go
19hp
2aiq
=> 3bjr
wEMU
xFNV
yGOW
zHPX

4cks
5dlt
6emu
7fnv =>
AIQY
BJRZ
CKS$
DLT.

08 go wEMU
19hp xFNV
2aiq yGOW
3bjr zHPX
4cks AIQY
5dlt BJRZ
6 emu CKS$
7fnv DLT.

, , . , , . 7.2.
8x8 ( 88 ). , 88- ( 88 ).
( , 8/, 8/).
7.2. 8x8
void transposes(unsigned char A[8], int m, int n,
unsigned char [8])
unsigned , , t;
//
([2*]8)
= ([0] 24)
([5*]1)
([*]8)
= ([4*]24)
t = (X * (X 7 ) ) & ; X = X
t = ( * ( 7)) & ; =

7.3.

t
t

[3*]
[7*]
(t 7) ;
(t 7) ;

115

t = ( (X 14)) & ; X = X * t '' (t 14);

A
t = ( ( 14)) & ; = * t (t 14);
!

t = ( & OxFOFOFOFO) | (( 4) & OxOFOFOFOF);


= ((X 4) & OXFOFOFOFO) | ( & OxOFOFOFOF);

X = t;
[0]=24;
[]=16;
[2*]=8; [3*]=;
[4*]=24; [5*]=16; [6*]=8; [7*]=;

t =

(X 7 ) )

& ; X = X

(t 7 ) ;

. 1 8 ( ), 3 10,5
12 .. ; 0, 2, 4 .. .
(. 2.19), .
0123 4567 89ab cdef ghij klmn opqr stuv
082a 4c6e 193b 5d7f goiq ksmu hpjr Itnv
, , ,
, 7.2. GNU , RISC .
, , , 219 101 7.2. (
, .) 7.2 64- RISC-
( ) 85 .
7.2 .
. 8x8
2x2,
4x4, 2x2.
4x4 2x2, 2x2 , 2x2 ..
, 7.2, , , .

32x32
,
, 8x8. , 32x32 .
32x32
, 7.2, , 32x32
, -

116

7.

.
.

16x16 :

] cl
[ [ D\
16 , 16 .. , :
0 16,
1 17,
15 31.
k,
0 15. k k
k+16.
16 8x8 :
"

F
J
N

D'

'

i
j

'

:
OxOOFFOOFF 0 OxFFOOFFOO 8,
OxOOFFOOFF 1 OxFFOOFFOO 9 ..
, 0-7 ( ) 0
8-15 8 ..
k = , 1, 2, 3, 4, 5, 6,7, 16, 17, 18, 19, 20, 21, 22, 23. k
k' = (k + 9) & -8 . k k k+S.
,
OxOFOFOFOF 0 OxFOFOFOFO 4,
OxOFOFOFOF 1 OxFOFOFOFO 5 ..
k = , 1, 2, 3, 8, 9, 10, 11,
16, 17, 18, 19, 24, 25, 26, 27. k k' - (k + 5) & 4. k k k + 4.

7.3 [19]. ; j 16, 8, 4, 2 1. ,
OxOOOOFFFF, OxOOFFOOFF, OxOFOFOFOF, 0x33333333 0x55555555.
( : m = m* (m j ) . , , .) k
7.3.

117

. [k], ,
a [k+j], j ,
a [k+j ] . , ,
, 2.19.

7.3. 32x32
void transpose32(unsigned A [ 3 2 ] )
{

int j, k;

unsigned m, t;
m = OxOOOOFFFF;
for (j = 16; j != 0; j = j 1, m = m * (m j ) )
{
for (k = 0; k < 32; k = (k + j + 1) & - j )
t = (A[k]

(A[k+j] j ) ) & m,A

A [ k ] = A[k]
t;
A [ k + j ] = A [ k + j ] " (t j ) ;

GNU , RISC , 31 20 7
. , 4 + 5(7 + 16-20) = 1639
. 16
8x8 7.2, 16(101+ 5) = 1696 , 16
. ,
, . , .
, 7.3 64-
64x64.
4+6(7 + 32 -20) = 3886 .
8x8 64(85+ 5) = 5760 .

, ,
, 32x32.
, .
, 7.3,
, .
, ,
? , , , ,
, . 80 (5 16)

118

7.

. , 32
32 ; 544 .
GNU
(16 ). 7.4 , . ,
, ; ,
, . swap 80.
GNU RISC- , , 576 , (
). ,
.

7.4. 32x32
define swap(aO, al, j , m) t = (aO A (al j)) & m; \
aO = aO
t; \
A
al = al
(t j) ;

void transpose32(unsigned A [ 3 2 ] , unsigned [32])


unsigned m, t;
unsigned aO, al, a2, , 4, 5, , 7,
a8, a9, alO, all, a!2, a!3, a!4, a!5,
a!6, a!7, a!8, a!9, a20, a21, a22, a23,
a24, a25, a26, a27, a28, a29, , 31;
aO = A[ 0]; al
a4 = A[ 4]; aS

= A[ 1]; a2
= A[ 5];

= A[ 2]; a3
= A[ 6]; a7

= A t 3] ;
= A[ 7] ;

a28 = A [ 2 8 ] ; a29 = A [ 2 9 ] ; = A [ 3 0 ] ; a31 .


m = OxOOOOFFFF;
swap(aO, al6, 16, m)
swap(al, a!7, 16, m)
swap(a!5, a31, 16, m)
m = OxOOFFOOFF;
swaptaO, a8, 8, m)
swap(al, a9, 8, m)
swap(a28, a29, 1, m)
swap(a30, a31, 1, m)
B[ 0] =
B[ 4] =

aO; B[ 1] =
a4; B[ 5] =

al; B[ 2]
a5; B[ 6]

a2; B[ 3] =
; [ 7] =

a3;
a7;

[28] = a28; [29] = a29; [30] = ; [31] = 31;

( ).
7.4, 7.3.

119

, , ( , ).
16 A[l6..3l] (..
A[k] 165*531). : [0]
[16], [1] [] .., 7.4.
[0..8] A[24..3l] ,
OxOOFFOOFF, [0] [8], [\] [9] ..
.
[1] , [2]
.. ( 31 ). , 4x4.
abed
efgh

ijkl

abed
=> efgh

abij
=> efmn

abij
=> nefm

aeim
=> nbfj

aeim
=> bfjn

opmn

opgh

hopg

hlpd

dhlp

klij

ranop

klcd

klcd

kocg

cgko

7.4, , 480 (80 ). 80 , 80 (16-5)


31 431 . , 49 .
,
[19]. ,
: 1) < / ; 2)
./ (y' + l)modn ; 3) t
(j + l)modn ; 4) , . 4x4.
abed
efgh
ijkl
mnop

abed
=> hefg
klij
nopm

hlpd
=> kocg
nbfj
aeim

dhlp
=> cgko
bfjn
aeim

aeim
=> bfjn
cgko
dhlp

- 2.
( , /2 ,
/2 ( /2-1 - 2 ),
/4 ..) 1 3 -1
, 4 , .
8x8 64- (
..),

120

7.

[19]. , ; RISC-
.

7.4. ,
APL ,
(compress) B/V, , V , . ,
V, 1. .
. ; , , (""). ,
abed efgh ijkl mnop qrst uvwx yzAB CDEF,
,
0000 1111 1010 1010 0101 0101,

0000 0000 0000 0000 efgh klop qsuw zBDF.
(generalized extract) no
, .
,
, , , 7.5.
260 ,
.
7.5.
unsigned compress(unsigned , unsigned m)
{
unsigned r, s, b; // , ,
= 0;
3 = 0;

do

b = m & 1;

r = r I ( ( & b) s) ;
s = s + b;
x = x 1,rn = m 1;

} while (m != 0 ) ;
return r;
}

"
" (. 5.2) [19] (
PP-XOR). , , ,
7.4. ,

121

( ,
, , ).
. , , 2 (2, 6, 10 ..), , . , ,
4, 8 16.
, . , :
= abed efgh ijkl mnop qrst uvwx yzAB CDEF,
m = 1000 1000 1110 0000 0000 1111 0101 0101,
1
1
111

333

4444

32

10

( 0 1).
, (
m ). ,
,
X = ijkO 0000 0000 uvwx OzOB ODOF.
, , . ,
PP-XOR ,
. , , .
mk = -mi PP-XOR . :

mk = 1110 1110 1111 1110 0001 0101 0100,


= 1010 0101 1110 1010 1010 0000 1100 1100.
, mk , ,
2. , , .
, , ,
( ),
m , ..
mv = mp&tn.
mv = 1000 0000 1110 0000 0000 0000 0100 0100
m
m = (m * mv) | (mv 1);

:
t = X St mv;
X = (X * t) | (t 1) ;

( ,
.) , 1
m , , m 0.
122

7.

. , mv,
:
m = 0100 1000 0111 0000 0000 1111 ,
= O i j k 0000 0000 uvwx OOzB OODF.
, ,
, , 2. , mk&~mp ,
m , ,
. ,
, m (.. mk , , ).
PP-XOR, ,
, (2, 6, 10 ..). ,
mk
. mk
mk = 0100 1010 0001 0101 0100 0001 0001 0000.
, , 7.6. 127 RISC-, .
7.7 , ( ,
). , , .
7.6.
unsigned compress (unsigned x, unsigned m)

unsigned mk, mp, mv, t;

int i;

x = x & m;
//
mk = ~m 1;
//
for (i = 0; i < 5; i++)

(mk 1); //
mp = mk
mp = mp
(mp 2);
mp = mp
(mp 4);
mp = mp
(mp 8);
mp = mp
(mp 16) 1
mv = mp & m;
II
m = m " mv 1 (mv (1 i)); // m
t = x & mv /
x = x t | (t (1 i));
// x

mk = mk & -mp;
}
return x;

7.4. ,

123

7.7.

= abed efgh ijkl mnop qrst uvwx yzAB CDEF
i = 0,
,

i = 1,
,

i = 2,
,

i = 3,
,

i = 4,
,

m =
x =
mk =
mp =
mv
m =
x =
mk =
mp =
mv =
m =
x =
mk =
mp =
mv =
m =
x =
mk =
mp =
mv =
m =
x =
mk =
mp =
mv =
m =
x =

1000
aOOO
1110
1010
1000
0100
OaOO
0100
1100
0100
0001
OOOa
0000
0000
0000
0001
OOOa
0000
0000
0000
0001
OOOa
0000
1111
0001
0000
0000

1000 1110 0000


eOOO ijkO 0000
1110 0011 1111
0101 1110 1010
0000 1110 0000
1000 0111 0000
eOOO Oijk 0000
1010 0001 0101
0110 0000 1100
0000 0000 0000
1000 0111 0000
eOOO Oijk 0000
1000 0001 0001
0111 1111 0000
0000 0111 0000
1000 0000 0111
eOOO 0000 Oijk
1000 0000 0001
0111 1111 1111
0000 0000 0111
1000 0000 0000
eOOO 0000 0000
1000 0000 0000
1000 0000 0000
1000 0000 0000
0000 0000 0000
0000 0000 0000

0000
0000
1110
1010
0000
0000
0000
0100
1100
0000
0000
0000
0000

1111

1111

0101
OzOB
0101
1100
0100
1111 0011
uvwx OOzB
0001 0001
0000 1111
0000 0011
1111 0000
uvwx 0000
0001 0000
1111 0000
1111 0000
0000 1111
0000 uvwx
0000 0000
0000 0000
0000 0000
0111 1111
Oijk uvwx
0000 0000
0000 0000
0000 0000

uvwx
0001
0000
0000

0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0001 1111 1111
OOOa eijk uvwx

0101
ODOF
0100
1100
0100
0011
OODF
0000
0000
0000

1111

zBDF
0000
0000
0000

1111

zBDF
0000
0000
0000

1111
ZBDF
0000
0000
0000

1111
zBDF

7.6 64- RISC- 169 , 7.5 516 .


, 7.6,
, m .
: 1) compress (x,m) ,
m , , 2) m
compress , , .
, , 7.6, , .
, mv. , , mv
tnvO, mvl, ..., mv4. 1 , compress (x, m ) , , ,
mvi, :
X = X
= X
= X
t = X
= X
t = X

t
t
t

124

&
&
&
&
&
&

m,mvO;
mvl;

mv2,mv3,mv4,-

X =

x
x
x
x

X =

X *

X =
X =
X =

t
t
t

(t > >
(t > >

(t >

1);

2);
4);
8);
> 16) /

(t > >
(t > >

7.

, 21 ( ), 127
, 7.6.
2, , , . , ,
0, . , , ,
ml 0; , 15 , 0 4.
,

m = 0101 0101 0101 0101 0101 0101 0101 0101


mvO
mvl
mv2
mv3
mv4

=
=
=
=
=

0100

0000
0000
0000

0100
0000
1111
0000
0000

0100

0000
1111
0000

0100
0000
0000
1111
0000

0100

0000
0000
0000

0100
0000
1111
0000
0000

0100

0000
0000
0000

0100
0000
0000
0000
0000

0, 17 ( ). ,
, 7.2, , 13 .


(insert), , ,
, . ,
, .
, m ,
. 2 + 1 , ( )
. 33 ,
16 ( ).
, , =0010084.
1, 2, 4, 8 16 . , 21 ,
11 ( ). =080000000.
31 , 21 ,
(
31 ), .
(extract) - 2 , .
7.4. ,

125

, , , .
!


, . pop (m) .
, . ,
7.6, ( ii, ).

7.5.
( - )
. . 32- 32! , , , [log2 (32!)~| = 118 , 3 22 .
, 7.4 [19]. ,
. , , , 0 ( ) 4, 1 5, ..., 31 3. 32 :
00100
00101

11111
00000
00001
00010
00011

, ,
, ,
.
32- .
[0]
[1]
[2]
[3]
[4]

=
=
=
=
=

1010
1100
0000
0000
0000

1010
1100
1111
1111
1111

1010
1100
0000
1111
1111

1010
1100
1111
0000
1111

1010
1100
0000
0000
1111

1010
1100
1111
1111
0000

1010
1100
0000
1111
0000

1010
1100
1111
0000
0000

[] , , [1] .. , m 126

7.

mv, , .
, , ,
, , 1.
" " (sheep and goats SAG) " " (general unshuffle).
SAG(x, m) = compress_left(x, m) | compress(x, -m).
SAG , ,
, 15 .
X

[1]
[2]
[3]
[4]

[2]
[3]
[4]

[3]
[4]

[4]
X

=
=
=
=
=
=
=
=
=
=
=
=
=
=
=

SAG(x,
SAG(p[l],
SAG(p[2],
SAG(p[3] ,
SAG(p[4] ,
SAG(x,
SAG(p[2] ,
SAG(p[3] ,
SAG(p[4] ,
SAG(x,
SAG(p[3] ,
SAG(p[4] ,
SAG(X,
SAG(p[4] ,
SAG(X,

p[0])
p[0])
p[0])
p[0])
p[0])
P L1J )
P L11 )
P L1J )
P L1J )
p[2])
p[2])
p[2])
p[3])
p[3])
p[4])

SAG ( ). 32
. , [0] =1, , , [0] = 0 , .
(.. ).
, . , , ..
, ,

.
:
= SAG(p[l] , [ 0 ] ) ;
[2] = SAG (SAG ( [2], [ 0 ] ) , [ 1 ] ) ;
[3] = SAG (SAG (SAG ( [3] , [ 0 ] ) , [ , [ 2 ] ) ;
[4] = SAG (SAG (SAG (SAG ( [4] , p [ 0 ] ) , p [ l ] ) , p [ 2 ] ) , p [ 3 ] ) ;
:
X
X
X
X
X

=
=
=
=
=

SAG(X,
SAG(X,
SAG(X,
SAG(x,
SAG(X,

p[0]);
p[l] ) ;
p[2] ) ;
p[3]) ;
p[4] ) ;

, , , , .
7.5.

127

(, , )
32 , k-
, k- . ( "", "", GAS.) 6 32- ; , 32
.
bitgather Rt, Rx, Ri,

Rt ( ), RX , Ri ( ). , .
,

t. i,
.
, .
,
Rt Rx, . /5. , , Rt.
, , , .
. SAG .
(..
). 32:1 .
2:1, ,
32- [45].
, (, ).
64- , 128-, 32-. SAG
bitgather .

(DES) ,
. , 17 . , 64 , 34 . 64 , . , 16
32- 48- 16
32- [11].
, DES , 1998
Electronic Frontier Foundation
. (National Institute of Standards and Technology NIST)
Triple DES ,
128

7.

64- DES , ( , 192 , 24 ).


DES.
"" DES Triple DES, Advanced Encryption Standard [1], .
, ,
, 8. DES.
. bitgather :
, , . SAG 1) , 2) ( RISC-), ) , 4) .
3 [43]. SAG SAG,
RISC- . bitgather
RISC-. , , ; .
4, , , bitgather , . SAG ,
,
. ,
2;
("log,n\ ( ,
).
SAG bitgather ( GRP PPERM ), [43] , .

7.6.

, [19].
, 2.
,
( ).
8, :

7.6.

129

4 -,;

-> 2 ;
5- 3 ;

,-6*;
Aj-B 5 ;

'
,- ,.

- -,
( ).
. . 7.1. , log, .

7.1.

(. 7.1)

k (mod n)

k (mod n)

k (mod )

k (mod )


8x8,
64-

(
)

(
)

130

7.

8.1.
" ". .
, ,
+ , , .
" " 32-
. , ,
64- 32- . , .
RISC-
, ,
. ( , , .)
8.1. ""
. w , v . ,
(w[0],u[o],v[o]) ( ).
v .
8.1.
void mulmns(unsigned short w[], unsigned short u[],
unsigned short v[], int m, int n) {
unsigned int k, t, b;
int i, j;
for (i = 0; i < m; i++)
W[i] = 0;

for (j = 0; j < n; j++) {


k = 0;
for (i = 0; i < m; i++) {

t = u [ i ] * v [ j ] + w [ i + j] + k;

w[i + j] = t; // ( . . , t & OxFFFP).


k = t 16;
}

w[j + m] = k,}
// w[] .
// v*2**16m < 0
// u*2**16n v <
if ((short)u[m - 1] < 0) {
b = 0; //
for (j = 0; j < n; j++) {
t = w [ j + m] - v[j] - b;
w [ j + m] = t,b = t 31;

if ((short)v[n - 1] < 0) {
b = 0;

for (i = 0; i < m; i++) {


t = w [ i + n] - u[i] - b;

}
}

W [ i + n] = t ;
b = t 31;

return;

8.1 ( n
m , ):

[39, 4.3.1], .
, t 8.1 , , t, (2 16 -l) 2 + 2(2 16 -l) = 2 3 2 -l.
. ,
8.1 , ,
,
.
.
1. , , , .
2. ,
; .
3. , .
+
. , ,

\(,) + + . ( ), . ,
. ,
.
(
, ), , .
132

8.

, . ,
, v
N . 8.1
, u + 2"uu_t , _, (..
_, = 1 , , _, = 0 ). , v v + 2"vA,_, .
, :

( uv ), 2"uu_lv + 2Nvf/,lu . 2Mtl"uu_lvN,l , , + N ,


, + N-1. 8.1
+ .
8.1 , .
, .
, [0] ( ), ( ) .

8.2. 64-
32 32 . RISC-
(multiply high signed, mulhs)
(multiply high unsigned, mulhu).
8.1. = = 2 ( 32- ).

8.1. , , . 8.2. int unsigned.
8.2.
int mulhs (int u, int v)
{
unsigned uO, vO, wO;
int
ul, vl, wl, w2, t;

uO = U & OxFFFF;
vO = v & OxFFFF;

Ul = u 16;
vl = V 16;

WO = U0*v0;
t = Ul*vO + (wO 16) ;
Wl = t & OxFFFF;
8.2. 64-

1 33

W2 = t 16;
wl = U0*vl + Wl;

return ul*vl + w2 + (wl 16) ;

16 RISC-
( ), .

8.3.
, 64 32- , .
8.2, . [6].
(. 8.1).
32- , . 3
, + 2 *}1 ,
, , 1, , 0 . 32

, , + 2 31 .
32 ,

, 232 (}1 + 31) + 23131 . ,


64 , 264. , , ( RISC-).
4- mulhu(*, )

64-

^ =

J&*

//'a = >V

- - <, - *2

(1)

//



. (1) , 64- , f- + tt +1,.

134

8.

8.4.
, . , 13 (1101
) ( ).

(, <- < 2

,
<-8 + 4 + ;
,
RISC-.
, ,
. , ,
. 45 (101 101 ).
t - 4*
f , <- 4*

t,<-32x
rt-r+t,
, , t , , . .
:

, , ;

.
. . , .
, (
) .
, , , (). , ,
. , (
, ..),
, , 8.4.

135

, HP PA-RISC ( , , ; 3,
5 9 , -, ). , 32 .
, . , 28 (11100 ) 32 - 4 ( ) , 16 + 8+4
. , 32 .

2(/)-1-(5 , <5 = 1,
(), 0 . ,
4g(m) + 2s (m) - 1 - 3 , g(w)
, a s(m) "" . S , .
.
, .
, , =55 (110111 ).
: (64-\6) + (8-) .
(-- , . , 1 101 1 101 1 , 512* - 64 -4- .
, . : = |_log2 mj + 1 .
. - , 1,
,
.
( ). 1 0 ,
= 1 . > 1 ( ) , - 1 m (.. /2 ) - 1 , ,
.. .
01,
, - 2 (
- 2 ), .
.
, ,
, 0111, 1011 1111.
136

8.

t : -4 . ,
= I6t + 2 + , ( -4) + 4 = . 0111, = l6t + 8x-x , 1111 = + 16- ; .
1011.
, , m 00101 1,01 1011 111011. 101011.
" " 101010. . . 1010101 1.
- 101010... 10101011, /2 + 1 . , 2(/2 + 1)-2 = .
, 32- , , 32
.
, , , - 101010... 101011
, 1010101... 0101 10 , .
,
, , . , . , , .
=45. , 45
5 9 .

t < 4 +
. , 106 , 106 7-15 + 1 , .
,
- , .
, . ,
= 32 ,
2- 5 -17 -257 -65537 + 1 , , 10 (, , ;
).
[39, 4.6.3] . . , , [5].

8.4.

137

9.1.
"
" . / , +
( ), + . / ,
, .
,
. , ,
.
. ( ).

7+3
(-7)1-3
7--)
(-7-3) =

2
-2
-2
2

1
-1
1
-1

2
-3
-2
3

1
2
1
2

2 1
-3 2
-3 -2
2 -1

= * + . "" , 1, , , .
. ,
, .
, . , 2" ,
2" 2" -1. ,

, . , ,
1

, , "" "". mod [38] (floor), (


0), . mod
. , , , .

,
imin imax, ,
10 , imin imax. (j/mn-t-10)*10
((/+9)-*-10)*10, "" . , :
if (imin >= 0) gmin
else
gmin
if (imax >= 0) gmax
else
gmax

=
=
=
=

(imin/10) *10,((imin - 9)/10)*10;


((imax + 9)/10)*10;
(imax/10) *10;

"" , ,
, , , , .
""
, , . , / . , ..
. . Fortran 90
MOD , MODULO ( ).
Common Lisp ADA REM , a MOD . PL/I MOD ( ). Pascal A mod
> , (..
).
, , 2,
, , () + .
, d * 0 :

(-) + d = n + (-d) = -(n + d ) .


,
d ( ) ,
- -d 32 . (-232)*(-1) ( ),
.

2
, . IBM PL.8 , MMIX [48].

140

9.

()
:

, d * 0, nd < 0.

(1)

, .. , d , (1).

, . , , (floor ""), ,
(ceil ""), [18, 38].
D1. k :

W=-M

M-L-'J

x>k=> |_jcj > k

x<k=$ \x~\ < k

x < k => [_;cj < k

x > k <^> \x~\ > k

D2. nud(d>0) :

n + d-l
d<0
n

n-d-\~

D3. d^O

"

* " - .

. b (b^O) d&O

D4. nud (d?tO)

9. 1.

141

iem(n,d) d. d : Km(n,-d) = rem(n,d),


. <0 rem(n,d) ( , ).
D5. >0 d*0 :
. [2(,4\
) = {
, .( \.\
[2rem(/i,d)-|rf|

,
rem(2n

( 0 \d\ ).
D6. >0 d*0 :
(2,2] = 2rem(n,d) .
D5 D6 , ..
q
= qd + rem(n,d) ,
>0 d*0 ( d ,
).

9.2.
, " ". . 9.1 [39, 4.3.1],

. 32+16 => 32 (
17 ).
9.1.
int divmnu (unsigned short q[], unsigned short r[],
const unsigned short u[],
const unsigned short v[],

int m, int n)

const unsigned b = 65536; // (16 )


unsigned short *un, *vn,- // v
unsigned qhat;
//
unsigned rhat;
//
unsigned p;
//

int s, i, j, t, k;

if (m < n | | n <= 0 | | v[n-l] == 0)


return 1; //
if (n == 1)
//

k = 0;
for (j = m - 1; j >= 0; j--)

142

9.

= (k*b + U[j])/v[0];
k = (k*b + u[j]) - q[j]*v[0] ;

}
if (r != NULL) r[0] = k;
return 0;
}
// v , ,
// ,
// .
// ;
s = nlz(v[n-l]) - 16; // 0 <= s <= 16.
vn = (unsigned short *) alloca (2*n) ;
for (i = n - 1,- i > 0; i--)
vn[i] = (v[i] s) | (v[i-l] 16-s);
vn[0] = v[0] s;
un = (unsigned short *) alloca (2* (m + 1) ) ;
un [m] = u[m-l] 16-s,for (i = m - 1; i > 0; i--)
un[i] = (u[i] s) | (u[i-l] 16-s);
un[0] = u [ 0 ] S;

for (j = m - n,- j >= 0; j - - )


//
{
// q [ j ]
qhat = ( u n [ j + n ] * b + un [j+n-1] ) /vn [n-1] ;
rhat = ( u n [ j + n ] * b + u n [ j + n - l ] ) - qhat*vn [n-1] ;
again:
if (qhat >= b | | qhat*vn[n-2] > b*rhat + u n t j + n - 2 ] )
qhat = qhat - 1;
rhat = rhat + vn[n-l],if (rhat < b) goto again;

}
//
k = ,for (i = 0; i < n; i
p = qhat*vn[i] ;
t = u n [ i + j ] - k - (p & OxFFFF) ;
u n [ i + j ] = t;
k = (p 16) - (t 16) ;
}

t = un[j+n] - k;

un[j+n] = t;
q [ j ] = qhat; //
if (t < 0)
// ,
{
//
q t j ] = q [ j ] - 1;
k = 0;
for (i = 0; i < n; i++)

t = un[i+j] + vn[i] + k;
u n [ i + j ] = t;
k = t 16;

un[j+n] = untj+n] + k;
} // for j
// ,
//
if (r != NULL)
9.2.

143

for (i = 0; i < n; i++)

r[i] = (un[i] s) | (un[i+l] 16-s);

return 0;
. ,
, 64-S-32 => 32. , (
).

32-5-32 => 32 , 32-6 => 16.


, b
65536. [39] .
v , .. [0] v [0]
(, , ). v ( m ). q ()
. , , m-n+l
n . , NULL.
, v[n-l] . , . , v[n-l]
, n > 1 m > . , ( 1).
,
1. ; , 2.
2 , ,
, , 1.
, . , , . nlz(x).
.
, , ,
, . , - . alloca (),
- . , .
,
; .
qhat ,
again, , 1.

144

9.

qhat
, . , 1
, , , . , , 1.
,
NULL.
, .
, , . , , ghat . again
( ; ,
b = ). , -
, /2, ( ).

? ,
. - ( ), u/v 10. ,
( ). , , , ,
. 49900.. .0/5099.. .9, 499/50=9.98.
499/51=9.7843. 0.1957 ,
1, 20% ( , ). , , , ,
, 20% .
() b ,
2/. =65536 ,
1 2/65536=0.00003. ,
0.003% .
, ,
4500/501. 65536
Ox7FFF 8000 0000 0000/0x8000 0000 0001.
, , man /. 16
RISC-, . for j ,
/ - + 1 , , (15 + 1)(- + \) , mul 169.2.

145

. , m-n + 1
.



, , .
1. , . .
2. .
3. .
4. .
5. , .
6. , .
. , ,
256 ( ) . . 255,
OxOOFF, 2 OxFF.
, 3 , .

9.3.
" " (.. 32-!-32=>32). , "/" . ,
. ? ,
, .


(64 + 32^32), ,
. XLC IBM RS/6000 q*-\n+d I ( ).

1. if n<d then q<-Q


2. else if d=l then q<-n
3. else if rf<l then q<-l
4. else q<r-(Q\\n)+d
146

9.

</>2. d
1, , 1 ( ), 0.
d=Q, , ,
, </, .. </2 31 . , ></, 2" -1, -/ = 1.
, 32 , 32- ,
d. d=l , ,
( n>2 JI , ).
3, 11 , . d=0 ( ), "else if </<0 then q*-l",
12- RS/6000.
,
(2<rf<231) (
if d < 1), .


, ,
n-t-d n,d<2}' . </>231, n+d 0 1,
. , (( - \ \
/-2 -/ 1x2 n+d , 0 1.
( ).
1. if d<0 then if n<d then
2.

else q*-l

3. else do
4.

f <-ffn+2l+rf)x2

5.

r<r-n-qd

6.

if r>d then q<-q + l

7. end
3

RS/6000 ,
, .
9.3.

147

d<Q , d >231 .
, (231 - 1) + 231 = 1 ,
U

d > 1 .
, 1 , , 1 . , *2<231 , , </<231 , ( d=Q, ). (
D3):

5 :
d

, 0<r<2d . r<d, q .
r>d, 1
4 ( , , > 2" ).
0 q n<d q -1
2 q < q + 1 6, 14 , . ,
. 1 <- , 2
< -d , "then" 6 <- -d (, , r<~n-qd
).
1 2
if n<d then q*-0
else if rf<0 then g<-l,
, 13 , .
( , n>d) .
, .
1. if rf<0 then q<-\n>d
2. else do
3.
4.

148

< n qd

9.

5.

q<-q + \r>d

6. end

( ). Compaq Alpha
(CMPULE), MIPS (SLTU, XORI).
(
) <, 2.11 , , 1 , rf31 = 1 , 5
d,t = 0 . .
!:

n2d=(n&-,(/i-rf))31

5:

r2rf = (r|-,(r-d))31

, , rfS231
0. ,
0 ( 1).
, , . ( 10 RISC-).

2.

n'<r-n&-t


"N
'+2 Urf x2

4.

r*-n-qd

5.

q<-q+

9.4.
" " .
32- 64*32 => 32
( , 0).
32- , ,

32-*-32=>32.
, 32-5-32. , .
9.4.

149


, . , (restoring) (nonrestoring)
[31, sec. A-2; 14]. " ".
, , , . , z
32- . \\ , z. . .
do i4-l to 32
|| || <- 2( || )

|| <- ( || ) - (<0 1| z)
<


if then ||*-(||;) + (0/0||)
end

// (33 )
II
//

, .
. || 0
, . , 0+0 => 232 - 1 rem 0 . .
, 232 . , , 97
|| || 64 ,
62+32 => 64 . 33- .
,
33- , || . 9.2
,
.
9.2. ,
unsigned divlu (unsigned x, unsigned , unsigned z)
{
// ( | | ) z
int i;
unsigned t;
for (i = 1; i <= 32;

t = ( i n t ) x 31;

// =

1,

// (31) = 1

x = (x 1) | (y 31); // | |
= 1;
/ /
if ( | t) >= z)

X = X - Z;
1 50

9.

= + 1;
5

return ;

//

t , .
| | 33- . 1
( ), , 33- , 32- . 11 ,
z (true). ,
32- .
9.2 321 358 RISC-, , . , . 225-289 ( ,
).
=0, 9.2

32+32 => 32. , t , 0.


.
, z 33-
|| z, .
.
, z ( , z ), z
( 2( + ) - z = 2 + ).
,
4. , ( ).
|| , - , .

=0
do i<-l to 32
if c = 0 then do
|| || <- 2(* || )
|| <- ( || )-((01|z)
end
else do

II
//

4
, ,
33- .
, , .

9.4.

151

|| || <_ 2( || )

|| *- ( || ) + (0*01| )
end
<
end
if = 1 then <- + z

II
//
II
II

801 ( RISC- ) , , , . MQ (32- )


. 33- 32-
. 801
, , ,
.
32 ,
( , ).


64+32 => 32
9.1 =4 =2. , .
, , .
;
, . . , qhat
, . , .
" " 9.1 ,
.
9.3. ul
; ul . v .
.
, , .

9.3.

unsigned divlu(unsigned ul, unsigned uO, unsigned v,
unsigned *r)
{
const unsigned b
= 65536; // (16 )
unsigned unl, unO,
//
vnl, vnO,
//
ql, qO,
//
un32, un21, //
unl ,
rhat;
//
int s;
//
if (ul >= v)
//
152

9.

//
if (r != NULL)
//
*r = OxFFFFFFFF; //
return OxFFFFFFFF;
//

}
s = nlz(v);
// 0 <= s <= 31.
v=vs;
//
vnl = v 16;
//
vnO = v & OxFFFF;
// 16-
un32 = (ul s) | (uO 32 - s) & (-s 31);
unlO = uO s;
//
unl = unlO 16;
//
unO = unl0 & OxFFFF;
//
ql = un32/vnl;
//
rhat = un32 - ql*vnl;
// ql
againl:
if (ql >= b || ql*vnO > b*rhat + unl)
ql = ql - 1;
rhat = rhat + vnl;
if (rhat < b) goto againl;
un21 = un32*b + unl - ql*v;//
qO = un21/vnl;
//
rhat = un21 - qO*vnl;
// qO
again2:
if (qO >= b || qO*vnO > b*rhat + unO)

qO = qO - 1;
rhat = rhat + vnl;
if (rhat < b) goto again2;
}
if (r != NULL)
*r = (un21*b + unO
- qO*v) s,return ql*b + qO;

// ,
//

}
,
. , . , , (
,
).
(-s3i) 2 ,
s = ,
32 (, Intel x86).
,
again 0.38 .
, 52.
, , 6.5
( , ).
, ( ),
.
9.4.

153

divlu, , , 9.3 . , , divlu


, . ,
. 9.4.

9.4.

int divls(int ul, unsigned uO, int v, int *r)
{
int q, uneg, vneg, diff, borrow;
uneg = ul 31; // -1, u < 0.
if (uneg)
{
//
uO = -uO;
//
borrow = (uO != 0 ) ;
ul = -ul - borrow;
vneg = v 31; // -1, v < 0.
v = (v vneg) - vneg; // v
if ((unsigned)ul >= (unsigned)v) goto overflow;
q = divlu(ul, uO, v, (unsigned *)r);
diff = uneg * vneg;
// q,
q = (q diff) - diff; // v
if (uneg && r != NULL)
*r = -*r;
if ((diff q) < 0 && q != 0)
//
overflow:
//
if (r != NULL)
//
*r = 0x80000000; //
0x80000000;
//
//
return q;

. , 9.4,
,
, , \/\>\ < 232. ,
0.

154

9.

10

,
. , , 20 ,
, .
, , .

10.1. 2
, k 2*
[20].
. q = +2*, 1 <, k < 31 [29].
shrsi
shri
add
shrsi

t,n,k-l
t,t,32-k
t,n,t
q,t,k


2**k - 1, n < 0;

.
2 (k= 1). ,
. =31
, 231 . (q = -1, = -23', q =
).
-2* . .
, 2*:

label

bge
n,label
>=
add! n,n,2**k-l 2**k - 1 n
shrsi n,n,k


.
PowerPC 2
[16]. ,
. ,
addze . () 2 :
shrsi q,n,k
addze q,q
shrsi k 2*,
, -

. , , ,
2 shrsi.
, shrsi neg
-2* , , , (, ,
).

10.2. 2
*- 2* ,
= * 2* - , :
shli
sub

r,q,k
r,r,n


.
2*,
. , ( k= 1).
shrsi
shri
add
andi
sub

t,n,k-l
t,t,32-k
t,n,t
t,t,-2**k
r,n,t


2**k - 1, n < 0;
,
k

,
fn&(2*-l),
n>0,
.
rem (/i,2*)H / 4 & 2. 1 \
[-((-") ( - ))' " <0 , - 3 1 ,

( ) k= 1 , (-) & 1 = & 1 ,

( ). &>1,
( ).
,
\&(2"-\), >0,
rem(n,2
) = {,,
\ , . ^ , . .
V
' [(( + 2* -1)4(2* -l))-(2*-l), n<0.

156

10.

( k> 1 , k= 1 ).
1 < k < 31 .
, , , 2* - 1 < 0 0 > 0 , :
, <-31,
<-(*,*)-*

103.

d , 232/ , 32
. , 7, .
, , . :
();
" ";
t ;
q ;
.

3
li
mulhs
shri
add

M, 0x55555556 " " (2**32+2)/3


q,M,n
q = floor (M*n/2**32 )
t,n,31
1 q,
q,q,t
n

muli
sub

t,q,3
r,n,t


r = n - q*3

.
, 32- 64- , mulhs 32 64-
32
. 64- 2
floorQ , , . , > 0
~\

;^5

In

l3 + 3-2 3 2 J'

10.3.

157

, <2 3 1 , 231-1 .
, - 2/(3-232) 1/3 ( ),
D4 9.1 q=|_n/3j,
( (1) 9.1).
0 1. ,

J2

I 2 n + 2n+3-2

32

"
32 +1 =

D2. ,

-231 < < -1

___
3

3-232

__
_
~
3-2 3 2 ~ 3-2 3 2 '

, , -1/3 , ,
D4 q = |~n/3] , ( (1) 9. 1).
, , .
, n = qd + r. 3 ( -231/35g^(231 -l)/3 ), ,
-2 +2 .

, ,
.
RISC- ,
, - , 20 .

5
5 , 3, ,
, (232 + 4) /5 . ,

\\ S 230 . , (233 + )/5 . :
li
mulhs
shrsi
shri
add

M, 0x66666667 " " (2**33+3)/5


q,M,n
q = floor (M*n/2**32)
q,q,l
t,n,31
1 q,
q,q,t
n

muli
sub

t,q,5
r,n,t

158


r=n-q*5
10.

. mulhs 32 64- ,
. 64- 232
floor() . , 0
2 33 +3 I .1 ,
5 2 M J"l5 5-2 _
0 5 < 231 /(5 233); ,
1/5, D4 q = [/Sj.
< 0
2 33 +3 I +
5 2 33 J |S 5-233
- -1/5, ^ = |"/5].
, 3. , .

7
7 . (232+3)/7 (233 + )/7
. (234 + 5)/7 , 32- .

(234 + 5)/7 - 232 .
7:
11
mulhs
add
shrsi
shri
add

,0x92492493 " " (2**34+5)/7 - 2**32


q,M,n
q = floor(M*n/2**32).
q,q,n
q = floor(M*n/2**32) + n
q,q,2
q = floor(q/4)
t,n,31
1 q,
q,q/t
n

mull
sub

t,q,7
r,n,t


r = n - q*7

. , "add q,q,n" . , q ;
. , " " , , > 0

( D3).

10.3.

159

0<<2 3 1 , 5/(7-2 ), 1/7,


q =[n/7J
< 0

-1/7 , q = f/7] .

.

10.4. , 2
,
. , :
d > 2 .
, , W.
, W>3 2<d<21V"1 0 < m < 2W > W , ,
1 = 1-1 0<<2"->

(U)

^ +1= ^ -21"-<<-!.
, (, 0)
" 5", " 7". < 2W -1
, 7 (..
2'"' 2* -1 ,
7,
). > W , , W .
, W .
" ", . , :
[,
[m-2w,

0<m<2w-1,
2 -l<m<2w.
w

, (1,6)
\-mdll' J + 1 = -1 ,

^>1.

160

n = -d, ..

(2)

10.

() , , ( ,d) = d-\.
, , , = d - 1 .

= [2w-l/d\d - 1 = 21"-1 - rem^-V) - 1 .
d ,

2w-l-dnc<2w->-l

(3,a)

nc>d-l.

(3,6)

, ,
(1,) - ,
\\

\n,\_n.-(d-\)

~~

+1
~2< d
(2),
2"

2" + 1

, (4), , 2p/d , ..

2f+d-Km(2f,d)

(5)

(4), :
nc(d-Km(2p,t

(6)

, s d , (6)
. < W ,
= W ( ,
(6)). > W , (6), (5) . ,
, (4) , , ,
. , s = - W
( mulhs).
, < W = W , .
DC1. (6),
.
10.4. , 2

161

. , (6) = . (6) 2

D5 rem(2
f

(>0+l

,/)s2rem(2'' ,rf)-d . ,

>

+1

2 + >nc(2d-(Km(2 + ,d) + d)) 2"

>ne(d-Km(2 '+ ,d)) .

, (6) = + 1 ,
.
, (6) , , -, , = W , d
, d .

, (6) 0 < < 2 ( , W , ).


, (6) , . ,
, W. ,
2,
2 . , (6):
p

nc(d-Km(2',d}}<2 <2nc(d-Km(2",d}].
0(2',</)</-1,

nc+l2f2ncd.

(7)

(3,) (3,6) ne^nax(2w'> -d,d-l) . fl(d) = 2w'l-d


f2(d) = d-l d = (21"'1 + 1) /2 . , > (2W~' - 1) /2 . , S 2W~2 . , nc,d 2*"' -1 , (7)

W-lp2W-2.

(8)

= W -1 (, W=32, d=3),
= W .
W , (4) (7)
. + 1
2n d , + 1
< m < r - '-
d
nc
(3,6)

d-1 + l

162

10.

nc<2w-l-l (3,a),
2<m<2w-\.
W, (4)

2W

2" +1

<m <
d
d

nc

2 < d < 2"' -1 /zc 2W-2,


2W2W~2+1

-<m<-

, , 7.

, (6) (5), (1,) (1,6).
(5) (6), , (4). ( ,
W, DC 1,
(6) .) :
< < ,

-<<.-\,
-nc-d + \<,n<-nc-\

= - - d .
(4), , :
2"

2 ' + .
dn

n/2f > 0
^ 2fn(n +\)-n
< - c ' ,
d 2"
Vdn,

d\

I 2"

2"dn,

0 < < , S (2 - l)n/(2pdnc) < l/d, D4


d

2"dn,

=14
UJ

, 0 < < (1,) .

10.4. , 2

163

>

nc + lnnc+d-l,

(9)

n>nr+d , ,
rem(ne,d) = d-l ( , (3,) , n^nc+d nS2 lt '~').
(4), > 0 , :

<

+ 1

<

~d ~V ~d
:
+1 ^ (-,)( +1)
d
dnc

^
d 2"
(9) \<- 5</-1,
0

>->+1)^-+1
dnc
d nc

( (3,6)) ne^d-l (+\)/ ,


d

dnc

d-1

(10) ( +l)/d . (-)( +l)/dnc 1.


, (10)

(9) \ji/d \ = (nc +l)/d . , (1,)


(nc+l<n<,nc+d-l).
< 0 (4), ,
d

5<
^

nc

/2 , < 0 , :
+ 1

2 + 1

d nc

|2'

| d 2"

D2, :
dnc

164

+ 1,

10.

I 12"

dn,

2'd

+1 < 0, ,

- < < -1
-+1 ^ + 1
-<
<0
dn,
dn

, D4
+-

dnc

( - < < -1 ) (1,6).


< -
-, -d<n<-nc -1.
w

(12)

( (3,), n<-nc-d n<2 ~ , .) (11),

dnc

d\

-nc-d<n<-nc-\
1)(,+1)| 1 ^(H + ^K
rfn,.
rfnc
dnc

dnc

(nc + l)/nc ; ,
=d-\ . ,
{

^
dnc~
t

dne

(13), (-nc-l)/d ,
-1 , :

10.4. , 2

165

- -
~1

d \

-,-1

, [mn/2 J +1 = [n/d~\, .. (1,6).


, = - - d , d. (3,) , -nc-d<-2w~l,
, = - - d = -2w~l , , nc-2w'1-d. ,
rem(2w~\d) = Km(nc+d,d) = d-l (.. d 2"'~1+1).
n = -nc-d (6) = W -1 ( ),
= (2w-l-d)(d-Km(2w-l,d)) =

ne(d-Km(2',d))

= 2w-l-d<2w->=21'.
(5)
1=-

d
1

= 1+1 =

2w-<

-*' -1
+i=l * . 1+

+1=

\d

(1,6) .
, (5)
(6), (1,) (1,6) .

10.5. , -2
n+(-d) = -(n+d),
n + \d\ . ( d = -2'"1, 2 10.1,
.) - ,
.
.
:

q=

166

< 0;

10.

>0.

1 (
), 1, q . , ( ).
W = 3 2 , d=-l .
li
mulhs
sub
shrsi
shri
add

M,Ox6DB6DB6D - (2**34+5) /7 + 2**32


q,M,n
q = floor (M*n/2**32)
q,q,n
q = floor (M*n/2**32) - n
q,q,2
q = floor (q/4)
t,q,31
1 q,
q,q/t
q ( )

muli
sub

t,q,-7
r,n,t


= n - q*(-7).

+7, ,
+7, ,
, , shri q, ( ). (, +7
q, .)
, .
. W = 32, d = -l
, 3 -3 W =32, = -231 .
.
W > 3 -21""1 <,d<-2 ( ) -2W < < 0 > W , ,
I mn I I n

-2- 5 SO,

(14 a)

L2'J U
mn I

_ n_

~\+ 'I'd

, .
, , = +1
k. ,
= d +1. , ,
=1 (-2W~' -l)A/ |rf + l = -2lt'~1 +rem(2H'~' +l,rf).
\d\ ,
s
s
iW-l
j
1
\
2W-1 <
<-2

/1 5 \

(.l- !^

, ,
n<d + l.

10.5. , -2

(15,6)

167

(14,6) = -d, (14,) = ,


(4)
2--1.
d

.2'

(16)

nc

, (16), , 2p/d , ..

2"-d-Tem(2p,d)

(17)

(16) ,
(18)

(17) (18)

. ,
-21" S < 0 . , d , .
d = -2* , = -2w~l + 1 , p = W + k-l / = -2'"1 - 1 (
). d,
-2* , , , .

m(d)

d.

m(-d) = -m(d),
\d\ , , " -7", .
(18) (6), (17) (5), , -d d, , m(-d) = -m(d) . , m(-d) # -m(d) , , , -2'"1 .
2'"1 + 1 . ,
.
215+1 = 3-11-331
231+1 = 3-715827883
2+1 = 3- 1943-5419-77 158673929
m(-d) -m(d) . . d >
w

= 2 ~ -d. iem(2 ~\d) = d-l, p = W-l


(6), , = W . d < 0
nc=-2w~' rem ( 2'"1, rf) = |rf|-l. , p = W-l,tm p = W (18), > W .
168

10.

10.6.
,
d s. (6) (18) = W,W + 1,... , . (5) (17) .
, , a s = - W.
d
,
.
, :
W

j 2 ~' + rem(2"'~ ,</)-!, d>0,


*** ~~ 1

_ ii/

' - u/ i{

+ l , d ) , d<0.

, \\ :

'= 2""'''ll, d<0,


\nc\ = t-l-rem(t,\d\).
,
. rem(/,|rf|),
rem(f,rf) , ,
( ) .
(6) (18),

(19)
|w| ( (5) (17)):

^'""'
rem(2 p ,|rf|) (19) " " ( 2^- W- 1-
), .
(19),
W- .
.
(2',|</|) , q
2 \d\ = W -1, q / .
1 q (. D5 ( )):
q = 2*q;
= 2*:

if ( >= a b s ( d ) )
10.6.

169

q = q + 1;
r = r - abs (d) ;
}

(4) (16)
, q = [Z'/HJ < 2W , q W-
. , 0 < < \d\ , W-
. (: 2
2w~l - 1 , ,
.)
8 = \d\ - . W-
( , 1 5 < \d\ ),
.
(19),

2"
>^-

2/\\ ^- ( (7), (19) , 2' S 2|nt| |</| d = -2w~l , nc=-2w'l+l


= 2W - 2 , 2/\\ = 22W'2/(2W'1 - 1) < 2W W > 3 ). ,
rem(2'',|rf|) , ( ).
2' /\\ 2W~' ( d) .
(20), . ,
j=

LM

2"/\\ > 8 . 2f/\nc\


q\ \. 2'>/|"| (
) ( d = 2W~2 + 1
d). 2/\\ S :

d 10.1,
W = 32 . , , ,
.
10.1.
struct ms
{
int M;
//
int s;
//

};

170

10.

struct ms magic(int d)
{
// d
// 2 <= d <= 2**31-1 -2**31 <= d <= -2
int p;
unsigned ad, anc, delta, ql, rl, q2, r2, t;
const unsigned two31 = 0x80000000; // 2**31
struct ms mag;
ad = abs(d);
t = two31 + ((unsigned)d 31);
anc = t - 1 - t%ad; //
p = 31;
//
ql = two31/anc;
// ql = 2**p/|nc|
rl = two31 - ql*anc;// rl = rem(2**p,|nc|)
q2 = two31/ad;
// q2 = 2**p/|d|
r2 = two31 - q2*ad; // r2 = rem(2**p,|d|)
do {
p = P + 1;
ql = 2*ql;
// ql = 2**p/|nc|.
rl = 2*rl;
// rl = rem(2**p,|nc|)
if (rl >= anc) //
{
//
ql = ql + 1,rl = rl - anc;
q2 = 2*q2;
//
r2 = 2*r2;
//
if (r2 >= ad)
//
{
//
q2 = q2 + 1;
r2 = r2 - ad;
}
delta = ad - r2;
} while (ql < delta ||
mag.M = q2 + 1;
if (d < 0)
mag.M = -mag.M; //
mag.s = p - 32;
//
return mag;

q2 = 2**p/|d|
r2 = rem(2**p,|d|)

(ql == delta && rl == 0) ) ;




mulhs, add d > < 0 sub 3 < > , ,
shrsi s > 0. shri add.
W = 32 , d=3 7=715827883, m(-d) = -m(d).
10.1 ( ).

10.7.
DC2. , W .

10.7.

171

. , (1,) (1,6) ( ) . , 2, 1 .
, ,
.

(, ,
W=32, rf=7), . , ,
. ,
W=32, d=6 :
= 715827833

((232+2)/)

s=0

32

= 1431655766

((2 +2)/3)

= -1431655765

((233+l)/3-232)

= -1431655764

((233+4)/3-232)

s = 2.

s=l
-2

.
DC3. d ,
, W .
. d > 0 . (4) 2p/dnc . (7), ..
, 2p/dnc < 2 . , , (4). , (5); + 1 .
, + 1
(4) ( pQ W ).
p

2 +d-rem(2",d)
2" +1

nc


2ft > nc(2d -rem(2" ,d)) .
2
2p-l>nc(d--Km(2p,d)\.
ft

D5 9.1 rem(2 ,d) < 2rem(2 '~ > d) ,

0.
d < .
172

10.


d = 3 , W = 32 , add shrsi mulhs. : ?
. ,
, (1,) (1,6) W
p = W 0 < m s2 ~*. , (1,) (1,6), (4),
1
d, (4) p = W 0 < m < 2*" . (4) = W
w

2 +kd-nm(2 ,d)
i
'-, /t = l,2,3...
d

(4) ,
w

rem(2 ,d)>kd- .

(21)

rem(2 ,d) k=l ,


2 W ~ 2 ,
nm(2w,d)>d-4,
.. d 2W +1, 2W + 2 2W + 3 .
, .
d 2W +1, rem(2w,d) = d-l. (6)
= W , :
2w>nc(d-(d-l)) = nc,
< 2W~'.
2w+d-(d-l) 2w+l

'
d > 3 , 2W'1 (d 2, 2W +1). , 2W +1 .
, d 2W + 2, rem(2w,d) = d-2. (6) - W ,
2w>nc(d-(d-2)) = 2nc,
, , . m
m=

10.7.

2w+d-(d-2) 2w+2
d
'= '

173

2'"1 -1 d=2, 21V~I-1 WS3, d23 ( W-3


</=3 , 3 23 + 2 = 10 ). ,
2W + 1 , 2 ( 2 (2' + 2)/2 , .. , W- ).
d 2W + 3 , ,
, . rem(2w,d) -d -3 , (21)

;= 1, < 2w/3 .
(3,) S 2w~l - d d 2w~l - , :
_w-i

a >2

-jW

2.

=.
3
6
, 2, 3 4 2W + 3 , 2W + 3 5 , , (2W +)/5 . , d 2/ +3 d
--

,
1

2"
2"'+3
<d< - .
6
5

, d (2* + 3)/
:
+

'

, W 5
5 6. W < 5 , 21" + 3 . 6 2W + 3 , 5. ,
2W + 3 , , (2W + 3) /5 .
d = (2w+3)/5
2 1 "+3
(2"+3)/5

-1,

W S 4
2*-i
2<-^ n-<2.5,

(2"+3)/5

=2

174

-1.

10.

2 /3 , d = (2 +3)/5 .
W

W <4 2 + 3 , ,
W
2 + 3 .
W

: 2 +1 2 + 2, 2 (2* + 2)/2, , .
, , 10.1 , .
W, 16, 32 64. .
16

2 +1=
16
2 + 2=
32
2 +1=
32
2 + 2=

2 +1=

2 + 2=

65537 ()
2
2-3 -11-331
641-6700417
2-3-715827883
274177-67280421310721
3
2-3 -19-43-5419-77158673929

, W = 16 20 , . 100 3, 6, 9,11, 18, 22, 33, 66 99.


W =32 : 3,6, 641,6700417, 715827883, 1431655766.
W = 64 126 . 100 3, 6, 9, 18, 19, 27,
38,43,54,57 86.

10.8.
, , ,
.
, :
d > 0, ,
1 . , .

3

3 32- .
2 32 -1, (232 + 2)/3 (- 2/(3-232)
1/3). (2" + l)/3.

11
, (2**33+1)/3
mulhu q,M,n
q = floor(M*n/2**32)

shri q , q / l

muli
sub

t,q,3
r,n,t

= n - q*3

10.8.

175

mulhu, 32
64- .
, , :
33

2 +1 I |
3

33

2 J

[3

3-2

0<,<21 0/(3-2 33 )<1/3, D4 q = [/3].



,
, , .
, 0 2, .

7
7 32- (2 32 +3)/7,
(233+)/7 (234 + 5)/7 ,
-. (233+)/7,
32- . (235 + 3)/7 - 232 . :
11
mulhu
add
shrxi

,0x24924925
q,M,n
q,q,n
q,q,3

(2**35+3)/7 - 2**32
q = floor(M*n/2**32)
()

muli
sub

t,q,7
r,n,t


r = n - q*7

: add .
,
(shrxi), add 32 q 33- ,
.
Motorola 68000 :

3 ( -,
add , ). . ,
PowerPC : q,
q .

176

10.

shrxi

0 < < 2

32

35

0 < ? 2 ) < 1/7 , -

D4 q = [/7 J .
(Granlund) (Montgomery) [21] , shrxi. , , , ; . :

L 2"
32

32

, ^ = [//2 ], 0</<2 .
,

.-[]*..
32

, 0 < - q < 2 . ,

2 J

[2

J L 2

32

0<7!,<?<2 .

7:
li
mulhu
sub
shri
add
shri

,0x24924925 (2**35+3)/7 - 2**32


q,M,n
q = floor(M*n/2**32)
t,n,q
t = n - q
t,t,l
t = (n - q)/2
t,t,q
t = (n - q)/2 + q = (n + q)/2
q,t,2
q = (n+Mn/2**32)/8 = floor(n/7)

muli
sub

t,q,7
r,n,t


r = n - q*7

, shrxi
0. , d > 1 > 232 (..
shrxi), 0.

10.9. , 1
W 1 1 < d < 2W
, ,

I \ \ \
. . _.
= - 0 < < 2" ,

10.9. , 1

/-/i.-)-!
(22)

177

0<,m<2 *'

= fm, w
m-2 ,
f

(22) n=d, [md/2 J = 1

(23)

, , ,
w
w
w
, rem(n c ,rf) = d-l. =[2 /d^d-l = 2 -rem(2 ,rf)-l.

2 -l

(24,a)
(24,6)

, > 2 ~*.
(22) = ,

(23)
1Lm<
<21ii

(25)

,
(25), , 2f/d , ..
2"+d -l-rem(2'-l,d)

(26)

(25) ,
2t>>nc(d-l-Km(2p-\,d)}

(27)


,
> W , (27), (26) .
, (22) z W . , (27) , . , ,

178

10.

DC1, D5 .


41

, (27) S < 2" .


2, ,
2:+1, (27)
f

ne[d 1 rem(2'' \,d\\< 2 5 2 n c [ d 1 rem^' l,<f)l + l .


p >

0<Km(2 - ,d)&d-l,
(28)

, nc,d 2" -1 ,

(29)

0<p<2W.
, (27) .
IV, (25) (28)
d
-2 + \/ncc

'

/? W, (25)
d

nc

, \<>d<.2w -\ nc S 2*"1 ,
21"

21" 2/-'+1
+1.

, ,
7.


, (27) (26),
(22).
, (26) (27) (25),
(4); ,
, > 0 .

10.9. , 1

179

10.10.
, , , . , , < 2W ,
= 2W (, d = 2W - 2, W 4). = 2W ,
, (26) 21- .
" ",
, 10.2 ( 1=32).
, , add (
, d ).

10.2.
struct mu
{
unsigned M;
int. a;
int s;

//
// "add"
//

};
struct mu magicu(unsigned d)
{
// d 1 <= d <= 2**32-1
int ;
unsigned nc, delta, ql, rl, q2, r2;
struct mu magu;
magu.a = 0 ;
// "add"
nc = -1 - (-d)%d; //
= 31;
// .
ql = OxSOOOOOOO/nc;
// ql=2**p/nc
rl = 0x80000000 - ql*nc;// rl=rem(2**p,nc)
q2 = Ox7FFFFFFF/d;
// q2=(2**p-l)/d
r2 = OxVFFFFFFF - q2*d; // r2=rem(2**p-l,d)
do {
P = P + 1;
if (rl >= nc - rl)
{
ql = 2*ql + 1 ;
// ql
rl = 2*rl - nc; // rl
else
{

ql = 2*ql;
rl = 2*rl;

if (r2
{
if
q2
r2

+ 1 >= d - r2)

(q2 >= Ox7FFFFFFF) magu.a = 1;


= 2*q2 + 1 ;
// q2
= 2*r2 + 1 - d; // r2

else

180

10.

if (q2 >= 0x80000000) magu.a = 1;


q2 2*q2;
2 = 2*r2 + 1;
}

delta = d - 1 - r2 ;
} while (p < 64 &&
(ql < delta | |
(ql == delta && rl == 0)));
magu.M = q2 + 1; //
magu.s = p - 32; //
return magu,// (magu.a )
.

,
.

2 ,
10.1, 2*ri
.
,

"if (rl>=nc-rl)" "if (2*ri>=nc)". 2 -1


nad.

0 < 5 < d - 1 , <? 32- .

2* ,
2W - 1 ; ,
q2 .

magu.a add -
232 q2 232 - 1 .
q2 , . q2 232 - 1 , 232, magu.a 1;
0.
(27) 2/ > 5 .

< 64 , ql , .

10.11. ( )
DC2U. ,
W.
10.11. ( )

181

DC3U. d ,
, W .
.

( )
( ) , (11,
mulhu), ,
(. " "). , 2W 2W +1 ( d = 1).

. 16- ; 32-
641 6700417. (274177 67280421310721)
64- .
d = 2*, * = 1,2,... . = W ( ) = 232~*. , . = W + k. = 2W, 0, , s k.
0
k. , 2, magicu. (
,
2. . d > (4), (3,6), d-l<2f/m<d. , 2/ .
d < 0 (16) (15,6).)
, shrxi,
> 2W , < 2W . ,
. 32
, 100, 31 "" : 1, 7, 14, 19, 21, 27, 28,
31, 35, 37,38, 39,42,45, 53,54, 55, 56, 57, 62, 63,70, 73,74,76, 78, 84, 90,91, 95 97.



mulhu, mulhs ( ), ,
8.3.
8.3 ,
mulhu mulhs. , ,


"mulhu q, M, n". t .
182

10.

1= 0

mulhs
shrsi
and
add

q,M,n
t,n,31
t,t,M
q,q/t

M3i = 1

mulhs q , M , n
shrsi t,n,31
and
t,t,M
add
t,t,n
add
q,q,t

, mulhu, , , .
mulhs mulhu.
, mulhs
mulhu, add sub.



. (27)
2p>2w(d-\-Km(2p-l,d)],

(30)

, , (26).
, (..
(22)), , ,
d . , /, (30) (26), 2'*'.
, 10.3.
10.3.

struct mu

};

unsigned M;
int a;
int s,-

//
// "add"
//

struct mu magicu2(unsigned d)
// d 1 <= d <= 2**32-1
int ;
unsigned p32, q, r, delta,struct mu magu;
magu.a = 0;
// "add"
p = 31;
//
q = Ox7FFFFFPF/d;
// q = (2**p-l)/d
r = OX7FFFFFFF - q*d; // r = rem(2**p-l,d)
do {
p = p + 1;
if (p == 32) p32 = 1; // p32 = 2**(p-32).
else p32 = 2*p32;

if (r + 1 >= d - r)

10.11. ( )

183

if (q >= OX7FFFFFFF) magu.a = 1;


q = 2*q + 1;
// q
r = 2*r + 1 - d; // r

}
else
{

if (q >= 0x80000000) magu.a = 1;


q = 2*q,-

r = 2*r + 1;
}

delta = d - 1 - r;
} while (p < 64 && p32 < delta);
magu.M = q + 1; //
magu.s = p - 3 2 ; //
return magu,// (magu.a )
[2] , , .
w l
magicu2 , d <, 2 ~ .
32 , , tnagicu2 , 102807 ( magicu ,
2737896999, a magicu2 5475793997).
magicu2
, .

10.12.

,
,
, " 1, ". . . ,
.

10.13.
magic . ,
, 2, .
1) d, k.
2) d/2* (
k ).
3) , .
4) , k.
184

10.

, 3, 5, 25, 6, 10, 100 ..


, .
32 , , , 334972. /=3361176179 5=18;
840294045, 16. d = -6.
.
[2] ,
. , ,
d
p = W+["log 2 rf] m=^1p/d~\
n + d = [/2 \ (.. ). ,
2"'+| ,
W- .
, , ,
, . / , 2W,
, 7 (.. add shrxi, ). , 2W, .
\d\
W -1 ( 21"'1 <m<2w), ,
, . (
, 2 W ~', ). ,
, > 2W.
7, (
, , -: = + 1):
abs
li
mulhu
shri
shrsi
xor
sub

an,n
M,0x92492493
q,M,an
q,q,2
t,n,31
q/q,t
q,q,t

(2**34+5)/7
q = floor(M*an/2**32)

q,

, ,
7 (7 6 ), ,
abs mulhu mulhs.

10.13.

185

10.14.
10.1. W = 32

d
-5
-3
k
-2
1
2k
3
5
6
7
9
10
11
12
25
125
625

()

99999999
55555555
7FFFFFFF
_
80000001
55555556
66666667
2
92492493
383839
66666667
2829
2
51EB851F
10624DD3
68DB8BAD

1
1

k-1
_
k-1
0

0
2
1
2
1
1
3
3
8

()

0
0

232~k

CCCCCCCD

24924925
383839
CCCCCCCD
28

51EB851F
10624DD3
D1B71759

0
0
0
0

0
0
0
0
0
0
0

2
2
3
1
3
3
3
3
3
9

()

0
0

2
2
3
3
3

1
1

3
5
7
7

10.2. W = 64

()

-5
-3

99999999 99999999
55555555 55555555

7FFFFFFF FFFFFFFF
_
80000000 00000001
55555555 55555556
66666666 66666667
2
49249249 24924925
17171 717172
66666666 66666667
2828 28
2
A3D70A3D 70A3D70B
20495 E353F7CF
346DC5D6 3886594

-2
1
2k
3

5
6
7
9
10
11
12
25
125
625

S
1
1

k-1
_
k-1
0

2
1
1
4
4
7

o64-k

24924924
3838

2828

47147
0624DD2F
346DC5D6

CCCCCCCD

92492493
8E38E38F
CCCCCCCD
28

14715
1A9FBE77
3886594

0
0
0
0
0
0
0
0

10.15.
" " , , ,
0. , ,
. - q,
q , , q

186

10.

[32, 7.6.2]. s,
( - q)/s .
[21, 9].
, .
Ml. ,
\<<=, , aa
,
. ,
[49]. , ,
.
. . (, , ),
, , . , a = 3 m = 8
7 , , 3, 6, 9, 12, 15, 18, 21,
8 0, 3, 6, 1, 4, 7, 2, 5. , 0 7.
, . , , , .. : ( y(mod ) ), = ay (mod m) .
k, ,
- = ,

(- y) = hn.
, - , ..
=( ) .
.
,
/, , 1.
,
( ), , axsl(modm), .. . , ( ) ,
, = b(mod ) , b .
= 16 . 3=11, 3 1 1 = 33 = l(mod 16) .
, 3=-5, 3-(-5) = -15 = l(modl6) . , -3 = 5,
(-3) 5 = -15 l(mod 16) .
,
, . 4- , 3=11;

10.15.

187

, 3 = -5 . 1 1 -5
( 16),
.
( ) d W- .
1
2 " , , d , d
1

W {

( 0 2 " - 1 -2 ' 2*"' - 1 ), , dd = l(mod 2"' ) .


, , d,

, n/d d ,
W6m .
d , d = da 2* , d0 ,
a k > 1 . k (
), d0 ( ).
7 , 7.
, .
11 M,OxB6DB6DB7 , (5*2**32+1) /7
mul q,M,n
q = n/7



? " ".
,
[49] [39, 4.5.2].
d , ,
dx = l(modm) ,
W

= 2 ( W ). , (, , 0)
dx + = 1 .

, d , (d d + km ).
( d,m>0):

d(-l) + m(\) = m-d,


m(0) = d.

188

(31, )
(31,6)

10.

d = l, , (31,6) , = 1.

q--

(31,6) q (31,).

d(-l- q) + (1) = - d - qd - rem(/n - d,d).


, . rtm(m-d,d) = \, ( ) = -1 - q .
, . , 1. d, , d.
m-d < d , 0,
, . , :
= 2W .
.
/=256 d=l. ( q = [249/7J = 35).
7(-1)
7( 1)
7(-36)
7( 37)
7(-73)

+
+
+
+
+

256( 1)
256( 0)
2 5 6 ( 1)
256(-1)
2 5 6 ( 2)

= 249
= 7
= 4
=3
= 1

, 256 7 -73 (,
0 255, 183). : 7 183 = 1281 = l(mod 256).
,
, , , , , 0 (
). , 0 1 . ,
, 1, 0.
, , b (, ktb) , 0.
, , , ktb
kfab + b. , ,
, ,
m-d d .

10.15.

189

,
, 1 , , d .
, d < 0, 2 ". , 2 , ; d , , .
q .
, ,
( 0 -1).
, " ", 2 W .
, 256,
dx + my = \ . d
. , .
255
1

249
7

220
37

4
3

183

- 10.4.

10.4.
32
2
unsigned mulinv (unsigned d)

// d

unsigned xl, vl, x2, v2, x3, v3 , q,Xl = OxFFFFFFFF; vl = -d;


X2 = 1; V2 = d;

while (v2 > 1)


{

q = vl/v2;
x3 = xl - q*x2 ; v3 = vl - q*v2 ;
xl = x2 ; vl = v2 ;

X2 = X3 ; V2 = v3 ;
}
return x2 ;
}

v2 > 1 v2 ! = 1 , , (
d , v2 1, 0).
, ? , , , dx & o(mod 232 ) ,
. (
v2 ! = xl 2) ;,

190

10.

32

32

, dx = g(mod 2 ) , g d 2 , ..
2, d. , ,
d ,
.
, , d, 20, ;
1.7. d 1000 1 1, .


, l/d , d&Q,

*,=* (2 -dO

(32)

XQ, l/d .
.
,
! , 3 256
AJ = 1 ( ).
xt =1(2-3-1) = -!,
*2=-1(2-3(-1)) = -5,
,=-5(2-3(-5)) = -85,
. = -85(2 -3(-85)) = -21845 = -85(mod 256).
256, -85, 171,
3 256. 256.
?
dxa = l(modm)
*n+i (32),
dx,H sl(modm 2 ) .
, dxa = 1 + km .
<&*/= (2 -**) =

= (l + km)(l-km) =
= 1-*V =
= l(modm 2 ).
2, , 2N. ,
dxa=\(mod2N),
, d , (32) "" .
10.15.

191

, 8
d . ,
0 = d . (32) xt, ^. . ., ,

<* 4 1(12 48 )..


, , 232 ( = l(mod 248) , = l(mod 2" ) S 48 ).
( 10.5), 232.

10.5.
232
unsigned mulinv (unsigned d)

// d

unsigned xn, t;
xn = d;
loop:
t = d*xn,if (t == 1) return xn;
= * (2 - t) ;

goto loop;

d 4.5 , ..
. ( " "
, .. d2 & l(mod 16) ) , , ( ) .
, , .

, d, .
" 4 ", ..
XQ, dxa =l(mod!6) .
. , , :

2 , 232 ( ). , .
, ,
, . , . , .

192

10.


. 10.3 .

10.3.

mod 16

mod 2

()

()

( )

-7
-5
-3
-1
1
3
5
7
9
11
13
15
25
125
625

-7
3
5
-1
1
11
13
7
9

49249249
33333333
55555555
FPFFFFFF
1

CCCCCCCD
B6DB6DB7
383839
28
445
EEEEEEEF
C28F5C29
26E978D5
3AFB7E91

5
15

mod 2

()
92492492
33333333
55555555
FFFFFFFF

49249249
33333333
55555555
FFFFFFFF
1

CCCCCCCD
6DB6DB6D B6DB6DB7
83838 383839
2828 28
444 445
EEEEEEEF
8F5C28F5 C28F5C29
10831 26E978D5
D288CE70 3AFB7E91

, (d=3,5,9,11)
d d (. 10.14).
. , , (2 + l)/d
> 32 .
= l(mod2 32 ),
32

s d (mod 2 ).

10.16.
d 0 d [21].


d.
d d . , dd sl(mod2 lt '), W
, d . , d
2W , Ml ,
2W 2W, rid
2 *" 2'.

10.16.

193

, d,

, nS = 0,l,2 ..... [(2" -l)/rfj(mod2 w ) n = 0,d,2d ..... [(2w-\)/d\d .


, , d, nd , 2W
0 2W - 1 , \(2W - l)/d J .
.
, , 25, 25
W \(2W -l)/25j. RISC-
:
li
mul
li
cmpleu
bt

M,OxC28F5C29 25
q,M,n
q = *
C.OXOA3D70A3 = floor((2**32-l)/25)
t,q,c
q ; ,
t,is_mult
25

,
: d = d0 2* , d0 , a k 2: 1 . , d , do 2*, nJ0
( d0 ), ,
d, :

q S I (2"' - l)/da J q k ,
"mod" nd0 , 2* - ll .

, ,
.

, ak , k ( S k < 32 ).

ZRU. < k -

, xk\a/2k \.
. ( , 32- .)
, *< ; k . , *< ,
|_*/2*Js[a/2*J. [jc/2*J = *;t , , x*:[e/2*J. rot

k , x-k k ,
/

**'

" I

[a/2 J , **>[/2* J . , > ; k

194

10.

, , 32 -k , 32 -k , I /2*|>|/2*|.
, d, d , d = da 2*, d0 , :
g<-mod(nda,2w);

, [[(21" - l)/da J/2* J = [(21* - l)/(d0 2* ) j = [(2"' - \)/d J .


, ,
100.
li
mul
shrri
li
cmpleu
bt

M,OxC28F5C29 25
g,M,n
q = *
q,q,2

c,x'028FSC28' = floor( (2**32-1) /100)
t,q,c
q , ,
t,is_mult
n 100

, >2
, ,
d d ,

,
1

n = ^-2w~l/d^-d,...-d,Q,d,---\(2"f~t -l)/d\-d

11

nd =["-2'"'" /</"],...-1,0,1,-"| (2 '" -l)/rfJ(mod2 '). , d 2W, 2W 2W, nd


2W 2W. , d
,
\-2w-l/d] <. mod(nd,2w)* [(21"-1 - \}/d\ ,
"mod" nd [-2w~l,2w~l -l] .
, , , d ,
, d 2w~l . ,
-2- [(-2-4 1)/ -[(2- - 1)/4
, d, d = da 2* , a do ,
:

-I (2w~l - l)/d0\ <,q< \(2W - l)/rf 0 J q k .


10. 16.

195

, .
, ,
, .
ZRS. 0, :
1)

-<< k ,

2)
3)
' 0 k (..
. ( , 32- .)
, 1 2, ,
-<< abs(*)<a . 1 2 ZRU.
1 3, , 1
' . , 4. 1, , ,
+'<2' .
+ k , k , ZRU, .
3 , d,
d , 2, d = da 2* , d0 , :

( d , ' .)
, ,
100. , I 2'/2* J
' k -1 ,
.

li
mul
li
add

M, OXC28F5C29

q. M,n
c, OX051EB850
q, q,c

shrri q. q,2
shri
c, c,l
cmpleu t,q,c
bt
t,is_mult
196

25
q = *
= floor((2**31 - 1)/25) & -4

2

q , ,
n 100
10.



11.1.
" " I V* I . , , : 0 < < 232 - 1 .


, , . g0,
-Ja .

, .. gn
, gn+\ 2 .
.
,
. , .
. +1 =1 (gn +|_a/S,,J)/2j, gn , 0.
) g, > [V^J , [ V^J S g<+, < ga ;
) ga = [Va J , [/ J <, ga+t S [Va J + 1 .
, gn
I Va J , ,
I I . , , . gn = Va , , 1 . , , : g0 > I Va J , gn
, gn+1 S g,, .
=0 , 0 0.

, ) gn ,
8,
gn >U/a| gn , ga >Va . :
/ . > 0
i

:+

|il
1_2* .
2

I (1 + ) +
2 + 2 + 6

= +!<,

^
2(1+.)
<8*1<8!

) # = I <Ja I , - -1 < < - , g < < (gn +I) . ,

2*.
1
2*.

< ^+1 < [_g, +1J (.. g, - < 1),

I <Jx I
. 11.1 go 2, -/ . ,
go=2 =4, :=5 g0=4.
11.1.
int isqrt(unsigned x)
unsigned xl;

int s, gO, gl;

if (x <= 1) return x,8=1;

Xl = X - 1;

if
if
if
if
gO

198

(Xl > 65535) s = s + 8; Xl = Xl 16;}


(Xl > 255)
S = s + 4; Xl = Xl 8;"
(Xl > 15)
S = S + 2; Xl = Xl 4;
(Xl > 3)
8=8+1;}
= 1 s;
// gO = 2**s
11.

gl = (gO + (x s 1; // gl = (gO + x/gO)/2


while {gl < gO) { // ,
gO = gl;
//
gl = (gO + (x/gO)) 1;
return gO;
g0 2, g\
.
1 ,
(
), ,
( 32- ), ( ). , ( , 16785407).
,
:

if ( <= 1) return x;
s = 16 - nlz(x - 1)/2, s . gQ: 2, I VJc J. : g0, ,
. .

11.1

0
1


11.2

2 4
5 16
17 64
65 256

9 24
25 80
81 288

0
1
2
4
8
16

2 28 +12 30
230+1232-1

(214+1)2(215+1)2-1
(215+1)2232-1

215
216

1
4 8

11.2.
*(0<<24), .

11.2.
int isqrt(unsigned x)
{
int s, gO, gl;
if (x <= 4224)
11.1.

199

if (X <= 24)
if (x <= 3) return (x + 3) 2;
else if (x <= 8) return 2;
else return (x 4) + 3;
else if (x <= 288)
if (x <= 80)
s = 3; else s = 4;
else if (x <= 1088) x = 5; else s = 6 ;
else if (x <= 1025*1025 - 1)
if (x <= 257*257 - 1)
if (x <= 129*129 - 1) S = 7; else 8 = 8 ;
else if (x <= 513*513 - 1) s = 9; else s = 10;
else if (x <= 4097*4097 - 1)
if (X <= 2049*2049 - 1) s = 11 else s
12;
else if (x <= 16385*16385 - 1)
if (x <= 8193*8193 - 1)
S = 13; else s
14;
16;
else if (x <= 32769*32769 - 1) s = 15; else s
gO = 1 s; // gO = 2**s
// , 11.1
11.1
RISC- 26+(> + 6) , D
, while.
11.2 27+(> + 6)
( ),
.
.
X

11.1

11.2

0 9
0 99
0 999
0 9999
232-1

0.80
1.46
1.58
2.13
2.97

0
0.83
1.44
2.06
2.97

, 20 ,
9999, 81 .


, ,
,
? : ,
216. . , ; . , 1. .
, 16, 0 216.
( .) -

200

11.

re 11.3 ,
0 216 . , 11.3 RISC- , b , S - 2 1 .
11.3.
int isqrt (unsigned x)
unsigned a, b, m,- //
= 1;
b = ( 5) + 8 ; //.
if (b > 65535) b = 65535;
do {

m = (a + b) 1;
if (m*m > x) b = m - 1;
else a = m + 1;
} while (b >= a) ;
return a - l;
a S I V* I + 1 6 S I /* I .
b I VJc I . : x, *+4+1, +8+2, +16+4, +32+8,
+64 + 16 .. , . ( x + 2 + l
, , - +4 + 1 .)
11.3 ,
+ 1 , / 7 - 1 , = ( + ) + 2 = ( + + \) + 2 .
11.3 6+(+7.5)
, ,
.
.
X

0 9
0 99
0 999
0 9999
2 32 -1

3.00
3.15
4.68
7.04
16.00

, 5 , 0 9999, 94 . (=16) 206 .

11.1.

201

,
:
= (1 (33 - n l z ( x ) ) / 2 ) - 1;
= ( + 3 ) / 2 ;

b = 2^2~"h^*2 -I. (, 0<<15


),
1 1.3. 0 9999
5.45, 74
( , ).


, , 9.2.
32- , 64- ,
, 32 . 64- , (
0) . 64- 2 + 1 . , 64 , 1 ( ,
). ,
64- , . 16 .
1945 [36].
, , , 64+32=^32
,
, .
, , ,
. 1 1.4 [19]
. 149 RISC-. | m +.
11.4.
int isqrt (unsigned x)
unsigned m, , b;
m = 0x40000000;
= 0;

while (m != 0) { // 16
b = | m;
= 1;
if (x >= b) {
X = X - b;

= | m;

m = m 2;
}
return y,-

202

1 1 .

. U/179 J 8- .

- 1

0111
- 101

XI 0100 0000 yl
2 0010 0000 2

0010
- 11 01

0001 1000

1011

179 ()

2 0000 2

0010 0001 1000 ( )


1 1001 4 0000 1100 4
0000 1010

4 0000 1101 4

13; 10.
31
if x>=b. , b ( < 5 2 ), >= (. 2.11). i f :
t = (int) ( | ~( - ) ) 31,- // -1, >= ,

= - ( & t) ;
= | ( & t) ;

, -, ,
5 .
, , .

. , .

RISC-
( )

0 1
0 3

0
1

0 3

0 5

1 8

4 15

(>0) + ( >3)

0 8

(: > 0) + ( > 3) + ( > 8)

0 15

>0

**
. ( \

11.1.

203

11.2.

, , XQ.
, 11.5,
.
11.5.
int icbrt(unsigned x)

int s;
unsigned , b;

s = 30;
= 0;

while (s >= 0) { // 11
- 2*y;
b = (3*y*(y + 1 ) + 1 ) S;
S = S - 3;

if (x >= b) {
X = X - b;

= + 1;
return ;

}
1 1,
. , - *( + 1).
. 2, 2
. =
2 = , = 2* 2 = 4*2. b = (3* (2+) +i)s, = +1
2 = 2+2*+1. , , .
1, . ,
( ).
: [19] , 64- . b . , s , b bs = bs
if (x>=b) {x=x-b... if (x>=bs&&b== (bss)) {x=x-bs....

204

11.

11.3.
"
", , . x'x-x'...-, , , , ( ) (
('ab')3='ababab').
=. 13 1 101 (.. 13 8+4+1),
" *4*1 . **.
, *13 , .

, , .
,
, 11.6.

11.6. "
int iexp(int x, unsigned n)
{
int p, y, = I,- //
p = ; //
while (1) {
if (n & 1)
= P*Y;
// n ,
n=nl;
//
if (n == 0)
return ;
//
= *;
//

,
S 1 ,
Llog,nJ + nbits(n)-l.
. , =27

1 6 -* 8 -* 2 -* 1 ,
.

')'.
11.3.

205

. , , 15 (: " = (3) ).
, , . . [39, 4.6.3].
, [53, 32], .
1 .
, ;
. , '3 = '""! :

, 1 1.6.

2" Fortran
IBM XL Fortran :
31

-2 , /1 = 31,
, < 0 S 32.
, ,
. ANSI/ISO Fortran , <0. > 31 , 232, ,
.
2" 1 . Fortran,
64 32 ( 32-
),
.
, pow2(/i) :

( "
- nlzl 5 ; // - 32 0 3 1 ; < 32
<_ ^5 ;
//*-! 0 31; jc =
pow2 < ;

,
.
nlz,
= 0 (. 2.11), 5 31. , 206

1 1 .

0 < : < 31

<32 2. 11
- & (; - 32) . (, -).
< &(-32); // < 0 , 0<^31

x-jc3>31; II = 1 0<<31,
pow2 < < ;

11.4.
" " |_logb \ , , , 2. =2 10; ilog2 iloglO .
=0, ilog(0) = -l [7].
.

i\og2(x)
nlz(x), ,
,

ilog2(*) + nlz(jt) = 31.

[log(jc)] ,
[log (jc)~| = ilog (*-!) + ! , , ilog(0) = -l .
*=1 ( =0)

|_log,0*j = |_(log 10 2)log 2 jcj.

ilog(x) ( -1 31 ilog2(jc) 32 ), .


ilog2(jc) iloglO(x) .

, "
", ilog(jt) + l , 0. -0 .
ilog(jt) .
, .

11.4.

207

2
ilog2(x), , ,
, 5.3.
ilog2(x) nlz(jc) 31 ( 5.10 return (-) return pop(x)-l.

10

.
10, . ,
, , .
iloglO(jc) . , , , , . 11.7 .
11.7. 10,

int iloglO(unsigned x)
{
int i,static unsigned table[11] =
{0, 9, 99, 999, 9999,
99999, 999999, 9999999, 99999999, 999999999,
OxFFPFFFFF

for (i = -1; ; i++) {

if (x <= table [i+1]) return i;

RISC-
9 + 4[loglo x\ , 45.
11.7 . 11.8.
10.
11.8. 10,
10

= 1;
for (i = -1; i <= 8; i++) {
if (x < p) return i;

p = 10*p;
return i,-

208

11.

10+6[log,0;eJ
RISC- ( ). 10
99 16 .
, . : 104, 102 10 ..,
, , 10" < < 10"+|. 10 18 ,
( ).
, 11.9,
, .
RISC- 10 <, < 99 11 16 S100.

11.9. 10,

int iloglO(unsigned x)
{
if (x > 99)
if (x < 1000000)
if ( < 10000)
return 3 + ((int)(x - 1000) 31);
else
return 5 + ((int)(x - 100000) 31);
else
if (x < 100000000)
return 7 -i- ((int)(x - 10000000) 31);
else
return 9 + ((int)((x-1000000000)&~x) 31);
else
if (x > 9) return 1;
else return ( ( i n t ) ( x - 1) 31);
}
( (int)). ,
,
. return.
, , . ( ),
return, . 231 -1000.
return 3 - (( - 1000) 31);
return 2 + ( (999 - ) 31);
return 2 + ( (X -I- 2147482648) 31) ;
return :
return 8 + ( ( X + 1147483648) I ) 31;

11.4.

209

2" -10'.
- .
if-else .
return ((int)(x - 1) 31) | ((unsigned)(9 - ) 31);
return ( > 9) + ( > 0) - 1;
.
nlz(x) ilog2(x),
iloglO(;t) . , , 11.10 [7].

11.10. 10
2,
int iloglO(unsigned x)
{
int ;

static unsigned char tablel[33] = {


9, 9, 9, 8, 8, 8,

};

7, 7, 7, 6, 6, 6, 6, 5, 5, 5, 4, 4, 4, 3, 3, 3, 3,
2, 2, 2, 1, 1, 1, , , ,

static unsigned table2[10] = {


1, 10, 100, 1000, 10000,
100000, 1000000, 10000000, 100000000, 1000000000

};

= tablel[nlz(x)];

if (x < table2[y]) = - 1;
return ;

tablel iloglO(jt). , ( 0 8 9,
64 99, 512 9999, 8192 9999 ..) 1.
, table2.
73 , System/370 [7] ( , tablel
). RISC- . , , .
, if. , ,
11.10,
, .
if 31 , .
3
*22 '+10'; table2 (. 11.11).

210

11.

11.11. 10
2,

int iloglO(unsigned x)
{
int ;
static unsigned char tablel[33] = {
10, 9, 9, 8, , B,

};

1, 7, 7, 6, 6, 6, 6, 5, 5, 5, 4, 4, 4, 3, 3, 3, 3,
2, 2, 2, 1, 1, 1, , , ,

static unsigned table2[11] = {


1, 10, 100, 1000, 10000,
100000, 1000000, 10000000, 100000000, 1000000000,

};

= tablel[nlz(x)];
= - ( ( x - t a b l e 2 t y ] ) 31);
return y;
}

11 RISC-, ,
"" . , =0 -1, 0 (
tablei 1, .. ", , , " ", , , 1").
,
. , log,0jc Iog 2 x
loglo x = log,0 2 log, x, Iog10 2 = 0.30103 . , iloglO(x) [cilog2(x)J ( , 11.11
table2).
. , log,02 = c+E,
> 0 log,0 2 , , > 0. >
iloglO(jc) = [log,0 JtJ = [( + e)log2 *J
\_c Iog2 *J S iloglO(jr) = [c Iog2 x + e Iog2 x j
[c ilog2(*)J S iloglO(jt) < [c(ilog2(jc) +1) + e log, x\ <
< \c ilog2 (x) + c + e Iog2 xj <
<[cilog2(x)J + Lc + elog2.*J + l.
, , + e Iog2 : < 1, [ ilog2(x)J
iloglO(jt) , 0 +1. , ,
ilog2(0) = iloglO(O) = -1, [ ilog2(0)J = iloglO(O), 0 < < 1, - (,
, - ,
ilog2(0) = iloglO(O) = 0).
11.4.

211

= log,,, 2-, ,
c + (log, 0 2-c)log 2 ;t<l
c(log, -1) > (log,0 2)log2 x -1.
= 1 ( < 1) 2.

,
. 32- < 232 ,
^030103.32-1,
32-1
>0, <0.30103,
= 9/32 = 0.28125 . , 5/16 1/4
.
, 11.12, , 1.
1 1 RISC-,
( ).
11.12. 10
2,
static unsigned table2 [10] = {
, 9, 99, 999, 9999,
99999, 999999, 9999999, 99999999, 999999999

};

= (9* (31 - nlz(x))) 5;


if ( > table2[y+l]) = + 1;
return ;
,
> 231 + 10' ,
. , 19/64 ,
. 11.13.
1 1 RISC-, ( ).
11.13. 10
2, ,

int ilogio (unsigned x)
{
int ;
static unsigned table2 [11] = {
0, 9, 99, 999, 9999,
99999, 999999, 9999999, 99999999, 999999999,
OxFFFFFFFF

};

= (19* (31 - nlz(x))) 6;

212

11.

= + ((table2[+1] - ) 31);
return ,-

}
S 2 3 '. , 11.12 :
= + (((table2[+1] - )

| ) 31);

, 19
9 ( ).
64- ,
^0.30103.64-1^^
64-1
19/64 = 0.296875 , ,
. :
unsigned table2 [20] = {, 9, 99, 999, 9999, . . . ,
9999999999999999999};
= ((19*(63 - n l z ( x ) ) ;
= + ((table2[+1] - ) 63;
return ;

11.4.

213

12



.
,
. , , (
) .

12.1. -2
-2
, , ,
[40]. 0 1, 2. ,
, ,

,
-2 -2, . ,
0 1 ( ), .. .
, -3 -2.
= 2reml
-2
= -lremO
= 1 rem 1
-2

=0reml
2

, (
0). ,
, , -3,
-2, 1 101.
. 12.1 ,
0000 1111 -2,
-15 +15.

12.1. -2

( -2)
0
1
10

()
0

-2
-1
4
5
2
3
-8
-7
-10
-9
-4
-3
-6
-5

11

100
101

111

1000
1001
1010
1011
1100
1101
1110

1111

()
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

( -2)
0
1

111
100
101
11010
11011
11000
11001

11111

11100
11101
10010
10011

( -2)
0
11
10
1101
1100

1111

1110
1001
1000
1011
1010
110101
110100
110111
110110
110001

, 2" ,
. , n-

-(2" +1 -2)/(2"-1)/3 ,

(1,)

-(2"-2)/(2" + 1 -1)/3 .

(1,6)

, . =2 10,
11, 00 01 -2, .. -2, -1,0, 1. (1,), .
+1 0 ,
(1,). 1,
, (-2)" = 2".
2" -(2"+| -2)/3 2" +(2" -1)/3

(2"-1)/3+1(2" +2 -1)/3.
(1,), +1

-(2" +1 -2)/(2" +2 -1)/3.
(1,6), + 1.
, (1,) (1,6)
, .

216

12.

: 0 + 1 = 1 1-1 = 0. 2 110, -1 11, , .


1+1=110
11 + 1 = 0
1+1+1=111
0-1=11
11-1=10
. .
( ) 11 + 1 = 0. 11
, 0, 1 1 .
.

11

1 11

11

1 11

1 0 1 1 1

1
0

1
1

0
0

1 9

1 +(-11)
0

1 0 1 0 1

- 1

1 0 0 1 1 1 1

2 1

0 -(-38)
5 9

: , 1 11.
, (1 11) .
, .
, 2
.
. ,
(.. -1), 0.
, " 1",
.

( - + + 1). 2 .
-2 . : 1x1 = 1 0 0; -2.
, , . , ..
, . 12.1 , 8- . ( ).

12.1. -2
int divbm2(int n, int d)
{
int r, dw, c, q, i;
r = n;
dw = (-128)*d;

= (-43)*d;

12.1. -2

// q = n/d -2
//
// d

//
217

if (d > 0) = + d;
q = 0;
//
for (i = 7; i >= 0,- i--) {
A

if (d > 0 (i&l) == r >= c) {


q = q | (1 i);//
= - dw;
// d
}
dw = dw/(-2);
// d
if (d > 0) = - 2*d; //
else = + d;
//
= /(-2);

return q;

//
// -2
// , 0 <= < |d|


, .
d , q,
- . q , -2.
.
-2, . , "if (d>o)" ,
d . " = c+d"
-2. . , , d .
,
, . -2,
, -128 , -2 .
:
divbm2(6,2) = 7

(, 2, 111.,)

divbm2(^,3) = 2 ( , 3, 10_2)
divbm2(-4,-3) = 6 ( , , 110_2)
q = q | ( i i ) ; /- q.
= r-dw,-
d.
, .
, 7 q. , -2, 8- , 1,
-170 -43. , , () 1 ( ), 43.
n = qd + r < d -1,
1 , 218

12.

n S -43d + (d - 1) < -43d + d . 1 , n > -43d ( > 0 ).


, 1 ,

d,
d >>,
c = -43d+d d c = -43d d .
.
. - if
(i&l) ==0 (, * ).
(
0) i d. , i q 1, , 1 ,
d. d
, .
. -
, , ( ), . "" , .. , , -2, -, .
, , , .
.

, . ,
( ), , (
). , .
,
. ,
, , , (
).
, , , , ,
d>0 . . .
,
.
-2

12.1. -2

219

. "" ,
, .
,
. . 12.2 , .

12.2.

-2

1 " ". ,
, , () .
(.. ).
2 "" ,
.
-2 ,
.
-2 , , ,
, ,
. , , , ,
.
-2
( 2", ),

-2. .

-2
110111 (-13)
- 1 1
(
)
...111110011 (-13)

220


-2
110111 (55)
+ 1 0 1
(
-2)
1001011 (55)

12.

, ,
, . -9 ( 6 ) -2.

-2 ,

-2. (Schroeppel) [25, item 128] ,
.
:

<r- (N eobio...ioio)-obio...ioio.
, , , -2 abed. , () , Sa + 4b + 2c + d . , , 8(1 - ) + 4 + 2(1 - ) + d .
() -8 + 4 - 2 + d , abed -2.
, , .
32- .
B<-(N& 0x55555555) -(N & -55555555),
<- N -((N )l),
<- ( OxA A A A A A A A ) - .

-2 :
N<-(B + OxAAAAAAAA) 0 .

12.2. -1+i
-1 + i , i >/-!,
(.. , ) ""
. ,
0 1,
. ,
, .
,
2 1. 2 .
? , -1+j
1

, .

12.2 -1+i

221

0 1, (, 0 1). , , , +bi -1+/. q ,


q , 0 1

qr <7/ q .
,

b-i
2

-- +
,,,.__.

, , =0 q. , ,
q = 1.
, 2 -1+ ,
.
2 ,
, , 0:

-l-i , 0:

ziz/ (-1-/X-1-/)
-! + ' (-1+ /)(-!-

<0

/
, 1. , 1 :

-1 + 1

= 1v ( 1).

1 , 1. ,

-1 + 1

= 0 ( 1).

, .
, , 2
-1 + / 1100.
. 12.3 0000 1111 -1+/,
-15 +15.

222

12.

12.3.
-1+/

(
-1 + 0
0
1
10
11
100
101

111

1000
1001
1010
1011
1100
1101
1110

1111

()

n
()
0
1

-1 + /

i
-2i
l-2i
-1-/
/
2 + 2/
3 + 2/
1+3/
2 + 3/
2
3
1+/
2+/

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

n
(
-1 + 0
0
1
1100
1101
111010000
111010001
111011100
111011101
111000000
111000001
111001100
111001101
100010000
100010001
100011100
100011101

-n
(
-1 +
0
11101
11100
10001
10000
11001101
11001100
11000001
11000000
11011101
11011100
11010001
11010000
1110100001101
1110100001100
1110100000001

-1 + i (
) :

1+1 =
1+1+1 =
1+1+1+1 =
1+1+1+1+1 =
1+1+1+1+1+1 =
1+1+1+1+1+1+1 =
1+1+1+1+1+1+1+1 =

1100
1101
111010000
111010001
111011100
111011101
111000000

, , , , , 8(111000000). . -
, , ,
2, , 2 .

12.3.
-1-i , , , -1 + i , . 2

1940 Bell Labs Complex Number Calculator


(George Stibitz) [35].
12.3.

223

13

13.1.
2"
? ""; (Gray codes). ,
, , ,
. , ,
.
,
: " () ".
, " ". ( , ), ,
.
.
1, 0 1.


1 , 0.
00
01
11
10

=2. =3,

000
001
011
010

111
101
100
, , -, 2" , ; -,
;
-,
. , , ,
.

> 2 , 2" , . : 000 001


011010110100101 111.
. 13.1 , .
" ", .

abed
0000
0001
0010

0100
0101

0111
1000
1001
1010
1011
1100
1101
1110

efgh
0000
0001


=

=
0010
f = ab
b = ef
c=efg

g=bc
d=efgh
0111
h = cd
0101
0100
1100
1101
1111
1110
1010
1011
1001
1111
1000
. 13.1.

, , .
, ,
. , , , 2" !
( ).
,
. 13.1 .

i i ( i , 0).

i i
.

228

13.

5.2. , , ( =32).

=G*
=*
=*
=*
=

(G 1) ;
( 2 ) ;
( 4) ;
( 8) ;
( 16) ;

, 2 [log, ] .
, :
for (i = 0; i < n; 1++)
G = i (i 1) ;
OUtpUt G;

13.2.
4- abed , :

d' = d
c' = cd
b' = bcd
= a bed
,

, ' , d' , . 13.1.
, :

p=efgh
h' = hp
g' = ghp
e' = efghp
,

0 1,
.
G'
G , , G ,
. , G.
, ,
G :
1 2 1 4 1 2 1 8 1 2 1 4 1 2 1 16

13.2.

229

,
, 0, 1,2,
3, ..., . ,
G
, 1 , G.
, 13.1 ( G , index (G)).

13.1.
= index(G) ;

= +
1;

Gp = ( 1) ;

= index(G);

= - &A ( + 1) ;
Gp = G M;

" " .
, , . .
, , .
G. , .
, .
.

13.3. -
-2
, , .
2, -2 5; -10 5.
, .
, 0 1, , .., .

-2 , , ,
( , , , ).

13.4.
(Frank Gray), Bell
Telephone Laboratories, 1930- , ,
- ; .. - .

230

13.

(Martin Gardner) [15] , , , . ,


.
. , , . . , ,
, , ( , ).
, . 13.2 ( );
. , , .

. 13.2.

13.4.

231

14

1890 (Giuseppe Peano) 1 " ".
(, ) .
, .
.
, ,
. ,
3; .
1891 (David Hilbert) [28] , ,
. ,
, ..
, .
( -)
, . , 2.
. 14.1 , , , 1891 .

. 14.1. ,

. " "
,
" n- " n- ( . 14.1 , ).
2" , . ,
0 2" -1 .
(, ) = (0,0) (2"-1,). . 14.2
.

-.

. 14.2. 1-6

234

14.

14.1.
, , . 14.2. 1 ,
, . 2 . U- , , ,
U- , .
, U- ,
U- , . U-
Y- .
U- , , ,
. U-
Y- ,
.
( U
) .
1. U .
2.

, .

3.

U .

4.

, .

5.

U .

6.

, .

7. U .
, , U,
, , . U-
. , 14.1 [58].
U ,
:
dir = 0 :
dir = 1 :
dir = 2 :
dir = 3 :

rot = +1 :
rot = -1 :

14.1.
void step(int) ,void hilbert(int dir, int rot, int order)
if (order == 0) return;

dir = dir + rot ;


14.1.

235

hilbert(dir, -rot, order - 1);


step(dir);
dir = dir - rot;
hilbert(dir, rot, order - 1);
step(dir);
hilbert(dir, rot, order - 1);
dir = dir - rot;
step(dir);
hilbert(dir, -rot, order - 1);

}
dir , 0-3, dir 4.
14.2 - step,
hilbert.
, , . , :
0000 00 00

0 0001 01 00
1 0010 01 01
2 00 01
1 0100 00 10
1 0101 00 11
01 11
-1 0111 01 10
0 1000 10 10
1 1001 10 11
1010 11 11
-1 1011 11 10
-1 1100 11 01
-2 1101 10 01
-1 1110 10 00
1111 11 00

14.2. -
#include <stdio.h>
#include <stdlib.h>
int x = -1, = 0; //
int i = 0;
//
int blen;
//
void hilbert(int dir, int rot, int order);
void binary(unsigned k, int len, char *s)
{
/* k
. s len.
*/
int i,s[len] = 0;

236

14.

for (i = len - 1; i >=


if (k & 1} s[i] =
else s[i] = ' 0';
k = k 1;

1") {

void step(int dir)


char ii[33], xx[17], yy[17] ;
switch (dir & 3) {
case 0: x = x + 1; break;
case 1: = + 1; break;
case 2: x = x - 1; break;
case 3: = - 1; break;
binary (i, 2*blen, ii) ,binary(x, blen, xx) ;
binary (y, blen, yy) ,printf("%5d %s %s %s\n", dir, ii, xx, yy);
i = i + 1; //
int main(int argc, char *argv[]) {
int order;
order = atoi (argv[l]) ,
blen = order;
step(O); //
hilbert(0, 1, order);
return 0;

14.2.

(,) , s , , 2- s ,
. , . s 00,
- ; 01 ,
10 , 11 . , s - , .
s

(,)

00
01
10
11

(0,0)
(0,1)

(1,1)
(1,0)


U- . . 14.1, s .
14.2. ...

237

14.1.

00 - (0, 0)
01 -> (0, 1)
10 -+(1,1)
- (1,0)

00 - (0, 0)
01-* (1,0)
10 -+(1,1)
- (0,1)

00-(1, 1)
01 -> (1, 0)
10 - (0, 0)
->(0, 1)

00 -(!,!)
01 - (0, 1)
10 - (0, 0)
11- (10)

, . 14.2 U- , ( ) U- , , , D ( , 1, 2 3). , U- ,
( ), , , ( 0, 1,2
3). . 14.2, , , . 14.1.

14.2. (,) s


() s


()

00

(0,0)

01

(0,1)

10
11

(1,1)
(1,0)

00

(0,0)

01

(1,0)

10

11

(1,1)
(0,1)

00

(1.1)

01
10

(1,0)
(0,0)
(0,1)

11

00

(1,1)

01
10

(0,1)
(0,0)

D
D

11

(1,0)

. s
, 2
, . s
. . 14.2 ,
s 00, (0,0) -

238

14.

, s. , . 14.2 , ,
i 01, (0,1) .
. s , - .
, =3 s= 110100.
11, (1,0) D ( . 14.2). , D
s, 01, 14- . 14.2
(0,1) D. , 00
(1,1) (, s , ).
, (101,011), .. *=5 =3.
14.3.
0-3, A-D. row
s,
15, . 14.2. row ,
. 14.2, .., , .
. 14.2,
.

14.3. s
void hil_xy_from_s(unsigned s,
int n,
unsigned *xp, unsigned *yp)

int i;
unsigned state, , , row,state = 0;

//

x = = 0;
for (i = 2*n - 2; i >= 0; i -= 2)

row = 4*state | (s i) & 3;


= ( 1)
(0x9360 row)
= ( 1)
( row)
state =
(6941 2*row) & 3;

* = ;
* = ;

//
//
& 1;
& 1;
//

//
//

}
[44] . 14.3, s . , s (,),
, s. 00, (,) ( = (. . 14.1, ). 01 10, .
14.2. ...

239

, 11,
. s. . 14.3, 14.4. ,
, & , ; ,
.

14.3. (,) s [44]



s


(,)

00

(0,0)

01

(0,1)

10

(1,1)

11

(1,0)

14.4. (,) s [44]


void hil_xy_from_s(unsigned s, int n,
unsigned *xp, unsigned *yp)

int i, sa, sb;


unsigned x, y, temp;
for (i = 0; i < 2*n; i += 2)

sa = (s (i+1) ) & 1;
sb = (s i) & 1;
if ( (sa A sb) == 0)

temp = x;
x = (-sa);
= tempA(-sa) ,-

i+1 s
i
s
sa,sb = 00 11,

, sa = 1,

x = (x 1) |

(sa 31);

= (y i) I
((sa * sb)
*xp
*yp

X (32 - n) ;
(32 - n) ;

sa

31);

(sa*sb) ()

14.4 , . , .
14.4 , 2.19 " ". if
( swap cmpl ):

240

14.

swap = (sa sb) - 1;


// , -1;
cmpl = -( & sb);
// , -1;

X = X ;
=
( & swap)

= ;

cmpl;

, , , , , .
" " [44]
. , , ,
, - , j
(,) . 14.1. , , , . ,
. 14.3, ,
s (,,,)
.
s 2-1
I

S 2 . ! S2i

U2n-2

-*.

.-.

0>

-..

0>

1
^o

ci+i

i c n-l -1

11

*i

2*4-1

-^

c,

...

-*

/T\ / 1
\ 1 /Tv /^
1^21^1 + 1 / 1 ^^ l'4-l

[52,.^,1(^5)]-+.

c=
. 14.3. (,) s
, , s .
s, ,
. 14.4. s , , s ,
. , , -ro , 2 ( ) ( 5 ( , ) ) .

14.2. ...

241

14.4. (,) s
()
s


(,)

00

(0, 0)*

swap = swap

01

(0, 1)*

10

, D*

(1, 0)*

swap = swap , cmpl = cmpl

11
* ,

. 14.3 (S
, ).
. 14.3 (,) 5. , . , ( -1, Iog2 n ) ,
. 14.3 .
, ,
7.2.
,
, .
, , 14.5 [19].
, , s ' (
). cs (complement-swap, -). CSCS...GS,
, 1, , ,
s . 14.3.
, s cs :
*2-1

*2,

0
0

1
1

cs
01
00
00

11

,
, PP-XOR. , , , , : .

14.5. (,)
s
void hil_xy_from_s(unsigned s, int n,
unsigned *xp, unsigned *yp)

242

14.

unsigned comp, swap, cs, t, sr;


s = s | (0x55555555 2*n); // s
sr = (s 1) & 0x55555555; // 01 ( )
cs = ((s & 0x55555555) + sr) //

0x55555555;
//
// PP-XOR
// , ( cs = cs 1
// ),
//
// 16
cs = cs *" (cs 2) ;
cs = cs (cs 4);
cs = cs (cs 8);
cs = csA (cs 16) ;
swap = cs & 0x55555555;
//
comp = (cs 1) & 0x55555555; //
t = (s & swap) comp,//
s = s A sr * t (t 1) ;
//
//
s = s & ((1 2*n) - 1);
//
//
t = (s ( 1)) & 0x22222222; s = s * t A (t 1);
t = (s (s 2)) & OxOCOCOCOC; s = s * t * (t 2) ;
t = (s * (s 4)) & OxOOFOOOFO; s = s * t A (t 4);
t = (s " (s 8)) & OxOOOOFFOO; s = S A t * (t 8);
*xp = s 16;
//
*yp = s & OxFFFF; //

}
( )
PP-XOR.
s (,),
, () , . , , s
. 14.3 (:
, , t sr 0).
. 66
RISC- ( ), 14.2 19 + 10 ( ). , > 3 .

14.3.

,
,
. 14.2. . 14.5.

14.3. ...

243

14.5. s
(,)


(,)

(0,0)

00

(0,1)
(1,0)

01

(1,1)

11
10

(0,0)

00

(0,1)
(1,0)

11

01

(1,1)

10

(0,0)

10

(0,1)
(1,0)

01

11

(1,1)

(0,0)

10

D
D

(0,1)
(1,0)

01

11

00

D
D

(1,1)

00

. , , ,
, . , s .
, ,
14.6.
14.6. s (,)
unsigned hil_s_from_xy(unsigned x,
unsigned , int n)

int i;
unsigned state, s, row;
state = 0;

//

S = 0;

for ( i = n - l ; i > = 0,- i--) {


row = 4*state | 2*((x i) & 1) | (y i) & i;
s = (s 2)
(OX361E9CB4 2*row) & 3,state = (Ox8FE65831 2*row) & 3;
return s;

244

14.

[44] s (,), (,) s. . 14.6 14.7.

14.6. s (,) [44]



(,)

(0,0)
(0,1)
(1,0)
(1,1)

00

01

11

10

14.7. s (,) [44]


unsigned hil_s_from_xy(unsigned x,
unsigned , int n)

int i, xi, yi,unsigned s, temp,s = 0;


for (i = n - 1; i >= 0; i-)
xi = (x i) & 1;
yi = (y i) & 1;
if (yi == 0) {
temp = x;
x = yA(-xi);
= temp*(-xi);

//
// i-
// i-
// ,
// xi = 1,
//

4*s + 2*xi + (xi*yi); // s


return s;

14.4.
(:, ) - .
? , , s, s 1 (:, ) .
( ) ,
, , , 1. ,
, U-
.
: .
, ,
U- .
14.4.

245

U-
, . U-
,
U- , ..
. 14.7, , , D U-
, . 14.1.
, -
( ). , . . 14.7 , , (0,0), -, , .
; "" ,
.
, -,
, .
( ) -.

14.7.


(,)

(0,0)

(0,1)

+
+

(1,0)

X-

(1,1)
(0,0)
(0,1)
(1,0)

(1,1)

X-

(1,1)

(0,0)

(0,1)
(1,0)

X-

(0,0)

(0,1)
(1,0)

D
D

(1,1)

, , 14.8. dx ,
.

246

14.

14.8.
void hil_inc_xy(unsigned *xp, unsigned *yp, int n)

int i,unsigned x, y, state, dx, dy, row, dochange;

x = *xp;
= *;

state = 0 ;
//
dx = -((1 n) - 1) ; // -(2**n - 1)
dy = 0;

for (i = n-1; i >= 0; i--) { // n


row = 4*state | 2*((x i) & 1) | (y i) & 1;
dochange = (OxBDDB row) & 1;
if (dochange) {
dx = ((0x16451659 2*row) & 3) - 1;
dy = ((0x51166516 2*row) & 3) - 1;
state = (OX8FE65831 2*row) & 3;

*xp = *xp + dx;


*yp = *yp + dy;
. 14.7 . 14.4 .
.
Xi
yf
X, Y
/
W
S

j-
i-
S,+i
CHI , yt
: 1 1, 0 1
: 1, jc,
0
S 1, xt ,
1, :, ,

S . 14.7: (5,), (0,0), (0,1),


(1,0) (1,1), , , D. /0
Wo ,
. ( / , , ;
, , , . /.)

14.4.

247

i i

-1

. 14.4.

14.5.
, . 14.2 14.7, .
.
. 14.2 s, (,). , . 14.7,
s, .

14.6. ,
, , 1890 ,
. ,
" ". 1900 (Eliakim Hastings Moore)
. "" ,
. . 14.5
. ,
"--" (),
U- .
.

248

14.

LTLJU
W]
1 1
I

"UU1

mum /
. 14.5. () ()

.
. 2x2x2. , , [55].

14.7.
, , , [44].

. ,

. , , ( ) . (, ,
.) ,
,
. , , .
, ,
( ) .

14.7.

249

, , . .
(Douglas Voorhies) [58] ,
.
. ,
, . , ,
, . ,
, .
1024x1024.
, , ,
. ,
D D .
, 2.7
.
, 1.4. , , .

250

14.

15

;

.

(Leopold Kronecker)

. IEEE Standard for Binary Floating-Point Arithmetic, Std. 754-1985, "IEEE-".
NaN (not a number, ) , . ,
, IEEE "".
"" , "" "" .
, . , ,
, , , .
, IEEE,
.

15.1. IEEE
(32 64 ),
IEEE 754. " "
" " , ,
(,
). .


j
1

e
8

f
23

11

52

0 1 .
/ , . , .

0
2-'(0./)

2'-|27(1./)

0
*0

NaN

0
0

/
0
*0

1 254

255
255

0
0

/
0
*0

2-1022(0./)

1 2046

2'-1<(1./)

2047
2047

00

9*0

NaN

00

. [38],
11.001001000011 1111 1010 1000 1000 10000101 10100011 0000 10...
"" ,
. ,
. , -127
1, = 128. ,
10000000 10010010000111111011011
:
40490FDB,
.
, 1 <, 254, "". . = 0 "",
.
" " (gradual underflow). . 15.1 . "
" , , ,
,
; .

15.1.

-149

0000 0001

007F FFFF

0080 0000

1.0

252

1.40145

2-'* (i_2-)

1. 175x10-"

2-126

1.17510"38

3F80 0000

480 0000

2"

1.677x10'

15.

. 15.1

7F7F FFFF

7F80 0000

2128(1-2-24)

3.403x1 038

0. . .0001

O O O F . . .F

0010. . .0

1.0

2~74

4.94110'324

2-1023(l-2-52)

2.225x1 0'308

2-1022

2.225x1 0"308

3FFO. . .0

4340. . .0

9.007 15

7EEF. . .F

21024(l-2-53)

1.79810308

7FFO. . .0

53

1/224 1/223 ( 5.9610"8 1.19x10"')


1/253 1/252 ( 1.11~16 2.22x10"16)
. " " .
, , -224 +224 (.. -16777216 +16777216) -253
+253 ( -9007199254740992 +9007199254740992) . , ,
2;
, .
()
, . 2 -127 127 -1023 1023 . , 2"127 2~1023 , , .

15.1.

253

15.2.

- ,
NaN- , .
, "". 754 ,
NaN.
NaN , .
, , -0.0
+0.0 ( 754).
<, < =,
= ,
0.0, :
/

'
(
\III (a<0&a>*
" ,
a<b~\aZO&a<b
<0&>
-0.0 +0.0,
( ,
).
a=bs(a=b)\(-a=a&-b = b)
= ( = ) | (( | ) = 0x80000000)

= ( = ) | ((( | ) & OxTFFFFFFF) = 0)

= > 0&e<*Va < 0&>* We |ftl = 8000(

,
. , ,
, , |~nlog2n~| ( ).
. 15.2 . -0.0 +0.0, -0.0 < +0.0 .
. , t
, , .

254

15.

15.2.
-0.0 = +0.0 (IEEE)

-0.0 < +0.0 (He-IEEE)

if (n >= 0) = +;
else n = -n;

if (n >= 0) n = +80000000;
else n = ~n;

= OX7FFFFFFF;

= OX7FFFFFFF;

if (n < 0) n = n ;

= 0x80000000;
if (n < 0) n = - n;

= OX7FFFFFFF;
if (n < 0) n = - n;

t = n 31;
n = (n (t 1 - t;

t = (unsigned) (n30) 1;

n = n t;

if ( < 0) = ( * ) + 1;

,
RISC- ,
, ( ).

15.3.
IBM 1964 System/360, . ,
704-709-7090, 36- .
9- , 27 . ,
27 .
System/360 32- .
IBM 8- 24- . 27
24 , , .
,
7- 16. , ,
, 0001 1111 ( 1 15). , 1, , 21 ( ), 1/15, 6.7% .
, . ,
, .
15.3.

255

. ,
, 23
, , .., "" (, 6.02210 ).
,
, ,
.. , , . , 2, ,
( 1.0 1.999..., 10)
, 2 3 ( 2.0 3.999. . ., 10), ,
, ..
/(*), 1<<10, . ,

,
. (.
):

+&

10

/(1)- 1
1 + ( 10), a f(x)-x&x
+ .
, , , , :
/(*) = /()/*
=\ :=10 1 (
1 .000. . . 9.999. . .), ,
.(1<<<10) ,

256

15.

, 1
Iog10(2/l) = 30.103% , Iog10 (10/9) = 4.58%.
, 1<<<16, log,6(/a). ,
log,6 (2/l) = I/log, 16 = 0.25.

1,

15.4.
. 15.3 IEEE- , .
.

15.3. IEEE-

( )

-2.0
-1.0
-0.5
-0.0
+0.0

/180 (0.01745...)
0.1
Iog102 (0.3010...)

FF80

BF80
BFOO
8000
0000

0000
0000
0000
0000
0000
0000


(
)
FFFO 0000
0000
BFFO 0000
BFEO 0000
8000 0000
0000 0000

0000
0000
0000
0000
0000
0000

0000
0000
0000
0000
0000
0000

0000 0001

0000 0000 0000 0001

007F FFFF

OOOF FFFF FFFF FFFF

0080 0000

0010 0000 0000 0000

8 5
3DCC CCCD
9 209

3F91 BF46 252 9D39


3FB9 9999 9999 999
3FD3 4413 509F 79FF

1/ (0.3678...)

52

3FD7 856 362 EF38

1/lnlO (0.4342...)

5BD9
3FOO 0000
3F31 7218
3F35 04F3

3FBB 7 1526 50
3FEO 0000 0000 0000
3FE6 242 FEFA 39EF
3FE6 A09E 667F 3BCD

3F69
3F80
3F8C
3FB5

20
3FFO 0000
3FF1 93
3FF6 09

0.5
In 2 (0.6931...)
1/>/2 (0.7071...)
1/1 (0.9102...)

1.0
1 (1.0986...)
>/2 (1.414...)
1/12 (1.442...)
/3 (1.732...)
15.4.

0570
0000
9F54
04F3

153
0000 0000
7AAD 030
667F 3BCD

3FB8

3FF7 1547 652 82FE

3FDD B3D7

3FFB 67 858 4

257

. 15.3

2.0
InlO (2.302...)
(2.718...)
3.0
(3.141...)
(3.162...)
logjlO (3.321...)
4.0
5.0
6.0
2 (6.283...)
7.0
8.0
9.0
10.0
11.0
12.0
13.0
14.0
15.0
16.0
180/ (57.295...)
2 -1
2
2 -1
2"
31
2 -1
31
2
2-1
32
2
52
2
2
2"

00

" " SNaN


" " SNaN
" " QNaN
" " QNaN

258

( )


(
)

0000
5D8E
F854
0000
OFDB
622

4000
4002
4005
4008
4009
4009

0000
61
BFOA
0000
21FB
458

0000
5
8B14
0000
5444
3ADA

0000
5516
5769
0000
2D18
553

4054 978
4080 0000
40 0000
40 0000
409 OFDB
40 0000
4100 0000
4110 0000
4120 0000
4130 0000
4140 0000
4150 0000
4160 0000
4170 0000
4180 0000
4265 21
4AFF FFFE
4 0000
4B7F FFFF
4B80 0000
4FOO 0000
4FOO 0000
4F80 0000
4F80 0000
5980 0000
5FOO 0000
5F80 0000

400
4010
4014
4018
4019
401
4020
4022
4024
4026
4028
402
402
402
4030
404
415F
4160
416F
4170
41DF
41EO
41EF
41FO
4330
43EO
43FO

934F
0000
0000
0000
21FB
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
A5DC
FFFF
0000
FFFF
0000
FFFF
0000
FFFF
0000
0000
0000
0000

0979
0000
0000
0000
5444
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
163
COOO
0000
EOOO
0000
FFCO
0000
FFEO
0000
0000
0000
0000

371
0000
0000
0000
2D18
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
C1F8
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000

7F7F FFFF

7FEF FFFF FFFF FFFF

7F80
7F80
7FBF
7FCO
7FFF

7FFO
7FFO
7FF7
7FF8
7FFF

4000
4013
402D
4040
4049
404

0000
0001
FFFF
0000
FFFF

0000
0000
FFFF
0000
FFFF

0000
0000
FFFF
0000
FFFF

0000
0001
FFFF
0000
FFFF

15.

754 ,
(SNaN) (QNaN). . 15.3 , PowerPC, AMD 29050, Intel 86 I860: 0 SNaN 1 QNaN.
Compaq Alpha, HP PA-RISC MIPS ,
( QNaN, I SNaN).

15.4.

259

16

16.1.
,
. ,
"", , : , n- , ,
, . , , , ? , , .
.
1640 ,

, " ". 0 4, 1732 ,


F 5 =2 2 +1 = 641-6700417.
( ,
32- .) 1 880 ,
F6 = 22" + 1 = 274 177 67 280 421 3 10 721 .

, , 7 16 . > 4 , [33]. , ...1


? ,
, 1, 2" +1 . m = ab ,
b 1 ,

, 2"1 + 1 , , , 1, .. = 2" .
, 22" + 1 .
, "". 1772

,
[61].

, 0 39. .
2

/(-) = -+41 = /(-1),


/(-) 1 40, .. /()
-1 -40. ,
/(-40) = (-40) 2 +(-40) + 41 = 2 -79 + 1601

0 79. ( ,
=0,1
79 1601, 1523,1447, ...,43,41,41,43,.... 1447,1523,1601.)
, , ,
/(), ( /() = 5). , [33].
. /() = (,2",3,...") /() >, /()
.
2

, -2" +2 +2+5
. , ,
2
2 ", " !.
-
, (""),
= 0.203005000700011000013...

, , ,
, , ..
,
< 10". , : , 2
( 2 ) , , 10 ,
, < 10". - :

,= 10

-10" 10

2 +
2

:
6

3 = |_10 aJ -10 [_10 aj = 203005 - 203000 = 5.

262

16.

,
. ,
,
, , .
, , .

16.2.

(.. Willans) - [62]:

, ,
1 , (/? -1)! = -1 (mod p). ,
(*-!)!+!
JC

( = 1) . ,
(*-1)!+1| J1, 1,
FW

"r "-|=|,
2

(1)

, () 2 , ,
(2)

, ( ) = , ,
()< <

();> .
, m 1 >, () < , , - 1 , ..

p,=i+Z(H<n),
1=1

(3)

-,
1.
() , (3)
n- . ,
: -,
.
2

, ,
- .
16.2.

263

, 21 , , 2.
, , 2",
, .. 7i(2")Sn . , ()< m S 2 " 0,
2".
-.
= 0,1,2.. .; = 1,2,...
, <, 1/(1+), l%jy/(l+x)<tfy<2. ,
S , 0 < /(1 + ) < I , 0 < fjy/(l + x) < 1. , (""),
<,

LT(JC, ) < ( ).
(3),

(2) () F(x)
(1) F(x) , .


- :

F , . ,
mF(m) /, , 0, .
(/)=. ,
, n- . :
+8-0-1 + 9-0-1 +10-0-1 + 11-1-0+... +16-0-0 = 7.


-ro
, ""3 "" . , =2, 3,...,

264

, .
16.

((* -1)!) _ J + , - ,
[, - 1.
,

( -!)!+! : . ,
"* " > 2 :

"-

. >
sin'


() =

2...

,
, "". * < , 1:

<,

= (-1).,.(0)(-1)...(-(-1)) = 0 ,

LT(jc,)>) = sin(jt/2) = l . >, 0, 1


LT (, ) = sin((TC/2) ( )) = 0 .
, ,

, "":

= -

I-^

16.2.

265


,
pn+i /?:

/(*) "jc ", 52,..

/ () = 1 - ( ) ,
"".
= 7 .

1 + 7 + 1 + 1 -1 + 1- 1-1 + 1-1 !<)+.. .1-1-1 -0- 1-0- 1 = 11.

16.3.
(.. Wormell) [63] ,
, "".
.
. , S 2
,
2 [ , - ,
() = 1 \(-) =<
ttef
[ 0, - .
, , /,

" ".
, > 1 , >
[0, < ,

, 1- + ) =<[ , 2 .
2

, , < :

266

16.

('-)

, . , , , -1, (-1)" = cosnn .

16.4.
, . , , ,
"" "
". \, , ..., 2 1 . ... -2, -1, 0, 1, 2, ....
... -1, 0, 1, ... .
/ ( ) = *, 1 < / < .
+ , - ,
.

(), .. /(g, (*),g2 (x),...,gm ()) , /
gi i = 1 , . . . ,/.

*(*>,

().

,=(1)

(()

/(/,*) /('')

, a, b /
, ..
. .

.
16.4.

267

, .
. , ,
.
" " " ", [9]. , [9],
( ).
, [9] ,
-1 ( ) ( 0, 1).
, , .
, .
,
. , , , . , .
"" ,
, 0 1, , /, " ", 0/1. 1 , 0 , (
). .
.
1. =, ?= ..
2. = b:
<-*)'

3. (*} = \-(-).
4. > b :
(-*)

(-J.)2 ((-*)-;)-

(<**)= ((-*)=)= Z -;)i=0

i=0

j=0

, , / / 0/1. ,
"",

,
, .
268

16.

5. (>*) =
6. (a<b) =
7. (a<b) =
8. || = (2(
9.

10. mm(a,b) = (a>b)(b-a) + a.


,
.

/()

,'=.()

MT)

, .
,
.
13. ! = J][i. ! = 1 < 0 .
/=1
Q .
14. -nPJc = l - P J .

16. / > j t
18. if />(3r) then f ( y ) else g ( z ) = P ( x ) f ( y ) + (\-P(x))g(z).
19. a" = if n > 0 then fja else 0.
0 0 1 0 , .
20.

21.

V , 3 .
16.4.

269

22.
jam j=m

, , , , + 1 , () . " " . ,
, (Goodstein) [23].
23.

" ",
0 0 .
24. d\n = (-\n\<3q<>\n\}(n = qd).
"d ", 0|0, -i(0|/i)
* 0 .

25. n+d= if n S O then (-n


else ( < minq S -)(-|</| + 1 <, 3r S 0)(n = qd + r).
. d = 0 \\ + 1 .
26. rem(n,d) = n-(n+d)d .
. rem (n,d) , . d - 0 , .
27.

28. 7c(n) = ^]isprime(i) ( , ).


i*i

29.
30.
> 1 .
31. >0:

270

16.

32. n- ->/2 :

, . , , .
. / , k, ,

k.
, , 1-5 . , f(x) = ( 1), h

h . ,
,241 .....
/(:)<22"
+ 2,
max(|jc1|,...,|x,,|)>0.
f ( x ) = xt ( 2) /(^*,),...,!*,,!) , Jt=0.
3.

,
,2-N ..... W)
-

<22"

, f(x,y) = xy .
, 4 5 , , .
,

/(*) = 22"}*

(4)

, :, (4) k.
, , , (4) . ,
,
. ,
16.4.

271

.
[9].
- ,
, , .
, " " ,
"", , -1 . , 0,
0 (. = ).
, , "" . , [53]
(W.H. Mills) , 9, ,

1. (, 1.3063778838+ 1.4537508625483+). , 3: ,
(, 0). , , ,
~ ( + 1)2 . ... ,
[13, 53] .

272

16.


4-
.

2
3
4
5
6
7
8
7
6
5
4
3
2
1

8
9

0
1
0
1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9

D
D

F
F 10

2
2
3
4
5
6
7

F
10
11

3
3
4

5
6
7

8
9

F
10
11

12

.
-8 -7
7
4
5
6
8
9
4
9
5
6
7
8
5
6
7

9
6
7
2
7
8 2

2 D
8
2 D

D
F 10

D F 2 11
D

F 10 11 12

F 10 11 12 13_
F 10 11 12 11 11
10 11 12 13 11 11
11 12 13 14 15 16
12 13 14 15 li 7
13 14 15 16 17 18

-6

-5

-3
D

-2
E

-1
F

F
10
11
2
I.
11

F 10
F 10 11
10 11 12
11 12 13
12 13 14
13 14 JL5_
14 5 16
15_ 11 17
16 1Z 18
1Z 18 19
18 19 1
19 1 IB
1 1 1C

E
F
10
11
12
13
14
15
16
17
18
19
1A
IB
1C
ID

F
10
11
12
13
14
15
16
1Z
18
19
1A
IB
1C
ID
IE

14

1S_

li
17
18
19

-4

(. .2) , - + +1, .
.2. ( - )

0
1
2
3
4

5
6
7
8 8
7 9
6
5
4
3 D

2
1 F

0
10
11
12
13
14
15
16
17
18
19
1
1
1C
ID
IE
IF

1
F
10
11
12
13
14
15
16
11
18
19

1A
IB
1C
ID
IE

E
F

D
E
F
10
11
12
13
14
15
16

10
11
12
13
14

15
1_6
17

18
19

1A
IB
1C
ID

11
18
19
1A
IB
1C

D
E
F
10
11

9
A

D
E
F
10
!_!

D
E
F
10
11
12
13
14

11

D
E
F
10
11
12
13
14

13.
11
11
15

12_

li !
11
1Z li
11
18 12 li 1J5
19 18 1Z ii
1A
IB

19
1A

18
19

7
18

-8
8
jj
9
A

D
E
F
10
11
12
13
14
15
16
17

-7
9
7

-6
A

-5
B

-4
C

-3
D

-2
E

5
6
7
jj
9
A

D
E
F
10
11
12
13
14

4
5
6
7
8

3
4
5
6
7
B_

2
3
4
5
6
7
8_
9_
A

D
E
F
10
11

2
A

D
E
F
10
11

12
13
14
15
16

8_

2
A

D
E
F
10
11
12
13
14

15

2
A

D
E
F
10
11
12

D
E
F
10
11
12
13

-1
F

1
2

3
4
5
6
7

D
E
F
10

(. . .4) ,
4- . (. . .)
, 5 8-
.

..

7
6
5
4
3
2
1

0
0
0
0
0

0
1
2
3
4
5
6
7
8
9

1
2
3
4
5
6
7
F8
F9
FA
FB
FC
FD
FE
FF

0
0
0
0
0
0
0
0
0
0
0

2
0
2
4
6
8

FO
F2
F4
F6
F8
FA
FC
FE

3
0

4
0
4
8

10
14
18
1C

4
8

FO
F4
F8
FC

F
12
15
8

F1
F4
F7
FA
FD

5
0
5

F
14
19
IE
23
D8
DD
2
7

Fl
F6
FB

6
0
6

12
18
IE
24
2A
DO
D6
DC
E2
E8

F4
FA

7
0
7
E
15
1C
23
2A
31
C8
CF
D6
DD
E4
EB
F2
F9

-8
8
0
F8
FO
E8
EO
D8
DO
C8
40
38
30
28
20
18
10
8

-7
9
0
F9
F2
EB
E4
DD
D6
CF
38
31
2A
23
1C
15
E
7

-6
A
0
FA
F4

E8
E2
DC
D6
30
2A
24
IE
18
12

-5

0
FB
F6
Fl
EC
E7
E2
DD
28
23
IE
19
14
F
A
5

-4

0
FC
F8
F4
FO
EC
E8
E4
20
1C
18
14
10

8
4

-3
D
0
FD
FA
F7
F4
Fl

EB
18
15
12
F

9
6
3

-2
E
0
FE
FC
FA
F8
F6
F4
F2
10
E

A
8
6
4
2

-1
F
0
FF
FE
FD
FC
FB
FA
F9
8
7
6
5
4
3
2
1

0
D
1A
27
34
41
4E
5B
68
75
82
8F
9C
A9
B6
C3

0
E
1C
2A
38
46
54
62
70
7E
8C
9A
A8
B6
C4
D2

0
F
IE
2D
3C
4B
5A
69
78
87
96
A5
B4
C3
D2
El

, 4.
0

2
3
4
5
6
7
8
9
A

D
E
F

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

2
3

4
5
6
7
8
9
A

D
E
F

0
2
4
6
8
A

E
10
12
14
16
18
1A
1C

IE

0
3
6
9

F
12
15
18
IB
IE
21
24
27
2A
2D

0
4
8

10
14

0
5
A
F
14
19
IE
23
28
2D
32
37
3C
41
46
4B

0
6

12
18
IE
24
2A
30
36
3C
42
48
4E
54
5A

0
7
E
15
1C
23
2A
31
38
3F
46
4D
54
5B
62
69

0
8

0
9
12
IB
24
2D
36
3F
48
51
5A
63
6C
75
7E
87

0
A
14
IE
28
32
3C
46
50
5A
64
6E
78
82
8C
96

16
21
2C
37
42
4D
58
63
6E
79
84
8F
9A
A5

18
1C

20
24
28
2C
30
34
38

3C

10

18
20
28
30
38
40
48
50
58
60
68
70
78

18
24
30
3C
48
54
60
6C
78
84
90
9C
A8
B4

.5 .6 . . .
-1 , 8 , , .

274

. 4-

.5. ( * )
0
0

8
7
6
5
4
3
2
1

2
3
4
5
6
7
8
9

1
0
1
2
3
4
5
6
7
8
9

2
0
0

1
1

2
2
3
3

D
D

F
F
0

3
0
0
0

1
1

2
2

F
F
F
0
0

4
0
0
0
0
1
1
1
1

F
F
F
F
0
0
0

5
0
0
0
0
0
1
1
1
F
F
F
F
0
0
0
0

6
0
0
0
0
0
0
1
1
F
F
F
0
0
0
0
0

-8
8
0
0
0
0
0
0

7
0
0
0
0
0
0
0

0
0

0
0
0

F
F
1
1
1
0

0
0

0
0

0
0
0
0
0

-4

0
0

-3
D
0
0
0
F
F
F
E
E
2
2
2
1
1
1
0
0

0
0

0
0
0

F
1
1
0
0

F
F
0
0
0
0
0
0

-5

0
0

A
0

0
F
F
F
1
1
1
1

0
0

-6

-7
9
0
0
0

0
F
F
F
F
2
1
1
1
1
0

0
0

-2
E
0
0
F
F
E
E
D
D
4
3
3
2
2
1
1
0

-1
F
0
F
E
D

A
9
8
7
6
5
4
3
2
1

.6. ( + )
0
0

2
3
4
5
6
7
8
9

1
0
1
2
3
4
5
6
7
8
9

2
0
0
1
1
2
2
3
3
4
4
5
5
6
6
7
7

3
0
0
0

1
1
1

2
2
2
3
3
3
4
4
4
5

4
0
0
0
0

1
1
1
1
2
2
2
2
3
3
3
3

5
0

0
0
0

1
1
1

2
2
2
2
2
3

6
0
0
0
0
0
0

1
1

1
1
1

2
2
2

8
0
0
0
0
0
0
0
1
1
1
1
1
1
1
2

9
0
0
0
0
0
0
0
0
1
1
1
1
1
1
1
1

A
0
0

0
0
0

0
0
0
0

1
1
1
1
1

1
1

B
0
0
0
0

0
0
0
0
0
0

1
1
1

C
0

D
0

E
0

0
0
0
0
0
0

0
0
0

0
0

0
0
0
0

0
0
0
0
0
0

0
0
0

1
1
1
1
1

1
1
1
1

0
0
0
0

1
1
1

0
0
0
0
0

0
0
0

0
0
0
0

0
0
0
0
0

0
0
0
0
0

.7 .8 . . .7 -1 ,
0 , , .

. 4-

275

. ( + )
0
0
-

8
7
6
5
4
3
2
1

2
3
4
5
6
7
8
9

1
0

0
0

2
0

3
0

4
0

5
0

6
0

7
0

2
0
1
2
0
1

F
0

F
0

2
3
0
1
2
3
0
D

F
0
D
E
F

2
3
4
0
1
2
D
E
F
0

D
E
F

2
3
4
5
0
1
E
F
0

D
E
F

2
3
4
5
6
0
F
0
A

D
E
F

0
0
0
0

0
0
0
0
0
0
0

1
1
0

1
0
F
0
F
0
F
0
F

-8
8
0

-7
9
0

-6
A
0

-5
B
0

-4
C
0

-3
D
0

-2
E
0

-1
F
0

2
3
4
5
6
7
0

2
3
4
5
6
0
F
0
A

D
E
F

2
3

2
3

4
0
1

2
3
0

2
0
1
2
0
1
E
F
0
E
F
0
E
F

0
1
0

0
0
0
0
0

D
E
F

5
0
1

2
3
0
D
E
F
0
D
E
F

2
D
E
F
0

E
F
0

D
E
F

D
E
F

1
0

0
F
0
F

0^
0
0
0

F
0
F

0
0

.8. ( * )
0
0
2
3
5
6
7
8
9

276

1
0

0
0
\

0
0
0
0
0
0
0
0
0

2
0

3
0

4
0

5
0

2
0

2
3

2
0

1
2

3
0
1
2

2
3
4
0
1
2
3
4
0

1
1
0

1
0

1
0

1
0

2
0
1
2
0
1
2
0

1
2
3

6
0

7
0

8
0

C
0

D
0

E
0

F
0

2
3

2
3

2
3

2
3

2
3

2
3

2
3

2
3

2
3

5
0
1
2
3
4
5
0
1
2
3

5
6
0
1

5
6
7
0
1
2

5
6
7
8

2
3
4

5
6
7
8
9
0
1
2
3
4
5

5
6
7
8
9

0
1
2
3
4

5
6
7
8
9

0
1
2
3

5
6
7
8
9

0
1
2

5
6
7
8
9

D
0
1

5
6
7
8
9

3
4
5
6
0
1

4
5
6
7

0
1

5
6

. 4-



, / /() = 0 . /
jcn+1
/'(*.) '
/'(*) / = . ( ,+| ).

=/(*)
=

/(*)

, , .
, .
, , ,

,
(, \ -\ < 0.001 , (^, -\< 0.000001 ).
, , ,
( ) .
- " ", " " " ".
, .
,
.
, , 11.1.
. . 1 , . , , , , .

.1.

- -

- -

Iog2a

i(*-+i]
i(.A)
^-(-2)
*(2-.)

2' -
*"+I^(jH

, . ,
, ,
.

. , / /() = -,
Iog2a f(x) = 2'-a ..1
Iog2a ( log, a )
1/12 (, 1 2).
. 1/12 3/2 23/16 (1/1 2 = 1.4427).

4000 .
278


1.

Advanced Encryption Standard (AES). National Institute of Standards and Technology,


FIPS PUB 197 (November 2001). : http: //csrc. nist. gov/
publications/fips/fips!97/fips-197.pdf.
2.
Alverson R. Integer Division Using Reciprocals. In: Proceedings IEEE 10th Symposium
on Computer Arithmetic. Grenoble, France. June 26-28,1991. P. 186-190.
3.
REXX, (Marc A. Auslander).
4. Auslander M. . .
5.
Bernstein R. Multiplication by Integer Constants // Software Practice and Experience. 1986. 16,7. P. 641-652.
6.
Burks A. W., Goldstine H. H., and von Neumann J. Preliminary Discussion of the Logical Design of an Electronic Computing Instrument, Second Edition (1947). In: Papers
of John von Neumann on Computing and Computing Theory (vol. 12 in the Charles
Babbage Institute Reprint Series for the History of Computing). MIT Press, 1987.
7. Stephenson C. J. .
8. (Norman H. Cohen).
9.
Cutland N. J. Computability: An Introduction to Recursive Function Theory. Cambridge University Press, 1980.
10. Hoxey, Karim, Hay, and Warren (Ed.). The PowerPC Compiler Writer's Guide.
Warthman Associates, 1996.
11. Data Encryption Standard (DES). National Institute of Standards and Technology, FTPS
PUB 46-2 (December 1993). : http: //www. itl. nist.gov/

fipspubs/fip46-2.htm.
12.
13.
14.
15.
16.
17.

18.

19.
20.

21.

Dewdney A. K. The Turing Omnibus. Computer Science Press, 1989.


Dudley, Underwood. History of a Formula for Primes // American Mathematics Monthly
76. 1969. P. 23-28.
Ercegovac M. D. and Lang T. Division and Square Root: Digit-Recurrence Algorithms
and Implementations. Kluwer Academic Publishers, 1994.
Gardner M. Mathematical Games // Scientific American. 1972. 227,2. P. 106-109.
Gregoire D. G., Groves R. D., and Schmookler M. S. Single Cycle Merge/Logic Unit.
US Patent No. 4,903,228. 1990.
Granlund T. and Kenner R. Eliminating Branches Using a Superoptimizer and the GNU
Compiler. In: Proceedings of the 5th ACM SIGPLAN Conference on Programming
Language Design and Implementation (PLDI), July 1992. P. 341-352.
Graham R. L., Knuth D. E., and Patashnik O. Concrete Mathematics: A Foundation for Computer Science, Second Edition. Addison-Wesley, 1994. (: ., .,
. . . .: , 1998.)
Steele G. L., Jr. .
Steele G. L., Jr. Arithmetic Shifting Considered Harmful. AI Memo 378, MIT Artificial Intelligence Laboratory (September 1976); also: SIGPLAN Notices 12. 1977.
11.P. 61-69.
Granlund T. and Montgomery P. L. Division by Invariant Integers Using Multiplication.
In: Proceedings of the ACM SIGPLAN '94 Conference on Programming Language Design
and Implementation (PLDI). August 1994. P. 61-72.

22. (Richard Goldberg).


23. Goodstein, Prof. R. L. Formulae for Primes // The Mathematical Gazette. 1967.
51. P. 35-36.
24. GNU Superoptimizer.
25. Beeler M., Gosper R. W., and Schroeppel R. HAKMEM. MIT Artificial Intelligence
Laboratory AIM 239. February 1972.
26. Hay R. W. .
27. , . (R. W. Hay).
28. Hilbert D. Ueber die stetige Abbildung einer Linie auf ein Flachenstiick // Mathematischen Annalen. 1891. 38. P. 459-460.
29. Hopkins M. E. .
30. Hillis W. D. and Steele G. L., Jr. Data Parallel Algorithms // Comm. ACM 29. 1986.
12. P. 1170-1183.
31. Hennessy J. L. and Patterson D. A. Computer Architecture: A Quantitative Approach.
Morgan Kaufmann, 1990.
32. Harbison S. P. and Steele G. L., Jr. C: A Reference Manual, Fourth Edition. PrenticeHall, 1995.
33. Hardy G. H. and Wright E. M. An Introduction to the Theory of Numbers, Fourth Edition. Oxford University Press, 1960.
34. IBM, 1961.
35. Irvine M. M. Early Digital Computers at Bell Telephone Laboratories // IEEE Annals oj
the History of Computing 23. 2001. 3. P. 22^2.
36. Von Neumann J. First Draft of a Report on the EDVAC. In: Papers of John von
Neumann on Computing and Computing Theory (vol. 12 in the Charles Babbage Institute Reprint Series for the History of Computing). MIT Press, 1987.
37. GNU , IBM RS/6000 (Richard Kenner).
38. Knuth D. E. The Art of Computer Programming. Vol. 1. Fundamental Algorithms, Third
Edition. Addison-Wesley, 1997. (: .. . 1. , 3- . .: . "", 2000.)
39. Knuth D. E. Tlie Art of Computer Programming. Vol. 2. Seminumerical Algoritluns, Third
Edition. Addison-Wesley, 1998. (: .. .
2. , 3- . .: . "", 2000.)
40.
. ,
. 1885 (Vittorio Griinwald).
[39].
41. Kruskal . P., Rudolph L., and Snir M. The Power of Parallel Prefix // IEEE Transactions on Computers C-34. 1985. 10. P. 965-968.
42. Lamport L. Multiple Byte Processing with Full-Word Instructions // Communications oj
the ACM 18. 1975. 8. P. 471^75.
43. Lee R. ., Shi Z., and Yang X. Efficient Permutation Instructions for Fast Software
Cryptography // IEEE Micro 21. 2001. 6. P. 56-69.
44. Lam W. M. and Shapiro J. M. A Class of Fast Algorithms for the Peano-Hilbert SpaceFilling Curve. In: Proceedings ICIP 94. 1994. 1. P. 638-641.
45. Denneau M. .
46. Kane G. and Heinrich J. MIPS RISC Architecture. Prentice-Hall, 1992.
280

47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.

1
2

Morton M. Quibbles & Bits // Computer Language 7. 1990. 12. P. 45-55.


. :
http://www-cs-faculty.stanford.edu/-knuth/taocp.html.
Niven I., Zuckerman H. S., and Montgomery H. L. An Introduction to the Theory oj
Numbers, Fifth Edition. John Wiley & Sons, Inc., 1991.
Purdom P. W., Jr., and Brown C. A. The Analysis of Algorithms. Holt, Rinehart and
Winston, 1985.
Oden P. H. .
PL.8.
Ribenboim P. The Little Book of Big Primes. Springer-Verlag, 1991.
Reingold E. M., Nievergelt J., and Deo N. Combinatorial Algorithms: Theory and Practice. Prentice-Hall, 1977.
Sagan H. Space-Filling Curves. Springer-Verlag, 19941.
Shepherd A. D. .
Stallman R. M. Using and Porting GNU CC. Free Software Foundation, 1998.
Voorhies D. Space-Filling Curves and a Measure of Coherence. In: Graphics Gems
II, AP Professional, 1991.
Warren H. S., Jr. Functions Realizable with Word-Parallel Logical and Two'sComplement Addition Instructions // Communications of the ACM 20. 1977. 6.
p. 439-441.
Wegner, P. A. A Technique for Counting Ones in a Binary Computer // Communications
of the ACM 3. 1960. 5. P. 3222.
Wells D. The Penguin Dictionary of Curious and Interesting Numbers. Penguin
Books, 1997.
Willans C. P. On Formulae for the nth Prime Number // The Mathematical Gazette 48.
1964. P. 413-415.
Wormell C. P. Formulae for Primes // The Mathematical Gazette 51. 1967. P. 36-38.

, , .
, .

281

Web- www. HackersDelight. org,


. , (,
, ), , " " .
Hacker's Delight - Microsoft Internet Explorer provided by ABBYY Sot... |
!

j=lle Edit' View.;: Favorites Tools Help:


Back

Address

, (3 | ^Search SlFavorites : OlHIstory


http ://www.hackersdelight.org

Links

Hacker's
This site is an adjunct to the book Hacker's Delight (AddisonWesley, 2003). It may become a fonmi for discussions about tlie kind
of algorithms that are in that book. There is no mechanism set up yet
for discussion threads, but if you send me email (see the bottom of
mis page) I'll post it if it seems appropriate. Please also send email
if you find errors in the book,
iTDone"
I My computer
,
Web-, . A Hacker's Assistant ,
. ,
, . .

, "" , , CRC32, , (
, Web- ).
- , : , , , !

R
RISC
, 18
, 20

, 71
, 32

, 31
, 188

RISC, 18
, 200

, 255
, 205
, 53
, 38

. .
, 68
, 65
. .

, 227
, 229
-, 2 30
, 227
, 233
, 248
,
243

,
237

, 255
, 233; 248

, 30

, 25
, 797; 797,- 277
, 188

, 142
, 146
, 149
, 146
3,157; 175
5,158
7, 759; 176
, -2,166
2, 755
, 760; 177
, 756
, 795
RISC, 20

, 30

, 52
, 57
, 57
, 39
, 726
, 727
, 727
2,58
2,57

283


, 84; 121; 229; 242
, 111
, 33
, 39
, 86
, 75
, 81
, 92
, 99

, 104

, 27
, 34
, 63
, 68
, 65

, 82
, 31
, 82
, 107
, 107

, 48
, 121

-1+i, 22/
-2, 2/5
, 224

, 263
, 272
, 113
, 33

, 131
, 135
, 52

284

, 25/
n- ,
266
n- ,
263

bitsize(), 91
ceil(), 58,141
clp2(), 58
cmp(), 33
dist(), 82
doz(), 50
floor(), 58,141
flp2(), 58
iloglOO, 205
ilog2(), 208
ISIGN, 33
max(), 50
min(), 50
nlz(), 86
ntz(), 92
pop(), 76
pow2(), 206
sign(), 32
zbytelQ, 99
zbyterQ, 99
, 33
, 33
, 267

, /97
, 204
, 207
, 46

, 83


, 253
, 254
IEEE, 25/
, 26/

-
.

..
.. , .
..
.. ..

"".
101509, , . , . 43, . 1.
. . 090230 23.06.99
.

15.12.2003. 70X100/16.
Times. .
. . . 23,22. .-. . 17,7.
3500 . 1296.

" "
,
.
197110, -, ., 15.

kmonunrropot

.
ISBN S-8459-0498-6

ISBN 5-8459-0080-8

ISBN 5-84S9-0360-2

ISBN 5-8459-0261-4

ISBN S-8459-0330-0

!
pi

ISBN

5-84S9-0192-8

CucitTiTi

www.dialektika.com

www.williamspublishing.com

www.ciscopress.ru

ISBN 5-8459-0276-2

OBJECT
ORIETED

ISBN 5-8459-0438-2

ISBN 5-8459-0250-9

ISBN 5-8459-0437-4

DATABASE
SYSLMS

ISBN 5-8459-0527-3

ISBN 5-8459-OS42-7

did SrlTinl

www.dialektika.com

www.williamspublishing.com

www.ciscopress.ru

ISBN 5-8459-03S2-1

ISBN 5-8459-0351-3

ISBN 5-8459-0513-3

ISBN 5-8459-0459-5

ISBN 5-8459-0471-4

ISBN 5-8459-0389-0

Clico SrlTim
www.dialektika.com

www.williamspublishing.com

www.ciscopress.ru