# Synthesis of Arithmetic Circuits

Anteprima

### ESTRATTO DOCUMENTO

## SYNTHESIS OF

## ARITHMETIC CIRCUITS

FPGA, ASIC, and Embedded Systems

## JEAN-PIERRE DESCHAMPS

University Rovira i Virgili

## GÉRY JEAN ANTOINE BIOUL

National University of the Center of the Province of Buenos Aires

## GUSTAVO D. SUTTER

University Autonoma of Madrid

## A JOHN WILEY & SONS, INC., PUBLICATION

#

Copyright 2006 by John Wiley & Sons, Inc. All rights reserved.

Published by John Wiley & Sons, Inc., Hoboken, New Jersey. Published simultaneously in Canada.

No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any

form or by any means, electronic, mechanical, photocopying, recording, scanning, or otherwise,

except as permitted under Section 107 or 108 of the 1976 United States Copyright Act, without

either the prior written permission of the Publisher, or authorization through payment of the

appropriate per-copy fee to the Copyright Clearance Center, Inc., 222 Rosewood Drive, Danvers,

MA 01923, 978-750-8400, fax 978-646-8600, or on the web at www.copyright.com. Requests to

the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc.,

111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008

or online at http://www.wiley.com/go/permission.

Limit of Liability/Disclaimer of Warranty: While the publisher and author have used their best

efforts in preparing this book, they make no representations or warranties with respect to the

accuracy or completeness of the contents of this book and speciﬁcally disclaim any implied

warranties of merchantability or ﬁtness for a particular purpose. No warranty may be created or

extended by sales representatives or written sales materials. The advice and strategies contained

herein may not be suitable for your situation. You should consult with a professional where

appropriate. Neither the publisher nor author shall be liable for any loss of proﬁt or any other

commercial damages, including but not limited to special, incidental, consequential, or other damages.

For general information on our other products and services please contact our Customer Care Department

within the U.S. at 877-762-2974, outside the U.S. at 317-572-3993 or fax 317-572-4002.

Wiley also publishes its books in a variety of electronic formats. Some content that appears in print,

however, may not be available in electronic format.

Library of Congress Cataloging-in-Publication Data:

Deschamps, Jean-Pierre, 1945-

Synthesis of arithmetic circuits: FPGA, ASIC and embedded systems/Jean-Pierre Deschamps, Gery

Jean Antoine Bioul, Gustavo D. Sutter.

p. cm.

ISBN-13 978-0471-68783-2 (cloth)

ISBN-10 0-471-68783-9 (cloth)

1. Computer arithmetic and logic units. 2. Digital electronics. 3. Embedded computer systems.

I. Bioul, Gery Jean Antoine. II. Sutter, Gustavo D. III. Title.

TK7895.A65D47 2006

621.39’5 - - dc22 2005003237

Printed in the United States of America

10 9 8 7 6 5 4 3 2 1 To Marc

## CONTENTS

Preface xvii

About the Authors xix

1 Introduction 1

1.1 Number Representation, 1

1.2 Algorithms, 2

1.3 Hardware Platforms, 2

1.4 Hardware – Software Partitioning, 3

1.5 Software Generation, 3

1.6 Synthesis, 3

1.7 A First Example, 3

1.7.1 Speciﬁcation, 3

1.7.2 Number Representation, 6

1.7.3 Algorithms, 6

1.7.4 Hardware Platform, 8

1.7.5 Hardware – Software Partitioning, 8

1.7.6 Program Generation, 9

1.7.7 Synthesis, 10

1.7.8 Prototype, 12

1.8 Bibliography, 14 vii

## CONTENTS

viii

2 Mathematical Background 15

2.1 Number Theory, 15

2.1.1 Basic Deﬁnitions, 15

2.1.2 Euclidean Algorithms, 17

2.1.3 Congruences, 19

2.2 Algebra, 25

2.2.1 Groups, 25

2.2.2 Rings, 27

2.2.3 Fields, 27

2.2.4 Polynomial Rings, 27

2.2.5 Congruences of Polynomial, 32

2.3 Function Approximation, 35

2.4 Bibliography, 36

3 Number Representation 39

3.1 Natural Numbers, 39

3.1.1 Weighted Systems, 39

3.1.2 Residue Number System, 42

3.2 Integers, 42

3.2.1 Sign-Magnitude Representation, 42

3.2.2 Excess-E Representation, 43

B’s

3.2.3 Complement Representation, 44

3.2.4 Booth’s Encoding, 47

3.3 Real Numbers, 51

3.4 Bibliography, 54

4 Arithmetic Operations: Addition and Subtraction 55

4.1 Addition of Natural Numbers, 55

4.1.1 Basic Algorithm, 55

4.1.2 Faster Algorithms, 57

4.1.3 Long-Operand Addition, 66

4.1.4 Multioperand Addition, 67

4.1.5 Long-Multioperand Addition, 70

4.2 Subtraction of Natural Numbers, 71

4.3 Integers, 71

B’s

4.3.1 Complement Addition, 71

B’s

4.3.2 Complement Sign Change, 72

B’s

4.3.3 Complement Subtraction, 74

CONTENTS ix

B’s

4.3.4 Complement Overﬂow Detection, 74

4.3.5 Excess-E Addition and Subtraction, 78

4.3.6 Sign – Magnitude Addition and Subtraction, 79

4.4 Bibliography, 80

5 Arithmetic Operations: Multiplication 81

5.1 Natural Numbers Multiplication, 82

5.1.1 Introduction, 82

5.1.2 Shift and Add Algorithms, 83

5.1.2.1 Shift and Add 1, 83

5.1.2.2 Shift and Add 2, 84

5.1.2.3 Extended Shift and Add Algorithm:

XY þ C þ D, 86

5.1.2.4 Cellular Shift and Add, 86

5.1.3 Long-Operand Algorithm, 90

5.2 Integers, 91

B’s

5.2.1 Complement Multiplication, 91

nþm

B B’s

5.2.1.1 Mod Complement Multiplication, 92

5.2.1.2 Signed Shift and Add, 93

B’s

5.2.1.3 Postcorrection Complement Multiplication, 93

5.2.2 Postcorrection 2’s Complement Multiplication, 96

5.2.3 Booth Multiplication for Binary Numbers, 97

5.2.3.1 Booth-r Algorithms, 97

Per Gelosia

5.2.3.2 Signed-Digit Algorithm, 98

5.2.4 Booth Multiplication for Base-B Numbers

## B),

(Booth-r Algorithm in Base 102

5.3 Squaring, 104

5.3.1 Base-B Squaring, 104

5.3.1.1 Cellular Carry –Save Squaring Algorithm, 104

5.3.2 Base-2 Squaring, 106

5.4 Bibliography, 107

6 Arithmetic Operations: Division 109

6.1 Natural Numbers, 110

6.2 Integers, 117

6.2.1 General Algorithm, 117

6.2.2 Restoring Division Algorithm, 121

6.2.3 Base-2 Nonrestoring Division Algorithm, 121

6.2.4 SRT Radix-2 Division, 126

6.2.5 SRT Radix-2 Division with Stored-Carry Encoding, 131

## P– D

6.2.6 Diagram, 139 CONTENTS

x 6.2.7 SRT-4 Division, 142

6.2.8 Base-B Nonrestoring Division Algorithm, 148

6.3 Convergence (Functional Iteration) Algorithms, 155

6.3.1 Introduction, 155

6.3.2 Newton –Raphson Iteration Technique, 155

6.3.3 MacLaurin Expansion—Goldschmidt’s Algorithm, 159

6.4 Bibliography, 161

7 Other Arithmetic Operations 165

7.1 Base Conversion, 165

7.2 Residue Number System Conversion, 173

7.2.1 Introduction, 173

7.2.2 Base-B to RNS Conversion, 173

7.2.3 RNS to Base-B Conversion, 177

7.3 Logarithmic, Exponential, and Trigonometric Functions, 180

7.3.1 Taylor – MacLaurin Series, 181

7.3.2 Polynomial Approximation, 183

7.3.3 Logarithm and Exponential Functions Approximation

by Convergence Methods, 184

7.3.3.1 Logarithm Function Approximation by

Multiplicative Normalization, 184

7.3.3.2 Exponential Function Approximation by

Additive Normalization, 188

7.3.4 Trigonometric Functions—CORDIC Algorithms, 194

7.4 Square Rooting, 198

7.4.1 Digit Recurrence Algorithm—Base-B Integers, 198

7.4.2 Restoring Binary Shift-and-Subtract Square Rooting

Algorithm, 202

7.4.3 Nonrestoring Binary Add-and-Subtract Square Rooting

Algorithm, 204

7.4.4 Convergence Method—Newton –Raphson, 208

7.5 Bibliography, 208

8 Finite Field Operations 211

## Z

8.1 Operations in , 211

m

8.1.1 Addition, 212

8.1.2 Subtraction, 213

8.1.3 Multiplication, 213

8.1.3.1 Multiply and Reduce, 214

8.1.3.2 Modiﬁed Shift-and-Add Algorithm, 214

CONTENTS xi

8.1.3.3 Montgomery Multiplication, 216

8.1.3.4 Speciﬁc Ring, 220

8.1.4 Exponentiation, 221

GF(p),

8.2 Operations in 222

## Z

8.3 Operations in [x]/f (x), 224

p

8.3.1 Addition and Subtraction, 224

8.3.2 Multiplication, 225

n

GF(p

8.4 Operations in ), 228

8.5 Bibliography, 236 f

Appendix 8.1 Computation of , 236

ki

9 Hardware Platforms 239

9.1 Design Methods for Electronic Systems, 239

9.1.1 Basic Blocks of Integrated Systems, 240

9.1.2 Recurring Topics in Electronic Design, 241

9.1.2.1 Design Challenge: Optimizing

Design Metrics, 241

9.1.2.2 Cost in Integrated Circuits, 242

9.1.2.3 Moore’s Law, 243

9.1.2.4 Time-to-Market, 243

9.1.2.5 Performance Metric, 244

9.1.2.6 The Power Dimension, 245

9.2 Instruction Set Processors, 245

9.2.1 Microprocessors, 247

9.2.2 Microcontrollers, 248

9.2.3 Embedded Processors Everywhere, 248

9.2.4 Digital Signal Processors, 249

9.2.5 Application-Speciﬁc Instruction Set Processors, 250

9.2.6 Programming Instruction Set Processors, 251

9.3 ASIC Designs, 252

9.3.1 Full-Custom ASIC, 252

9.3.2 Semicustom ASIC, 253

9.3.2.1 Gate-Array ASIC, 253

9.3.2.2 Standard-Cell-Based ASIC, 254

9.3.3 Design Flow in ASIC, 255

9.4 Programmable Logic, 256

9.4.1 Programmable Logic Devices (PLDs), 256

9.4.2 Field Programmable Gate Array (FPGA), 258

9.4.2.1 Why FPGA? A Short Historical Survey, 258

9.4.2.2 Basic FPGA Concepts, 258 CONTENTS

xii TM

9.4.3 Xilinx Speciﬁcs, 260

9.4.3.1 Conﬁgurable Logic Blocks (CLBs), 262

9.4.3.2 Input/Output Blocks (IOBs), 262

9.4.3.3 RAM Blocks, 262

9.4.3.4 Programmable Routing, 264

9.4.3.5 Arithmetic Resources in Xilinx FPGAs, 264

9.4.4 FPGA Generic Design Flow, 264

9.5 Hardware Description Languages (HDLs), 267

9.5.1 Today’s and Tomorrow’s HDLs, 267

9.6 Further Readings, 268

9.7 Bibliography, 268

10 Circuit Synthesis: General Principles 271

10.1 Resources, 272

10.2 Precedence Relation and Scheduling, 277

10.3 Pipeline, 281

10.4 Self-Timed Circuits, 282

10.5 Bibliography, 288

11 Adders and Subtractors 289

11.1 Natural Numbers, 289

11.1.1 Basic Adder (Ripple-Carry Adder), 289

11.1.2 Carry-Chain Adder, 292

11.1.3 Carry-Skip Adder, 294

11.1.4 Optimization of Carry-Skip Adders, 298

s

11.1.5 Base-B Adder, 301

11.1.6 Carry-Select Adder, 303

11.1.7 Optimization of Carry-Select Adders, 307

11.1.8 Carry-Lookahead Adders (CLAs), 310

11.1.9 Preﬁx Adders, 318

11.1.10 FPGA Implementation of Adders, 322

11.1.10.1 Carry-Chain Adders, 322

11.1.10.2 Carry-Skip Adders, 323

11.1.10.3 Experimental Results, 326

11.1.11 Long-Operand Adders, 327

11.1.12 Multioperand Adders, 328

11.1.12.1 Sequential Multioperand Adders, 328

11.1.12.2 Combinational Multioperand Adders, 330

CONTENTS xiii

11.1.12.3 Carry-Save Adders, 333

11.1.12.4 Parallel Counters, 337

11.1.13 Subtractors and Adder-Subtractors, 344

11.1.14 Termination Detection, 346

11.1.15 FPGA Implementation of the Termination Detection, 348

11.2 Integers, 350

B’s

11.2.1 Complement Adders and Subtractors, 350

11.2.2 Excess-E Adders and Subtractors, 352

11.2.3 Sign-Magnitude Adders and Subtractors, 355

11.3 Bibliography, 357

12 Multipliers 359

12.1 Natural Numbers, 360

12.1.1 Basic Multiplier, 360

12.1.2 Sequential Multipliers, 363

12.1.3 Cellular Multiplier Arrays, 363

12.1.3.1 Ripple-Carry Multiplier, 365

12.1.3.2 Carry-Save Multiplier, 368

12.1.3.3 Figures of Merit, 370

12.1.4 Multipliers Based on Dissymmetric

r s

B B Cells, 370

12.1.5 Multipliers Based on Multioperand Adders, 378

12.1.6 Per Gelosia Multiplication Arrays, 383

12.1.6.1 Introduction, 383

12.1.6.2 Adding Tree for Base-B Partial Products, 384

12.1.7 FPGA Implementation of Multipliers, 386

12.2 Integers, 388

B’s

12.2.1 Complement Multipliers, 388

12.2.2 Booth Multipliers, 390

12.2.2.1 Booth-1 Multiplier, 390

12.2.2.2 Booth-2 Multiplier, 392

12.2.2.3 Signed-Digit Multiplier, 397

12.2.3 FPGA Implementation of the Booth-1 Multiplier, 404

12.3 Bibliography, 406

13 Dividers 407

13.1 Natural Numbers, 407

13.2 Integers, 415

13.2.1 Base-2 Nonrestoring Divider, 415

13.2.2 Base-B Nonrestoring Divider, 421 CONTENTS

xiv 13.2.3 SRT Dividers, 424

13.2.3.1 SRT-2 Divider, 424

13.2.3.2 SRT-2 Divider with Carry-Save Computation

of the Remainder, 428

13.2.3.3 FPGA Implementation of the Carry-Save SRT-2

Divider, 434

13.2.4 SRT-4 Divider, 435

13.2.5 Convergence Dividers, 439

13.2.5.1 Newton– Raphson Divider, 439

13.2.5.2 Goldschmidt Divider, 441

13.2.5.3 Comparative Data Between Newton –Raphson

(NR) and Goldschmidt (G) Implementations, 444

13.3 Bibliography, 444

14 Other Arithmetic Operators 447

14.1 Base Conversion, 447

14.1.1 General Base Conversion, 447

14.1.2 BCD to Binary Converter, 449

p

14.1.2.1 Nonrestoring 2 Subtracting Implementation, 449

14.1.2.2 Shift-and-Add BCD to Binary Converter, 450

14.1.3 Binary to BCD Converter, 452

14.1.4 Base-B to RNS Converter, 455

14.1.5 CRT RNS to Base-B Converter, 456

14.1.6 RNS to Mixed-Radix System Converter, 458

14.2 Polynomial Computation Circuits, 463

14.3 Logarithm Operator, 467

14.4 Exponential Operator, 468

14.5 Sine and Cosine Operators, 470

14.6 Square Rooters, 472

14.6.1 Restoring Shift-and-Subtract Square Rooter (Naturals), 472

14.6.2 Nonrestoring Shift-and-Subtract Square Rooter

(Naturals), 475

14.6.3 Newton –Raphson Square Rooter (Naturals), 477

14.7 Bibliography, 479

15 Circuits for Finite Field Operations 481

## Z

15.1 Operations in , 481

m

15.1.1 Adders and Subtractors, 481

15.1.2 Multiplication, 484

15.1.2.1 Multiply and Reduce, 484

CONTENTS xv

15.1.2.2 Shift and Add, 485

15.1.2.3 Montgomery Multiplication, 487

k 2c)

15.1.2.4 Modulo (B Reduction, 490

15.1.2.5 Exponentiation, 494

GF(p),

15.2 Inversion in 497

## Z

15.3 Operations in [x]/f (x), 500

p n

GF(p ), 504

15.4 Inversion in

15.5 Bibliography, 510

16 Floating-Point Unit 513

16.1 Floating-Point System Deﬁnition, 513

16.2 Arithmetic Operations, 515

16.2.1 Addition of Positive Numbers, 515

16.2.2 Difference of Positive Numbers, 517

16.2.3 Addition and Subtraction, 518

16.2.4 Multiplication, 520

16.2.5 Division, 521

16.2.6 Square Root, 522

16.3 Rounding Schemes, 524

16.4 Guard Digits, 525

16.5 Adder-Subtractor, 527

16.5.1 Alignment, 527

16.5.2 Additions, 529

16.5.3 Normalization, 530

16.5.4 Rounding, 530

16.6 Multiplier, 537

16.7 Divider, 542

16.8 Square Root, 546

16.9 Comments, 548

16.10 Bibliography, 548

Index 549

## PREFACE

From the beginnings of digital electronic science, the synthesis of circuits carrying

out arithmetic operations has been a central topic. As a matter of fact, it is an activity

directly related to computer development. From then on, a well-known technical dis-

cipline was born: computer arithmetic. Traditionally, the study of arithmetic circuits

has been oriented toward applications to general-purpose computers, which provide

the most important applications of digital circuits. However, the electronic market

share corresponding to speciﬁc systems (embedded systems) is signiﬁcant. It is

important to point out that the huge business volume that corresponds to general-

purpose computers (personal computers, servers, main frames) is distributed

among a relatively reduced number of different models. Therefore the number of

designers involved in general-purpose computer development is not as big as it

might seem and is much less than the number of engineers dedicated to production

and sales. The case of embedded systems is different. Embedded systems are circuits

designed for speciﬁc applications (special-purpose devices), so a great diversity of

products exist in the market, and the design effort per fabricated unit can be a lot

bigger than in the case of general-purpose computers. In consequence, the design

of speciﬁc computers is an activity in which numerous engineers are involved, in

all type of companies—even small ones—within numerous countries.

In this book methods and examples for synthesis of arithmetic circuits are described

with an emphasis somewhat different from the classic texts on computer arithmetic.

It is not limited to the description of the arithmetic units of computers.

. Descriptions of computation algorithms are presented in a section apart from

. the one dedicated to their materialization or implementation by digital circuits.

The development of an embedded system is an operation of hardware– software

codesign for which it is not known beforehand what tasks will be executed by a

microprocessor and what other tasks by speciﬁc coprocessors. For this reason, it

xvii

xviii PREFACE

appeared useful to describe the algorithms in an independent manner, without

any assumption on subsequent executions by an existent processor (software) or

by a new customized circuit (hardware).

A special, although not exclusive, importance has been given to user program-

. mable devices (ﬁeld programmable devices such as FPGAs), especially to the

families Spartan II and Virtex. Those devices are very commonly used for the

realization of speciﬁc systems, mainly in the case of small series and proto-

types. The particular architecture of those components leads the designer to

use synthesis techniques somewhat different from the ones applied for ASICs

(application-speciﬁc integrated circuits) for which standard cell libraries exist.

In what concern circuits description, logic schemes are presented, sometimes

. with some VHDL models, in such a way that the corresponding circuits can

easily be simulated and synthesized.

After an introductory chapter, the book is divided in two parts. The ﬁrst one is

dedicated to mathematical aspects and algorithms: mathematical background

(Chapter 2), number representation (Chapter 3), addition and subtraction (Chapter

4), multiplication (Chapter 5), division (Chapter 6), other arithmetic operations

(Chapter 7), and operations in ﬁnite ﬁelds (Chapter 8). The second part is dedicated

to the central topic—the synthesis of arithmetic circuits: hardware platforms

(Chapter 9), general principles of synthesis (Chapter 10), adders and subtractors

(Chapter 11), multipliers (Chapter 12), dividers (Chapter 13), other arithmetic primi-

tives (Chapter 14), operators for ﬁnite ﬁelds (Chapter 15), and ﬂoating-point unit.

Numerous VHDL models, and other source ﬁles, can be downloaded from http://

com-

www.ii.uam.es/gsutter/arithmetic/. This will be indicated in the text (e.g.,

plete VHDL source code available). As regards the VHDL models, they are of two

types: some of them have been developed for simulation purposes only, so the work-

ing of the corresponding circuit can be observed; others are synthesizable models that

have been implemented within commercial programmable components (FPGA’s).

The authors thank the people who have helped them in developing this book,

especially Dr. Tim Bratten, for correcting the text, and Paula Mirón, for the cover

design. They are grateful to the following universities for providing them the

means for carrying this work through to a successful conclusion: University

Rovira i Virgili (Tarragona, Spain), University Rey Juan Carlos (Madrid, Spain),

State University UNCPBA (Tandil, Argentina), University FASTA (Mar del

Plata, Argentina), and Autonomous University of Madrid (Spain).

## J -P D

## EAN IERRE ESCHAMPS

University Rovira i Virgili

## J A B

## G

## ÉRY EAN NTOINE IOUL

National University of the Center of the Province of Buenos Aires

## D. S

## G USTAVO UTTER

University Autonoma of Madrid

## ABOUT THE AUTHORS

received a MS degree in electrical engineering from the

Jean-Pierre Deschamps

University of Louvain, Belgium, in 1967, a PhD in computer science from the

Autonomous University of Barcelona, Spain, in 1982, and a PhD degree in electrical

engineering from the Polytechnic School of Lausanne, Switzerland, in 1983. He has

worked in several companies and universities. He is currently a professor at the

University Rovira i Virgili, Tarragona, Spain. His research interests include ASIC

and FPGA design, digital arithmetic, and cryptography. He is the author of six

books and about a hundred international papers.

received a MS degree in physical aerospace engineering

Géry Jean Antoine Bioul

from the University of Liège, Belgium. He worked in digital systems design with

PHILIPS Belgium and in computer-aided industrial logistics with several For-

tune-100 U.S. companies in the United States, and Africa. He has been a professor

of computer architecture in several universities mainly in Africa and South America.

He is currently a professor at the State University UNCPBA of Tandil (Buenos

Aires), Argentina, and a professor consultant at the Saint Thomas University

FASTA of Mar del Plata (Buenos Aires), Argentina. His research interests include

logic design and computer arithmetic algorithms and implementations. He is the

author of about 50 international papers and patents on fast arithmetic units.

received a MS degree in Computer Science from the State

Gustavo D. Sutter

University UNCPBA of Tandil (Buenos Aires), Argentina, and a PhD degree

from the Autonomous University of Madrid, Spain. He has been a professor at

the UNCPBA, Argentina and is currently a professor at the University Autonoma

of Madrid, Spain. His research interests include ASIC and FPGA design, digital

arithmetic, and development of embedded systems. He is the author of about 30

international papers and communications. xix

1

## INTRODUCTION

The design of embedded systems, that is, circuits designed for speciﬁc applications,

is based on a series of decisions as well as on the use of several types of development

techniques. For example:

Selection of the data representation

. Generation or selection of algorithms

. Selection of hardware platforms

. Hardware – software partitioning

. Program generation

. New hardware synthesis

. Cosimulation, coemulation, and prototyping

.

Some of these activities have a close relationship with the study of arithmetic

algorithms and circuits, especially in the case of systems including a great

amount of data processing (e.g., ciphering and deciphering, image processing,

digital signature, biometry).

1.1 NUMBER REPRESENTATION

When using general-purpose equipment, the designer has few possible choices

concerning the internal representation of data. He must conform to some ﬁxed

Synthesis of Arithmetic Circuits: FPGA, ASIC, and Embedded Systems

By Jean-Pierre Deschamps, Géry J. A. Bioul, and Gustavo D. Sutter

#

Copyright 2006 John Wiley & Sons, Inc. 1

2 INTRODUCTION

integer, ﬂoating-point, double precision, char-

and predeﬁned data types such as and

acter. On the contrary, if a speciﬁc system is under development, the designer can

choose, for each data, the most convenient type of representation. It is no longer

necessary to choose some standard ﬁxed-point or ﬂoating-point numeration

system. Nonstandard speciﬁc formats can be used. In Chapter 3 the main number

representation methods will be deﬁned.

1.2 ALGORITHMS

Every complex data processing operation must be decomposed into simpler

operations — the computation primitives — executable either by the main pro-

cessor or by some speciﬁc coprocessor. The way the computation primitives are

algorithm.

used in order to perform the complex operation is what is meant by

Obviously, knowledge of algorithms is of fundamental importance for developing

arithmetic procedures (software) and circuits (hardware). It is the topic of

Chapters 4 –8.

1.3 HARDWARE PLATFORMS

The selection of a hardware platform is based on the answer to the following ques-

tion. How do we get the desired behavior at the lowest cost, while fulﬁlling some

additional constraints? As a matter of fact, the concept of cost must be carefully

deﬁned in each particular case. It can cover several aspects: for example, the unit

production cost, the nonrecurring engineering costs, and the implicit cost for a

late introduction of the product to the market. Some examples of additional technical

constraints are the size of the system, its power consumption, and its reliability and

maintainability. microcontrollers

For systems requiring little data processing capability, and low-

microprocessors

range can be the best choice. If the computation needs are greater,

digital signal processors

more powerful microprocessors, or even (DSPs), should be

considered. This type of solution (microprocessors and DSPs) is very ﬂexible as the

development work mainly consists in generating programs.

For getting higher performances, it may be necessary to develop speciﬁc circuits.

ﬁeld-programmable

A ﬁrst option is to use a programmable device, for example, a

gate array (FPGA). It could be an interesting option for prototypes and small series.

application-speciﬁc integrated circuit

For greater series, an (ASIC) should be

gate

developed. ASIC vendors offer several types of products: for example,

arrays, standard cell libraries,

with relatively small prototyping costs, or integrating

system-on-chip

a complete (SOC) including processors, program memories, data

memories, logic, macrocells, and analog interfaces.

A brief presentation of the most common hardware platforms is given in

Chapter 9. 3

1.7 A FIRST EXAMPLE

1.4 HARDWARE – SOFTWARE PARTITIONING

The hardware –software partitioning consists of deciding which operations will be

executed by the central processing unit (the software) and which ones by speciﬁc

coprocessors (the hardware). As a matter of fact, the platform selection and the

hardware –software partitioning are tightly related operations. For systems requiring

little data processing capability, the whole system is implemented in software. If

higher performances are necessary, the noncritical operations, as well as control

of the operation sequence, are executed by the central processing unit, while the

critical ones are implemented within speciﬁc coprocessors.

1.5 SOFTWARE GENERATION

The operations belonging to the software block of the chosen partition must be pro-

grammed. In Chapters 4 –8 the algorithms are presented in an Ada-like language that

can easily be translated to C or even to the assembly language of the chosen

microprocessor.

1.6 SYNTHESIS

Once the hardware– software partition has been deﬁned, all the tasks assigned to the

speciﬁc hardware (FPGA, ASIC) must be translated into circuit descriptions. Some

important synthesis principles and methods are described in Chapter 10. The syn-

thesis of arithmetic circuits, based on the algorithms of Chapters 4 –8, is the topic

of Chapters 11– 15, and an additional chapter (16) is dedicated to the implemen-

tation of ﬂoating-point arithmetic.

1.7 A FIRST EXAMPLE

Common examples of application ﬁelds resorting to embedded solutions are crypto-

graphy, access control, smart cards, automotive, avionics, space, entertainment, and

electronic sales outlets. In order to illustrate the main steps of the design process, a

small digital signature system will now be developed (complete assembly language

and VHDL code available).

1.7.1 Speciﬁcation

The system under development (Figure 1.1) has three inputs,

is an 8-bit vector.

character

. is a signal used for synchronizing the input of successive

new_character

. characters. digital signature.

is a control signal ordering the computation of a

sign

.

4 INTRODUCTION

character signature

signature

new_character generator done

sign System under development.

Figure 1.1

and two outputs,

is a status variable indicating that the signature computation has been

done

. completed, is a 32-bit vector, namely, the signature of the message.

signature

. c . . . c

c , , , of

The working of the system is shown in Figure 1.2: a sequence n

1 2

n

any number of characters (the message), synchronized by the signal new_char-

, is inputted. When the control signal goes high, the ﬂag is low-

sign done

acter ﬂag will be raised as

ered and the signature of the message is computed. The done

s

soon as the signature is available.

In order to sign the message two functions must be deﬁned:

a function associating a 32-bit vector (the ) to every message,

hash summary

. whatever its length;

an function computing the signature corresponding to the summary.

encode

.

The following (naive) hash function is used:

Algorithm 1.1 Hash Function

summary:=0;

not(end_of_message)

while loop

get(character);

a:=(summary(7 downto 0)+character) mod 256;

summary(23 downto 16):=summary(31 downto 24);

summary(15 downto 8):=summary(23 downto 16);

c c c

character 1 2 n

sign

new_character

done

signature s

Input and output signals.

Figure 1.2 5

1.7 A FIRST EXAMPLE

summary(7 downto 0):=summary(15 downto 8);

summary(31 downto 24):=a;

end loop;

As an example, assume that the message is the following (every character can

be equivalently considered as an 8-bit vector or a natural number smaller than

256, i.e. a base-256 digit; see Chapter 3):

12, 45, 216, 1, 107, 55, 10, 9, 34, 72, 215, 114, 13, 13, 229, 18:

The summary is computed as follows:

summary ¼ (0, 0, 0, 0),

summary ¼ (12, 0, 0, 0),

summary ¼ (45, 12, 0, 0),

summary ¼ (216, 45, 12, 0),

summary ¼ (1, 216, 45, 12),

summary ¼ (119, 1, 216, 45),

summary ¼ (100, 119, 1, 216),

summary ¼ (226, 100, 119, 1),

summary ¼ (10, 226, 100, 119),

summary ¼ (153, 10, 226, 100),

summary ¼ (172, 153, 10, 226),

summary ¼ (185, 172, 153, 10),

summary ¼ (124, 185, 172, 153),

summary ¼ (166, 124, 185, 172),

summary ¼ (185, 166, 124, 185),

summary ¼ (158, 185, 166, 124),

summary ¼ (142, 158, 185, 166):

The ﬁnal result, translated from the base-256 to the decimal representation, is

summary ¼ þ þ þ ¼

3 2

142 256 158 256 185 256 166 2392766886:

The encode function computes

encode(y) ¼ y m

x mod

x private key, m

being some and a 32-bit number. Assume that

x ¼ m ¼ ¼

32

1937757177 and 2 1 4294967295:

6 INTRODUCTION

Then the signature of the previous message is ¼

s ¼ 1937757177 mod 4294967295 37998786:

(2392766886)

1.7.2 Number Representation

In this example all the data are either 8-bit vectors (the characters) or 32-bit vectors

m).

(the summary, the key, and the module So instead of representing them in the

decimal numeration system, they should be represented in the binary or, equiva-

lently, the hexadecimal system. The message is

0C, 2D, D8, 01, 6B, 37, 0A, 09, 22 48, D7, 72, 0D, 0D, E5, 12:

The summary, the key, the module, and the signature are

summary ¼ 8E9EB9A6,

private key ¼ 737FD3F9,

m ¼ FFFFFFFF,

s ¼ 0243D0C2:

1.7.3 Algorithms

The hash function amounts to a mod-256 addition, that is, a simple 8-bit addition

m

without output carry. The only complex operation is the mod exponentiation.

x, y, m n-bit

Assume that and are numbers. Then

n1

x ¼ x(0) þ þ þ :x(n

2:x(1) 2 1),

e

and can be written in the form

x(n1) x(n2) x(1) x(0)

e ¼ :y :y :y :y m:

2 2 2 2 2

(( ((1 ) ) ) ) mod

The corresponding algorithm is the following (Chapter 8, Algorithm 8.14).

Algorithm 1.2 Exponentiation

e:=1;

i 1..n

for in loop

e:=(e*e) mod m;

x(n-i)=1 e:=(e*y) mod m;

if then end if;

end loop; m

The only computation primitive is the modulo product, which, in turn, is

m

equivalent to a natural multiplication followed by a modulo reduction, that is,

m.

an integer division by The following algorithm (Chapter 8, Algorithm 8.5)

7

1.7 A FIRST EXAMPLE

r ¼ x.y m. multiply,

computes mod It uses two procedures: which computes the

z x y, divide, q

product of two natural numbers and and which generates (the

r z ¼ q.m þ r r , m.

quotient) and (the remainder) such that with

m

Algorithm 1.3 Modulo Multiplication

multiply (x, y, z);

divide (z, m, q, r); z x y

A classical method for computing the product of two natural numbers and is the

shift and add algorithm (Chapter 5, Algorithm 5.3). In base 2:

Algorithm 1.4 Natural Multiplication

p(0):=0;

i 0..n-1

for in loop

p(i+1):=(p(i)+x(i)*y)/2;

end loop;

z:=p(n)*(2**n);

q r z ¼ q.m þ r r , m,

For computing and such that with the classical restoring

x

division algorithm can be used (Chapter 6, Algorithms 6.1 and 6.2). Given and

y x , y, p

(the operands) such that and (the desired precision), the restoring division

q r

algorithm computes and such that

p

x:2 ¼ q:y þ r: (1:1)

e y n-bit

Within the exponentiation algorithm 1.2, the operands and are numbers.

e m, z ¼ e e

Furthermore, is always smaller than so that both products or

z ¼ e y are 2.n-bit numbers satisfying the relation

n :

z , m:2 n

x z, p n, y m.2 in (1.1), the restoring division

Thus by substituting by by and by

0

q r such that

algorithm computes and

n n 0 0 n

¼ q:(m:2 þ r r , m:2

z:2 ) with ,

that is, 0 n

:2 , m:

z ¼ q:m þ r r ¼ r

with

The restoring algorithm is similar to the pencil and paper method. At every step

r(i 2

the latest obtained remainder, say, 1), is multiplied by 2 and compared with the

y. 2 y,

divider If 2.r(i 1) is greater than or equal to then the new remainder is

8 INTRODUCTION

r(i) ¼ 2 2 y

2.r(i 1) and the corresponding quotient bit is equal to 1. In the contrary

r(i) ¼ 2

case, the new remainder is 2.r(i 1) and the corresponding quotient bit equal

r(0)

to 0. The initial remainder is the dividend.

Algorithm 1.5 Restoring Division

r(0):=z; y:=m*(2**n);

i 1..n

for in loop

2*r(i-1)-y<0 q(i):=0; r(i):=2*r(i-1);

if then else

q(i):=1; r(i):=2*r(i-1)-y; end if;

end loop;

r:=r(n)/(2**n);

By merging Algorithms 1.4 and 1.5, the following modular product algorithm is

obtained.

Algorithm 1.6 Modular Product

p(0):=0;

i 0..n-1

for in loop

p(i+1):=(p(i)+x(i)*y)/2;

end loop;

r(0):=p(n)*(2**n); y:=m*(2**n);

i 1..n

for in loop

2*r(i-1)-y<0 q(i):=0; r(i):=2*r(i-1);

if then else

q(i):=1; r(i):=2*r(i-1)-y; end if;

end loop;

r:=r(n)/(2**n); n

p(n) m r(n)

Observe that the multiplication of and by 2 , as well as the division of

n r(0) ¼ p(n)

can be deleted. Then is a 2.n-bit ﬁxed-point number (Chapter 3)

by 2 n m. q

and the divider is equal to The quotient and the remainder

smaller than 2 n ¼ q.m þ r(n) r ¼ r(n).

r(n) p(n).2 so that

satisfy the relation

1.7.4 Hardware Platform

For implementing this illustrative example, a prototyping board will be used,

namely, an XSA-100 board from XESS Corporation. It includes an XC2S100

FPGA (Spartan-II family of Xilinx) integrating the complete digital signature

system. The design environment includes virtual components (synthesizable

VHDL models, Chapter 9), among others PicoBlaze, an 8-bit microprocessor, and

its program memory ([XIL2002]).

1.7.5 Hardware– Software Partitioning x m.

y modulo

As mentioned above, the only complex operation is the computation of

All the other operations can be carried out by the processor. The corresponding

system architecture is shown in Figure 1.3. It works as follows: 9

1.7 A FIRST EXAMPLE ws and port_id = 3

Y(3)

port_id(0) port_id ws and port_id = 2

out_port

character PicoBlaze and

0 in_port program Y(2)

1

command memory ws

(write_strobe) ws and port_id = 1

ws and port_id = 4 Y(1)

out_port(0)

start

signature z ws and port_id = 0

exponentiator y Y(3)&Y(2)&Y(1)&Y(0)

x 737FD3F9 Y(0)

done done m FFFFFFFF

System architecture.

Figure 1.3

PicoBlaze reads the input at address 0 and the input at

character command

. address 1, where

command = 0 0 0 0 0 0 sign new_character.

It computes the 32-bit summary and writes it, under the form of four separate

. bytes,

summary = Y(3) Y(2) Y(1) Y(0),

into four registers whose addresses are 3, 2, 1 and 0, respectively.

A speciﬁc coprocessor receives the signal from PicoBlaze at address 4,

start

. computes 737FD3F9

s =(summary) mod FFFFFFFF,

and generates the ﬂag.

done

1.7.6 Program Generation

The program executed by PicoBlaze is made up of three parts (assembly language

code available):

reading of the and input signals,

new_character sign

. reading of the input and updating of the ,

character summary

. writing of the and of the command within the interface

summary start

. registers:

10 INTRODUCTION

summary:=(0, 0, 0, 0);

start:=0;

loop

--wait for command=0

command>0 null;

while loop end loop;

--wait for command=1 (new_character) or 2 (sign)

command=0 null;

while loop end loop;

command=1

if then

a:=(summary(0)+character) mod 256;

summary(0):=summary(1);

summary(1):=summary(2);

summary(2):=summary(3);

summary(3):=a;

command=2

elsif then

Y(3):=summary(3);

Y(2):=summary(2);

Y(1):=summary(1);

Y(0):=summary(0);

start:=1;

summary:=(0, 0, 0, 0);

start:=0;

end if;

end loop;

1.7.7 Synthesis exponentiator

The synthesis (complete VHDL code available) of the block of

Figure 1.3 is based on the algorithms of Section 1.7.3. A summary of the main

principles for translating an algorithm to a circuit is given in Chapter 10. The

data path of Figure 1.4 allows executing Algorithm 1.2. It includes:

e,

two 32-bit registers: a parallel register storing and a loadable shift register,

. x x(n 2

initially storing and allowing to successively read the value of 1),

x(n 2 . . . x(0);

2), ,

m

a mod multiplier with a input signal and a output ﬂag;

start done

. e y

a 32-bit 2-to-1 multiplexer selecting either or as the second multiplier operand.

.

The complete circuit is described by the following VHDL model (including the

control unit):

entity exponentiator is

port (

x, y, m: in std_logic_vector(n-1 downto 0);

z: inout std_logic_vector(n-1 downto 0);

clk, reset, start: in std_logic;

done: out std_logic

);

end exponentiator; 11

1.7 A FIRST EXAMPLE x

m

y

0 1

sel_y m

x y

start_mult start mod m multiplier

done_mult done z

enable register

preset (e) load

shift shift register

reg_x(n–1) serial_out reg_x

z Exponentiator.

Figure 1.4

architecture circuit of exponentiator is

component sequential_mod_mult..end component;

signal start_mult, sel_y, done_mult: std_logic;

signal reg_x, input_y, output_z: std_logic_vector(n-1 downto

0);

subtype step_number is natural range 0 to n;

signal count: step_number;

subtype internal_states is natural range 0 to 14;

signal state: internal_states;

begin

label_1: sequential_mod_mult port map(z, input_y, m,

output_z, clk, reset, start_mult, done_mult);

with sel_y select input_y<=z when ‘0’, y when others;

process (clk, reset)

begin

if reset=‘1’ then

state<=0; done<=‘0’; start_mult<=‘0’; count<=0;

elsif clk’event and clk=‘1’ then

case state is

when 0=>if start=‘0’ then state<=state+1; end if;

12 INTRODUCTION

when 1=>if start=‘1’ then state<=state+1; end if;

when 2=>z<=conv_std_logic_vector(1, n);

reg_x<=x; count<=0; done<=‘0’; state<=state+1;

when 3=>

sel_y<=‘0’; start_mult<=‘1’; state<=state+1;

when 4=>state<=state+1;

when 5=>start_mult<=‘0’; state<=state+1;

when 6=>

if done_mult=‘1’ then state<=state+1; end if;

when 7=>z<=output_z;

if reg_x(n-1)=‘1’ then state<=state+1;

else state<=13; end if;

when 8=>

sel_y<=‘1’; start_mult<=‘1’; state<=state+1;

when 9=>state<=state+1;

when 10=>start_mult<=‘0’; state<=state+1;

when 11=>

if done_mult=‘1’ then state<=state+1; end if;

when 12=>z<=output_z; state<=state+1;

when 13=>reg_x(0)<=reg_x(n-1);

for i in 1 to n-1 loop reg_x(i)<=reg_x(i-1);

end loop;

count<=count+1; state<=state+1;

when 14=>

if count>=n then done<=‘1’; state<=0;

else state<=3; end if;

end case;

end if;

end process;

end circuit;

1.7.8 Prototype

All the ﬁles (complete source ﬁles available) necessary for programming an XSA-

100 board are included in the ﬁle :

section1_7.zip

is the complete description of the exponentiation circuit

exponentiator.vhd

. (including the modular multiplier model);

is the assembly language program;

signatu.psm

. is the PicoBlaze model;

kpcsm.vhd

. is the program memory model generated from the assembly

signatu.vhd

. (the PicoBlaze assembler released by

language program with kcpsm.exe

Xilinx [XIL2002]).

In order to test the complete system, the circuit of Figure 1.5 has been

synthesized. It is made up of:

the circuit of Figure 1.3 including PicoBlaze, its program memory, the interface

. exponentiator;

registers, and the 13

1.7 A FIRST EXAMPLE

a ﬁnite state machine generating the commands and characters corresponding to

. the example of Section 1.7.1;

a circuit that interfaces the board with signals controllable from the

d(7..0)

. host computer ([XSA2002]):

cannot be used,

d(7) are used for selecting one of the outputs or

d(3..0) (out_0 to out_15)

,

inputs (in_0 to in_15)

are control signals,

d(6..4) d(6.4) Command

000 nop

001 write

010 read

011 reset

100 address

strobe

character

in_15 done

message and system

in_14 command under test

command

... ... generation (figure 1.3) signature

in_0

reset done&done&done&done

out_15

out_14

... ...

out_8

host interface out_7 signature(31:28)

d signature(27:24)

out_6 signature(32:20)

out_5 signature(19:16)

out_4 signature(15:12)

out_3 signature(11:8)

out_2 signature(7:4)

out_1 signature(3:0)

out_0 LED

result decoder

Prototype.

Figure 1.5

14 INTRODUCTION

in this application the and commands are not used;

write address strobe

command is active, the hexadecimal representation of the 4-bit

when the read is displayed on the LED of the board;

vector selected with d(3..0)

the 7-segment LED decoder.

.

The VHDL model of the circuit of Figure 1.5 is also included in

(ﬁrma.vhd)

section1_7.zip .

as well as the ﬁle describing the pin assignment (pines.ucf)

The whole system (Figure 1.5) can be synthesized with ISE, the synthesis program

of Xilinx, and downloaded to the XSA-100 board.

1.8 BIBLIOGRAPHY Readings in Hardware/Software

[DEM2002] G. De Micheli, R. Ernst, and W. Wolf (eds.),

Co-Design, Morgan Kaufmann Publishers, San Francisco, CA, 2002.

Speciﬁcation and Design of

[GAJ1994] D. D. Gajski, F. Vahid, S. Narayan, and J. Gong,

Embedded Systems, Prentice Hall, Englewood Cliffs, NJ, 1994.

PicoBlaze 8-bit Microcontroller for Virtex-E and Spartan-II/IIE

[XIL2002] Xilinx Inc,

Devices, application note XAPP213 (v2.0), Dec. 2002; http://www.xilinx.com

XSA Board V1.1, V1.2 User Manual,

[XSA2002] June 2002; http://www.xess.com.

2

## MATHEMATICAL BACKGROUND

This chapter presents some topics in mathematics; it is intended to make this

book self-contained. For further details the reader is referred to textbooks on

algebra ([COH1993], [GIL2003], [HER1975], [HUN1974]), mathematical analysis

([APO1974], [RUD1976]), number theory ([KOB1994], [ROS1992]), ﬁnite ﬁelds

([McC1987]), and cryptography ([MEN1996]).

2.1 NUMBER THEORY

2.1.1 Basic Deﬁnitions

Deﬁnitions 2.1 N ¼ f0, . . .g.

1

1. The set of natural numbers 1, 2, 3,

Z ¼ f . . . 23, 22, 21, . . . g.

2. The set of integers , 0, 1, 2, 3,

Deﬁnition 2.2 x y, y divides x divisor x)

Given two integers and (y is a of if there

z x ¼ z.y.

exists an integer such that

zero N.

1 For convenience, the element has been included in

Synthesis of Arithmetic Circuits: FPGA, ASIC, and Embedded Systems

By Jean-Pierre Deschamps, Géry J. A. Bioul, and Gustavo D. Sutter

#

Copyright 2006 John Wiley & Sons, Inc. 15

16 MATHEMATICAL BACKGROUND

Deﬁnition 2.3 x y, y . q

Given two integers and with 0, there exist two integers

quotient) r remainder)

(the and (the such that

x ¼ q:y þ r, r , y:

where 0

q r

It can be proved that and are unique. Then (notation)

r ¼ x y, q ¼ x y:

mod div

An alternative deﬁnition is the following.

Deﬁnition 2.4 (Integer Division) x y, y .

Given two integers and with 0, there

q quotient) r remainder)

exist two integers (the and (the such that

x ¼ q:y þ r, r , y x y , r x ,

where 0 if 0 and 0 if 0:

q r

It can be proved that and are unique. Then (notation)

r ¼ x y, q ¼ x=y:

rem

Examples 2.1

x ¼ 216, y ¼

1. 3:

16 ¼ 16 ¼ 6, 16 ¼ 6:3 þ

mod 3 2, div 3 2,

16 ¼ 1, 16=3 ¼ 5, 16 ¼ 5:3 þ

rem 3 ( 1)

x ¼ 215, y ¼

2. 3:

15 ¼ 15 ¼ 5, 15 ¼ 5:3 þ

mod 3 0, div 3 0,

15 ¼ 15=3 ¼ 5, 15 ¼ 5:3 þ

rem 3 0, 0:

Deﬁnitions 2.5 x y, z greatest common divisor x y

1. Given two integers and is the of and if

z is a natural number (nonnegative integer),

z x y,

divides both and x y z.

any other common divider of and is also a divider of

z ¼ gcd(x, y).

Notation: x y, relatively prime gcd(x,

2. Given two integers and they are said to be if

y) ¼ 1. p . prime p.

3. An integer 1 is said to be if its only positive divisors are 1 and 17

2.1 NUMBER THEORY

2.1.2 Euclidean Algorithms

x y, Euclidean algorithm

Given two natural numbers and the for natural numbers

gcd(x, y).

computes It is based on a series of integer divisions:

r(i ¼ q(i):r(i) þ r(i þ r(i þ , r(i):

1) 1), where 0 1)

r(i 2 r(i) r(i) r(i þ

Observe that any divider of 1) and is also a divider of and 1)

so that gcd(r(i r(i)) ¼ gcd(r(i), r(i þ

1), 1)):

Initially, r(0) ¼ x r(1) ¼ y:

and

Then compute r(0) ¼ q(1):r(1) þ r(2),

r(1) ¼ q(2):r(2) þ r(3),

r(2) ¼ q(3):r(3) þ r(4),

...

r(n ¼ q(n þ r(n

3) 2):r(n 2) 1),

r(n ¼ q(n þ r(n),

2) 1):r(n 1)

r(1) . rð2Þ . r(n) ¼ gcd(r(i 2 r(i)) ¼ gcd(r(i), r(i þ

where 0 and 1), 1)), so that

gcd(x, y) ¼ gcd(r(0), r(1)) ¼ ¼ gcd(r(n r(n)) ¼ gcd(r(n

1), 1), 0)

¼ r(n 1):

r(0) ¼ x ¼ r(1) ¼ y ¼

Let 8580; 4070;

Example 2.2 ¼ þ

8580 2:4070 440

¼ þ

4070 9:440 110

¼ þ

440 4:110 0

gcd(8580,4070) ¼

Then 110.

18 MATHEMATICAL BACKGROUND

extended Euclidean algorithm b(i) c(i)

In the a series of coefﬁcients and are

r(0), r(1), r(2), . . . r(n):

calculated in parallel with the computation of ,

b(0) ¼ c(0) ¼

1, 0,

b(1) ¼ c(1) ¼

0, 1,

b(2) ¼ b(0) b(1):q(1), c(2) ¼ c(0) c(1):q(1),

... ...

b(n ¼ b(n b(n c(n ¼ c(n c(n

1) 3) 2):q(n 2), 1) 3) 2):q(n 2)

It can be demonstrated by induction that

r(i) ¼ b(i):x þ c(i):y, 8 i ¼ . . . n

0, 1, 2, , 1:

In particular, gcd(x,y) ¼ r(n ¼ b(n x þ c(n

1) 1): 1):y:

In conclusion, the extended Euclidean algorithm expresses the greatest common

z x y x y,

divisor of two natural numbers and as a linear combination of and that is,

z ¼ b: x þ c:y: (2:1)

Algorithm 2.1 Extended Euclidean Algorithm

x=0 z:=y; b:=0; c:=1;

if then

y=0 then z:=x; b:=1; c:=0;

elsif

else

r_i:=x; r_iplus1:=y; b_i:=1; c_i:=0; b_iplus1:=0;

c_iplus1:=1;

r_iplus1>0

while loop

q:=r_i/r_iplus1; r_iplus2:=r_i mod r_iplus1;

b_iplus2:=b_i-b_iplus1*q; c_iplus2:=c_i-c_iplus1*q;

r_i:=r_iplus1; r_iplus1:=r_iplus2;

b_i:=b_iplus1; b_iplus1:=b_iplus2;

c_i:=c_iplus1; c_iplus1:= c_iplus2;

end loop;

z:=r_i; b:=b_i; c:=c_i;

end if; r ¼ x ¼ r ¼ y ¼ b ¼ c ¼

Let 230490; 43290; 1;

Example 2.3 i iþ1 i iþ1

b ¼ c ¼ 0.

iþ1 i 19

2.1 NUMBER THEORY

Step 1: q ¼ ¼ r ¼ ¼

230490=43290 5; 230490 mod 43290 14040;

iþ2

b ¼ ¼ c ¼ ¼ 5;

1 0 5 1; 0 1 5

iþ2 iþ2

¼ r ¼

r 43290; 14040;

i iþ1

b ¼ b ¼

0; 1;

i iþ1

c ¼ c ¼ 5;

1;

i iþ1

Step 2: q ¼ ¼ r ¼ ¼

43290=14040 3; 43290 mod 14040 1170;

iþ2

b ¼ ¼ 3; c ¼ þ ¼

0 1 3 1 5 3 16;

iþ2 iþ2

¼ r ¼

r 14040; 1170;

i iþ1

b ¼ b ¼ 3;

1;

i iþ1

c ¼ 5; c ¼ 16;

i iþ1

Step 3: q ¼ ¼ r ¼ ¼

14040=1170 12; 14040 mod 1170 0;

iþ2

b ¼ þ ¼ c ¼ 5 ¼ 197;

1 3 12 37; 16 12

iþ2 iþ2

r ¼ r ¼

1170; 0;

i iþ1

¼ 3; b ¼

b 37;

i iþ1

c ¼ c ¼ 197;

16;

i iþ1

b ¼ b ¼ 3; c ¼ c ¼ gcd(230490, ¼ z ¼ r ¼

16; 432900) 1170

i i i

¼ 3 þ

230490 16 43290

2.1.3 Congruences

Deﬁnition 2.6 x y, n, x congruent

Given two integers and and a positive integer is

to y modulo n n 2 y).

if divides the difference (x

Notation: x ; y(mod n):

Property 2.1 (Basic Properties of Congruences)

x ; y n) n) ¼ n)

1. (mod if and only if (x mod (y mod (Deﬁnition 2.3).

x ; y n)

2. The relation (mod is an equivalence relation (reﬂexive, symmetric,

and transitive).

20 MATHEMATICAL BACKGROUND

x ; y n) x ; y n),

3. If (mod and (mod then

1 1 2 2

þ x ; þ y n), x ; y n),

(x ) (y )(mod (x ) (y )(mod

1 2 1 2 1 2 1 2

:x ; :y n):

(x ) (y )(mod (2:2)

1 2 1 2 n

From Properties 2.1(1 and 2), it can be seen that the mod congruence relation

Z n

partitions into equivalence classes. Each equivalence class contains exactly

f0, . . . n 2 n)

one element of the set 1, 2, , 1g, namely, the common value (x mod

x

for all elements of the class. Furthermore, according to Property 2.1(3), the

addition, subtraction, and multiplication of congruence classes can be deﬁned. As

a matter of fact, the set of equivalence classes is isomorphic to

Z ¼ . . . n

{0, 1, 2, , 1}

n

where the addition, the subtraction, and the multiplication are deﬁned by

þ y) n, y) n, n, 8 x y Z :

(x mod (x mod (x:y) mod and in n

x.y ¼ y

Deﬁnition 2.7 x y Z , such that 1, then is said to

Given two elements and of n

multiplicative inverse x.

be the of If such an inverse exists, it is unique.

Notation: 1 :

y ¼ x

x gcd(x, n) ¼

has a multiplicative inverse if and only if 1.

Property 2.2

Proof x.y ¼ n, x.y ¼ q.nþ1 x n

If 1 mod then so that any divisor of and is also a

gcd(x, n) ¼

divisor of 1. Thus 1.

gcd(x, n) ¼ b c ¼ b.x þ c.n,

If 1, then (relation (2.1)) there exist and such that 1 so

21

x ¼ b.

that

More generally, we have the following.

Properties 2.3

g ¼ gcd(a, n). a.x ; d n) x

1. Let Then the equation (mod has a solution if and

g d.

only if divides a.x ; d n)

2. The solutions of (mod are the same as the solutions of

; n/g).

(a/g).x (d/g) (mod

g n/g.

3. There are solutions, all of them congruent modulo

Proof ax ; d n), a.x 2 d ¼ q.n. g a n,

1. If (mod then As divides both and it also

d. g d, d ¼ q.g. g

divides If divides then According to (2.1), is a linear

21

2.1 NUMBER THEORY

a n; g ¼ b.a þ c.n. d ¼ q(b.a þ c.n) x ¼ q.b

combination of and that is, So and

is a solution.

g d a.x ; d n), a.x 2 d ¼ q.n, 2

2. If divides and (mod that is, then (a/g).x

¼ q.(n/g) ; n/g). ;

(d/g) and (a/g).x (d/g) (mod Inversely, if (a/g).x

n/g) a.x ; d n).

(d/g) (mod then (mod

a/g n/g

3. As and are relatively prime, then there is a unique solution within

21

Z x ¼ x ¼ n/g.

, namely, (d/g).(a/g) mod The complete set of solutions

n/g 0

Z is

within n x ¼ x þ k:(n=g), 8k ¼ . . . g

0, 1, , 1:

k 0 , x 2 þ 2

k , g x (n/g), then (n/g) 1 (g 1).(n/g)

Observe that if and k

0

¼ n 2 1. Remainder Theorem) s

Consider pairwise relatively

Properties 2.4 (Chinese

m m . . . m M.

prime integers , , , whose product is equal to Then the system

s

1 2 N ; r m

(mod ),

1 1

m

N ; r (mod ),

2 2

...

N ; r m

(mod ), (2:3)

s s

N Z a m):

has a unique solution within (jaj stands for mod

M m

N ¼ S m :jr =m j , (2:4)

i m

i i

1is M

i

where

M ¼ P m m ¼ M=m :

; (2:5)

i i

i

1is

residues m

r are called modulo .

The i i

Proof m

In order to compute a solution of system (2.3) observe that every is

i

m j = i) m m ¼

relatively prime with every ( so that every is relatively prime with

j j j

m

M/m . Then has a multiplicative inverse and

j j

N ¼ =m m þ =m m þ

(m ):(r ) mod (m ):(r ) mod

1 1 2 2

1 1 2 2

þ =m m

(m ):(r ) mod , (2:6)

s s

s s

is obviously a solution. The uniqueness is deduced from the fact that different sys-

tems have different solutions, and that there are exactly as many different systems as

Z .

elements in M

22 MATHEMATICAL BACKGROUND

21

m

The computation of (m ) mod can be performed with the extended Euclidean

i i M/m b c

m is relatively prime with , the algorithm generates and such that

algorithm: as i j

¼ b:m þ c:(M=m

1 ),

i j

and 1 ¼ c m :

) mod

(m i

i N

Garner’s algorithm 2.2 ([GAR1959], [MEN1996]) computes using a technique

slightly different from the straight computation of (2.4). It computes ﬁrst the

mixed-radix digits within a preliminary step of a procedure step computing the

base-B digits through a mixed-radix to base-B conversion (see mixed-radix system—

Chapter 3). 21

A procedure using the Euclidean algorithm to compute (m )

inversion_step j

m

mod is ﬁrst deﬁned as

i inversion_step (m(j), m(i): natural; invm(j):

procedure in out

natural);

Algorithm 2.2 Garner’s Algorithm

N r ¼ N m

Assume is given, according to (2.3), by its set of residues mod :

i i

i 2..s

for in loop

c(i):=1;

j 1..(i-1)

for in loop

inversion_step (m(j), m(i), invm(j));

c(i):=invm(j)*c(i) mod m(i);

end loop;

end loop;

u:=r(1); x:=u; b(1):=1;

i 2..s

for in loop

b(i):=b(i-1)*m(i-1);

end loop;

i 2..s

for in loop

2

u:=(r(i) x)*c(i) mod m(i);

x:=x+u*b(i);

end loop;

Examples 2.4

fr g ¼ f1,

1. Let 2, 3, 4, 5g be the set of remainders (residual expression) of a

i g ¼ f2,

N fm 3,

natural number with respect to the respective set of moduli i

## N

5, 7, 11g. To compute the base-10 expression of using (2.4), one ﬁrst

fm g f1/m m g.

needs to compute and mod A straightforward base-10

i i i 23

2.1 NUMBER THEORY

calculation leads to

M ¼ P m ¼ ¼

2:3:5:7:11 2310,

i

1is

¼ ¼

{m } {M=m } {1155, 770, 462, 330, 210},

i

i

while the Euclidean algorithm is used to compute

m ¼

mod } {1, 2, 3, 1, 1}:

{1=m i

i

Formula (2.4) yields

þ þ þ þ j

N ¼ j1155:j1:1j 770:j2:2j 462:j3:3j 330:j4:1j 210:j5:1j ,

2 3 5 7 11 2310

¼

N ¼ j6143j 1523:

2310

2. Garner’s algorithm is now used to solve the same problem. The Euclidean

algorithm is used in the ﬁrst loop of Algorithm 2.2. It computes:

m ¼ ¼ cð2Þ

i :¼ j :¼ ! mod 1=2 mod 3 2;

2; 1 1=m :¼ 2;

1 2

i :¼ j :¼ ! m ¼ ¼ cð3Þ :¼

3; 1 1=m mod 1=2 mod 5 3; 3;

1 3

i :¼ j :¼ ! m ¼ ¼ cð3Þ

3; 2 1=m mod 1=3 mod 5 2; :¼ 1;

2 3

i :¼ j :¼ ! m ¼ ¼ cð4Þ :¼

4; 1 1=m mod 1=2 mod 7 4; 4;

1 4

i :¼ j :¼ ! m ¼ ¼ cð4Þ :¼

4; 2 1=m mod 1=3 mod 7 5; 6;

2 4

i :¼ j :¼ ! m ¼ ¼ cð4Þ

4; 3 1=m mod 1=5 mod 7 3; :¼ 4;

3 4

i :¼ j :¼ ! m ¼ ¼ cð5Þ :¼

5; 1 1=m mod 1=2 mod 11 6; 6;

1 5

i :¼ j :¼ ! m ¼ ¼ cð5Þ :¼

5; 2 1=m mod 1=3 mod 11 4; 2;

2 5

i :¼ j :¼ ! m ¼ ¼ cð5Þ :¼

5; 3 1=m mod 1=5 mod 11 9; 7;

3 5

m ¼ ¼ cð5Þ

i :¼ j :¼ ! mod 1=7 mod 11 8;

5; 4 1=m :¼ 1:

4 5 b( j) P m

The second loop computes the weights as :

j i 2 i

1 1

bð1Þ :¼ bð2Þ :¼ bð1Þ:m ¼ bð3Þ :¼ bð2Þ:m ¼ ¼

1; 2; 2:3 6;

1 2

bð4Þ :¼ bð3Þ:m ¼ ¼ bð5Þ :¼ bð4Þ:m ¼ ¼

6:5 30; 30:7 210:

3 4

x

The third loop ﬁnally computes as

u :¼ rð1Þ ¼ x :¼ u ¼

1; 1

i :¼ u :¼ ðrð2Þ 2 xÞ:cð2Þ ¼ ð2 ¼

2; mod 3 1Þ:2 mod 3 2;

x :¼ x þ u:bð2Þ ¼ þ ¼

1 2:2 5;

i :¼ u :¼ ðrð3Þ xÞ:cð3Þ ¼ ð3 2 ¼

3; mod 5 5Þ:1 mod 5 3;

x :¼ x u:bð3Þ ¼ þ ¼

5 3:6 23;

i :¼ u :¼ ðrð4Þ xÞ:cð4Þ ¼ ð4 ¼

4; mod 7 23Þ:4 mod 7 1;

x :¼ x þ u:bð4Þ ¼ þ ¼

23 1:30 53;

i :¼ u :¼ ðrð5Þ xÞ:cð5Þ ¼ ð5 ¼

5; mod 11 53Þ:1 mod 11 7;

x :¼ x þ u:bð5Þ ¼ þ ¼

53 7:210 1523:

24 MATHEMATICAL BACKGROUND

Observe that the ﬁrst two loops are independent and therefore may be computed

c(i) b(i)

in parallel. Moreover, if the modulus system is ﬁxed, the and are computed

once then stored for further use.

Deﬁnitions 2.8 x Z n multiplicative

1. The set of elements of relatively prime with is the

n

group Z :

n

Z ¼ [ Z jgcd(x, n) ¼

{x 1}:

n

n

f Z

Euler phi function (n) is the number of elements in .

2. The n

Z

Z is the set of invertible elements of . In particular,

According to Property 2.2, n n

p

if is a prime number then

f

Z ¼ . . . p p) ¼ p

{1, 2, , 1} and ( 1:

p Little Theorem) p x

Let be a prime.. Any integer

Properties 2.5 (Fermat’s

p p21

x ; x p), x p x ;

satisﬁes (mod and any integer not divisible by satisﬁes 1

p).

(mod

Proof x p i..x ; j.x p), 2 j).x ¼ q.p,

If is not divisible by and if (mod that is, (i then

i ; j p).

(mod Thus . . . ; . . . p p),

(1:x):(2:x): ((p 1):x) 1:2: ( 1)(mod

p 2 x

as the 1 above multiples of are distinct and nonzero, they must be congruent to

. . . p 2

1, 2, 3, , 1 in some order.

So p1

p ; p p),

( 1)!:x ( 1)! (mod

or p1

ð p ; p):

1)!:(x 1) 0 (mod

p 2

As does not divide (p 1)!, p1 ; p),

(x 1) 0 (mod 25

2.2 ALGEBRA

that is, p1 p

x ; p) x ; x p):

1(mod and (mod

p ; x ; p).

x p, x 0 (mod

If is divisible by then

p x p r ; s p 2

Let be a prime.. If is not divisible by and if (mod 1),

Corollary 2.1

then r s

; x p):

x (mod q p21 q r2s

; ; x

Proof r . s. r ¼ q.(p 2 þ s ; (x )

Assume that Then 1) and 1 1

r s

p), x ; x p).

(mod so that (mod

Deﬁnitions 2.9 t

t x ;

order x Z is the least positive integer such that 1

1. The of an element of n

n).

(mod

f

x Z x

2. If the order of is equal to the number (n) of elements in , then is said to

n

generator primitive element Z .

be a or of n

Z cyclic.

Z has a generator, then is said to be

3. If n n f

¼ fx x x . . . x g.

x Z 1 2 3 (n)

, , , ,

Observe that if is a generator then n

Example 2.5

Z ¼ f0, Z ¼ f1,

1, 2, 3, 4, 5, 6g and 2, 3, 4, 5, 6g;

7 7

f ¼

(7) 6;

7 is prime and

; ; ; ; ;

1 3 6 3 6

1 1 (mod 7), 2 1 (mod 7), 3 1 (mod 7), 4 1 (mod 7), 5

;

2 1 (mod 7).

1 (mod 7), 6

There are two generators: 3 and 5. For example,

; ; ; ; ;

1 2 3 4 5

3 3 (mod 7), 3 2 (mod 7), 3 6 (mod 7), 3 4 (mod 7), 3

;

6 1 (mod 7).

5 (mod 7), 3

2.2 ALGEBRA

2.2.1 Groups

Deﬁnition 2.10 group , G

A (G, 1) consists of a set with a binary operation and

identity element

an 1 satisfying the following three axioms:

x z) ¼ y) z,8x, y, z [ G

1. (y (x (associativity);

x ¼ x ¼ x, 8 x [ G element);

2. 1 1 (identity

26 MATHEMATICAL BACKGROUND

21

x G x inverse x,

3. for each element of there exists an element , called the of

such that 1 1

¼ x x ¼

x x 1:

If, furthermore,

y ¼ y x,8x, y [ G commutative

x (commutativity), the group is said to be (or

4. Abelian). semigroup.

Axioms 1 and 2 deﬁne a

Examples 2.6

þ, þ, .,

(Z, 0), (Z , 0), (Z , 1)

n n

The following deﬁnitions generalize Deﬁnitions 2.9.

Deﬁnitions 2.11

order x G t

1. The of an element of a ﬁnite group is the least positive integer such that

t

x ¼ x x x ¼ 1:

x n G, x

2. If the order of is equal to the number of elements in then is said to be a

generator G.

of

G G cyclic.

3. If has a generator, then is said to be

x G

The order of an element of a ﬁnite group divides the number of

Property 2.6

## G..

elements in

Proof H G, G

First observe that if is a subgroup of then an equivalence relation on

g ; g h H g ¼ g

can be deﬁned: if there exists an element in such that .h . The

1 2 1 2

jHj

number of elements in an equivalence class is equal to the number of elements

H. jGj G jHj. jG/Hj, G/H

in Thus the number of elements in is equal to with the set

jG/Hj

of classes and the number of classes. In other words the number of elements

of a subgroup divides the number of elements of the group. It remains to observe that

t

fx, x . . . x ¼ t x,

2

the set , , 1g, where is the order of is a subgroup, so that the number

t G.

of elements of the subgroup divides the number of elements in

Example 2.7

.,

(Z , 1);

7

3 and 5 are generators; f2, f2,

the subgroup generated by 2 is 4, 1g; the corresponding classes are then 4,

f6,

1g and 5, 3g; the number of elements (3) of the subgroup divides the number of

## Z

elements (6) of .

7 27

2.2 ALGEBRA

2.2.2 Rings

Deﬁnition 2.12 ring þ, , R

A (R, 0, 1) consists of a set with two binary

þ , additive identity element multiplicative identity

operations and an 0, and a

element 1 satisfying the following axioms:

þ,

1. (R, 0) is a commutative group;

x z) ¼ y) z,8x, y, z [ R

2. (y (x (associativity);

x ¼ x ¼ x,8x [ R;

3. 1 1

x þ z) ¼ y) þ z) þ y) z ¼ z) þ z),8x, y, z [ R

4. (y (x (x and (x (x (y

(distributivity).

If, furthermore,

x y ¼ y x, 8x, y [ R commutative.

5. (commutativity), the ring is said to be

Examples 2.8

þ, þ,

., .,

(Z, 0, 1), (Z , 0, 1)

n

2.2.3 Fields

Deﬁnition 2.13 ﬁeld þ, , F

A (F, 0, 1) consists of a set with two binary

þ , additive identity element multiplicative identity

operations and an 0, and a

element 1 satisfying the following axioms:

þ, ,

1. (F, 0, 1) is a commutative ring;

## F

2. all nonzero elements of have a multiplicative inverse.

Example 2.9

þ, p

.,

(Z , 0, 1), where is a prime.

p

2.2.4 Polynomial Rings

Deﬁnitions 2.14

F polynomial x F

1. If is a ﬁeld, then a in the indeterminate over is an expression

of the form n n1

f ¼ a : x þ a :x þ þ a : x þ a

(x) ,

n n1 1 0

[ F, 8 i [ f0, . . . ng.

a 1, ,

where i = degree f

m a 0 is the of (x). It is

2. The largest integer (if any) such that m

deg( f a leading coefﬁcient.

denoted ) and is called the If all the coefﬁcients

m

f f zero polynomial

of (x) are equal to 0, then (x) is called the and its degree

21. constant

deﬁned to be equal to The 0-degree polynomials are also called

polynomials.

28 MATHEMATICAL BACKGROUND

monic polynomial

3. A is a polynomial whose leading coefﬁcient is equal to 1.

F[x]

4. The polynomial ring is the ring formed by the set of all polynomials in

x F.

the indeterminate with coefﬁcients in The two operations are the standard

polynomial addition and multiplication, with coefﬁcient arithmetic performed

F. zero polynomial.

in The additive identity element 0 is the The multiplica-

tive identity element 1 is the monic constant polynomial.

Deﬁnition 2.15 F

Thanks to the fact that is a ﬁeld, so that all the nonzero

coefﬁcients have an inverse, the standard polynomial division can also be per-

g(x) h(x) = F[x],

formed. Thus, if and 0 are polynomials in then there exist two

q(x) quotient) r(x) remainder) F[x]

polynomials (the and (the in such that

g(x) ¼ q(x):h(x) þ r(x), deg(r) , deg(h):

where (2:7)

Notation: r(x) ¼ g(x) h(x), q(x) ¼ g(x) h(x):

mod div

Deﬁnitions 2.16 g(x) h(x), h(x) g(x) h(x)

1. Given two polynomials and divides (or is a divisor of

g(x)) q(x) g(x) ¼ q(x).h(x).

if there exists a polynomial such that

g(x) h(x), greatest common

2. Given two polynomials and not both equal to 0, the

divisor g(x) h(x)

of and is the monic polynomial of greatest degree which

g(x) h(x).

divides both and

gcd(0, ¼

3. 0) 0. f

4. A polynomial (x) of degree at least 1 is said to be irreducible if it cannot be

written as the product of two polynomials, each of positive degree.

Euclidean algorithm

A variant of the for polynomials (VZG2003) expresses the

g(x) h(x)

greatest common divider of two polynomials and in the form

gcd(g, h) ¼ b(x):g(x) þ c(x):h(x):

u(x) v(x)

The algorithm is based on the fact that if and are two polynomials such

that deg(u) ¼ m, deg(v) ¼ t m . t,

and

that is, m m1

:x þ u :x þ þ u :x þ u

u(x) ¼ u ,

m m1 1 0

t t1

v(x) ¼ v : x þ v : x þ þ v : x þ v ,

t t1 1 0 29

2.2 ALGEBRA

then 1 mt t t1 1 mt

v(x):u :(v :x ¼ : x þ v :x þ þ v : x þ v :(v : x

) (v ):u )

m t t t1 m t

1 0

m 0

¼ u : x þ r (x)

m

0

deg(r , m,

where ) so that

1 mt 0 m1

:(v :x r þ u :x þ þ u :x þ u

u(x) ¼ ) (x))

(v(x):u m t m1 1 0

1 mt

¼ v(x):u :(v :x þ r(x)

) (2:8)

m t

where m1 0

:x þ þ u :x þ u r

r(x) ¼ u (x)

m1 1 0

so that deg(r) , m max(deg(r), deg(v)) , deg(u):

and

Furthermore, gcd(u,v) ¼ gcd(v,r):

The sequence of operations is almost the same as for computing the greatest

r(0), r(1), r(2), . . .

common divider of two integers. A series of polynomials are

deg(g) . deg(h)

generated. Initially, assume that and deﬁne

r(0) ¼ g(x) r(1) ¼ h(x):

and

At each step the decomposition (2.8) is used:

u(x) ¼ r(i v(x) ¼ r(i), m ¼ deg(r(i t ¼ deg(r(i)),

1), 1)),

deg(r(i . deg(r(i))

1))

so that r(i ¼ q(i): r(i) þ r(i þ

1) 1)

where 1 mt

q(i) ¼ u :(v : x r(i þ ¼ r(i q(i):r(i),

) , 1) 1)

m t deg(r(i þ , m ¼ deg(r(i

1)) 1)):

r(i) r(i þ deg(r(i)) ,

At the end of the step, and 1) are interchanged if

deg(r(i þ 1)).

30 MATHEMATICAL BACKGROUND

Operations:

r(0) ¼ g(x),

r(1) ¼ h(x),

r(0) ¼ r(1):q(1) þ r(2), deg(r(1)) , deg(r(2)) r(1) r(2),

if interchange and

r(1) ¼ r(2):q(2) þ r(3), deg(r(2)) , deg(r(3)) r(2) r(3),

if interchange and

r(2) ¼ r(3):q(3) þ r(4), deg(r(3)) , deg(r(4)) r(3) r(4),

if interchange and

...

r(n ¼ r(n þ r(n deg(r(n , deg(r(n

3) 2):q(n 2) 1), if 2)) 1))

r(n r(n

interchange 2) and 1),

r(n ¼ r(n þ r(n),

2) 1):q(n 1)

where deg(r(0)) . deg(r(1)) . . deg(r(n)) ¼ 0

and gcd(r(i), r(i þ ¼ gcd(r(i þ r(i þ

1)) 1), 2)),

so that gcd(g, h) ¼ gcd(r(0), r(1)) ¼ ¼ gcd(r(n r(n)):

1),

r x r(n). r ¼

0

Let be the coefﬁcient of in If 0, then

0 0

gcd(g, h) ¼ gcd(r(n ¼ r(n

1), 0) 1):

=

r 0, then

If 0 ¼

gcd(g, h) ¼ gcd(r(n r ) 1:

1), 0

r(0), r(1), r(2), . . . r(n),

In parallel with the computation of , two series of poly-

b(i) c(i)

nomials and are generated:

b(0) ¼ 1,

b(1) ¼ 0,

b(2) ¼ b(0) b(1):q(1), deg(r(1)) , deg(r(2)) b(1) b(2),

if interchange and

...

b(n ¼ b(n b(n deg(r(n , deg(r(n

1) 3) 2):q(n 2), if 2)) 1))

b(n b(n

interchange 2) and 1),

b(n) ¼ b(n b(n

2) 1):q(n 1): 31

2.2 ALGEBRA

c(0) ¼ 0,

c(1) ¼ 1,

c(2) ¼ c(0) c(1):q(1), deg(r(1)) , deg(r(2)) c(1) c(2),

if interchange and

...

c(n ¼ c(n c(n deg(r(n , deg(r(n

1) 3) 2):q(n 2), if 2)) 1))

c(n c(n

interchange 2) and 1),

c(n) ¼ c(n c(n

2) 1):q(n 1):

It can be demonstrated by induction that

r(i) ¼ b(i):g(x) þ c(i):h(x), 8 i ¼ . . . n:

0, 1, 2, ,

r ¼

So, if 0, then

0 gcd(g, h) ¼ r(n ¼ b(n þ c(n

1) 1):g(x) 1):h(x),

r =

and if 0, then

0 1 1 1

:r(n) ¼ r :b(n):g(x) þ r :c(n):h(x):

gcd(g, h) ¼ ¼ r

1 0 0 0

u r(i 2 v r(i), r r(i þ b

In the following algorithm stands for 1), for for 1), for

b(i 2 d b(i), bb b(i þ c c(i 2 e c(i), cc c(i þ

1), for for 1), for 1), for and for 1):

Algorithm 2.3 Variant of the Extended Euclidean Algorithm for Polynomials

u:=g; v:=h; b:=1; c:=0; d:=0; e:=1;

m:=degree(u); t:=degree(v);

t=0

if then -1

v(0)=0 z=u; z:=1; b:=0; c:=(v(0)) ;

if then else end if;

m=0

elsif then -1

u(0)=0 z=v; b:=0; c:=1; z:=1; b:=(u(0)) ;

if then else

end if;

else t>0

while loop

m<t swap(u, v); swap(b, d); swap(c, e); swap(m, t);

if then

end if; -1 m-t

q:=u(m)*(v(t)) *x ; r:=u-v*q; bb:=b-d*q; cc:=c-e*q;

u:= v; v:=r; b:=d; c:=e; d:=bb; e:=cc;

m:=t; t:=degree(v);

end loop; -1

v(0)=0 z:=u; z:=1; b:=d*(v(0)) ;

if then else

-1

;

c:=e*(v(0))

end if;

end if;

32 MATHEMATICAL BACKGROUND

2.2.5 Congruences of Polynomial

Deﬁnition 2.17 g(x), h(x), f(x) F[x], g(x)

Given three polynomials and in is

h(x) f(x) f(x) g(x) 2 h(x).

congruent to modulo if divides

Notation: g(x) ; h(x)(mod f (x)):

Properties 2.7 (Properties of Congruences)

g(x) ; h(x) f(x)) f(x)) ¼ f(x))

1. (mod if and only if (g(x) mod (h(x) mod

(Deﬁnition 2.15);

g(x) ; h(x) f(x))

2. the relation (mod is an equivalence relation (reﬂexive,

symmetric, and transitive);

g ; h f(x)) g ; h f(x)),

3. if (x) (x) (mod and (x) (x) (mod then

1 1 2 2

g þ h ; g þ h f g h ; g h

(x) (x) (x) (x)(mod (x)), (x) (x) (x) (x)

1 1 2 2 1 1 2 2

; g f

f g (x):h (x) (x):h (x)(mod (x)): (2:9)

(mod (x)), 1 1 2 2

From Properties 2.7(1 and 2) it can be seen that the congruence relation partitions

F[x] n f(x),

into equivalence classes. If is the degree of then each equivalence class

d , n. F

contains exactly one polynomial of degree So, if is a ﬁnite ﬁeld, then the

n

jFj jFj

number of equivalence classes is equal to , where is the number of elements

## F.

in Furthermore, according to Property 2.7(3), the addition, subtraction, and

multiplication of congruence classes can be deﬁned. As a matter of fact, the set of

equivalence classes is isomorphic to

[ F½xjdeg(g) , n}

{g(x)

where the addition, the subtraction, and the multiplication are deﬁned by

þ h(x)) f h(x)) f f

(g(x) mod (x), (g(x) mod (x), (g(x):h(x)) mod (x):

F[x]/f(x).

The set of equivalence classes is denoted

Properties 2.8

F[x]/f(x)

1. is a commutative ring.

f(x) F[x]/f(x)

2. If is irreducible, then is a ﬁeld.

Proof

1. Consequence of Property 2.7(3).

f(x) f(x) g(x) =

2. If is irreducible, then the greatest common divisor of and 0 is

b(x) c(x)

1. Using the Euclidean algorithm (Algorithm 2.2), and can be

33

2.2 ALGEBRA

computed such that ¼ b(x):f þ c(x):g(x)

1 (x)

and 1

c(x) ¼ f

(g(x)) mod (x):

f(x)

Deﬁnition 2.18 p F ¼ Z , and be an irreducible polynomial of

Let be a prime, p n

F[x]/f(x) q ¼ p

n Z . The corresponding ﬁeld contains elements and is

degree over p GF(q)

F or (Galois ﬁeld).

called either q n

q ¼ p

As a matter of fact, it can be demonstrated that any ﬁnite ﬁeld contains

p n, F

elements, for some prime and some positive integer and is isomorphic to q

f(x) n Z

(whatever the irreducible polynomial of degree over ). In particular, if

p

## Z

n ¼ F is isomorphic to .

1, then the corresponding ﬁeld p p F isomorphic

The set of 0-degree polynomials (the constants) is a subﬁeld of q

g(x) F

F . If is a 0-degree polynomial (an element of ) then, according to the

to p p

p ¼ g(x). Conversely, it can be demonstrated that if

Fermat’s little theorem, (g(x)) p ¼ g(x), g(x)

g(x) then is a constant.

a polynomial satisﬁes the condition (g(x))

## F

F is that the set of nonzero polynomials is a

Another interesting property of q q

g(x) F , and assume

cyclic group. Let be a nonzero polynomial, that is, an element of q

g(x) t. t q 2

that the order of is According to the Property 2.6, divides 1, so that

q21 t.k k

¼ ¼ ¼ g(x)

(g(x)) (g(x)) 1 1. Consider now a polynomial and deﬁne

r r ¼ 2 p 2

h(x) ¼ , where (q 1)/( 1). According to the previous property,

(g(x))

p21 q21 p

¼ ¼ ¼ h(x), h(x)

(g(x)) 1 and (h(x)) so that is a constant polynomial.

(h(x)) þ

A last property, useful for performing arithmetic operations, is that (g(x)

p p p

¼ þ

h(x)) (g(x)) (h(x)) . It is a straightforward consequence of the fact that

p!/(i!).(p 2 i)!) p, i ¼

all the binomial coefﬁcients ( are multiples of except for 0

p.

or To summarize:

Properties 2.9 (Some Useful Properties of Finite Fields)

## F F F

1. The set of 0-degree polynomials in is a subﬁeld of isomorphic to .

q q p

p

g(x) F ¼ g(x)

2. Given in , then (g(x)) (Fermat’s little theorem).

p p

g(x) F ¼ g(x); g(x) [ F

3. Given in such that (g(x)) then .

q p

## F F

4. The set of nonzero polynomials of is a cyclic group denoted .

q q

q ¼ g(x).

g(x) F , then (g(x))

5. Given in q p p p

g(x) h(x) F þ h(x)) ¼ þ

6. Given and in , then (g(x) (g(x)) (h(x)) .

q

n n1

r ¼ 2 2 r ¼ þ p þ p þ þ p g(x)

2

7. If (p 1)/(p 1), that is, 1 , and is an

r

## F F

element of , then (g(x)) is an element of .

q p

34 MATHEMATICAL BACKGROUND

a

p ¼ n ¼ f(x) ¼ þ x þ x x ; þ x f(x); ¼ x

4 4

2, 4, 1 so that 1 mod

Example 2.10

F :

is a generator of the cyclic group 16

a ¼ x,

1

a ¼ x

2 2 ,

a ¼ x

3 3 ,

a ¼ x ; þ x,

4 4 1

a ¼ x::(1 þ x) ¼ x þ x

5 2 ,

a ¼ x:(x þ x ¼ x þ x

6 2 2 3

) ,

a ¼ x:(x þ x ¼ x þ x ; þ x þ x

7 2 3 3 4 3

) 1 ,

a a

¼ ¼ þ x) ¼ þ x

8 4 2 2 2

( ) (1 1 ,

a ¼ x:(1 þ x ¼ x þ x

9 2 3

) ,

a ¼ x:(x þ x ¼ x þ x ; þ x þ x

10 3 2 4 2

) 1 ,

a ¼ x:(1 þ x þ x ¼ x þ x þ x

11 2 2 3

) ,

a ¼ x:(x þ x þ x ¼ x þ x þ x ; þ x þ x þ x

12 2 3 2 3 4 2 3

) 1 ,

a ¼ x:(1 þ x þ x þ x ¼ x þ x þ x þ x ; þ x þ x

13 2 3 2 3 4 2 3

) 1 ,

a ¼ x:(1 þ x þ x ¼ x þ x þ x ; þ x

14 2 3 3 4 3

) 1 ,

a ¼ x:(1 þ x ¼ x þ x ;

15 3 4

) 1;

g(x) ¼ g þ g þ g þ g

2 3

Given a polynomial .x .x .x , then

0 1 2 3

¼ g þ g :x þ g :x þ g :x ; g þ g :x þ g :(1 þ x) þ g :x :(1 þ x)

2 2 4 6 2 2

(g(x)) 0 1 2 3 0 1 2 3

¼ þ g þ g :x þ þ g þ g :x

2 3

(g ) (g ):x ;

0 2 2 1 3 3

¼ g(x),

2

if (g(x)) then þ g ¼ g g ¼ g g þ g ¼ g

g , , ;

0 2 0 2 1 1 3 3

thus ¼ g ¼ g ¼

g 0,

1 2 3

## F

g(x) ¼ g , that is, an element of (Property 2.9(3)).

and p

0 35

2.3 FUNCTION APPROXIMATION

2.3 FUNCTION APPROXIMATION

Numerous techniques are used to evaluate functions. According to the type of the

function at hand, some evaluation methods may be more appropriate than others.

For instance, a method well suited for a polynomial may not be the best for an expo-

nential function. Polynomial approximation is most often recommended for function

evaluation as any continuous function can be approximated in this way, and the

implementation only consists of additions, multiplications, and powers.

Taylor and MacLaurin series are the most classic approaches to approximate

functions. The series lead to precise numerical techniques to compute a function

very near to one point, but precision can be lost for a bigger range of values.

Trigonometric, logarithmic, and exponential function computations are typical

applications.

Deﬁnition 2.19

Taylor series. f(x) þ

1. If a function has continuous derivatives up to (n 1)th

order, then this function can be expanded in the following fashion:

i i i

S a) =i!):(d f þ R

((x (x)=dx ) , (2:10)

n

x¼a

0in

Taylor expansion a. R nþ1

called a at point is called the remainder after

n x,

terms. When this expansion converges over a certain range of that is, when

R ¼ 0, (2:11)

lim

n!1 n

Taylor series f(x) a.

the expansion is called a of at point

MacLaurin series. a ¼

2. If (2.10) is expressed at point 0, the series is called a

MacLaurin series: i i i

S =i!):(d f þ R :

((x) (x)=dx ) (2:12)

n

x¼0

0in

Taylor– MacLaurin series expansions of exponential functions:

Examples 2.11

x

e ¼ þ x þ x =2! þ x =3! þ 1 , x , 1

2 3

1 (2::13)

x:x

e ¼ x þ x =2! x =3! þ x =4! 1 , x , 1

2 4 6 8

1 (2:14)

x x: a

a ¼ e ¼ þ x: a þ a) =2! þ a) =3! þ 1 , x , 1

ln 2 3

1 ln (x: ln (x: ln (2:15)

36 MATHEMATICAL BACKGROUND

Taylor– MacLaurin series expansions of logarithmic functions:

x ¼ =2 þ =3 , x

2 3

ln (x 1) (x 1) (x 1) 0 2 (2:16)

x ¼ þ þ þ =3

3

ln 2:½(x 1)=(x 1) ((x 1)=(x 1))

=5 þ x .

þ þ 5 0 (2:17)

((x 1)=(x 1))

x ¼ þ =2 þ =3 þ x

2 3

ln (x 1)=x ((x 1)=x) ((x 1)=x) 1=2 (2:18)

=2 þ x =3 x =4 þ x =5 , x

þ x) ¼ x x 2 3 4 5 1 1 (2:19)

ln (1

Taylor– MacLaurin series expansions of trigonometric functions:

x ¼ x x =3! þ x =5! x =7! þ 1 , x , 1

3 5 7

sin (2:20)

=2! þ x =4! x =6! þ 1 , x , 1

x ¼ x x

2 4 6 (2:21)

cos =3 þ =15 þ x =315

x ¼ x þ x

3 5 7

2:x 17:

tan p

þ þ :(2 :x =(2:n)! þ jxj , =2

2:n 2:n 2:n1

2 1):B (2:22)

n

=45 =945 :B : x þ

x ¼ x=3 x 3 5 2:n 2:n1

2:x 2 (2:n)!

cot 1=x n p

, jxj ,

0 (2:23)

## B

where are the Bernoulli numbers ([ROS2000]).

n

2.4 BIBLIOGRAPHY

Mathematical Analysism,

[APO1974] T. M. Apostol, 2nd ed., Addison Wesley, Reading, MA,

1974. A Course in Computational Algebraic Number Theory,

[COH1993] H. Cohen, Springer-

Verlag, Berlin, 1993. IRE Trans. Electron. Comput.,

[GAR1959] H. Garner, The residue number system. EC-8:

140 – 147 (1959). Modern Algebra with Applications,

[GIL2003] W. J. Gilbert and W. K. Nicholson, John

Wiley & Sons, Hoboken, NJ, 2003.

Topics in Algebra,

[HER1975] I. N. Herstein, 2nd ed., Xerox College Pub., Lexington, MA,

1975. Algebra,

[HUN1974] T. W. Hungerford, Holt, Rinehart and Winston, New York, 1974.

A Course in Number Theory and Cryptography,

[KOB1994] N. Koblitz, Springer-Verlag,

New York, 1994. Finite Fields for Computer Scientists and Engineeers,

[McC1987] R. J. McCeliece, Kluwer

Academic Publishers, Boston, 1987. 37

2.4 BIBLIOGRAPHY Handbook of Applied

[MEN1996] A. J. Menezes, P.C. van Oorschot, and S. C. Vanstone,

Cryptography, CRC Press, Boca Raton, FL, 1996.

Elementary Number Theory and Its Applications,

[ROS1992] K. H. Rosen, Addison-Wesley,

Reading, MA, 1992. Handbook of Discrete and Combinatorial

[ROS2000] K. H. Rosen (Editor-in-Chief),

Mathematics, CRC Press, Boca Raton, FL, 2000.

Principles of Mathematical Analysis

[RUD1976] W. Rudin, (International Series in Pure &

Applied Mathematics), McGraw-Hill Science/Engineering/Math, New York, 1976.

Modern Computer Algebra,

[VZG2003] J. von zur Gathen and J. Gerhard, Cambridge Univer-

sity Press, Cambridge, UK, 2003.

3

## NUMBER REPRESENTATION

Arithmetic deals with operations on numbers: addition, subtraction, and so on.

Thus number representation is a fundamental topic in arithmetic ([ERC2004],

number representation system

[PAR1999]). The choice of a has repercussions on

the complexity of the algorithms executing the arithmetic operations, and thus on

the costs and performances of the circuits that implement those algorithms. Apart

from the cost and performance, another aspect to take into account, when choosing

a number representation system, is the interface with other circuits or, simply, the

residue number system

human interface. Consider an example: the (RNS) allows

the implementation of very fast and cost-effective arithmetic circuits. Nevertheless,

the RNS needs some type of relatively expensive input and output interfaces since

## AD/DA

human beings don’t use it, and the converters don’t understand this type

of representation. Thus the use of a RNS is limited to cases in which the extra

cost of the RNS encoding and decoding is negligible with respect to the total

cost. In this chapter the most common number representation systems are described.

The chapter is divided into three sections corresponding to natural numbers,

integers, and real numbers.

3.1 NATURAL NUMBERS

3.1.1 Weighted Systems

Any natural number (nonnegative integer) can be represented, in a unique way, in

i B

B of some natural number greater than 1, each of

the form of a sum of powers

Synthesis of Arithmetic Circuits: FPGA, ASIC, and Embedded Systems

By Jean-Pierre Deschamps, Géry J. A. Bioul, and Gustavo D. Sutter

#

Copyright 2006 John Wiley & Sons, Inc. 39

40 NUMBER REPRESENTATION

## B.

them multiplied by a natural number smaller than The following theorem deﬁnes

base-B numeration system.

the B x

Given a natural number greater than 1, any natural number

Theorem 3.1 n

B can be expressed in the form

smaller than n1 n2

x ¼ x :B þ x :B þ þ x :B 0

n1 n2 0

x B.

where every coefﬁcient is a natural number smaller than Furthermore, there is

i x x x.

) representing

only one possible vector (x n21 n22 0 x :

The following algorithm computes the coefﬁcients i

Algorithm 3.1 2

i 0..n 1 x(i):= x mod B; x:= x/B;

for in loop end loop;

Deﬁnitions 3.1 B system),

1. The most commonly used values of are 10 (decimal 2 (binary

system), system), system). x

16 (hexadecimal and 8 (octal The coefﬁcients i

x x.

of the base-B representation of are called the base-B digits of The

bits.

binary digits are called The hexadecimal digits 10, 11, 12, 13, 14, and

15 are usually replaced by letters: A, B, C, D, E, and F. i

positional B

2. This type of representation is called as the weight associated with

i,

x depends on that is, on the position of the digit within the vector

the digit i

x x ).

(x n21 n22 0

3. The base-B digits could in turn be encoded in another base. As an example,

B ¼

if 10 and the decimal digits are represented in the form of 4-bit binary

binary-coded decimal

vectors, the so-obtained system is called (BCD).

Compute the hexadecimal representation of 287645:

Example 3.1 ¼ þ

287645 17977:16 13

¼ þ

17977 1123:16 9

¼ þ

1123 70:16 3

¼ þ

70 4:16 6

¼ þ

4 0:16 4

¼ þ þ þ þ ¼ ½4639D :

4 3 2 1 0

287645 4:16 6:16 3:16 9:16 13:16 base16

mixed numeration systems mixed-radix systems,

It is possible to deﬁne or that is

with several bases. For instance, the time is expressed in days of 24 hours, hours of

41

3.1 NATURAL NUMBERS

60 minutes, minutes of 60 seconds, seconds of 1000 milliseconds, and so on. The

generalization of Theorem 3.1 is the following.

n b b . . . b

Given natural numbers , , greater than 1, any

Theorem 3.2 n21 n22 0

¼ b b

x B .b . . , can be expressed in the form

natural number smaller than n n21 n22 0

:B þ x :B þ þ x :B

x ¼ x n1 n1 n2 n2 0 0

where B ¼ B ¼ b B ¼ b :b . . . B ¼ b :b : :b

1, , , , ,

n2 n3

0 1 0 2 1 0 n1 0

b

x is a natural number smaller than . Furthermore, there is

and every coefﬁcient i i

x x x.

only one possible vector (x ) representing

n21 n22 0

mixed-radix B

Base-B and numeration systems are weighted systems. In base the

i B, mixed-

B , that is the successive powers of while the weights in the

weights are

radix system are given by B ¼ b :b : :b :

i i1 i2 0

x

The following algorithm computes the coefﬁcients .

i

Algorithm 3.2

i 0..n-1 x(i):=x mod b(i); x:=x/b(i);

for in loop end loop;

Compute the representation of 287645 in the mixed base (13, 12, 15,

Example 3.2

11, 12): ¼ þ

287645 23970:12 5

¼ þ

23970 2179:11 1

¼ þ

2179 145:15 4

¼ þ

145 12:12 1

¼ þ

12 0:13 12

¼ þ þ þ þ

287645 12:(12:15:11:12) 1:(15:11:12) 4:(11:12) 1:12 5:

Comment 3.1 s,

Given a natural number the conversion from the base-B represen-

s

x

tation of (Theorem 3.1) to its base-B representation, and inversely, is straight-

n ¼ s.q n s, – n)

forward. Suppose that (if were not divisible by then (dn/se.s

initial 0’s should be added). Then

s q1 s q2 s

x ¼ X :(B þ X :(B þ þ X :(B 0

) ) )

q1 q2 0

42 NUMBER REPRESENTATION

where s1 s2

X ¼ x :B þ x :B þ þ x :B :

0

i i:sþs1 i:sþs2 i:s

As an example, the binary representation of the decimal number 287645 is

01000110001110011101. The conversion to its hexadecimal representation is

straightforward: ½0100 ¼ ½4639D :

0110 0011 1001 1101 base 2 base 16

3.1.2 Residue Number System s fm g. m

A residue number system (RNS) is deﬁned by a set of moduli If the s are

i i

nonredundant. RNS-representation

pairwise prime, the RNS is called The of a given

N R(N), r are the respective

natural number is the vector whose components i

residues modulo m N/m

, that is, the successive remainders of the integer division

i i

r ¼ N m :

mod

i i

fm g range

The least common multiple (lcm) of is the of the RNS, generally denoted

i g

M. fm is

The greatest natural number that can be represented in the RNS deﬁned by i

m . . . m

M ¼ 1, 1, , 1):

1 (m s

1 2

m s are pairwise prime then

If the i m

M ¼ P i

1is u,

Garner’s algorithm 2.2, restricted to the computation of the successive values of

provides the mixed-radix components with respect to the weights

m ; s i

B(1) ¼ B(i) ¼ P 2

1, j

1ji1

(see Example 2.4).

3.2 INTEGERS sign-magnitude represen-

The most natural way of representing an integer is the

tation system. Nevertheless, it is not the most convenient for executing arithmetic

operations. Several representation methods are now described.

3.2.1 Sign-Magnitude Representation þx 2x, x

Any integer can be represented in the form or where is a natural number.

x B

The natural number can be represented in base (Theorem 3.1), and instead of

43

3.2 INTEGERS

using the ‘þ’ and ‘2’ symbols, an additional (sign) digit equal to 0 (nonnegative

number) or 1 (negative number) is added: x x x x

Deﬁnition 3.2 x , where

The integer represented in the form n21 n22 n21

1 0

is the sign bit, is n2 n3

:B þ x :B þ þ x :B x ¼

x 0 if 0,

n2 n3 n1

0

n2 n3

(x :B þ x :B þ þ x :B x ¼

0 ) if 1:

n2 n3 n1

0

n21 n21

2B , x , B

The range of represented numbers is .

Comment 3.2 x x x x

The number of vectors (x ), where is the sign

n21 n22 n21

1 0

n21 n21 n21 n21

2B , x , B 2

, while the range only includes 2.B 1

bit, is equal to 2.B

integers. The difference is due to the fact that the vector (100 0) does not rep-

resent any number (zero is a natural number so that its sign bit should always be

zero

equal to 0). Nevertheless, the integer could also be accepted with two represen-

tations, namely, 000 0 (plus zero) and 100 0 (minus zero).

3.2.2 Excess-E Representation x

Another way of representing a negative number consists in associating a natural

R(x) x, R R(x)

number to where is a one-to-one function, and is represented in

## B.

base

Deﬁnition 3.3 excess-E numeration system, E

In the where is a natural number,

R(x) ¼ x þ E,

x x x x

so that the integer represented in the form is

n21 n22 1 0

n1 n2

x :B þ x :B þ þ x :B E

0

n1 n2 0

and the range of represented numbers is n E:

E x B

Comments 3.3 n

B E B /2;

1. If is even, an is chosen equal to then the number represented in the

x x x

x is

form n21 n22 1 0

n1 n2

:B þ x :B þ þ x :B E

x 0

n1 n2 0

n1 n2

B=2):B þ x :B þ þ x :B :

¼ 0

(x n1 n2 0

44 NUMBER REPRESENTATION

x x , B/2;

The sign deﬁnition rule is the following one: if is negative then n21

B/2.

x x

if is nonnegative then n21 n

E B /2.

2. In some practical cases the value of is different from As an example,

in the ANSI/IEEE simple-precision ﬂoating-point system (Section 3.3), the

x

exponent is an 8-bit number representing an integer belonging to the

2127 x E ¼

range 128, according to the excess-E method with 127

and not 128. n21 x

B ¼ E ¼ , then the number represented in the form

3. If 2 and 2 n21

x x x is

n22 1 0 n1 n2

þ x :2 þ þ x

1):2

(x n1 n2 0

0 n1 n2

¼ x :2 þ x :2 þ þ x ,

n2

n1 0

0

x x

where stands for the complement of .

n21 n21

## R

4. The representation function is unate, so that the magnitude comparison is

easy. x ¼ 2287645 n ¼ B ¼

Represent with 6 digits in base 10 with

Example 3.3

E ¼ /2.

6

10 ¼

B 6 1000000,

B =2 ¼

6 500000,

R(x) ¼ x þ E ¼ ¼

500000 287645 212355

Observe that þ ¼ 300000 þ ¼ 287645

5

(2 10=2):10 12355 12355

B’s

3.2.3 Complement Representation R(x),

As in the preceding case, a one-to-one function associating a natural number to

x, is deﬁned as follows.

Deﬁnition 3.4 B’s complement numeration system x

In the every integer belonging

n n

B /2 x , B /2

to the range – is represented by

n

R(x) ¼ x B

mod , 45

3.2 INTEGERS x x x x

so that the integer represented in the form is

n21 n22 1 0

n1 n2

x :B þ x :B þ þ x

n1 n2 0

n1 n2 n

x :B þ x :B þ þ x , B =2,

if (3:1)

n1 n2 0

n1 n2 n

x :B þ x :B þ þ x B

n1 n2 0

n1 n2 n

x :B þ x :B þ þ x B =2:

if (3:2)

n1 n2 0

Conditions (3.1) and (3.2) can be written in the form

n1 n2

:B þ x :B þ þ x

x n1 n2 0

n1 n2

B=2):B þ x :B þ þ x ,

if (x 0, (3:3)

n1 n2 0

n1 n2 n

x :B þ x :B þ þ x B

n1 n2 0

n1 n2

B=2):B þ x :B þ þ x

if (x 0, (3:4)

n1 n2 0

## B

and if is even the latter conditions are equivalent to

n1 n2

:B þ x :B þ þ x x , B=2,

x if (3:5)

n1 n2 n1

0

n1 n2 n

x :B þ x :B þ þ x B x B=2

if (3:6)

n1 n2 n1

0

n22 n21

x þ þ x , B B

(take into account that .B ). Thus, if is even, the integer

n22 0

x x x

x is

represented by n21 n22 1 0

0 n1 n2

:B þ x :B þ þ x

x ¼ x , where

n2

n1 0

0 0

x ¼ x B x B=2 x ¼ x x , B=2,

if and if (3:7)

n1 n1 n1 n1

n1 n1

and the sign deﬁnition rule is the following one: B=2;

x x

if is negative then n1 , B=2:

x x

if is nonnegative then n1

B ¼ x x x x

In particular, if 2 the number represented in the form is

n21 n22 1 0

n1 n2

:2 þ x :2 þ þ x

x , (3:8)

n1 n2 0

x sign bit:

and the most signiﬁcant bit is also the

n21

x , x ¼ x x ¼

if 0 then 1, and if 0 then 0: (3:9)

n1 n1

46 NUMBER REPRESENTATION

Comments 3.4

B’s R(x) ¼ x

1. The complement system is based on a congruence, namely, mod

n

B , so that the arithmetic operations are easy (Chapters 4 and 5).

n-digit n þ extension),

2. In order to represent an number with 1 digits (digit the

following rule must be used (B even):

x B=2, x ¼ B x , B=2, x ¼

if then 1, and if then 0:

n1 n n1 n

Actually, in the ﬁrst case,

n n1 n n1 n1

B):B þ x :B ¼ B þ x :B ¼ B):B

(B 1 (x ,

n1 n1 n1

and in the second case, n n1 n1

þ x :B ¼ x :B :

0:B n1 n1

0 þ x x x x

B ¼ s complement system) the (n 1)-bit vector

3. If 2 (2 n21 n21 n22 1

x n-bit x x x x bit

represents the same number as the vector (sign

n21 n22

0 1 0

extension).

B’s B ¼

The complement method is almost exclusively used with 2, in which case

the most signiﬁcant bit is also the sign bit (3.9). In the general case, the most signiﬁ-

B/2 x.

cant digit must be compared with in (3.7) in order to deduce the sign of A

reduced B’s complement numeration system could also be deﬁned in which the

x B 2

most signiﬁcant digit is either 0 or 1.

n21

Deﬁnition 3.5 reduced B’s complement numeration system

In the (B even), every

n21 n21

x B x , B

integer belonging to the range – is represented by

n

R(x) ¼ x B :

mod

n1

x , B

If 0 , then n1 x ¼

R(x) ¼ x , B and 0,

n1

n1

B x ,

and if 0, then

n n n1 n1

þ x B B ¼ x ¼ B

R(x) ¼ B (B 1):B and 1:

n1

x x x x

Thus the integer represented by is

n21 n22 1 0

n1 n2

þ x :B þ þ x x ¼ B

x ¼ B if 1 and

n2 n1

0

n2

x ¼ x :B þ þ x x ¼

if 0, (3:10)

n2 n1

0

and the sign deﬁnition rule is the following one:

¼ B x x ¼

x x 1; if is nonnegative then 0:

if is negative then n1 n1 47

3.2 INTEGERS B’s

In fact, the reduced complement representation is deduced from the nonreduced

extension,

one by adding a digit (digit Comment 3.4(2)) if the most signiﬁcant digit

B 2

is different from 0 or 1. þ x x x x x

As in the binary case the (n 1)-digit vector represents

n21 n21 n22 1 0

x x x

n-digit x .

the same number as the vector n21 n22 1 0

x ¼ 2287645 n ¼ B’s

Represent with 6 digits in complement form

Example 3.4

B ¼

with 10: ¼

B 6 1000000,

B =2 ¼

6 500000,

¼

R(x) ¼ x þ B 6 712355

Observe that 0

x ¼ ¼ 3,

7 10

5 þ ¼ 287645:

5 12355

3:10

B’s n ¼

In reduced complement form, 7 digits are necessary

n21 ¼ 250000):

,2 B

(2287645 ¼

R(x) ¼ x þ B 7 9712355:

210 þ ¼ 2287645

6

Observe that 712355 and that 9712355 is deduced from 712355

by adding one digit according to the digit extension rule.

3.2.4 Booth’s Encoding 0 x x x x

s complement representation of

According to relation (3.8) the 2 n21 n22 1 0

x signed-digit

an integer could also be seen as a representation

n1 n2

x :2 þ x :2 þ þ x

n1 n2 0

x [ x [

where {21, 0} and all other digits {0, 1}. The Booth’s encoding

n21 i

([BOO1951]) generates another signed-digit representation:

Deﬁnition 3.6 y x

Consider an integer whose 2’s complement representation is n21

x x and deﬁne

n22 0 ¼ x

y ,

0 0

¼ x þ x

y ,

1 1 0

¼ x þ x

y ,

2 2 1

.

. .

. .

.

y ¼ x þ x : (3:11)

n1 n1 n2

48 NUMBER REPRESENTATION

0 1

Then by multiplying the ﬁrst equation by 2 , the second by 2 , the third one

n

2

, and so on, and adding up the equations, the following relation is

by 2

obtained: n1 n2 n1

:2 þ y :2 þ þ y :2 ¼ x :2

y 0

n1 n2 n1

0

n2

þ x :2 þ þ x :2 :

0

n2 0

y y y

) whose components belong to {21, 0, 1} is the

The vector (y n21 n22 i

0

Booth-1 representation x

of and

n1 n2

:2 þ y :2 þ þ y :2

x ¼ y 0 (3:12)

n1 n2 0

Observe that the Booth’s representation of an integer is formally the same as the

binary representation of a natural number. The Booth’s encoding method can be

generalized.

Deﬁnition 3.7 x

Consider an integer whose 2’s complement representation is n21

x x n

, with = 2.m bits, and deﬁne

n22 0 ¼ 2:x þ x

y ,

0 1 0

y ¼ 2:x þ x þ x ,

1 3 2 1

y ¼ 2:x þ x þ x ,

2 5 4 3

.

. .

. .

.

y ¼ 2:x þ x þ x (3:13)

m1 2:m1 2:m2 2:m3

0 1 2

Then by multiplying the ﬁrst equation by 4 , the second by 4 , the third by 4 , and

m

so on, and adding up the equations, the following relation is obtained:

m1 m2 n1

:4 þ y :4 þ þ y :4 ¼ x :2

y 0

m1 m2 n1

0

n2

þ x :2 þ þ x :2 :

0

n2 0

y y y 21,

The vector (y ) whose components belong to {22, 0, 1, 2} is

m21 m22 i

0

Booth-2 representation x

the of and

m1 m2

x ¼ y :4 þ y :4 þ þ y :4 0 (3:14)

m1 m2 0

Booth-r representation

More generally, a can be deﬁned as follows: 49

3.2 INTEGERS

Deﬁnition 3.8 x x

Let be an integer whose 2’s complement representation is n21

x x n ¼ r.m

, with bits, and deﬁne

n22 0 r1 r2

¼ x :2 þ x :2 þ þ x :2 þ x

y ,

r1 r2

0 1 0

r1 r2

y ¼ x :2 þ x :2 þ þ x :2 þ x

i i:rþr1 i:rþr2 i:rþ1 i:r

þ x 8i [ . . . m

, {1, 2, , 1}: (3:15)

i:r1

y y y

) whose components belong to

The vector (y m21 m22 i

0

r1 r1 r1 r1

. . . . . .

2 (3:16)

, (2 1), , 2, 1, 0, 1, 2, , 2 1, 2

Booth-r representation x

is the of and

m1 m2 r

x ¼ y :B þ y :B þ þ y :B B ¼ :

0 , where 2 (3:17)

m1 m2 0

Comments 3.5 x x x . . . x

1. Given an integer whose 2’s complement representation is ,

n21 n22 0

n ¼ r.m

with bits, the following signed digits could be deﬁned (one for

r-bit

each slice): r1 r2

yi ¼ x :2 þ x :2 þ þ x :2

i:rþr1 i:rþr2 i:rþ1

þ x 8i [ . . . m

, {0, 1, 2, , 2}

i:r r1 r2

y ¼ x :2 þ x :2 þ þ x :2 þ x , (3:18)

m1 m:r1 m:r2 m:rþ1 m:r

so that r

yi [ . . . [ . . . m

{0, 1, 2, , 2 1}8i {0, 1, 2, , 2}, (3:19)

r1 r1

y [ 2 (2 . . . 2, 1,

{ , 1), , 0},

m1

and m1 m2 r

x ¼ y :B þ y :B þ þ y :B B ¼ :

, where 2 (3:20)

m1 m2 0 0

r .

Nevertheless, for 1, the total range deﬁned by (3.19), namely

r1 r1 r

(2 . . . 2, . . .

, 1), , 1, 0, 1, 2, , 2 1},

{2

is larger then the range deﬁned by (3.16). r

B þ 2B/2 B/2, B . This

2. The range (3.16) contains 1 values, from to where = 2

m

þ

means that the total number of expressions (3.17) is equal to (B 1) . The

50 NUMBER REPRESENTATION

m

x 2(B/2).(B 2 2

numbers deﬁned by (3.17) are included between 1)/(B 1)

m m

2 2 x þ B.(B 2

1)/(B 1), so that the range of contains 1 1)/

and (B/2).(B

2 m

(B 1) integers. Except when = 1, the following inequality is satisﬁed:

m1 m

þ B:(B , þ :

1 )=(B 1) (B 1)

redundant

Thus the set of digits (3.16) is as the number of different expressions

is greater than the range of the represented numbers.

2287645;

Compute the Booth’s encoding of the 2’s complement

Example 3.5 2287645

representation of is;

1 0 1 1 1 0 0 1 1 1 0 0 0 1 1 0 0 0 1 1;

according to (3.11) its Booth-1 representation is

1 1 1 1 1,

1 0 0 0 1 0 0 0 0 1 0 0 0 1 0

and according to (3.13) its Booth-2 representation is

1 2 1 2 1:

0 2 0 2 1

By substituting two successive bits by a 4-valued digit (comments 3.5) the following

representation is obtained: 2 3 2 1 3 0 1 2 0 3:

Other expressions can be deduced from the previous one by applying simple rules

such as þ ¼ þ þ ¼ þ þ

(2):4 3:1 (1):4 (1):1, 1:4 3:1 2:4 (1):1, 0:4 3:1

¼ þ

1:4 (1):1:

22 3 2 1 3 0 1 2 0 3, the underlined pairs can

Thus, in the preceding expression

21 2 21 2

be substituted by 1, 2 and 1 1, respectively, yielding the following

equivalent expression: 1 1 1 1:

2 2 0 1 2 1

Observe that the latter is different from the Booth-2 representation, in spite of using

the same digits (Comment 3.5(2)). 51

3.3 REAL NUMBERS

3.3 REAL NUMBERS ﬁxed-point

As regards the real numbers, there are two types of approximations: and

ﬂoating-point numeration systems. The ﬁxed-point system is a simple extension of

reduced

the integer representation system; it allows the representation of a relatively

range absolute precision.

of numbers with some constant The ﬂoating point system

allows the representation of a very large range of numbers, with some constant

relative precision.

Deﬁnitions 3.9

ﬁxed-point numeration system,

1. In a the number represented in the form

x x x x :x x x (3:21)

np1 np2 1 2 p

1 0

p x

x/B , where is the integer represented by the same sequence of digits with-

is

out point.

x x

2. Let and be the minimum and maximum integers that can be rep-

min max n21 n21

n x ¼ 2 B x ¼ B 21

resented with digits, that is, 1 and in

min max

n n

¼ 2B /2 x ¼ B /2 21

x and in

sign-magnitude representation, and min max

n /2 x

B’s representation. Then, any real number

complement or excess-B

belonging to the interval p p

:x x B :x

B min max

error

can be represented in the form (3.21) with some equal to the absolute

x

value of the difference between and its representation.

distance d unit in

3. The between exactly represented numbers is equal to the

2p

the least signiﬁcant position B maximum error

(ulp), that is, , so that the

is equal to p

ulp=2 ¼ B =2: p

maximum relative error ulp/(2.jxj) ¼ x =

4. The is equal to 1/(2.jxj.B ). If 0

2p

jxj B 12

, so that the maximum relative error is less than or equal to .

then x B’s

The range of numbers that can be represented in complement,

Example 3.6 23

B ¼ n ¼ ulp ¼

with 10, 9 digits, and 10 is

10 =2 x , =2:

6 6

10

The following numbers can be exactly represented:

500000:000, 499999:999, 499999:998, . . . 0:001,

, 0:000,

. . .

0:001, , 499999:999:

52 NUMBER REPRESENTATION

ulp ¼

The distance between them is equal to 0.001.

Deﬁnitions 3.10

ﬂoating-point numeration system,

1. In a the representation consists of two

signiﬁcand) þs 2s, s

numbers: a ﬁxed-point number (the or where is a non-

exponent) e.

negative number, and an integer (the The corresponding number

e

+s.b b B).

is , where is the chosen base (not necessarily equal to

s e e s e,

s , , , and be the minimum and maximum values of and

2. Let min max min max

respectively. The range of represented numbers is

e e

:b x s :b

s (3:22)

max max

max max

and the minimum absolute value of a represented number is

e

:b :

jxj s (3:23)

min

min

ulp

3. Let be the unit in the least representative position of the signiﬁcand. Then

e

distance D D ¼ d.b

the between exactly represented numbers is , where

d ¼ ulp is the distance between two successive values of the signiﬁcand.

D e. maximum error

Thus the value of depends on the exponent The is

equal to e

D =2 ¼ ulp:b =2:

max

max e e

maximum relative error D/(2.jxj) ¼ ulp.b /(2.s.b ¼ ulp/2.s.

4. The is equal to )

As in the preceding case (Deﬁnition 3.9(4)) the maximum relative error is less

12 .

than or equal to

Comment 3.6 q

In a ﬂoating-point system, with digits for representing the absolute

s t

value of the signiﬁcand and digits for representing the exponent, the range of

positive numbers is e q e

ulp:b x , ulp:B :b ,

min max

the maximum error is equal to e

max error ¼ ulp:b =2,

max

floating 12

and the maximum relative error is equal to .

q þ t

In a ﬁxed-point system with digits, the range of positive numbers is

qþt

ulp x , ulp:B , 53

3.3 REAL NUMBERS

the maximum error is equal to

max error ¼ ulp=2,

fixed 12

.

and the maximum relative error is equal to rr

In order to compare both systems, one can compute the quotient (relative

range) x

between the maximum and the minimum value of (x positive). In the ﬂoat-

ing-point system q e e

rr ¼ B :b , (3:24)

max min

floating

and in the ﬁxed point system qþt

rr ¼ B : (3:25)

fixed t

e 2 e ﬃ B

Taking into account that , it is obvious that

max min .. rr :

rr floating fixed

Nevertheless, the maximum relative errors are equal. As regards the maximum

ulp

errors, their values depend on the (not necessarily the same value in both cases).

ANSI/IEEE single-precision ﬂoating-point

In the ([ANS1985])

Example 3.7

system, the signiﬁcand is a sign-magnitude integer

+s ¼ +1:s s s ,

1 2 23

s

s mantissa, 2

s . . . is called the and the exponent is an excess 127

where 21 22 223

e

e e

. . . . The 32-bit word

integer 7 6 0 e e s s s

sign e 1 2 23

7 6 0

represents the number 1 2 23 e

:(1 þ s :2 þ s :2 þ þ s :2

sign ):2 ,

(1) 1 2 23

e ¼ e :2 þ e :2 þ þ e :2

7 6 0

where 127:

7 6 0

Thus 23

¼ s ¼ ﬃ ulp ¼ e ¼ 127, e ¼

s 1, 1:11 1 2, 2 , 128:

min max min max

54 NUMBER REPRESENTATION

e e

Nevertheless, and are not used for representing ordinary numbers; they are

min max

used for representing

127 127

ﬃ 1:0 þ 1 ﬃ 1 ﬃ 1:0

ﬃ þ1:0 128 128

, 0 2 , 1:0 2 , 2 ,

0 2

and other nonordinary numbers. The actual minimum and maximum values are

e ¼ 126, e ¼ 127,

min max , x ,

22.2 127 127

2.2 , that is

so that the range of represented numbers is

, x ,

2 128 128

2 , 2126 .

and the minimum positive represented number is 1.2

3.4 BIBLIOGRAPHY IEEE Standard for Binary Floating-Point Arithmetic,

[ANS1985] ANSI and IEEE, ANSI/

IEEE Standard, Std 754-1985, New York, 1985. Q. J. Mechanics Appl.

[BOO1951] A. D. Booth, A signed binary multiplication technique.

Math., 236– 240 (1951).

June: Digital Arithmetic,

[ERC2004] M. Ercegovac and T. Lang. Morgan Kaufmann Publishers,

San Francisco, CA, 2004.

Computer Arithmetic: Algorithms and Hardware Designs,

[PAR1999] B. Parhami, Oxford

University Press, New York, 1999.

4

## ARITHMETIC OPERATIONS:

## ADDITION AND SUBTRACTION

Addition is used as a primitive operation for computing most arithmetic functions,

so that it deserves particular attention. The classical pencil and paper algorithm

implies the sequential computation of a set of carries, each of them depending on

the preceding one. As a consequence, the execution time of any program, or circuit,

n

based on the classical algorithm is proportional to the number of digits of the oper-

ands. In order to minimize the computation time, several general ideas have been

proposed. One of them consists of modifying the classical algorithm in such a

way that the computation time of each carry is minimal; the time complexity is

n,

still proportional to but the proportionality constant is smaller. Another approach

n-

rests on the use of a different numeration system; instead of adding two base-B

s (n/s)-digit numbers are considered. Several algorithms,

digit numbers, two base-B

different from the classical one and generally based on some kind of tree structure,

have been proposed. If their implicit parallelism can be exploited, execution times

n

proportional to log are reached.

## 4.1 ADDITION OF NATURAL NUMBERS

4.1.1 Basic Algorithm n-digit

Consider the base-B representations of two numbers:

n1 n2

:B þ x :B þ þ x :B

x ¼ x 0 ,

n1 n2 0

n1 n2

y ¼ y :B þ y :B þ þ y :B :

0

n1 n2 0

Synthesis of Arithmetic Circuits: FPGA, ASIC, and Embedded Systems

By Jean-Pierre Deschamps, Géry J. A. Bioul, and Gustavo D. Sutter

#

Copyright 2006 John Wiley & Sons, Inc. 55

## 56 ARITHMETIC OPERATIONS: ADDITION AND SUBTRACTION

þ

The following (pencil and paper) algorithm computes the (n 1)-digit represen-

c

z ¼ x þ y þ c where is an initial carry equal to 0 or 1.

tation of in in

Algorithm 4.1 Classic Addition

q(0):=c_in;

i 0..n-1

for in loop

x(i)+y(i)+q(i)>B-1 q(i+1):=1; q(i+1):=0;

if then else

end if;

z(i):=(x(i)+y(i)+q(i)) mod B;

end loop;

z(n):=q(n);

q(i þ q(i)

As 1) is a function of the execution time of Algorithm 4.1 is proportional

n.

to In order to reduce the execution time of each iteration step, Algorithm 4.1 can

B-valued

be modiﬁed. First, deﬁne two binary functions of two variables, namely,

the propagate (p) and generate (g) functions:

p(a, b) ¼ a þ b ¼ B p(a, b) ¼

1 if 1, 0 otherwise; (4:1)

g(a, b) ¼ a þ b . B g(a, b) ¼

1 if 1, 0 otherwise:

q can be calculated as follows:

The next carry iþ1

p(x(i), y(i))=1 q(i+1):=q(i); q(i+1):=g(x(i),

if then else

y(i)); end if;

The corresponding modiﬁed algorithm is the following one.

Algorithm 4.2 Carry-Chain Addition

--computation of the generation and propagation conditions:

i 0..n-1 g(i):=g(x(i),y(i)); p(i):=p(x(i),y(i));

for in loop

end loop;

--carry computation:

q(0):=c_in;

i 0..n-1

for in loop

p(i)=1 q(i+1):=q(i); q(i+1):=g(i);

if then else end if;

end loop;

-sum computation

i 0..n-1 z(i):=(x(i)+y(i)+q(i)) mod B;

for in loop end loop;

z(n):=q(n);

Comments 4.1 B-ary

1. Observe that the ﬁrst iteration includes 2.n operations (computation of

g(i) p(i))

and that could be executed in parallel. The second iteration is made

n q(i þ

up of iteration steps that must be executed sequentially (as 1) is a func-

q(i))

tion of and consists of binary operations only. The last iteration includes

57

## 4.1 ADDITION OF NATURAL NUMBERS

n B-ary z(i))

operations (computation of that could be executed in parallel.

B-ary

Algorithm 4.2 thus splits the operations into concurrent ones (ﬁrst

and third iterations) and sequential binary ones (second iteration). The sequen-

## B.

tial binary operations are the same whatever the base The expected compu-

tation time reduction is due to the substitution of the (relatively) complex

instruction

x(i)+y(i)+q(i)>B-1 q(i+1):=1; q(i+1):=0;

if then else

end if;

by the simpler one

p(i)=1 q(i+1):=q(i); q(i+1):=g(i);

if then else end if;

2. The preceding instruction sentence is equivalent to the following Boolean

equation: q(i þ ¼ p(i):q(i) _ not(p(i)):g(i):

1) (4:2)

Furthermore, if the preceding relation is used, then the deﬁnition of the

generate function can be modiﬁed:

g(a, b) ¼ a þ b . B g(a, b) ¼ a þ b , B

1 if 1, 0 if 1,

g(a, b) ¼ 0 or 1 (don’t care) otherwise:

3. Another Boolean equation equivalent to (4.2) is

q(i þ ¼ g(i) _ p(i):q(i):

1) (4:3)

If the preceding relation is used, then the deﬁnition of the propagate function

can be modiﬁed:

p(a, b) ¼ a þ b ¼ B p(a, b) ¼ a þ b , B

1 if 1, 0 if 1,

p(a, b) ¼ 0 or 1 (don’t care) otherwise:

4.1.2 Faster Algorithms

q(1), q(2), . . . q(n)

The values of , could also be calculated in parallel:

Property 4.1

8i ¼ . . . n:

1, 2, ,

q(i) ¼ g(i _ g(i _ g(i

1) 2)::p(i 1) 3):p(i 2):p(i 1)

_ g(i

4):p(i 3):p(i 2):p(i 1)_ (4:4)

_ g(0):p(1): :p(i _ q(0):p(0):p(1): :p(i

1) 1),

_ g(i) ¼ g(x(i), y(i)) p(i) ¼ p(x(i),

where symbol stands for the Boolean sum, and

y(i)).

## 58 ARITHMETIC OPERATIONS: ADDITION AND SUBTRACTION

Relation (4.4) is deduced from (4.3) by induction. The corresponding algorithm is

the following one.

Algorithm 4.3

--computation of the generation and propagation conditions:

i 0..n-1 g(i):=g(x(i),y(i)); p(i):=p(x(i),y(i));

for in loop

end loop;

--carry computation:

q(0):=c_in;

i 1..n

for in loop

q(i):=g(i-1) or g(i-2)*p(i-1) or...or g(0)*p(1)*...*

p(i-1) or q(0)*p(0)*p(1)*...*p(i-1);

end loop;

--sum computation

i 0..n-1 z(i):=(x(i)+y(i)+q(i)) mod B;

for in loop end loop;

z(n):=q(n);

The preceding algorithm is made up of three iterations whose operations could be

q(i) , , and but not on

executed in parallel as just depends on the operands x y c_in

the preceding carries. Nevertheless, the execution of

2

q(i):=g(i-1) or g(i-2)*p(i 1) or...or g(0)*p(1)*...*p(i-1)

or q(0)*p(0)*p(1)*...*p(i-1);

þ þ

implies the computation of a (2.i 1)-variable switching function—a (2.n 1)-vari-

q(n). n,

able function in the case of Except for small values of speciﬁc algorithms must

be deﬁned for computing these functions. For that purpose two new concepts are intro-

dot operation and the generalized generate and propagate functions:

duced: the

Deﬁnitions 4.1 a ¼ a a ¼ a

1. Given two 2-component binary vectors (a , ) and (a , ) the

i i0 i1 k k0 k1

B B

dot operation † B 2 2 2

into :

deﬁnes an application from 2 2 2

† a ¼ _ a :a a :a

a (a , ):

i k i0 k0 i1 i1 k1

It can easily be demonstrated that it is a noncommutative associative oper-

ation; (0,1) is the neutral element and (0,0) the left 0-element.

g(i) p(i),

2. Given the generate and propagation functions and for

i [ f0, . . . n 2 generalized generate and propagate functions

1, , 1g, the i [ f0, . . . n 2 k [ f0, . . . ig

and , for 1, , 1g and 1, , are

g(i:i-k) p(i:i-k)

deﬁned as follows:

k), p(i:i k)) ¼ p(i)) † p(i

(g(i:i (g(i), (g(i 1), 1))

†(g(i p(i † † k), p(i k)):

2), 2)) (g(i (4:5)

The following property is deduced from (4.4) and from the preceding deﬁnitions.

59

## 4.1 ADDITION OF NATURAL NUMBERS

Property 4.2 q(i þ ¼ g(i:i k) _ p(i:i k)::q(i k):

1) (4:6)

Then Algorithm 4.3 can be modiﬁed as follows.

Algorithm 4.4

--computation of the generation and propagation conditions:

i 0..n-1 g(i):=g(x(i),y(i)); p(i):=p(x(i),y(i));

for in loop

end loop;

--computation of the generalized generation and propagation

conditions:

i 1..n

for in loop

(g(i-1:0), p(i-1:0)):=(g(i-1), p(i-1)) dot (g(i-2),

p(i-2)) dot ... dot (g(0), p(0));

end loop;

--carry computation:

q(0):=c_in;

i 1..n q(i):=g(i-1:0) or p(i-1:0)*q(0);

for in loop end loop;

--sum computation:

i 0..n-1 z(i):=(x(i)+y(i)+q(i)) mod B;

for in loop end loop;

z(n):=q(n);

The second iteration of Algorithm 4.4, that is, the computation of all pairs

2 p(i 2

(g(i 1, 0), 1, 0)), can be performed in several ways. It is a particular case

a(0), a(1), . . . a(n 2

of a more general problem: Given a set of input data , 1) and

†

an associative operator (dot), compute

b(0) ¼ a(0),

b(1) ¼ a(1) † a(0),

b(2) ¼ a(2) † a(1) † a(0), (4:7)

...

b(n ¼ a(n † † a(1) † a(0):

1) 1)

The simplest (naı̈ve) algorithm is 2

b(0):=a(0); i 1..n21 b(i):=a(i) dot b(i 1);

for in loop end loop;

n.

whose execution time is proportional to Nevertheless, better algorithms have been

proposed, among others ([BRE1982], [LAD1980], [KOG1973], [HAN1987],

[SUG1990]). Two of them are described below; they are based on the deﬁnition

of a procedure computing Equations (4.7); its input and output

dot_procedure

## 60 ARITHMETIC OPERATIONS: ADDITION AND SUBTRACTION

n n-component

parameters are a natural number (the number of input data), and two

vectors (the input data and the output result):

dot_procedure (n:in natural;

procedure

a:in data_vector(0..n-1); b:out data_vector(0..n-1));

n

Assume that is a power of 2 (0’s should be added if necessary). A ﬁrst algorithm

consists of:

computing c(0) ¼ a(1) † a(0),

c(1) ¼ a(3) † a(2),

...

c((n=2) ¼ a(n † a(n

1) 1) 2);

n/2, c, d,

calling with parameters and so that

dot_procedure d(0) ¼ b(1),

d(1) ¼ b(3),

d(2) ¼ b(5),

...

d((n=2) ¼ b(n

1) 1);

b;

computing the missing components of

b(2) ¼ a(2) † d(0), b(4) ¼ a(4) † d(1), . . . b(n ¼ a(n † d((n=2)

, 2) 2) 2):

computation scheme

The (or precedence graph, Chapter 10) is shown in

n ¼

Figure 4.1 (with 16). The corresponding recursive algorithm is the following.

Algorithm 4.5 Dot Procedure (1)

dot_procedure (n:in natural; a:in data_vector(0..

procedure

n-1); b:out data_vector(0..n-1)) is

c,d: data_vector(0..(n/2)-1);

begin n=2 b(0):=a(0); b(1):=a(1) dot a(0);

if then

else

i 0..(n/2)-1 c(i):=a((2*i)+1) dot a(2*i);

for in loop

end loop;

dot_procedure (n/2, c, d);

b(0):=a(0);

i 1..(n/2)-1 b(2*i):=a(2*i) dot d(i-1);

for in loop

b((2*i)+1):=d(i); end loop;

end if;

dot_procedure;

end 61

## 4.1 ADDITION OF NATURAL NUMBERS

a(15) a(14) a(13) a(12) a(11) a(10) a(9) a(8) a(7) a(6) a(5) a(4) a(3) a(2) a(1) a(0)

c(7) c(6) c(5) c(4) c(3) c(2) c(1) c(0)

8-input dot procedure

d(7) d(6) d(5) d(4) d(3) d(2) d(1) d(0)

a(14) a(12) a(10) a(8) a(6) a(4) a(2)

b(15) b(14) b(13) b(12)b(11) b(10) b(9) b(8) b(7) b(6) b(5) b(4) b(3) b(2) b(1) b(0)

A 16-input dot procedure (ﬁrst algorithm).

Figure 4.1

Both loops are made up of dot operations that can be executed in parallel.

for þ T(n/2) þ T T(2) ¼ T

T(n) T , with ,

The total execution time is equal to dot dot dot

so that T ¼ n) :

(n) (2:( log 1):T (4:8)

Algorithm 4:5 dot

2

The second algorithm consists of: n/2, a(0 2 b(0

with parameters . . (n/2) 1), and . .

calling dot_procedure

2

(n/2) 1); n/2, a((n/2) n 2 c,

calling with parameters . . 1), and so that

dot_procedure

c(0) ¼ a(n=2),

c(1) ¼ a((n=2) þ † a(n=2),

1)

...

c((n=2) ¼ a(n † a(n † † a(n=2);

1) 1) 2)

b,

computing the missing components of

b(n=2) ¼ c(0) † b((n=2) 1),

b((n=2) þ ¼ c(1) † b(

1) (n=2) 1),

...

b(n ¼ c((n=2) † b((n=2)

1) 1) 1):

## 62 ARITHMETIC OPERATIONS: ADDITION AND SUBTRACTION

a(15) a(14) a(13) a(12) a(11) a(10) a(9) a(8) a(7) a(6) a(5) a(4) a(3) a(2) a(1) a(0)

8-input dot procedure 8-input dot procedure

c(7) c(6) c(5) c(4) c(3) c(2) c(1) c(0) b(6) b(5) b(4) b(3) b(2) b(1) b(0)

b(15) b(14) b(13) b(12) b(11) b(10) b(9) b(8) b(7)

A 16-input dot procedure (second algorithm).

Figure 4.2 n ¼

The computation scheme is shown in Figure 4.2 (with 16), and the correspond-

ing recursive algorithm is the following.

Algorithm 4.6 Dot Procedure (2)

dot_procedure (n:in natural; a:in data_vector(0..

procedure

n-1);b:out data_vector(0..n-1)) is

c: data_vector(0..(n/2)-1);

begin n=2 b(0):=a(0); b(1):=a(1) dot a(0);

if then

else

dot_procedure (n/2, a(0..(n/2)-1), b(0..(n/2)-1);

dot_procedure (n/2, a((n/2)-1..n-1), c);

i 0..n/2-1 b(i+(n/2)):=c(i) dot b((n/2)-1);

for in loop

end loop;

end if;

dot_procedure;

end

Both procedure calls can be executed in parallel, and the loop is made up of dot

for T(n)

operations that can also be executed in parallel. The total execution time is

T(2) ¼ T

T(n/2) þ T , with , so that

equal to dot dot ¼ n):T :

T (n) ( log (4:9)

Algorithm 4:6 dot

2

The following algorithm is deduced from Algorithm 4.4 and the deﬁnition of dot_

.

procedure 63

## 4.1 ADDITION OF NATURAL NUMBERS

Algorithm 4.7 Parallel-Preﬁx Addition

a, b: data_vector(0..n-1, 0..1);

begin

--computation of the generation and propagation conditions:

i 0..n-1 a(i,0):=g(x(i),y(i)); a(i,1):=p(x(i),

for in loop

y(i))); end loop;

--computation of the generalized generation and propagation

--conditions:

dot_procedure(n, a, b);

--carry computation:

q(0):=c_in;

i 1..n q(i):=b(i,0) or b(i,1)*q(0);

for in loop end loop;

--sum computation

i 0..n-1 z(i):=(x(i)+y(i)+q(i)) mod B;

for in loop end loop;

z(n):=q(n);

The preceding algorithm is made up of three iterations, whose operations can be

. The procedure execution time

executed in parallel, and a call to dot_procedure

n;

depends on the number of digits according to (4.8) or (4.9) it is proportional to

n.

log(n). The execution time of the iterations is independent of Thus for

n,

great values of the execution time of Algorithm 4.7 is practically proportional

to log(n).

A logarithmic execution time can be obtained with a different algorithm using

two new procedures. The ﬁrst one,

carry_lookahead_procedure (n:in natural; a:in data_

procedure

vector(0..n-1, 0..1); c_in: bit; q:out bit_vector(1..n));

in

n q(1), q(2), . . . q(n), n

computes the carries , in function of the generation and

g(t) ¼ a(t,0) p(t) ¼ a(t,1), , that is,

propagation conditions and and of c_in

q(t) ¼ a(t _ a(t _ _ a(0,0):a(1,1): :a(t

1,0) 2,0):a(t 1,1) 1,1)_

c in:a(0,1):a(1,1): :a(t 8t [ . . . n}:

1,1), {1, 2, ,

The second one,

carry_procedure (n:in natural; b:in data_vector(0..

procedure

n-1, 0..1); c_in: bit; q:out bit_vector(1..n));

in

n q(1), q(2), . . . q(n), n

computes the carries , in function of the generalized

g(t:0) ¼ b(t,0) p(t:0) ¼ b(t,1),

generation and propagation conditions and and of

, that is,

c_in q(t) ¼ b(t _ c in:b(t

1,0) 1,1): (4:10)

## 64 ARITHMETIC OPERATIONS: ADDITION AND SUBTRACTION

n n ¼ k.s.

Assume that can be factorized under the form The algorithm

consists of:

k s, a( j.s j.s þ s 2

calling times the with parameters . . 1) and

dot_procedure

c( j, 2 j [ f0, . . . k 2

0. .s 1), where 1, , 1g, so that

c( j, ¼ a( j:s) ¼ j:s), p( j:s)),

0) (g(

c( j, ¼ a( j:s þ † a( j:s) ¼ j:s þ j:s), p( j:s þ j:s),

1) 1) (g( 1: 1:

...

c( j, s ¼ a( j:s þ s † a( j:s þ s † † a( j:s)

1) 1) 2)

¼ j:s þ s j:s), p( j:s þ s j:s);

(g( 1: 1: k, c(0 k 2 s 2

calling with parameters . . 1, 1),

carry_lookahead_procedure

d,

and so that

c_in in:p( j:s ¼ q( j:s);

d( j) ¼ g( j:s _ c 1:0)

1:0) s 2 c( j, s 2

k with parameters 1, 0 . . 2),

calling times the carry_procedure

d( j), e( j, s 2 j [ f0, . . . k 2

and 0 . . 2), where 1, , 1g, so that

e( j,i) ¼ j:s þ i: j:s) _ q( j:s):p( j:s þ i: j:s) ¼ q(j:s þ i þ

(g( 1):

k ¼ s ¼

The computation scheme is shown in Figure 4.3 (with 4), and the corre-

sponding recursive algorithm is the following:

Algorithm 4.8

carry_lookahead_procedure

procedure

(n:in natural; a:in data_vector(0..n-1, 0..1); c_in: bit;

in

q:out bit_vector(1..n)) is

c: data_vector(0..k-1, 0..s-1, 0..1); d: bit_vector(0..

k-1);

begin j 0..k-1 dot_procedure(s, a(j*s..j*s+s-1), c(j,

for in loop

0..s-1)); end loop;

carry_lookahead_procedure (k, c(0..k-1, s-1), c_in, d);

j 0..k-1

for in loop

carry_procedure(s-1, c(j, 0..s-2), d(j), e(j, 0..s-2));

end loop;

j 1..k-1 q(j*s):=d(j);

for in loop end loop;

j 0..k-1

for in loop

i 0..s-2 q(j*s+i+1):=e(j, i);

for in loop end loop;

end loop;

carry_lookahead_procedure;

end 65

## 4.1 ADDITION OF NATURAL NUMBERS

a(15) a(14) a(13) a(12) a(11) a(10) a(9) a(8) a(7) a(6) a(5) a(4) a(3) a(2) a(1) a(0)

4-input dot

4-input dot

4-input dot 4-input dot procedure

procedure

procedure procedure

c(3,2)c(3,1)c(3,0) c(2,2)c(2,1)c(2,0) c(1,2)c(1,1)c(1,0) c(0,2)c(0,1)c(0,0)

c(3,3) c(2,3) c(1,3) c(0,3)

4-input carry_lookahead_procedure q(0)

d(2) d(1)

d(4) d(3) c(0,2)c(0,1)c(0,0)

c(3,2)c(3,1)c(3,0) c(2,2)c(2,1)c(2,0) c(1,2)c(1,1)c(1,0)

carry_ carry_

carry_ carry_

procedure procedure

procedure procedure

q(16) q(15) q(14) q(13) q(12) q(11) q(10) q(9) q(8) q(7) q(6) q(5) q(4) q(3) q(2) q(1)

A 16-input

Figure 4.3 carry_lookahead_procedure.

The procedure computes (4.10):

carry_procedure

carry_procedure

procedure 2

(n:in natural; b:in data_vector(0..n 1, 0..1); c_in: bit;

in

q:out bit_vector(1..n))

is begin 2 2

t 1..n q(t)=b(t 1,0) v c_in.b(t 1,1);

for in loop end loop;

carry_procedure;

end T(n) T

Let be the execution time of , (n)

carry_lookahead_procedure 1

## T

the execution time of , and the execution time of any one

dot_procedure 2

k can be executed

of the equations (4.10). The calls to dot_procedure

k .

in parallel, and the same occurs with the calls to carry_procedure

Furthermore, within the equations (4.10) can be calculated in

carry_procedure

parallel. Thus T(k:s) ¼ T þ T(k) þ T :

(s)

1 2

n ¼ s s s

. . .

Assume now that . . . . The algorithm obtained by recursively

m

1 2 has a computation time that can be

calling the carry_lookahead_procedure

## 66 ARITHMETIC OPERATIONS: ADDITION AND SUBTRACTION

calculated as follows:

T(s :s : :s ¼ T þ T(s : :s þ T

) (s ) ) ,

m m

1 2 1 1 2 2

T(s : :s ¼ T þ T(s : :s þ T

) (s ) ) ,

m m

2 1 2 3 2

...

T(s :s ¼ T þ T(s þ T

) (s ) ) ,

m1 m m1 m

1 2

T(s ¼ T þ T

) (s ) ,

m m

1 2

so that T(s :s : :s ¼ T þ T þ þ T þ m:T :

) (s ) (s ) (s ) (4:11)

m m

1 2 1 1 1 2 1 2

m

n ¼ s

In particular, if then þ T m ¼ n:

T(n) ¼ m:(T (s) ), where log (4:12)

s

1 2

The complete addition algorithm is the following.

Algorithm 4.9 Carry-Lookahead Addition

a: data_vector(0..n-1, 0..1);

begin

--computation of the generation and propagation conditions:

i 0..n-1 a(i,0):=g(x(i),y(i)); a(i,1):=p(x(i),

for in loop

y(i))); end loop;

--carry computation

carry_lookahead_procedure(n, a, c_in, q);

q(0):=c_in;

--sum computation

i 0..n-1 z(i):=(x(i)+y(i)+q(i)) mod B;

for in loop end loop;

z(n):=q(n);

4.1.3 Long-Operand Addition

long-operand additions n-digit

In the case of it may be necessary to break down the

s-digit n-bit

operands into slices. A typical example is the implementation of arith-

m-bit m , n.

metic operations within an microprocessor, with Taking into account

s

n-digit

that an base-B number can also be considered as being an (n/s)-digit base-B

number (Comment 3.1) a modiﬁed version of the basic algorithm 4.1 can be used.

The iteration body of Algorithm 4.1 must be substituted by a procedure

s-digit

, which computes the sum of two numbers:

natural_addition

natural_addition (s: in natural; carry: bit; x, y:

procedure in

digit_vector(0..s-1); next_carry: bit; z: digit_

in out out

vector(0..s-1); 67

## 4.1 ADDITION OF NATURAL NUMBERS

Any one of the previously proposed algorithms (4.1, 4.2, 4.7, or 4.9) can be used

procedure. Then the following algorithm

for deﬁning the natural_addition

x þ y þ c

computes .

in

Algorithm 4.10 Long-Operand Addition

q:=c_in;

i 0..n/s-1

for in loop

natural_addition(s, q, x(i*s..(i*s)+s-1), y(i*s..(i*s)+s-1),

q, z(i*s..(i*s)+s-1));

end loop;

z(n):=q;

Depending on the selection of the procedure, the correspond-

natural_addition

¼ n s.

ing execution time is proportional to either (n/s).s or (n/s).log

Observe that modiﬁed versions of the other algorithms would not give shorter

n

execution times: all of them include sentences

z(i):=(x(i)+y(i)+q(i)) mod B;

s

B n/s

equivalent, in base , to sentences

natural_addition(s, q(i), x(i*s..(i*s)+s-1), y(i*s..(i*s)+

s-1), not_used, z(i*s..(i*s)+s-1));

n/s

As the preceding sentences must be executed sequentially (long-operand

¼ n

constraint), the execution time would still be proportional to either (n/s).s or

(n/s).log(s).

4.1.4 Multioperand Addition multioperand addition,

Another important operation is the that is, the computation

z ¼ x þ x þ þ x x

(0) (1) (m21) (i)

. . .

of , where every is a natural number. Assume that

z n

the overall sum does not exceed digits and that all operands are expressed

n z.

with digits. The following algorithm computes

Algorithm 4.11 Basic Multioperand Addition

accumulator:=0;

j 0..m-1

for in loop

natural_addition(n, 0, accumulator, x(j), not_used,

accumulator);

end loop;

z:=accumulator; m.n m.log n

Its execution time is proportional to or depending on the selected

procedure.

natural_addition

## 68 ARITHMETIC OPERATIONS: ADDITION AND SUBTRACTION

stored-carry

An interesting concept for executing multioperand additions is the

form encoding of the result of a 3-operand addition. Assume that a procedure

three-to-two(w, x, y: natural; u, v: natural);

procedure in out

u v

has been deﬁned; it computes and such that

w þ x þ y ¼ u þ v:

z ¼ x þ x þ þ x m

(0) (1) (m21)

. . .

Then the following algorithm computes the sum of

natural numbers.

Algorithm 4.12

three-to-two(x(0), x(1), x(2), u(0), v(0));

j 3..m-1

for in loop

three-to-two (x(j), u(j-3), v(j-3), u(j-2), v(j-2));

end loop;

natural_addition(n, 0, u(m-3), v(m-3), not_used, z);

z

The procedure consists in expressing the sum of three natural

three-to-two

x, y) v)

numbers (w, under the form of a pair (u, of two natural numbers in such a

z ¼ u þ v. w, x, y n-digit

way that Assume now that and are numbers, and q_in

n-digit u

is a 1-digit number. The following algorithm computes two numbers and

v, , such that

and a 1-digit number q _out n

in ¼ q out:B þ u þ v:

w þ x þ y þ q (4:13)

Algorithm 4.13 Stored-Carry Encoding

stored-carry_encoding(w, x, y: digit_vector(0..

procedure in

n-1); q _in: digit; u, v: digit_vector(0..n-1); q _out:

in out

digit)

out is

begin

q(0):=q _in;

i 0..n-1

for in loop

q(i+1):=(w(i)+x(i)+y(i))/B;

u(i):=(w(i)+x(i)+y(i)) mod B;

end loop;

v:=q(0..n-1); q_out:=q(n);

stored-carry_encoding;

end

Algorithm 4.13 is similar to the basic addition algorithm 4.1: two digits are

q(i þ B-ary

computed at each step, and the ﬁrst one, 1), can be considered as a

B . q(i þ

carry (instead of a binary one when 2). Nevertheless, 1) does

q(i) n

not depend on so that the iteration steps can be executed in parallel. In

q(i þ

other words, at each step the carry 1) is stored instead of being transferred

69

4.1 ADDITION OF NATURAL NUMBERS v) stored-carry

to the next iteration step. For that reason the pair (u, is said to be the

form z.

of j)

x( j, i) x (

The following multioperand algorithm, where stands for (i), is deduced

z n-digit

from Algorithms 4.12 and 4.13 (assuming that is an number and that all

n

operands are expressed with digits).

Algorithm 4.14 Carry-Save Addition

stored-carry_encoding(x(0, 0..n-1), x(1, 0..n-1), x(2,

0..n-1), 0, u(0, 0..n-1), v(0, 0..n-1), not_used);

j 3..m-1

for in loop

stored-carry_encoding (x(j,0..n-1), u(j-3, 0..n-1),

2

v(j 3, 0..n-1), 0, u(j-2, 0..n-1),

v(j-2, 0..n-1), not_used);

end loop;

z(0):=u(m-3, 0);

natural_addition(n-1, 0, u(m-3, 1..n-1), v(m-3, 1..n-1),

not_used, z(1..n-1)); m 2

The carry-save addition algorithm is made up of 2 calls to stored-carry_

2

and a call to an (n 1)-digit addition procedure, so that the execution

encoding m þ n m þ m.n m.log(n).

time is roughly proportional to or log(n), instead of or

Comments 4.2 p-to-k

1. Instead of the procedure, more general procedures

three-to-two

could be deﬁned, as well as multioperand addition algorithms in which

p 2 k new operands are added at each step. The generalized version of Algor-

m ﬃ 2 k)/(p 2 k) k

ithm 4.12 would include (n steps to reach operands. Each

step could be decomposed in a similar way as in the case of Algorithm 4.13.

p ¼ k ¼

For instance, with 7 and 3, each step of the generalized algorithm

w w . . . w

(0) (1) (6)

4.12 should compute the sum of seven numbers , , , , and

encode the result as a three-component vector; the generalized version of

Algorithm 4.13 should compute

i)+...+w(6,

q(i+2):=(w(0, i)+w(1, i))/(B**2);

i)+...+w(6,

r(i+1):=(w(0, i)+w(1, i)-q(i+2)*(B**2))/B;

i)+...+w(6,

u(i):=(w(0, i)+w(1, i)) mod B;

B 2 , B 2

at each iteration step (observe that if 2, then 7.(B 1) ). This idea,

mainly applicable to the case of hardware implementations, will be developed

in Chapter 11.

2. Another idea mainly applicable to hardware implementations is the

substitution of the iterations (as in Algorithms 4.11, 4.12, and 4.14) by tree

structures. It will also be developed in Chapter 11.

## 70 ARITHMETIC OPERATIONS: ADDITION AND SUBTRACTION

4.1.5 Long-Multioperand Addition

long-multioperand addition

A can be executed by combining Algorithms 4.10 and

4.11.

Algorithm 4.15

accumulator:=0;

j 0..m-1

for in loop

q:=0;

i 0..n/s-1

for in loop

natural_addition(s, q, accumulator(i*s..(i*s)+s-1),

x(j,i*s..(i*s)+s-1), q, accumulator(i*s..(i*s)+s-1));

end loop;

end loop;

z:=accumulator; m.(n/s).s ¼ m.n m.(n/s).log(s).

Its execution time is proportional to either or

m n-digit

The stored-carry encoding could be used too. The reduction of operands

n-digit n-digit

to 2 operands can be performed by breaking down each operand

n/s s-digit procedure

into ones and calling the stored-carry_encoding

2

(m 2).(n/s) times. Then the so-obtained operands are added.

Algorithm 4.16 Carry-Save Long-Multioperand Addition

--m-to-2 reduction:

q:=0;

i 0..n/s-1

for in loop

stored-carry_encoding (x(0, i*s..(i*s)+s-1),

x(1, i*s..(i*s)+s-1), x(2, i*s..(i*s)+s-1),

q, u(i*s..(i*s)+s-1), v(i*s..(i*s)+s-1), q);

end loop;

j 3..m-1

for in loop

q:=0;

i 0..n/s-1

for in loop

stored-carry_encoding (x(j, i*s..(i*s)+s-1), u(i*s..(i*s)

+s-1), v(i*s..(i*s)+s-1),q, u(i*s..(i*s)+s-1),

v(i*s..(i*s)+s-1), q);

end loop;

end loop;

--2-operand addition:

q:=0;

i 0..n/s-1

for in loop

natural_addition(s, q, u(i*s..(i*s)+s-1), v(i*s..(i*s)+

s-1), q, z(i*s..(i*s)+s-1));

end loop; 71

4.3 INTEGERS

m-to-2

The reduction is performed in (m22).(n/s) steps, and the 2-operand addition

¼ n s.

execution time is proportional to either (n/s).s or (n/s).log The total

þ s) þ s)

execution time is roughly proportional to either (n/s).(m or (n/s).(m log

s.

instead of (n/s).m.s or (n/s).m.log

4.2 SUBTRACTION OF NATURAL NUMBERS n-digit

The following (pencil and paper) algorithm computes the representation of

z ¼ x 2 y 2 b b borrow z

where is an initial equal to 0 or 1; if is negative—

in in

z q(n)

that means that is not a natural number—the output borrow is equal to 1.

Algorithm 4.17 Subtraction

q(0):=b_in;

i 0..n-1

for in loop

x(i)-y(i)-q(i)<0 q(i+1):=1; q(i+1):=0;

if then else

r(i):=(x(i)-y(i)-q(i)) mod B;

end if;

end loop;

negative:=q(n);

Another method consists in treating the subtraction of natural numbers as a

particular case of the subtraction of integers (next section).

4.3 INTEGERS

In the case of integer numbers, the addition and subtraction algorithms depend

on the particular representation. Three nonredundant representation methods are

B’s

considered in what follows: complement, sign-magnitude, and excess-E

(Chapter 3).

B’s

4.3.1 Complement Addition

n-digit B’s x y, c

Given two complement integers and and an initial carry equal to 0

in

þ B’s

z ¼ x þ y þ c is an (n 1)-digit complement integer. Assume that

or 1, then in

x y nþ1

and are represented with digits. Then the natural numbers associated

nþ1 nþ1

R(y) ¼ y B R(z) ¼ z

x, y, z R(x) ¼ x B , mod , and mod

with and are mod

nþ1

B (Deﬁnition 3.4), so that nþ1 nþ1

B ¼ þ R(y) þ c B :

R(z) ¼ þ y þ c ) mod (R(x) ) mod

(x in in x y nþ1

Thus a straightforward addition algorithm consists in representing and with

digits and adding the corresponding natural numbers, as well as the initial carry,

nþ1

B (that means without taking into account the output carry). In order

modulo

## 72 ARITHMETIC OPERATIONS: ADDITION AND SUBTRACTION

x y

to represent and with one additional digit, Comment 3.2 is taken into account. As

computes the sum of two natural

before, the procedure natural_addition

numbers. B’s

Algorithm 4.18 Complement Addition

x(n-1)<B/2 x(n):=0; x(n):=B-1;

if then else end if;

y(n-1)<B/2 y(n):=0; y(n):=B-1;

if then else end if;

natural_addition(n+1, c_in, x, y, not_used, z);

B ¼ n ¼ c ¼ x ¼ 22345, y ¼ 23674..

Assume that 10, 4, 0, and

Example 4.1 in

x y R(x) ¼

Both and are negative so that they are represented by

¼ R(y) ¼ 23674 þ ¼ x y

22345 þ 4 4

7655 and 10 6326. First represent and

10 R(x) ¼ R(y) ¼ R(x) R(y)

with ﬁve digits: 97655 and 96326. Then add up and

þ ¼ B/2 ¼

5 5

modulo 10 : (97655 96326) mod 10 93981. As 9 5, the integer rep-

¼ 26019,

2 5 that is, the sum

resented by 93981 is negative and equal to 93981 10

x y.

of and

B’s

4.3.2 Complement Sign Change

n-digit B’s x, z ¼ 2x x þ

Given an complement integer the inverse of is an (n 1)-

B’s 2x

digit complement integer (actually the only case when cannot be represented

n n n

n x ¼ 2B /2 2x ¼ B /2; 2x ¼ 20.B þ

with digits is when and i.e., (B/2).

n21 n22

þ þ þ 2x

B 0

. . .

0.B 0.B ). The computation of the representation of is

based on the following property. m21

B þ a

m-digit a ¼ a

Given two base-B natural numbers

Property 4.3 m21. m22.

m22 m21 m22

B þ þ a B b ¼ 2 2 a þ 2 2 a þ

0

. . . . . .

and (B 1 ).B (B 1 ).B

m21 m22

0.

þ 2 2 a 0

).B , then

(B 1 0 m a

b ¼ B 1: (4:14)

0

x n þ x as being the natural

Assume that is represented with 1 digits, and deﬁne 0

x ¼ B 2 2 x

R(x) x by 1 . Then,

number deduced from by substituting every digit i

i i

according to Property 4.3, 0 nþ1

¼ B R(x)

x 1

and nþ1 nþ1 nþ1 0 nþ1

R(x) ¼ B ¼ R(x)) B ¼ þ B :

(x) mod (B mod (x 1) mod (4:15)

x n þ

A straightforward inversion algorithm consists in representing with 1 digits,

B 2

complementing every digit to 1, then adding 1. 73

4.3 INTEGERS B’s

Algorithm 4.19 Complement Sign Change

x(n-1)<B/2 x(n):=0; x(n):=B-1;

if then else end if;

i 0..n x’(i):=B-1-y(i);

for in loop end loop;

natural_addition(n+1, 1, x’, 0, not_used, z);

Examples 4.2 B ¼ n ¼ x ¼ x

1. Assume that 10, 4, 2345; is nonnegative and is represented by

R(x) ¼ x ¼ x R(x) ¼

2345. First represent with ﬁve digits: 02345. Then com-

B 2 ¼ þ ¼

5

plement all digits to 1 9, and add 1: (97654 1) mod 10 97655.

¼ 22345, 2x.

2 5 that is,

The integer represented by 97655 is 97655 10

x ¼ 25000 x

2. If then the four-digit representation of is 5000 and its ﬁve-digit

one is 95000. By complementing all digits and adding 1 the obtained result is

¼

þ 5 05000, which is the representation of the nonnegative

(04999 1) mod 10

number 5000.

x ¼ x

3. If 0 then the four-digit representation of is 0000 and its ﬁve-digit one is

00000. By complementing all digits and adding 1 the obtained result is

þ ¼

5

(99999 1) mod 10 00000, which is the representation of the nonnegative

number 0. x

An alternative sign-change algorithm is based on the following observation: if

is represented under the form n kþ1 k

R(x) ¼ x :B þ þ x :B þ x :B ,

n kþ1 k

. 2x

x 0, then the representation of is

where k n kþ1

þ þ x

R(x) ¼ x ):B (B 1 ):B

(B 1 n kþ1

k k1

þ x þ þ þ þ

0

(B 1 ):B (B 1):B (B 1):B 1

k n kþ1 k

¼ x þ þ x þ x :

(B 1 ):B (B 1 ):B (B ):B

n kþ1 k

, initially equal

In the following algorithm, the binary variable ﬁrst_non_zero

x

to 0, is set to 1 as soon as the ﬁrst nonzero digit of is encountered.

B’s

Algorithm 4.20 Complement Sign Change (Alternative Algorithm)

x(n-1)<B/2 x(n):=0; x(n):=B-1;

if then else end if;

ﬁrst_non_zero:=0;

i 0..n

for in loop

ﬁrst_non_zero=0

if then

x(i)=0 z(i):=0; z(i):=B-x(i); ﬁrst_non_zero

if then else

:=1; end if;

z(i):=B-1-x(i);

else

end if;

end loop;

## 74 ARITHMETIC OPERATIONS: ADDITION AND SUBTRACTION

B’s

4.3.3 Complement Subtraction

n-digit B’s x y, b

Given two complement integers and and an input borrow equal to

in

z ¼ x 2 y 2 b þ B’s

0 or 1, then is an (n 1)-digit complement integer. Assume that

in

x y n þ

and are represented with 1 digits. Then the natural numbers associated with

nþ1 0 nþ1

x, 2y, z R(x) ¼ x B R(2y) ¼ þ B

and are mod , (y 1) mod (relation (4.15))

nþ1

R(z) ¼ z B , so that

and mod nþ1 0 nþ1

B ¼ þ y þ b B :

R(z) ¼ y b ) mod (R(x) (1 )) mod

(x in in x y

Thus a straightforward subtraction algorithm consists in representing and with

nþ1

n þ y, B , the corre-

1 digits, complementing the digits of and adding, modulo

sponding natural numbers, as well as the inverted input borrow.

B’s

Algorithm 4.21 Complement Subtraction

x(n-1)<B/2 x(n):=0; x(n):=B-1;

if then else end if;

y(n-1)<B/2 y(n):=0; y(n):=B-1;

if then else end if;

i 0..n y’(i):=B-1-y(i);

for in loop end loop;

c_in:=1-b_in;

natural_addition(n+1, x, y’, c_in, z, not_used);

B ¼ n ¼ b ¼ x ¼ 22345, y ¼ x

Assume that 10, 4, 1, and 3674; is

Example 4.3 in

y R(x) ¼

negative and nonnegative, so that they are represented by

22345 þ ¼ R(y) ¼ y ¼ x y

4

10 7655 and 3674. First represent and with ﬁve

0 ¼ c ¼

R(x) ¼ R(y) ¼ y 96325,

digits: 97655 and 03674. Then compute in

0

¼ þ y þ c ¼ þ ¼

2 b 5 5

0 and (R(x) ) mod 10 (97655 96325) mod 10 93980.

1 in in ¼ 26020,

2 5 that is,

The integer represented by 93980 is equal to 93980 10

2234523674 2 1.

B’s

4.3.4 Complement Overﬂow Detection

In some cases it may be necessary to know whether the result of an operation actu-

þ n-digit

ally is an (n 1)-digit number and not an one. A typical case is the arithmetic

n-bit

unit of a general-purpose computer: both the operands and the result are num-

overﬂow ﬂag n

bers, and an is raised if the result does not ﬁt within bits. Assume that

the previous algorithms (addition, inversion, and subtraction) are executed without

n þ

extending the operands to 1 bits:

1. Consider the case of addition. An overﬂow can occur when both operands

x y

have the same sign. First observe that if and belong to the interval

n n n n n

/2 x, y , B /2, 2B x þ y þ c /2 2 þ ¼ B 2

2B then 2.(B 1) 1 1;

in

that is, n n

B x þ y þ c , B : (4:16)

in 75

4.3 INTEGERS

x y x þ y þ c

So, if and are nonnegative, the sum could be greater than or

in

n n

/2. R(x) ¼ x R(y) ¼ y, R(z) ¼ þ y þ c B

B As and then (x ) mod ,

equal to in

and according to the previous hypothesis and to (4.16)

n n

B =2 x þ y þ c , B ,

in

that is, n1 n

x þ y þ c , B ,

(B=2):B in

z(n) ¼ z(n 2 B/2.

so that 0 and 1)

The conclusion is that the sum of two nonnegative numbers, plus an initial

n

carry, generates an apparently negative number if only digits are available

2 B/2).

(z(n 1) n

x y x þ y þ c 2B /2.

If and are negative the sum could be smaller than As

in

n n n n

þ x R(y) ¼ B þ y, R(z) ¼ þ x þ y þ c B

R(x) ¼ B and then (2.B ) mod ,

in

and according to the previous hypothesis and to (4.16)

n n n n n

B þ x þ y þ c , B =2,

2:B 2:B

2:B in

that is, n n n n1

þ x þ y þ c , B þ

B 2:B (B=2):B ,

in

so that z(n) ¼ z(n , B=2:

1, 1)

The conclusion is that the sum of two negative numbers, plus an initial

n

carry, generates an apparently nonnegative number if only digits are avail-

2 , B/2).

able (z(n 1)

To summarize, the overﬂow detection is carried out just looking at the sign

digits of the operands and the result. Under Boolean form:

¼ ½(x(n , B=2) , B=2) B=2)

1) and (y(n 1) and (z(n 1)

ovf

add ½(x(n B=2) B=2)

or 1) and (y(n 1)

, B=2):

and (z(n 1) (4:17)

2. It has already been observed that, in the case of the sign-change operation, the

n

x ¼ 2B /2, x(n 2 ¼ B21

only overﬂow situation is when namely, 1) and

x(n 2 ¼ ¼ x(0) ¼ n

. . .

2) 0. The inversion algorithm, with digits, generates

z ¼ x. Once again it’s just a matter of looking at the sign digits of both the

operand and the result:

¼ B=2) B=2):

(x(n 1) and (z(n 1) (4:18)

ovf

inv

3. If a subtraction is performed, an overﬂow could occur if one operand is

x y

negative and the other one nonnegative. First observe that if and belong

## 76 ARITHMETIC OPERATIONS: ADDITION AND SUBTRACTION

n n n

2B /2 x, y , B /2 22.(B /2) 2 , x 2 y 2 b ,

to the interval then 1 in

n /2), that is,

2.(B n n

x y b , B :

B (4:19)

in

x y , x 2 y 2 b could be greater than or equal to

If 0 and 0 the difference in

n n 0 n

/2. R(x) ¼ x, R(y) ¼ B þ y, y þ ¼ B 2 R(y) ¼ 2y, R(z) ¼ þ

B As 1 (R(x)

0 n n

þ 2 b B ¼ 2 y 2 b B

y 1 ) mod (x ) mod , then according to the previous

in in

hypothesis and to (4.19) n1 n n

¼ B =2 x y b , B ,

(B=2):B in

z(n) ¼ z(n 2 B/2.

so that 0, 1)

The conclusion is that the difference between a nonnegative number and a nega-

n

tive one, minus an initial borrow, generates an apparently negative number if only

2 B/2).

digits are used (z(n 1) n

x , y x 2 y 2 b 2B /2.

If 0 and 0 the difference could be smaller than As

in

n 0 n n 0

þ x, R(y) ¼ y, y þ ¼ B 2 R(y) ¼ B 2 y, R(z) ¼ þ y þ 2 b

R(x) ¼ B 1 (R(x) 1 )

in

n n n

B ¼ þ x 2 y 2 b B

mod (2.B ) mod , then according to the previous hypothesis

in

and to (4.19)

n n n n n n n n1

B ¼ B þ x y b , B =2 ¼ B þ ;

2:B 2:B 2:B (B=2):B

in

z(n) ¼ z(n 2 , B/2.

so that 1, 1)

The conclusion is that the difference between a negative number and a nonnega-

tive one, minus an initial borrow, generates an apparently nonnegative number if

n 2 B/2).

only digits are used (z(n 1)

As in the preceding cases the overﬂow detection is carried out just looking at the

sign digits of the operands and the result. Under Boolean form:

¼ ½(x(n , B=2) B=2) B=2)

1) and (y(n 1) and (z(n 1)

sub ovf ½(x(n B=2) , B=2)

or 1) and (y(n 1)

, B=2):

and (z(n 1) (4:20)

¼ n ¼

(B 10, 4)

Examples 4.4 c ¼ x ¼ y ¼ x þ y þ c

1. Assume that 0, 2345, and 4674, and that the value of

in in

R(x) ¼ x ¼ R(y) ¼ y ¼ þ

is computed. Then 2345 and 4674, so that (R(x)

R(x) þ c ¼

) mod 10000 7019, that is, the representation of the negative

in

22981.

number c ¼ x ¼ 24726, y ¼ 22174,

2. Assume now that 0, and and that the value

in

x þ y þ c R(x) ¼ 2 ¼

of is computed. Then 10000 4726 5274 and

in 77

4.3 INTEGERS

R(y) ¼ 2 ¼ þ R(x) þ c ¼

10000 2174 7826, so that (R(x) ) mod 10000

in

3100, that is, the representation of the nonnegative number 3100.

x ¼ y ¼ 24726, b ¼

3. Compute the difference between 2345 and with 0. The

in

R(x) ¼ x ¼ R(y) ¼ 2

corresponding representations are 2345 and 10000

0 ¼ þ þ ¼

¼ y 4725 and (2345 4725 1) mod 10000 7071,

4726 5274, so that 22929.

that is, the representation of the negative number

Comments 4.3 B’s

About the reduced complement representation (Comment 3.2):

1. The sign extension just consists in duplicating the sign digit.

B ¼

2. If 2, there is no difference between the reduced and the nonreduced 2’s

complement representation. n21 x,

x y n-digit B’s 2B

3. If and are reduced complement numbers, then

n21

y , B , so that

n1 n1 n1 n1

x þ y þ c , x y b , :

2:B 2:B and 2:B 2:B

in in

n21 n

B /2

B . B B and

If, furthermore, 2 and is even, so that 4, then 2.B

n n n n

=2 x þ y þ c , B =2 B =2 x y b , B =2:

B and

in in

x 2 y 2 b n-digit B’s

x þ y þ c and are complement numbers.

Thus both in in B’s

There is an overﬂow if the result is not a reduced complement number,

f0, B 2

that is, if the sign digit does not belong to 1g. Actually the sign

n21

B22 , 2B

digit is equal to in the case of a negative overﬂow (result )

n21

B ).

and to 1 in the case of a positive one (result

¼ n ¼

(B 10, 3, 10’s complement reduced form)

Examples 4.5 c ¼ x ¼ y ¼ x þ y

1. Assume that 0, 74, and 41, and that the value of is com-

in

R(x) ¼ x ¼ R(y) ¼ y ¼ þ R(x) þ c )

puted. Then 074 and 041, so that (R(x) in

¼ f0,

mod 1000 115, a number whose sign digit does not belong to 9g.

c ¼ x ¼ 274, y ¼ 241, x þ y

2. Assume now that 0, and and that the value of

in

R(x) ¼ 2 ¼ R(y) ¼ 2 ¼

is computed. Then 1000 74 926 and 1000 41 959, so

þ R(x) þ c ¼

that (R(x) ) mod 1000 885, a number whose sign digit does not

in

f0,

belong to 9g—actually the representation in nonreduced form of

2 ¼ 2115.

885 1000 x ¼ y ¼ 241, b ¼

3. Compute the difference between 74 and with 0. The cor-

in

R(x) ¼ x ¼ R(y) ¼ 2 ¼

responding representations are 074 and 1000 41 959,

0 ¼ þ þ ¼

y 040 and (074 040 1) mod 1000 115, a number whose sign

so that f0,

digit does not belong to 9g—actually the representation in nonreduced

form of 115.

## 78 ARITHMETIC OPERATIONS: ADDITION AND SUBTRACTION

4.3.5 Excess-E Addition and Subtraction

The addition and subtraction algorithms are based on the following properties.

x y, c

Given two excess-E integers and an initial binary carry and

Properties 4.4 in

b

an initial borrow , then

in

R(x þ y þ c ¼ R(x) þ R(y) þ c 2 E,

)

in in

¼ R(x) 2 R(y) 2 b þ E,

R(x 2 y 2 b )

in in

R(2x) ¼ 2R(x) þ 2.E.

Proof R(x) ¼ x þ E, R(y) ¼ y þ E, R(x þ y þ

According to Deﬁnition 3.3, and

c ¼ x þ y þ c þ E,

) so

in in E ¼ ðx þ EÞ þ ðy þ EÞ þ c E ¼ x þ y þ c þ E

RðxÞ þ RðyÞ þ c in in in

Þ;

¼ Rðx þ y þ c in

RðxÞ RðyÞ b þ E ¼ ðx þ EÞ ðy þ EÞ b þ E ¼ x y b þ E

in in in

¼ Rðx y b Þ;

in

RðxÞ þ ¼ ðx þ EÞ þ ¼ x þ E ¼ RðxÞ:

2:E 2:E

x y n-digit z ¼ x þ y þ c n-digit

If and are two excess-E integers, and if is also an

in x

excess-E integer, then a straightforward addition algorithm consists in representing

E. R(z)

y n þ c and subtracting The result is

and with 1 digits, adding them up with in

þ

an (n 1)-digit natural number whose ﬁrst digit is 0. has been deﬁned:

Assume that a procedure natural_subtraction

natural_subtraction (s: in natural; borrow: bit;

procedure in

x, y: digit_vector(0..s-1); next_borrow: bit; z:

in out out

digit_vector(0..s-1); z ¼ x þ y þ c

The following algorithms compute .

in

Algorithm 4.22 Excess-E Addition

x(n):=0; y(n):=0;

natural_addition(n+1, x, y, c_in, w, not_used);

natural_subtraction(n+1, w, E, 0, z, not_used);

z(n)>0 overﬂow:=true;

if then end if; z ¼ x 2 y 2 b z ¼ 2x:

Similar algorithms can be deﬁned for computing and

in

Algorithm 4.23 Excess-E Subtraction

x(n):=0; y(n):=0;

natural_addition(n+1, x, E, 0, w, not_used);

natural_subtraction(n+1, w, y, b_in, z, not_used);

z(n)>0 overﬂow:=true;

if then end if; 79

4.3 INTEGERS

Algorithm 4.24 Excess-E Sign Change

x(n):=0;

E_by_2(0):=0;

i 1..n E_by_2(i):=E(i-1);

for in loop end loop;

natural_subtraction(n+1, E_by_2, x, 0, z, not_used);

z(n)>0 overﬂow:=true;

if then end if;

¼ n ¼

(B 10, 4, excess 5000)

Examples 4.6 c ¼ x ¼ y ¼ x þ y þ c

1. Assume that 0, 2345, and 1674, and that the value of

in in

R(x) ¼ R(y) ¼ R(x) þ R(y) þ

is computed. Then 07345 and 06674, so that

2 ¼

c 05000 09019, that is, the representation of 4019.

in c ¼ x ¼ 22345, y ¼ x þ

2. Assume now that 0, and 1674, and that the value of

in R(x) ¼ R(y) ¼ R(x) þ

y þ c is computed. Then 02655 and 06674, so that

in 2 ¼ 2671.

R(y) þ c 05000 4329, that is, the representation of

in x ¼ y ¼ c ¼

3. Compute the sum of 2345 and 4726, with 0. The corresponding

in

R(x) ¼ R(y) ¼ R(x) þ R(y) þ

representations are 07345 and 09726, so that

2 ¼

c 05000 12071 and the overﬂow ﬂag is raised.

in x ¼ y ¼ b ¼

4. Compute the difference between 2345 and 4726, with 0. The

in

R(x) ¼ R(y) ¼

corresponding representations are 07345 and 09726, so that

þ ¼ 22381.

R(x) 2 R(y) 2 b 05000 2619, that is, the representation of

in ¼

x ¼ 22345 y ¼ b 0. The

5. Compute the difference between and 4726, with in

R(x) ¼ R(y) ¼

corresponding representations are 02655 and 09726, so that

nþ1

þ ¼ B ¼

R(x) 2 R(y) 2 b 05000 97929 (modulo 100000) and the over-

in

ﬂow ﬂag is raised. x ¼ 25000.

6. Compute the inverse of The corresponding representation is

R(x) ¼ 2 R(x) þ ¼

.

00000 so that 2 05000 10000 and the overﬂow ﬂag is

raised.

4.3.6 Sign – Magnitude Addition and Subtraction

n-digit x y, z ¼ x þ y þ c

Given two sign-magnitude integers and then is an

in

þ z.

(n 1)-digit sign-magnitude integer. The following algorithm computes

Algorithm 4.25

sign(x)=sign(y) a:=abs(x)+abs(y); a:=abs(x)-

if then else

abs(y);

end if;

a<0 sign(z):=sign(y); abs(z):=-a; sign(z):=

if then else

sign(x); abs(z):=a; end if;

It is equivalent to the following algorithm (at the digit level).

## 80 ARITHMETIC OPERATIONS: ADDITION AND SUBTRACTION

Algorithm 4.26 Sign-Magnitude Addition

abs_x:=x(0..n-2)&0; abs_y:=y(0..n-2)&0;

x(n-1)=y(n-1)

if then

natural_addition(n, abs_x, abs_y, 0, a, not_used);

else i 0..n-1 abs_y’(i):=B-1-abs_y(i);

for in loop end loop;

natural_addition(n, abs_x, abs_y’, 1, a, not_used);

end if;

a(n-1)=B-1

if then

z(n):=y(n-1);

i 0..n-1 a’(i):=B-1-a(i);

for in loop end loop;

natural_addition(n, 0, a’, 1, z(0..n-1), not_used);

else

z(n):=x(n-1); z(0..n-1):=a;

end if; ¼ n ¼ x ¼ þ2345 y ¼ 27674.

(B 10, 5). Assume that and First

Example 4.7 abs(x) ¼ abs(y) ¼

express the absolute values with ﬁve digits: 02345 and 07674.

þ þ ¼

As the signs are different, compute 02345 92325 1 94671. The ﬁrst digit is

equal to 9, indicating a negative value. The sign of the result is the same as the

y þ ¼

sign of (2) and the absolute value of the result is 05328 1 05329. So the

25329.

ﬁnal result is

Comment 4.4 sign(x) ¼ sign(y) ¼ abs(x) ¼

With algorithm 4.26, if 0, 1, and

abs(y), þ0; sign(x) ¼ sign(y) ¼ abs(x) ¼ abs(y),

the result is if 1, 0, and the

20.

result is x 2 y x þ minus_y

The subtraction is equivalent to the addition where

minus_y ¼ 2y, minus_y

and the computation of is straightforward:

minus_y(n-1):=1-y(n-1); minus_y (0..n-2):=y(0..n-2);

4.4 BIBLIOGRAPHY IEEE Trans.

[BRE1982] R. Brent and H. T. Kung, A regular layout for parallel adders.

Comput., 260– 264 (1982).

C-31(3): Proceedings of the

[HAN1987] T. Han and D. A. Carlson, Fast area-efﬁcient VLSI adders. In:

8th Symposium on Computer Arithmetic, 1987, pp. 49 – 56.

[KOG1973] P. M. Kogge and H. S. Stone, A parallel algorithm for the efﬁcient solution of a

IEEE Trans. Comput.,

general class of recurrence equations. 786 – 793 (1973).

C-22(8):

## J. ACM

[LAD1980] R. E. Ladner and M. J. Fischer, Parallel preﬁx computation. 831 –

27(10):

838 (1980). IEEE Trans.

[SUG1990] B. Sugla and D. Carlson, Extreme area-time tradeoffs in VLS.

Comput. 251– 257 (1990).

39(2):

5

## ARITHMETIC OPERATIONS:

## MULTIPLICATION

Basically, multiplication is a very simple operation as it most often reduces to multi-

operand addition. In early computers, multiplication was assumed too complex to

receive a combinational implementation, typically considered too expensive at

this time. For this historical reason, in most textbooks on computer arithmetic,

multiplication algorithms are strongly biased by the sequential implementations.

In this chapter, the authors attempt to remain consistent with their general philo-

sophy, presenting the algorithms in a way that never settles on a speciﬁc imple-

mentation technique. Although the Ada-like language, utilized in the algorithm

descriptions, could suggest some kind of sequential implementations, the actual

interpretations cannot involve any choice between space or time iteration of the pre-

sented step-by-step processes. This approach is particularly well suited to provide

the designer, with a range of options, based on the diversity of technologies at

hand, speed –cost compromises, and other constraints to be dealt with. Actually, it

is important to realize that the algorithmic complexity is not necessarily tied to

the actual required performance of some practical application.

Base-B is generally assumed, while base-2 is extensively treated whenever the

speciﬁcity of the binary system results in prominent features or allows signiﬁcant

algorithmic simpliﬁcations. Most multiplication algorithms share a common feature:

they produce, in one way or another, all the digitwise partial products of the oper-

ands. The complexity of the corresponding cell or procedure is thus a key point to

be considered by the designer when selecting the base. As quoted in Chapter 3,

Synthesis of Arithmetic Circuits: FPGA, ASIC, and Embedded Systems

By Jean-Pierre Deschamps, Géry J. A. Bioul, and Gustavo D. Sutter

#

Copyright 2006 John Wiley & Sons, Inc. 81

82 ARITHMETIC OPERATIONS: MULTIPLICATION

the most used bases are 2 (binary), 4 (quaternary or radix-4), 8 (octal or radix-8), 16

(hexadecimal or radix-16), and 10 (decimal). The examples treated in this chapter

## B.

will be limited to those bases, although most theorems hold for any base As

far as the technology deals with two-level signals and devices, binary coding is

assumed in most practical implementations. Nevertheless, from the algorithmic

point of view, the base coding aspect is not relevant.

Logarithmic techniques for multiplication are not generally used because log-

arithm computation algorithms do not exhibit a better complexity behavior than

multiplication itself. Actually, if look-up tables (LUTs) are available, the process

is interesting because it reduces to a simple addition. Nevertheless, the cost of

look-up tables is formidable except for small operand sizes.

Let us point out, ﬁnally, that the fast evolution of technology may change the

optimization criteria and the performance factors of some types of physical

implementations. So it is quite difﬁcult to forecast future interest in the respective

algorithm options. This chapter presents the most used multiplication algorithms

while Chapter 12 is devoted to multiplier design with some typical FPGA and IC

implementations.

5.1 NATURAL NUMBERS MULTIPLICATION

5.1.1 Introduction n-digit m-digit B-ary

The most basic multiplication algorithms for natural

numbers (shift and add algorithms) proceed in two phases:

m),

1. Digitwise partial products (n

Multioperand

2. addition.

The classic computation scheme to introduce multiplication is given in Figure 5.1a,

where partial products appear lined up according to their respective weight. This

scheme is historically related to the pencil and paper implementation of the opera-

x y

tion. This simple scheme is easily built up by noting that the partial products are

i j

k

k ¼ i þ j B . Observe

lined up in the column whose index corresponds to the weight

B . B .

that whenever 2, the partial products may need two base-B digits. For 2, a

Y x y

X and stand,

possible multiplication scheme is displayed at Figure 5.1b, where i j i j

respectively, for the integer product

Y ¼ :y

X (x )=B (5:1)

i j i j

and the mod B product x y ¼ :y B

(x ) mod (5:2)

i j i j

k i þ j

Observe that the column index remains for products (5.2) but is computed as

i þ j þ 1 for products (5.1). 83

5.1 NATURAL NUMBERS MULTIPLICATION

x y x y x y x y x y

…

(a) m

0 –1 0 3 0 2 0 1 0 0

x y x y x y x y x y

…

m

1 –1 1 3 1 2 1 1 1 0

x y x y x y x y x y

…

m

2 –1 2 3 2 2 2 1 2 0

x y x y x y x y x y

…

m–1

3 3 3 3 2 3 1 3 0

… …

x y x y x y x y x y

…

n–1 m–1 n n n n

–1 3 –1 2 –1 1 –1 0

x y x y x y x y

x y

…

(b) m

0 –1 0 1 0 0

0 3 0 2

## X Y X Y X Y X Y

## X Y

…

m

0 –1 0 3 0 0

0 2 0 1

x y x y

x y x y x y

…

m 1 2

1 –1 1 3 1 0

1 1

## X Y

## X Y X Y X Y X Y

…

m 1 1

1 –1 1 3 1 2 1 0

x y x y

x y x y x y

…

m 2 1

2 –1 2 3 2 2 2 0

## X Y X Y X Y X Y X Y

…

m

2 –1 2 3 2 2 2 1 2 0

x y x y x y x y x y

…

m

3 –1 3 3 3 2 3 1 3 0

## X Y X Y X Y X Y X Y

…

m

3 –1 3 3 3 2 3 1 3 0

… …

x y x y x y x y x y

…

n m n n n n

–1 –1 –1 3 –1 2 –1 1 –1 0

## X Y X Y X Y X Y X Y

…

n m n n n n

–1 –1 –1 3 –1 2 –1 1 –1 0 B .

(a) Multiplication scheme and (b) multiplication scheme for 2.

Figure 5.1

The cost/speed constraints are key factors to set trade-offs between combina-

tional parallel schemes and sequential implementations. The most popular algor-

ithms, with a number of implementation schemes, are proposed in what follows,

n-digit m-digit

where by operands are considered for generality.

5.1.2 Shift and Add Algorithms

5.1.2.1 Shift and Add 1 Multiplication is known as a commutative operation in

which both operands play the same mathematical role. At the algorithmic point of

view, the situation is somewhat different. Actually, in most algorithm descriptions,

multiplicator,

one of the operands, called the is viewed as some kind of parameter

multiplicand,

set, while the other one, the is viewed as a data set.

## X Y

Let the multiplicator and the multiplicand be given by

n1 n2

X ¼ x :B þ x :B þ þ x :B 0 ,

n1 n2 0

m1 n2

Y ¼ y :B þ y :B þ þ y :B x y [ . . . B

0 , , {0, 1, , 1},

m1 m2 i i

0

Let Z ¼ X:Y (5:3)

with nþm1 nþm2

:B þ z :B þ þ z :B :

Z ¼ z 0

nþm1 nþm2 0

84 ARITHMETIC OPERATIONS: MULTIPLICATION

Since n m

X B Y B

0 1 and 0 1,

then n m

Z

0 (B 1):(B 1):

Equation (5.3) can be written

n1 n2

Z ¼ x :Y:B þ x :Y:B þ þ x :Y:B þ x :Y:B þ x :Y,

2 (5:4)

n1 n2 2 1 0

then expanded as

Z ¼ . . . þ x :Y):B þ x :Y):B þ þ x :Y):B þ x :Y):B þ x :Y,

(( ((0:B (5:5)

n1 n2 2 1 0

Hörner expansion. 1

called the This suggests the following algorithm.

Algorithm 5.1 Hörner Shift and Add 1

P(n):=0;

i 0..n-1

for in loop

P(n-1-i):=P(n-i)*B+X(n-1-i)*Y;

end loop;

Z:=P(0);

5.1.2.2 Shift and Add 2 It will be shown in Chapter 12 that a right to left

factorization (5.6) reduces by half the adder length.

n 1 1 1 1

¼ B :(x :Y þ B :(x :Y þ þ B :(x :Y þ B :(x :Y þ

Z=B 0)) )):

n1 n2 1 0 (5:6)

Algorithm 5.2 Hörner Shift and Add 2

P(0):=0;

i 0..n-1

for in loop

P(i+1):=(P(i)+X(i)*Y)/B;

end loop;

Z=P(n)*(B**n); X Y

The difference between the roles of and clearly appears in the recurrence

## Y

formula of the Hörner Algorithms 5.1 and 5.2. At each step, the multiplicand is

/

1 * * *

In the used Ada-like language, stands for multiplication, stands for exponentiation, and stands

without ambiguity for division integer or not. 85

5.1 NATURAL NUMBERS MULTIPLICATION

X(k)

multiplied by then either (Algorithm 5.1) added to the (left-)shifted result of the

preceding step or (Algorithm 5.2) added to the result of the preceding step and then

X(k)

(right-)shifted. The multiplicator component sets how many times the multipli-

Y B ¼

cand has to be added. For 2, the process is quite simple because, at each step,

X(k) [ Y B

{0,1} just sets if has to be added or not, while, whenever 3, nontrivial

partial products have to be generated.

X ¼ Y ¼ n ¼

Let 367169 and 24512 be two decimal numbers: 6,

Example 5.1

m ¼ 5

The Hörner expressions (5.5) and (5.6) backing Algorithms 5.1 and 5.2 respect-

ively, are:

Z ¼ þ þ þ

(( (3:24512):10 6:24512):10 7:24512):10 1:24512):10

þ þ

6:24512):10 9:24512

and 1 1 1

Z=10 ¼ þ þ

6 (( (9:24512):10 6:24512):10 1:24512):10

1 1 1

þ þ þ

7:24512):10 6:24512):10 3:24512):10

Let us compute step by step.

Hörner Algorithm 5.1

Step 1: P(5) ¼ ¼

(3 24512) 73536

þ ¼

Step 2: P(4) ¼ 10 6 24512)) 882432

(P(5)

Step 3: P(3) ¼ þ ¼

(P(4) 10 7 24512)) 8995904

Step 4: P(2) ¼ þ ¼

(P(3) 10 1 24512)) 89983552

Step 5: P(1) ¼ þ ¼

(P(2) 10 6 24512)) 899982592

Step 6: P(0) ¼ þ ¼ Z

(P(1) 10 9 24512)) 9000046528 =

Hörner Algorithm 5.2 21

Step 1: P(1) ¼ ¼

(9 24512) 10 22060.8

21 ¼

Step 2: P(2) ¼ þ 24512)) 10 16913.28

(P(1) (6 21

Step 3: P(3) ¼ þ ¼

(P(2) (1 24512)) 10 04142.528

21

Step 4: P(4) ¼ þ ¼

(P(3) (7 24512)) 10 17572.6528

21

Step 5: P(5) ¼ þ ¼

(P(4) (6 24512)) 10 16464.46528

21

Step 6: P(6) ¼ þ ¼ ¼ Z/10 6

(P(5) (3 24512)) 10 09000.046528

Z ¼ 9000046528

This example illustrates the fact that in Algorithm 5.2, a digit is extracted at each

n

step, so the complexity of the sum is always limited to digits. In Algorithm 5.1,

86 ARITHMETIC OPERATIONS: MULTIPLICATION

n þ m digits are involved, and no digit is extracted before the end of the process. The

same would occur in a standard addition process, whenever adding from left to right

instead of right to left.

5.1.2.3 Extended Shift and Add Algorithm: XY 1 C 1 D Observe that

n m nþm n m

Z ¼ X:Y ¼ B B B þ

(B 1Þ:(B 1) 1;

Z n þ m

in such a way that the full digit capacity of is not used, namely, digits that

nþm 21). So, for the design of a

would allow one to represent numbers up to (B

device using this capacity, it is convenient to deﬁne the function

Z ¼ X:Y þ C þ D,

C D n-digit m-digit

where and are and numbers, respectively, in such a way that

n m n m nþm

þ þ ¼ B

Z 1):(B 1) (B 1) (B 1) 1:

## (B

The modiﬁed Hörner Algorithm 5.2 is expressed as follows.

Algorithm 5.3 Extended Shift and Add

P(0):=D;

i 0..n21

for in loop

* Y+C(i))/B;

P(i+1):=(P(i)+X(i)

end loop;

Z:=P(n)*(B**n); P(i) m þ i

In the preceding algorithm, is an number whose integer

Property 5.1 m i

and fractional parts contain and digits, respectively

5.1.2.4 Cellular Shift and Add

Cellular Ripple-Carry Algorithm The recurrence operation of Algorithm 5.3 can

be written in the form m1

P(i) þ x :(y þ y :B þ þ y :B þ c

)

i m1 i

0 1

where, according to Property 5.1,

m1 m2 1

P(i) ¼ p :B þ p :B þ þ p :B þ p :B

0

m1þi m2þi i i1

2 i

þ p :B þ þ p :B

i2 0

so that m1

P(i þ ¼ p þ x :y þ þ p þ x :y þ c

0

1) (( ):B ( ):B

m1þi i m1 i i i

0

1 i

þ p :B þ þ p :B )=B: (5:7)

i1 0

p(i, j) jth P(i).

In what follows, stands for the digit of Expression (5.7) can be

computed by an algorithm for adding natural numbers (basic addition Algorithm

87

5.1 NATURAL NUMBERS MULTIPLICATION

p(i, m þ i 2 . . . p(i, m þ i P(i),

4.1). Assuming that 1), , 0) are the digits of then the

m þ i þ p(i þ m þ i), . . . p(i þ P(i þ

1 digits 1, , 1, 0) of 1) are computed as follows

/

(symbol stands for integer division):

j 0..m-1 p(0, j):=D(j);

for in loop end loop;

carry(i, 0):=C(i);

j 0..i-1 p(i+1, j):=p(i, j);

for in loop end loop;

j 0..m-1

for in loop * Y(j)+carry(i, j)) mod B;

p(i+1, i+j):=(p(i, i+j)+X(i) * Y(j)+carry(i, j))/B;

carry(i, j+1):=(p(i, i+j)+X(i)

end loop;

p(i+1, m+i):=carry(i, m);

p(i, i þ j) B 2 x 2 carry(i, j) B 2

2

Observe that 1 and .y (B 1) , so that if 1

i j

carry(i, j) þ p(i, i þ j) þ x y B 2

2

then . is a two-digit number ( 1), namely,

i j

j þ 1), sum(i, j)] carry(i, j þ 1).B þ sum(i, j).

[carry(i, or Thus an essential differ-

B .

ence with the basic addition algorithm 4.1 (in base 2) comes from the range

B 2

of the carry [0, 1] instead of [0,1]. n ¼

The precedence graph (Chapter 10) of the multiplication algorithm for 3,

m ¼ 4, is shown in Figure 5.2.

Algorithm 5.4 Cellular Ripple-Carry

/

(Symbol stands for integer division)

j 0..m-1 p(0, j):=D(j);

for in loop end loop,

i 0..n-1

for in loop D(3) D(2) D(1) D(0)

p(0,3) p(0,2) p(0,1) p(0,0)

c(0,4) c(0,3) c(0,2) c(0,1) c(0,0)

x(0), y(3) x(0), y(2) x(0), y(1) x(0), y(0) C(0)

p(1,4) p(1,3) p(1,2) p(1,1) p(1,0)

c(1,1)

c (1,2) c(1,0)

c(1,4) c(1,3)

x(1), y(3) x(1), y(2) x(1), y(1) x(1), y(0) C(1)

p(2,5) p(2,4) p(2,3) p(2,2) p(2,1) p(2,0)

c(2,4) c(2,3) c(2,2) c(2,1) c(2,0)

x(2), y(3) x(2), y(2) x(2), y(1) x(2), y(0) C(2)

p(3,6) p(3,5) p(3,4) p(3,3) p(3,2) p(3,1) p(3,0)

z(6) z(5) z(4) z(3) z(2) z(1) z(0)

¼ m ¼

Ripple-carry multiplication algorithm– precedence graph (n 3; 4).

Figure 5.2

88 ARITHMETIC OPERATIONS: MULTIPLICATION

carry(i, 0):=C(i);

j 0..i-1 p(i+1, j):=p(i, j);

for in loop end loop;

j 0..m-1

for in loop

p(i+1, i+j):=(p(i, i+j)+X(i)*Y(j)+carry(i, j)) mod B;

carry(i, j+1):=(p(i, i+j)+X(i)*Y(j)+carry(i, j))/B;

end loop;

p(i+1, m+i):=carry(i, m);

end loop;

j 0..m+n-1 Z(j):=p(n, j);

for in loop end loop;

Comment 5.1 j)-cell

The main loop, named (i, loop, computes the functions

carry(i, j þ 1) sum(i, j).

and The other loops are used for indexing purposes, such

p(0, j) D( j) carry(i, 0) C(i). cell

as assigning to or to The name takes its origin

from the full-combinational cellular array implementation (Chapter 12, Section

(i, j)-cell

12.2.3) of this algorithm. Actually, the can be implemented by any mix

of hardware and ﬁrmware, where the choice can be made by the designer according

to the resources at hand. As it will be shown in Chapter 12, the indexing is not trivial

because the time performances can be directly and signiﬁcantly affected. In a full-

p(i, j)

hardware implementation (Chapter 12), indexing corresponds to connection

assignments between cells, as it already appears in Figure 5.2, where clear relation-

j)-cells.

ships come out between input and output indexes of (i,

Cellular Carry –Save Algorithm The following carry–save algorithm differs from

the ripple-carry algorithm 5.4 by the indexing loops and a ﬁnal adding stage loop.

The basic concepts of carry-save adders (CSAs, Chapter 4) are applied in the way

carries are saved from one loop to the other one, allowing more parallelism in the

cell computation. The precedence graph of the carry-save multiplication algorithm

n ¼ m ¼

for 3, 4, is shown in Figure 5.3.

Algorithm 5.5 Carry-Save

/

(Symbol stands for integer division)

j 0..m-1 p(0, j):=D(j); carry(0, j):=C(j);

for in loop end

loop;

i 0..n-2 p(i+1, m+i):=0;

for in loop end loop;

i 0..n-1

for in loop

j 0..i-1 p(i+1, j):=p(i, j);

for in loop end loop;

j 0..m-1

for in loop

p(i+1, i+j):=(p(i, i+j)+X(i)*Y(j)+carry(i, j)) mod B;

carry(i+1, j):=(p(i, i+j)+X(i)*Y(j)+carry(i, j))/B;

end loop;

end loop;

j 0..n-1 Z(j):=p(n, j);

for in loop end loop;

k(0):=0; p(n, n+m-1):=0;

j 0..m-1

for in loop

Z(j+n):=(p(n, n+j)+c(n, j)+k(j)) mod B;

k(j+1):=(p(n, n+j)+c(n, j)+k(j))/B;

end loop;

C(0) c(0,0)

p(0,0) p(1,0) p(2,0) p(3,0)

y(0)

D(0) z(0)

x(0), 4).

C(1) ¼

c(1,0)

c(0,1) m

p(0,1) p(1,1) p(2,1) p(3,1) 3;

y(1) y(0) ¼

D(1) z(1) (n

x(0), x(1),

C(2) graph

c(2,0)

c(0,2) c(1,1) precedence

p(0,2) p(1,2) p(2,2) p(3,2)

y(2) y(1) y(0)

D(2) z(2)

x(0), x(1), x(2), c(3,0) 0

0 c(2,1)

c(1,2) k(0)

c(0,3) algorithm–

p(0,3) p(1,3) p(2,3) p(3,3)

y(3) y(2) y(1)

D(3) H.A. z(3)

x(0), x(1), x(2), multiplication

c(3,1)

c(2,2)

c(1,3) k(1)

p(2,4) p(3,4)

p(1,4) y(3) y(2) z(4)

## F.A.

0 x(1), x(2), Carry-save

c(3,2)

c(2,3) k(2)

p(3,5)

p(2,5) y(3) F.A. z(5) 5.3

0 x(2), c(3,3) Figure

k(3)

p(3,6) z(6)

## H.A.

0 k(4) 89

90 ARITHMETIC OPERATIONS: MULTIPLICATION

Comment 5.2 (i, j)-cell

The loop computation scheme of the carry-save algorithm

is much the same as that of the ripple-carry one. But a ﬁnal adding loop has to be

executed. Nevertheless, thanks to the reindexing, signiﬁcant time is saved by

taking a better proﬁt of parallelism, either in the software execution or in hardware

m

implementation. In particular, a data-ﬂow machine with processing units could

n

implement the program within steps whose elementary time delay would be that

j)-cell.

of one (i, Despite their aspect, the precedence graphs of Figures 5.2 and 5.3

are not circuits, because no assumption is made about the way cells are implemented.

Nevertheless, assuming a full combinational circuit implementation for such cells

would convert those ﬁgures to explicit circuit schemes (Chapter 12).

B ¼ j)-cell

Whenever 2, the practical implementation of the (i, is quite simple,

B-ary

x .y are expressed by a single bit while two

because the partial products i j

digits are generally needed for higher base values.

Let us point out that the precedence graph of Figure 5.3 presents several 0 inputs.

Therefore the related border cells may be accordingly simpliﬁed.

5.1.3 Long-Operand Algorithm

long-operand multiplications,

In the case of it may be necessary to break down

n-digit m-digit) s-digit

the (or operands into slices. Assume that a procedure

s-digit

has been deﬁned; it computes the product of two numbers

multiplier

/

(symbol stands for integer division):

multiplier (s: natural; carry, w, x, y:

procedure in in

2

digit_vector (0..s 1); next_carry, z: digit_vector

out

(0..s-1)) is

begin

z:=(carry+w+x*y)mod(B**s);

next_carry:=(carry+w+x*y)/(B**s);

multiplier;

end s

s-digit B 2

The procedure generates two numbers ( 1). The next

multiplier Z ¼ X.Y, X n-digit Y

algorithm computes the product where is an number and is an

m-digit one.

Algorithm 5.6 Long-Operand

k 0..s-1 zero(k):=0;

for in loop end loop;

j 0..m/s-1 p(0, j):=zero;

for in loop end loop;

i 0..n/s-1

for in loop

carry(i,0):=zero;

j 0..m/s-1

for in loop

multiplier(s, carry(i, j), p(i, i+j), X(i*s..i*s+s-1),

Y(i*s..i*s+s-1), carry(i, j+1), sum(i, j));

end loop; 91

5.2 INTEGERS

p(i+1, m/s+i):=carry(i, m);

j 0..i-1 p(i+1, j):=p(i, j);

for in loop end loop;

j 0..m-1 p(i+1, j+i):=sum(i, j);

for in loop end loop;

end loop;

j 0..m/s+n/s-1 Z(j*s..j*s+s - 1):=p(n,j);

for in loop

end loop;

5.2 INTEGERS

Conceptually, the simplest method for multiplying signed integers consists in com-

## X Y,

puting, from the representations of the operands and the corresponding absolute

value of the product together with the appropriate sign. The absolute value of

the product would be obtained by one of the methods described before, while the

product sign would be readily given through the exclusive OR function applied to

a suitable binary representation of the respective signs. The process would then

be completed by the computation of the signed representation of the result. This

method is appropriate whenever the operands are provided in sign-magnitude

B’s

form. In this case, signed multiplication appears trivial. In the case of com-

plement or signed-digit representations, other methods have to be recommended.

B’s

5.2.1 Complement Multiplication

X Y B’s

Let and be two integers represented in the reduced complement numeration

system (Chapter 3, Comment 3.2):

n1 n2

R(X) ¼ x :B þ x :B þ þ x :B 0 ,

n1 n2 0

m1 m2

R(Y) ¼ y :B þ y :B þ þ y :B 0 ,

m1 m2 0 (5:8)

y [ B x y [ . . . B

x , {0, 1} and , {0, 1, , 1},

n1 m1 i j

8i = n 8j = m

1, 1;

x y B ¼

and are called sign-digits (sign-bits for 2). The respective weights of

n21 m21

## X Y

and can be positive or negative according to the following deﬁnitions.

## X Y

and can be expressed in the form

0 n1 n2

:B þ x :B þ þ x :B

X ¼ x 0 ,

n2

n1 0 (5:9)

0 m1 m2

Y ¼ y :B þ y :B þ þ y :B 0 ,

m2

m1 0

0 0 0

x ¼ x ¼ x ¼ 21 x ¼ B 2 y ¼ y ¼

where 0 if 0 and if 1; 0 if 0

n21 n21 n21

n21 n21 n21

0 ¼ 21 y ¼ B 2

y if 1.

and n21 n21

It can easily be shown that this sign-digit convention is well suited for the sign

x

extension operation. Actually, under the above sign-digit interpretations, n21

y

(resp. ) can be indeﬁnitely reproduced on the left side of the digit string without

m21

changing the numerical value of the expressed number. This takes for granted that

92 ARITHMETIC OPERATIONS: MULTIPLICATION

the leftmost digit of the new string is viewed as the new sign-digit while the other

digits remain positively weighted.

## X Y,

So and respectively belong to the ranges

n1 n1 m1 m1

X B B Y B

B 1 and 1,

Z ¼ X.Y

and the product belongs to the range

nþm2 nþm2

, Z B

## B

and can be represented in the form

0 nþm1 nþm2

:B þ z :B þ þ z :B :

R(Z) ¼ z 0

nþm2

nþm1 0

nþm

5.2.1.1 Mod B B’s Complement Multiplication A straightforward multipli-

cation algorithm can be deduced from the fact that the representation of a number

n n

x B’s R(x) ¼ x B ,

in the complement system can be mapped to a number mod

## X Y

being the number of digits of the representation. and are ﬁrst represented

n þ m

with digits. Then nþm nþm

X ; R(X) B Y ; R(Y) B

mod and mod ,

so that nþm

Z ¼ X:Y ; R(X):R(Y) B

mod

and nþm

R(Z) ¼ R(X):R(Y) B

mod

Assume that a procedure has been deﬁned, which

truncated_multiplication

nþm

c ¼ a.b B a b þ m)-digit

computes mod , and being two (n base-B natural num-

bers: truncated_multiplication

procedure

(a, b: digit_vector(0..n+m-1); c: digit_vector(0..

in out

n+m-1); nþm

## B

Any natural-number multiplication algorithm may be used, as the mod

reduction is just a matter of truncating the results.

n1m

B B’s

Algorithm 5.7 Mod Complement Multiplication

i n..m+n-1 x(i):=x(n-1);

for in loop end loop;

i m..m+n-1 y(i):=y(m-1);

for in loop end loop;

truncated_multiplication(x, y, z); n þ m

The ﬁrst two loops consist of sign extension up to digits. 93

5.2 INTEGERS

Example 5.2

B ¼ n ¼ m ¼ X ¼ 253, Y ¼

10 3, 65,

R(X) ¼ 2 ¼ R(Y) ¼

1000 53 947, 065,

R(X) ¼ R(Y) ¼

6-digit representations: 999947, 000065,

R(X). R(Y) ¼ ¼

999947 65 64996555

R(Z) ¼ ¼

6

64996555 mod 10 996555

Z ¼ 210 þ ¼ 23445

5 96555

5.2.1.2 Signed Shift and Add Another method is based on a modiﬁcation of the

Y P(n 2

Hörner algorithm 5.2. It consists in subtracting from the last partial result 1)

X(n 2 ¼ B21

whenever the digit 1) (X negative).

Algorithm 5.8 Signed Shift and Add

P(0):=0;

i 0..n-2

for in loop

P(i+1):=(P(i)+X(i)*Y)/B;

end loop;

X(n-1)=0 P(n):=P(n-1)/B; P(n):=(P(n-1)-Y)/B;

if then else

end if;

Z:=P(n)*(B**n);

Let

Example 5.3

X ¼ 53 Y ¼ B ¼ n ¼ m ¼

and 65; 10, 3,

R(X) ¼ ¼ R(Y) ¼ R(65) ¼

1000 53 947, 065, 9935

21

Step 1: P(1) ¼ ¼

(7 65) 10 0045.5

21

Step 2: P(2) ¼ þ ¼

(P(1) (4 65)) 10 0030.55

21

Step 3: P(3) ¼ þ ¼

(P(2) (9935)) 10 996.555

¼

3

996:555 10 996555

þ ¼ 3445

Z ¼ 10 5 96555

5.2.1.3 Postcorrection B’s Complement Multiplication A third method, intro-

duced by Baugh and Wooley ([BAU73]) for array multipliers in base 2, is deduced

from relations (5.9). The negative (sign-digit) and positive parts of the multiplicator

multiply the negative and positive parts of the multiplicand. Actually, the result of

multiplication of the positive parts is then corrected by three supplementary terms

depending on the respective sign-digits. The computation scheme is described in

what follows. Z ¼ X.Y

The product can be expressed in the form

0 n1 0 m1

Z ¼ :B þ X :B þ Y

(x ):(y )

n1 m1

0 0

94 ARITHMETIC OPERATIONS: MULTIPLICATION

0

x y X Y 2

where and are the sign-digits while and , are, respectively, (n 1)-

n21 m21 0 0

2

and (m 1)-digit natural numbers:

n2 m2

¼ x :B þ þ x :B Y ¼ y :B þ þ y :B :

X 0 0

and

n2 m2

0 0 0 0

Thus 0 0 nþm2 0 n1 0 m1

Z ¼ x :y :B þ x :Y :B þ y :X :B þ X :Y : (5:10)

n1 m1 n1 m1

0 0 0 0

X .Y is a straight multiplication of natural numbers. The sign-digit

The product 0 0

product is different from zero only when both operands are negative, and consists

þ m 2

of a (positive) shifted 1, (n 2) positions to the left. The other two terms

2 X

2 Y and (m 1)-shifted , to be subtracted only

are, respectively, (n 1)-shifted 0 0

0 0

= y =

x 0 or 0, respectively. Suitable sign extensions have to be

when n21 m21

performed on each of the four terms.

Assume that the following procedures have been previously deﬁned: a

c ¼ a.b, a 2

procedure that computes being an (n 1)-digit

multiplication b 2

base-B natural number and an (m 1)-digit one:

multiplication (a: digit_vector(0..n-2); b:

procedure in in

digit_vector(0..m-2); c: digit_vector(0..n+m-3);

out nþm

c ¼ a þ b B a, b, c

an procedure that computes mod , and being

addition

þ m)-digit B’s

(n complement numbers:

addition (a, b: digit_vector(0..n+m-1); c:

procedure in out

digit_vector(0..n+m-1); nþm

c ¼ a 2 b B a, b, c

a procedure that computes mod , and being

subtraction

þ m)-digit B’s

(n complement numbers:

subtraction (a, b: digit_vector(0..n+m-1); c:

procedure in out

digit_vector(0..n+m-1). þ m)-digit

In the following algorithm, stands for the (n

shifted_one

nþm22

B :

representation of B’s

Algorithm 5.9 Postcorrection Complement Multiplication

multiplication (X(0..n-2), Y(0..m-2), a(0..n+m-3));

a(m+n-1):=0; a(m+n-2):=0;

x(n-1)=B-1

if then

i 0..n-2 b(i):=0;

for in loop end loop;

i 0..m-2 b(i+n-1):=y(i);

for in loop end loop; 95

5.2 INTEGERS

b(n+m-1):=0; b(n+m-2):=0;

subtraction (a, b, c);

c:=a;

else end if;

y(m-1)=B-1

if then

i 0..m-2 d(i):=0;

for in loop end loop;

i 0..n-2 d(i+m-1):=x(i);

for in loop end loop;

d(n+m-1):=0; d(n+m-2):=0;

subtraction (c, d, e);

e:=c;

else end if;

x(n-1)=B-1 and y(n-1)=B-1 addition (e, shifted_one,

if then

z); z:=e;

else end if;

The above program is structured in four parts: a multiplication, two conditional

shift-and-subtract procedures, and a conditional shifted-one addition. According

to the resources at hand, it may be suitable to implement the shift-and-subtract pro-

procedure followed by a shift, and then an

cedure by a sign-change addition

procedure. In this case as well, the sign-digit of each operand is conditioning the pro-

n-digit X

procedure on an number is described

cedure execution. The sign-change

as follows:

i 0..n-1 x(i):=B-1-x(i);

for in loop

end loop;

X:=X+1; Let

Example 5.4

X ¼ 53 Y ¼ 65; B ¼ n ¼ m ¼

and 10, 3,

R(X) ¼ ¼ R(Y) ¼ ¼

1000 53 947, 1000 65 935

multiplication X ¼ Y ¼ ¼

procedure 47, 35, 47 35 1645

0 0

a ¼ 001645(6-digit representation)

b ¼ 003500

c ¼ þ þ ¼

6

(001645 996499 1) mod 10 998145

d ¼ 004700

e ¼ þ þ ¼

6

(998145 995299 1) mod 10 993445

z ¼ þ ¼

6

(993445 010000) mod 10 003445

Comment 5.3 Z þ n 2

The only case where cannot be expressed as an (m 1)-

n21 m21 nþm22

X ¼ 2B Y ¼ 2 B Z ¼ B

digit number is when and , so that and its

. . .

representation is 010 0. The previous algorithms could be modiﬁed accord-

n21

R(X) ¼ R(2B ¼ 2 . . . R(Y) ¼

ingly: ﬁrst detect whether ) (B 1)0 0 0 and

m21 ¼ 2 . . . R(Z) ¼ . . .

R(2B ) (B 1) 0 0 0; if so, 010 0. In the contrary case the

n þ m21

computation can be performed with digits. Then the obtained result

Z(n þ m 2 n þ m Z(n þ m 2 ¼ Z(n þ m 2

2..0) is extended to digits by deﬁning 1) 2).

96 ARITHMETIC OPERATIONS: MULTIPLICATION

5.2.2 Postcorrection 2’s Complement Multiplication 2

In base 2, the postcorrection 2’s complement multiplication algorithm is simpliﬁed

x

thanks to the carry-free feature of the partial products .y computation (BAU1973).

i j

Since the base is 2, equation (5.10) can be written

nþm2 n1 m1

Z ¼ X :Y þ x :y :B x :Y :B y :X :B : (5:11)

n1 m1 n1 m1

0 0 0 0

As well as in the general case, the ﬁrst two terms may be computed through a straight

þ n 2

natural number multiplication procedure and a correction by a (m 2)-shifted

.y x

). This shifted-one correction occurs whenever and

positive bit (x n21 m21 n21

n21 m21

2x .Y .B 2y .X .B

y are both 1. The two terms and can be com-

m21 n21 n21

0 0

puted through (sign-change procedure) a bitwise complementation of the shifted

y n 2 m 2

x .(0Y ) and .(0X ), then adding 1 at levels 1 and 1,

products n21 m21

0 0 2

2 x .(0Y ) and (m 1)

respectively. Observe that the sign-bit of (n 1)-shifted n21 0

n þ m 2

y .(0X ), share the same position: 2. So a single negative-

shifted m21 0 n þ m21

weight 1 at level will replace the two negative-weight 1’s at level

n þ m 2 n ¼ m, n21

2. Whenever adding 1 twice at level can be done through

y

n. x and/or are

adding 1 once at level Let’s point out that, whenever n21 n21

.Y y .X

x and/or doesn’t affect the result. Figures

zero, changing sign of n21 n21

0 0

5.4 and 5.5 illustrate the computation scheme. Although this scheme suggests a

¼ m ¼

A 2’s complement multiplication scheme (n 6, 4)

Figure 5.4 ¼ m ¼

A 2’s complement multiplication scheme (n 4).

Figure 5.5

^ þ x not

2 , dot, or no-symbol stand for Boolean AND, while stands for real sum. or stand for the Boolean

complementation (not function). 97

5.2 INTEGERS

combinational circuit (Chapter 12), any sequential or programmed implementation

n þ m 2

can proﬁt from the described features. The sign-bit 1, set at level 1, will

vanish whenever a positive carry will occur at this level. An equivalent option, with-

z

out assigning the initial sign-bit 1, would be to complement the ﬁnal bit .

nþm21

5.2.3 Booth Multiplication for Binary Numbers

Booth algorithm modiﬁed Booth algorithm

The and the ([BOO1951], [DAV1977]),

have been and still are very popular ([KOR1993], [OBE1964], [PAR1999]) in a

number of implementations of signed multiplication for 2’s complement binary

r)

numbers. In most applications, the size of the slices (parameter does not

exceed 2, in order to limit the complexity of the coded-digit products. Actually,

r ¼

for 2, one has to deal with shift, add, and subtract operations only. For greater

r,

values of the complexity of partial products is a signiﬁcant part of the speed/cost

n/r

compromises. Observe that, thanks to the sign extension availability, may be

B’s

considered integer without loss of generality. For complement representations,

the method doesn’t seem attractive, not only because of the partial products com-

plexity but because of the coding itself. Nevertheless, from the theoretical point

of view, the extension is straightforward, as will be shown in Section 5.2.4.

5.2.3.1 Booth-r Algorithms Booth-r representation X

Assume that the of is used

(Chapter 3, Deﬁnition 3.6):

0 k1 0 k2 0

X ¼ x :B þ x :B þ þ x :B 0 ,

k1 k2 0

with r

n ¼ r:k, B ¼ 2

and (Booth-r coding)

0 r1 r2 r3

x ¼ x :2 þ x :2 þ x :2

i:rþr1 i:rþr2 i:rþr3

i þ þ x :2 þ x :2 þ x þ x

2 (5:12)

i:rþ2 i:rþ1 i:r i:r1

Then, Hörner Algorithm 5.2 (shift and add 2) can readily be used for computing

Z ¼ X.Y.

Algorithm 5.10 Booth-r Multiplication

P(0):=0;

i 0..k-1

for in loop

P(i+1):=(P(i)+X’(i)*Y)/(2**r);

end loop;

Z:=P(n)*(2**n);

98 ARITHMETIC OPERATIONS: MULTIPLICATION

0

x

The product .Y is equal to

i

0 r1 r2

:Y ¼ x :Y:2 þ x :Y:2 þ þ x :Y:2

x 2

i:rþr1 i:rþr2 i:rþ2

i (5:13)

þ x :Y:2 þ x :Y þ x :Y

i:rþ1 i:r i:r1

so that the computation can be performed as follows.

Algorithm 5.11 Booth-r Multiplication

P(0):=0;

i 0..k-1

for in loop

X((i*r)-1)=1 sum(-1):=Y; sum(-1):=0;

if then else end if;

j 0..r-2

for in loop

X((i*r)+j)=1 sum(j):=sum(j-1)+Y*(2**j);

if then else

sum(j):=sum(j-1); end if;

end loop;

X((i*r)+r-1)=1 sum(r-1):=sum(r-2)-Y*(2**(r-1));

if then else

sum(r-1):=sum(r-2); end if;

P(i+1):=(P(i)+sum(r-1))/(2**r);

end loop;

Z:=P(n)*(2**n);

5.2.3.2 Per Gelosia Signed-Digit Algorithm Per Gelosia

Another method, the

([DAV1977]), consists in encoding both operands according to the Booth-r

coding formula (5.12) and modifying the cellular shift and add Algorithm 5.4. It

r21

a, b, c, d

can be proved that, given integers and belonging to the interval [22 ,

r21 p ¼ a.b þ c þ d

], can be decomposed in a unique way under the form

2 r

p :2 þ p (5:14)

1 0 r21 r21

p p 2 p p

where both and belong to the interval [22 , 2 1]. Digits and can

1 0 1 0

r-tuples

thus be represented by in 2’s complement notation. Booth-r coded digits

0 0

y j),

x (i) or ( according to formula (5.12), can be proved to belong to the interval

r21 r21 0 0

x y j)

, 2 ]. So the products of Booth-r coded digits (i) and ( can be speciﬁed

[22 G H

in the form (5.14). Assume that two functions and have been deﬁned which

p ¼ G(a, b, c, d p ¼ H(a, b, c, d).

compute ) and Assume moreover that a

1 0 X

procedure has been deﬁned to compute the Booth-r digits of

Booth_encode

## Y,

and respectively. An extra procedure is needed to express the

Booth_decode

r-bit

ﬁnal result, given in signed digits, in the classical 2’s complement binary form.

Algorithm 5.12 Per Gelosia Booth-r Signed Digit Multiplication

XX:=Booth_encode(X); YY:=Booth_encode(Y);

j 0..m/r-1 p(0, j):=0;

for in loop end loop;

i 0..n/r-1

for in loop

carry(i, 0):=0;

j 0..m/r-1

for in loop

sum(i, j):=H(XX(i), YY(j), carry(i, j), p(i, i+j));

carry(i, j+1):=G(XX(i), YY(j), carry(i, j), p(i, i+j));

end loop; 99

5.2 INTEGERS

p(i+1, m/r+i):=carry(i, m/r);

j 0..i-1 p(i+1, j):=p(i, j);

for in loop end loop;

j 0..m/r-1 p(i+1, j+i):=sum(i, j);

for in loop end loop;

end loop;

j 0..n/r+m/r-1 ZZ(j):=p(n/r, j);

for in loop end loop;

Z:=Booth_decode(ZZ); j)-cell,

Figure 5.6 displays the (i, to illustrate the indexing suggested in the above

algorithm. The carry-save Algorithm 5.5 could have been used to provide a better

overall algorithmic complexity.

Let

Example 5.5 n ¼ m ¼ r ¼

12; 3;

X ¼

101 011 110 001(decimal 1295)

Y ¼ 011 100 010 011 (decimal 1811)

Booth-3 coding (expressed in decimal for convenience) is performed as follows

XX ¼ þ ¼ YY ¼ þ ¼

(0) 1 0 1 (0) 3 0 3

XX ¼ 2 þ ¼ 2 YY ¼ þ ¼

(1) 0 (1) 2 0 2

XX ¼ þ ¼ YY ¼ 4 þ ¼ 4

(2) 3 1 4 (2) 0

XX ¼ 3 þ ¼ 3 YY ¼ þ ¼

(3) 0 (3) 3 1 4

XX ¼ 3 2 YY ¼ 4

4 1 4 2 3

The operations are illustrated in Figure 5.7 where the result is expressed as

Z ¼ 1, 1, 4, 4, 4, 4,

1, 3

or þ þ Z ¼ 2345245:

1:8 7 6 5 4 3 2

1:8 1:8 4:8 4:8 4:8 4:8 3; in decimal

p(i,i+j) = s(i–1,j+1)

i,j c(i,j)

c(i,j+1) p(i+1,i+j) = s(i,j)

(i, j)-cell.

Booth-r

Figure 5.6

100 ARITHMETIC OPERATIONS: MULTIPLICATION

c 0 0 0 0

d

G(a,b,c,d) a,b 1 –1 0 0

3,–3 3,4 3,–2 3,1

–1 0 3 2

H(a,b,c,d) 1 0 1

2,–3 2,4 2,–2 2,1 0

–1 2 0 0

–2 1 0

–4,–3 –4,4 –4,–2 –4,1 0

1 1 3 0

2 0 1

4,–3 4,4 4,–2 4,1 0

–1 –1 1 –4 –4 –4 –4 3

Booth multiplication, Example 5.5.

Figure 5.7 r21

Let us point out that, although Booth-coded operands are in the range [22 ,

r21 r21 r21

Z(i) þ2 2

þ2 ], signed-digit results are in the range [22 , 1]. So in

¼

the example (r 3), 4 will never appear as a digit result. This means that the

þ

procedure successively generates (r 1)-bit signed digits while

Booth_encode r-bit-digit r-bit

will generate, step-by-step, outputs from

the Booth_decode

inputs and carry-in. and functions are deﬁned as follows:

The Booth_encode Booth_decode

Algorithm 5.13 Booth_encode

X(-1):=0;

i 0..n/r-1

for in loop

a:=X((i*r)-1);

j 0..r-2 a:=(a+X((i*r)+j))/2;

for in loop end loop;

a:=(a-X((i*r)+r-1))/2;

XX(i):=a*(2**r);

end loop;

Algorithm 5.14.a 1

Booth_decode

a:=0;

i 0..n/r-1

for in loop

a:=(a+XX(i))/(2**r);

end loop;

X:=a*(2**n); 101

5.2 INTEGERS Let

Example 5.6

n ¼ r ¼ YY ¼ 4 4 ¼

15; 3; 1 2 3 001, 100, 100, 010, 011

The result is built up in 2’s complement representation,

Step 1: þ ¼ ¼

3 3

(000 011)/2 000011/2 000.011

¼ ¼

Step 2: þ 3 3

010.011/2 000.010011

(000.011 010)/2

Step 3: 2 ¼

3 3

(000.010011 100)/2 100.010011/2 -the ﬁrst bit (sign-bit) is negative

¼

3 111.100010011

so the shift involves sign extension: 100.010011/2 ¼

Step 4: 2 3 3

= 1011.100010011/2 111.011100010011

(111.100010011 100)/2

Step 5: þ001)/2 ¼

3

(111.011100010011 0.000011100010011

¼

Step 6: YY ¼ 15

2 011100010011 (decimal 1811).

0.000011100010011 Z(i)

Another way to decode the signed-digit expression of rests on a procedure that

roughly corresponds to the reverse application of the coding process described in

Algorithm 5.13 ( ). Assuming

Booth_encode

r1 r2 r3

z ¼ z :2 þ z :2 þ z :2 þ þ z :2 2

i i:rþr1 i:rþr2 i:rþr3 i:rþ2

þ z :2 þ z

i:rþ1 i:r (5:15)

þ r1 r2 r3

z ¼ z :2 þ z :2 þ z :2 þ þ z :2 2

i:rþr1 i:rþr2 i:rþr3 i:rþ2

i þ z :2 þ z i ¼ . . . k

, 0,1, , 1

i:rþ1 i:r

and assuming moreover

z ¼ c in ¼ c in ¼ z

0; (sign-bit),

k k:r

0:1 0 00

## Z

The 2’s complement expression of the result will be given by the following

algorithm.

Algorithm 5.14.b 2

Booth_decode

z z)

( stands for the Boolean complement of

i 0,1,..k-2

for in loop

c _in=0

if i

then

00 +i

z =z ; c _in=z

i+1 i.r+r-1

i

else

00 _

+i r

z =(z -c _in) mod 2 ; c _in=z (z̄ .z̄ .

i i i+1 i.r+r-1 i.r+r-2 i.r+r-3

...z̄ ); end if;

i.r

end loop;

_in=0

c

if k-1

then

00 +k-1

z =z ; z =z

k.r k.r-1

k-1

else

00 _

+k-1 r

z =(z -c _in) mod 2 ; z =z (z̄ .z̄ .

k-1 k.r kr-1 k.r-2 k.r-3

k-1 );

...z̄ end if;

(k-1).r

102 ARITHMETIC OPERATIONS: MULTIPLICATION

The above algorithm actually sets all signed digits to a positive value carrying a

negative correction bit to the following digit (on the left) whenever necessary.

z

The last correction bit is the sign bit.

k.r

Let

Example 5.7

n ¼ r ¼ k ¼ Z ¼ 4 4 ¼

15; 3; 5; 1 2 3 001, 100, 100, 010, 011

The result is built up in 2’s complement representation,

00

¼ ¼) z ¼ c _in ¼ z ¼

Step 1: c _in 0 011; 0.

0

0 1 2

00

Step 2: c ¼ ¼) z ¼ c _in ¼ z ¼

_in 0 010; 0.

1

1 2 5

00

Step 3: c ¼ ¼) z ¼ c ¼ z ¼

_in 0 100; _in 1.

2

2 3 8

00

Step 4: c ¼ ¼) z ¼ ¼ c ¼ z _

_in 1 (100 – 001) mod 8 011; _in (z̄ .z̄ )

4

3

3 11 10 9

¼ 1. 00

Step 5: c _in ¼ ¼) z ¼ ¼ z ¼ z _

1 (001 – 001) mod 8 000; (z̄ .z̄ )

4

4 15 14 13 12

¼ 0;

00 00 00 00 00 00

Z ¼ z z z z z ¼ 0 000011100010011

4 3 2 1 0

5.2.4 Booth Multiplication for Base-B Numbers

## B)

(Booth-r Algorithm in Base B .

The above algorithms may be extended to base 2, within the following

conditions. [ . . . B þ x

B x {0,1, , 1}is valued

is assumed even, and the sign-digit n1 n1

x , B=2 x B,

whenever and otherwise. The sign function sign(x) is

n1 n1

deﬁned as ¼ b2: x=Bc:

sign(x) (5:16)

This means that ¼ x B=2

sign(x) 1 if (X negative)

and ¼ x , B=2

sign(x) 0 if (X positive):

x . . . x x

X ¼ x , , , , is thus given by the expression

A base-B signed number n21 n22 1 0

n1 n2

X ¼ B:sign(x þ x :B þ þ x :B þ x :B þ x

2

(x )):B (5:17)

n1 n1 n2 2 1 0

n-digit n/r r-digit

Then, given an base-B integer shredded into slices, the general-

ized Booth coding can be performed according to

0 r1 r2

¼ B:sign(x þ x :B þ þ x :B

x 2

(x )):B

i:rþr1 i:rþr1 i:rþr2 i:rþ2

i (5:18)

þ x :B þ x þ sign(x )

i:rþ1 i:r i:r1 103

5.2 INTEGERS B’s r-digit

This means that each slice is viewed as a complement number. So when-

B/2, þ

x a correction sign(x ) has to be made at the next left

ever i.rþr21 i.rþr21

x

digit .

i.rþr Let

Example 5.8 n ¼ r ¼ B ¼

24; 4; 10

X ¼ 0000 9124 6458 2123 5252 5632 2145

Booth-4 coding 0 0 0 0 0 0 0

X ¼ ¼ x x x x x x x

0001 9125 6458 2124 5253 5632 2145 , , , , , ,

6 5 4 3 2 1 0

1 has to be added at the end of every slice whose next digit (on the right) is greater

than 4.

The decimal values of Booth digits are given by

0

x ¼ 1

6

0

x ¼ 1000 þ ¼ 875

125

5

0 ¼ 4000 þ ¼ 3542

x 458

4

0

x ¼ 2124

3

0 ¼ 5000 þ ¼ 4747

x 253

2

0

x ¼ 5000 þ ¼ 4368

632

1

0 ¼

x 2145

0

C ¼ 4 ,

Assuming 10

þ þ

X ¼ 6 5 4 3 2

875:C 3542:C 2124:C 4747:C 4368:C 2145

1:C

¼ 9124 6458 2123 5252 5632 2145

## B

Algorithm 5.15 Booth-r in Base

The Booth-r multiplication, Algorithm 5.11, can be modiﬁed as follows.

P(0):=0;

i 0..k-1

for in loop

X((i*r)-1) B/2 sum(-1):=Y; sum(-1):=0;

if then else end if;

j 0..r-2

for in loop

sum(j):=sum(j-1)+X((i*r)+j)*Y*(B**j);

end loop;

X((i*r)+r-1) B/2 sum(r-1):=sum(r-2)+(X((i*r)+r-1)

if then

-B)*Y*(B**(r-1));

sum(r-1):=sum(r-2)+X((i*r)+r-1)*Y*(B**(r-1));

else P(i+1):=(P(i)+sum(r-1))/(B**r);

end if;

end loop;

Z:=P(n)*(B**n);

104 ARITHMETIC OPERATIONS: MULTIPLICATION

The complexity of the partial products computation is an important drawback for the

above algorithm. Moreover, increasing the base or increasing the side of the slices in

Booth coding are approaches conceptually similar. Both techniques reduce the

number of computational steps, but the rise of the step complexity is the price to

be paid. The Booth algorithm in base-B could be more suitable for some Per Gelosia

inspired applications with suitable hardware resources such as look-up table (LUT)

## B

to implement the partial products within acceptable times. Obviously, increasing

r

and/or would quickly make the LUT size unmanageable.

5.3 SQUARING

5.3.1 Base-B Squaring

Although any multiplication system could readily be used for squaring, specialized

systems can provide both time and cost savings. Actually, normal multiplication

n 2

requires partial products while the maximum depth of the adding tree reaches

n n(n 2

(maximum column depth in Figure 5.1a). Squaring only needs 1)/2 partial

n

products, plus digit-squares. Moreover, the adding process is simpliﬁed by the fact

that each nonsquare partial product digit appears twice. As far as partial products are

multiplied by two beforehand, the maximum depth of the adding tree is reduced to

dn=2e. B .

The complexity of squaring in base 2 comes from the fact that digit

double products could need up to three digits while squaring only needs two. So

the saving in column depth is consumed by second-order carries. For the particular

B ¼

case 2, the digit squaring is trivial as double products are performed through

straight shifts. Moreover, Boolean simpliﬁcation provides further reductions in

the depth of the adding tree.

5.3.1.1 Cellular Carry – Save Squaring Algorithm Algorithm 5.16 is a modiﬁ-

þ C þ D;

X 2 the main

cation of the cellular carry-save Algorithm 5.5. It computes

(i, j)-cell i ¼ j,

difference rests upon the loops, adding squares whenever and

adding double products otherwise, to the successive carries and partial sums. What

is saved in computing the double products is somewhat consumed by the carries, to

be handled by additional adding procedures. Actually, to cope with the basic

scheme suggested by Algorithm 5.5, the only modiﬁcation (besides that of the partial

j)-cell

products) rests on the substitution of some (i, procedures by half-adder ones.

Algorithm 5.16 Carry-Save Squaring

/

(Symbol stands for integer division)

j 0..n-1 p(0, j):=D(j); carry1(0, j):=C(j);

for in loop

carry2(0, j):=0;end loop;

p(1, n):=0;

sum(0, 0):=(carry1(0, 0)+p(0, 0)+X(0)**2) mod B;

carry1(1, 0):=((carry1(0, 0)+p(0, 0)+X(0)**2)/B) mod B;

carry2(1, 1):=(carry1(0, 0)+p(0, 0)+X(0)**2)/B**2;

j 1..n-1

for in loop 105

5.3 SQUARING

sum(0, j):=(carry1(0, j)+carry2(0, j)+p(0, j)+2*X(0)*X(j))

mod B;

carry1(1,j):=((carry1(0,j)+carry2(0,j)+p(0,j)+2*X(0)*

X(j))/B) mod B;

carry2(1,j+1):=(carry1(0,j)+carry2(0,j)+p(0,j)+2*X(0)*X(j))/B**2;

end loop;

j 0..n-1 p(1,j):=sum(0,j);end

for in loop loop;

p(2, n+1):=carry2(1, n);

i 1..n-2

for in loop

j 0..i-1

for in loop

sum(i, j):=(carry1(i,j)+p(i, i+j)) mod B;

carry1(i+1,j):=(carry1(i,j)+p(i, i+j))/B;

end loop;

sum(i,i):=(carry1(i,i)+carry2(i,i)+p(i,i+i)+X(i)**2) mod B;

carry1(i+1,i):=((carry1(i,i)+carry2(i,i)+p(i,i+i)+X(i)**2)

/B) mod B;

carry2(i+1,i+1):=(carry1(i,i)+carry2(i,i)+p(i, i+i)+X(i)**2)

/B**2;

j i+1..n-1

for in loop

sum(i,j):=(carry1(i,j)+carry2(i,j)+p(i,i+j)+2*X(i)*X(j))

mod B;

carry1(i+1, j):=((carry1(i, j)+carry2(i, j)+p(i,i+j)+2*

X(i)*X(j))/B) mod B;

carry2(i+1, j+1):=(carry1(i, j)+carry2(i, j)+p(i, i+j)+2*

X(i)*X(j))/B**2;

end loop;

j 0..i-1 p(i+1, j):=p(i, j);

for in loop end loop;

j 0..n-1 p(i+1, j+i):=sum(i, j);

for in loop end loop;

p(i+2, n+i+1):=carry2(i+1, n);

end loop;

j 0..n-2

for in loop

sum(n-1, j):=(carry1(n-1, j)+p(n-1, n-1+j))mod B;

carry1(n, j):=(carry1(n-1, j)+p(n-1, n-1+j))/B;

end loop;

sum(n-1, n-1):=(carry1(n-1, n-1)+carry2(n-1, n-1)+p(n-1,

2*n-2)+X(n-1)**2) mod B;

carry1(n, n-1):=((carry1(n-1, n-1)+carry2(n-1, n-1)+p(n-1,

2*n-2)+X(n-1)**2)/B) mod B;

carry2(n, n):=(carry1(n-1, n-1)+carry2(n-1, n-1)+p(n-1,

2*n-2)+X(n-1)**2)/B**2;

j 0..n-2 p(n, j):=p(n-1, j);

for in loop end loop;

j 0..n-1 p(n, n-1+j):=sum(n-1, j);

for in loop end loop;

p(n, 2*n):= carry2(n, n);

j 0..n-1 Z(j):=p(n, j);

for in loop end loop;

k(0):=0;

j 0..n-1

for in loop

Z(j+n):=(p(n, n+j)+carry1(n, j)+k(j)) mod B;

k(j+1):=(p(n, n+j)+carry1(n, j)+k(j))/B;

end loop;

106 ARITHMETIC OPERATIONS: MULTIPLICATION

It is straightforward to ﬁgure out the relative computational complexity of a special-

ized squaring procedure, with respect to classic multiplication. Let us point out that

n(n þ

the 1)/2 base-B partial products (either double products or squares) will gen-

þ

erate less than 3n(n 1)/2 base-B digits to be added according to some selected

2 . Asymp-

algorithm. Using the common multiplication algorithm, this quantity is 2n

totically, a 25% saving can be expected, through the best use of the adding tree

reduction (multioperand addition; Chapter 11). Although some extra beneﬁt could

be taken from the fact that the upper carry digit belongs to {0; 1}, the potential

time/cost saving doesn’t look attractive for general-purpose computers. Some com-

binational implementations are presented in Chapter 12. As a matter of fact, squar-

ing is not statistically frequent in most applications where suitable multiplication

resources are generally available. So the interest of designing special devices for

base-B squaring remains limited. On the contrary, signiﬁcant advantages can be

taken from speciﬁc features of base-2 representation, as shown in the following

section.

5.3.2 Base-2 Squaring

The following three Boolean relations are key properties allowing important simpli-

ﬁcations to the squaring computation scheme.

x ^ x ¼ x x [

1: , {0, 1} (5:19)

0 0 0 0

the square of a binary digit is itself (carry-free).

x þ x x ¼ x ¼ x x x [

x 2x (x , 0) , , {0, 1} (5:20)

2: i j i j i j i j i j

base 2

the operation of adding a partial product to itself may be replaced by a left-

shift.

x þ x ¼ x x x :

x (x , ) (5:21)

3: i j j i j i j base 2

Thanks to these properties, the computation scheme of Figure 5.1 can be reduced as

shown in Figure 5.8. k

This scheme is easily built up by noting that columns of index even are made up

x i , j i þ j ¼ k 2 x x

x such that and 1 and , while columns of

with products k=21 k=2

i j

k x x

index odd follow the same rule but with the additional term (k21)/221 (k21)/2

x x n(n þ

instead of . In total, 1)/2 partial products have to be considered

k/221 k/2 Squaring computation scheme.

Figure 5.8 107

5.4 BIBLIOGRAPHY

n 2

instead of for the classical multiplication scheme. With respect to a full multipli-

cation scheme, the saving is now 50% (asymptotically). The most popular tech-

niques to deal with this problem are related to multioperand addition procedures

(Chapter 11) using various methods for reducing the number of operands

([DAD1965], [OBE1964], [WAL1964]).

Whenever methods for squaring can be fast enough to compete with multi-

plication, an interesting alternative for multiplying is inferred from the following

formula: Y)

¼ ½(X þ Y)

2 2

(X (5:22)

4X:Y

So binary multiplication can be made from two squares, three signed additions, and

one shift. For small numbers, a look-up table inspired technique may be an interest-

ing approach for fast multiplication using squares. We conclude this section by

observing that general integer exponentiation can be performed using squaring

and multiplication as primitive operations. A suitable factorization of the exponent

would minimize the quantity of multiplying/squaring steps.

5.4 BIBLIOGRAPHY

[BAU1973] C. R. Baugh and B. A. Wooley, A two’s complement parallel array multiplication

IEEE Trans. Comput.,

algorithm. 1045– 1047 (1973).

C-22: Q. J. Mech. Appl. Math.

[BOO1951] A. D. Booth, A signed binary multiplication technique.

236 – 240 (1951).

4: Alta Frequenza

[DAD1965] L. Dadda, Some schemes for parallel multipliers. 349 – 356

34:

(1965). Philips Res. Rpts.

[DAV1977] M. Davio and G. Bioul, Fast parallel multiplication. 44– 70

32:

(1977). Computer Arithmetic Algorithms,

[KOR1993] I. Koren, Prentice Hall, Englewood Cliffs, NJ,

1993.

[MAS1990] M. Nagamatsu, et al., A 15-ns 32 32-b CMOS multiplier with an improved

IEEE J. Solid-State Circuits

parallel structure. 494 – 499 (1990).

25(2):

Advanced Computer Arithmetic Design,

[OBE1964] S. F. Oberman and M. Flynn, Wiley-

Interscience, Hoboken, NJ, 2001.

[OKL1996] V. G. Oklobdzija, D. Villeger, and S. S. Liu, A method for speed optimized

partial product reduction and generation of fast parallel multipliers using an algorithmic

IEEE Trans. Comput.

approach. 294– 305 (1996).

45(3):

Computer Arithmetic, Algorithms and Hardware Designs,

[PAR1999] Behrooz Parhami,

Oxford University Press, New York, 1999.

[WAN1995] Z. Wang, G. A. Jullien, and W. C. Miller, A new design technique for column

IEEE Trans. Comput.

compression multipliers. 962 – 970 (1995).

44(8): IEEE Trans. Electron. Comput.

[WAL1964] C. S. Wallace, A suggestion for fast multipliers.

14– 17 (1964).

EC-13(Feb):

6

## ARITHMETIC OPERATIONS:

## DIVISION

Integer or ﬁnite length fractional numbers can be multiplied exactly, whenever

sufﬁcient length is allowed for the result. Division doesn’t share this feature. As a

matter of fact, division generally does not provide a ﬁnite length result. The accu-

racy must be deﬁned beforehand by setting the unit in the least signiﬁcant position

(ulp) of the result. The number of algorithmic cycles will therefore depend on the

desired accuracy, not on the operand length.

Digit recurrence algorithms represent the most common class of division tech-

niques: a single quotient-digit is produced at each computation step. The classic

pencil and paper method belongs to this class. The time complexity is thus a

linear function of the desired number of quotient-digits. SRT division

([SWE1957], [ROB1958], [TOC1958]) has been widely used in computer appli-

digit recurrence

cations. The algorithmic step mainly consists in an estimation of

the greatest multiple of the divisor to be subtracted from the remainder.

Functional iteration is another class of algorithms. These algorithms use func-

tion-solving techniques to converge, from an initial estimation, toward the quotient

with the required precision. The main feature of this method rests on the faster than

linear convergence, typically quadratic. The main drawbacks are the step complex-

ity and the need for additional computations to provide the ﬁnal remainder, thus

functional iteration

increasing the rounding complexity. The most used techniques

Newton –Raphson Taylor –MacLaurin

are based on convergence equations and

algorithm).

expansions (Goldschmidt’s This technique has been used in several

commercial applications.

Synthesis of Arithmetic Circuits: FPGA, ASIC, and Embedded Systems

By Jean-Pierre Deschamps, Géry J. A. Bioul, and Gustavo D. Sutter

#

Copyright 2006 John Wiley & Sons, Inc. 109

110 ARITHMETIC OPERATIONS: DIVISION

Very high radix variable latency

and classes of algorithms are described in the

literature ([OBE1995]). Their practical use is more limited and more justiﬁed for

speciﬁc applications ([OBE1995], [BRI1993]).

A number of division methods, more or less related to the above four main

classes, are described in the literature ([OBE1997], [PAR1999], [FLY2001],

[ERC2004]).

6.1 NATURAL NUMBERS

## X Y Y . Q R,

Let and be two natural numbers with 0. Deﬁne and respectively, as the

quotient remainder X Y, p

and the of the division of by with an accuracy of fractional

base-B digits: p :X ¼ Q:Y þ R;

## B

## Q R R , Y.

where and are natural numbers, and In other words,

p p p p

þ R:B , Y:B

X ¼ ):Y (R:B ), with , (6:1)

(Q:B 2p 2p

## R.B

Q.B and is equal to

so that the unit in the least signiﬁcant position (ulp) of

2p p ¼

B . In the particular case where 0, that is,

X ¼ Q:Y þ R, R , Y,

with (6:2)

Q R quotient remainder integer division X Y.

and are the and the of the of by

## X Y

The basic algorithm applies to operands and such that

X , Y: (6:3)

X , Y,

In the general case, to ensure that a previous alignment step is necessary.

m

X m-digit X , B ; then

Assume that is an base-B number, that is,

0 m 0 m

Y Y ¼ B .Y, Y B X;

substitute by so that .1.

0 0

## Q R X Y

compute the quotient and the remainder of the division of by , with an

p þ m

accuracy of fractional base-B digits, that is,

pþm 0 0 0 0

B :X ¼ Q:Y þ R R , Y

, with

so that p 0 m

B :X ¼ Q:Y þ R, R ¼ R =B , Y:

with

The next theorem constitutes the justiﬁcation of the basic division algorithm. a

Given two natural numbers

Theorem 6.1 Fundamental Equation of Division

b a , b, q r

and such that there exists two natural numbers and satisfying

Ba ¼ q.b þ r, q [ . . . B r , b.

with {0, 1, , 1} and 111

6.1 NATURAL NUMBERS

The iterative application of the preceding theorem, that is,

B:r(0) ¼ q(1):Y þ r(1), r(1) , Y,

B:r(1) ¼ q(2):Y þ r(2), r(2) , Y, (6:4)

...

B:r(p ¼ q(p):Y þ r(p), r(p) , Y,

1)

r(0) ¼ X,

with generates the following relation,

p p1 p2

X:B ¼ þ q(2):B þ þ q(p):B þ r(p),

0

(q(1):B ):Y (6:5)

so that p1 p2

Q ¼ q(1):B þ q(2):B þ þ q(p):B R ¼ r(p):

0 and (6:6)

has been deﬁned

Assume that a procedure division_step

division_step (a, b: natural; q, r:

procedure in out

natural); q r B:a ¼ q:b þ r, q [ . . . B

that computes and such that with {0, 1, , 1} and

r , b. Then the following basic division algorithm is a straightforward application

of (6.4) and (6.5).

Algorithm 6.1 Basic Division

r(0):=X;

i 1..p

for in loop 2

division_step (r(i 1), Y, q(i), r(i));

end loop, q(1)q(2) q( p) Q

It generates the base-B representation of and the remainder

R ¼ r(p). B ¼ procedure is very simple.

If 2 the division_step

Algorithm 6.2 Base-2 Division Step

z:=2*a-b;

z<0 q:=0; r:=2*a; q:=1; r:=z;

if then else end if;

## B

If is greater than 2 the division step is more complex, namely:

Algorithm 6.3 Base-B Division Step

B*a<b q:=0; r:=B*a;

if then

B*a<2*b q:=1; r:=B*a-b;

elsif then

B*a<3*b q:=2; r:=B*a-(2*b);

elsif then

.. B*a<(B-1)*b q:=B-2; r:=B*a-((B-2)*b);

elsif then

q:=B-1; r:=B*a-((B21)*b);

else

112 ARITHMETIC OPERATIONS: DIVISION

r(i)

Observe that at every step of Algorithm 6.1, the new remainder is equal to

B.r(i 2 2 q(i).Y. q(i) ¼ r(i) ¼ B.r(i 2

1) If 0 then 1) and the preceding

r(i 2 restored

remainder 1) is said to be (actually, restored and shifted). For that

restoring division

reason, the basic division algorithm is also known as the

algorithm.

Comment 6.1 Y

If a previous alignment is necessary, instead of substituting by

0 m 0

Y ¼ B .Y, X X ¼ X/B Y

an alternative option is to substitute by and by

0 m21 0

## ¼ B .Y, Q R

Y and to compute the quotient and the remainder of the division

0 0

Y p þ m

X by , with an accuracy of fractional base-B digits, that is,

of pþm 0 pþm1 0 0 0 0

B :X ¼ B :X ¼ Q:Y þ R R , Y

, with ,

so that p 0 m1

:X ¼ Q:Y þ R, R ¼ R =B , Y:

B with

0

X X is equivalent to the substitution of the ﬁrst

Observe that the substitution of by

algorithm step, namely, B:X ¼ q(1):Y þ r(1), r(1) , Y

by X ¼ q(1):Y þ r(1), r(1) , Y:

Examples 6.1

1. Compute 12/15 with an accuracy of 8 fractional bits:

r(0) ¼ 12,

! q(1) ¼ r(1) ¼ ¼

2:12 15 0 1, 24 15 9,

! q(2) ¼ r(2) ¼ ¼

2:9 15 0 1, 18 15 3,

, ! q(3) ¼ r(3) ¼

2:3 15 0 0, 6,

, ! q(4) ¼ r(4) ¼

2:6 15 0 0, 12,

! q(5) ¼ r(5) ¼ ¼

2:12 15 0 1, 24 15 9,

! q(6) ¼ r(6) ¼ ¼

2:9 15 0 1, 18 15 3,

, ! q(7) ¼ r(7) ¼

2:3 15 0 0, 6,

, ! q(8) ¼ r(8) ¼

2:6 15 0 0, 12:

Q ¼ ¼ R ¼ ¼ þ

8

So 11001100 204, 12, and 2 .12 204.15 12.

X ,

2. (Integer division) Given an 8-bit natural number (X 256) and a positive

## Y, X Y Y

integer the integer division of by is computed as follows. The divisor is

0 ¼ Y.256, p þ m ¼ þ ¼

Y the accuracy is equal to 0 8 8 bits, and

substituted by 0 0

R R ¼ R /256.

the ﬁnal remainder will be substituted by As an example, assume

113

6.1 NATURAL NUMBERS

X ¼ Y ¼

that 124 and 15:

0 ¼ ¼

Y 15:256 3840,

r(0) ¼ 124,

, ! q(1) ¼ r(1) ¼

2:124 3840 0 0, 248,

, ! q(2) ¼ r(2) ¼

2:248 3840 0 0, 496,

, ! q(3) ¼ r(3) ¼

2:496 3840 0 0, 992,

, ! q(4) ¼ r(4) ¼

2:992 3840 0 0, 1984,

! q(5) ¼ r(5) ¼ ¼

2:1984 3840 0 1, 3968 3840 128,

, ! q(6) ¼ r(6) ¼

2:128 3840 0 0, 256,

, ! q(7) ¼ r(7) ¼

2:256 3840 0 0, 512,

, ! q(8) ¼ r(8) ¼

2:512 3840 0 0, 1024:

Q ¼ ¼ R ¼ ¼ ¼ þ

Thus 00001000 8, 1024/256 4, and 124 8.15 4.

The same operation can be performed taking into account Comment 6.1:

0

Y ¼

15:128 1920,

r(0) ¼ 124=2,

, ! q(1) ¼ r(1) ¼

124 1920 0 0, 124,

, ! q(2) ¼ r(2) ¼

2:124 1920 0 0, 248,

, ! q(3) ¼ r(3) ¼

2:248 1920 0 0, 496,

, ! q(4) ¼ r(4) ¼

2:496 1920 0 0, 992,

! q(5) ¼ r(5) ¼ ¼

2:992 1920 0 1, 1984 1920 64,

, ! q(6) ¼ r(6) ¼

2:64 1920 0 0, 128,

, ! q(7) ¼ r(7) ¼

2:128 1920 0 0, 256,

, ! q(8) ¼ r(8) ¼

2:256 1920 0 0, 512:

Q ¼ ¼ R ¼ ¼ ¼ þ

Thus 00001000 8, 512/128 4, and 124 8.15 4.

## X Y, X/Y

3. Given a 6-bit natural number and a 3-bit positive integer compute

p ¼ X , Y,

with an accuracy of 4. To ensure that the divisor is substituted by

0

Y ¼ Y.2 p ¼ þ ¼

6

, the division is performed with an accuracy of 4 6 10, and the

X ¼ Y ¼

6 . Assume that 101011 (43) and 111

ﬁnal remainder will be divided by 2

0

Y ¼

(7), so that 111000000:

i ¼

Initial step, 0

r(0) ¼ X ¼ 000101011;

0

i ¼ Y

1; 2:r(0) , ! q(1) ¼ r(1) ¼

0 0; 001010110 (restoring)

0001010110 0111000000

0

i ¼ Y

2; 2:r(1)

114 ARITHMETIC OPERATIONS: DIVISION

, ! q(2) ¼ r(2) ¼

0 0; 010101100 (restoring)

0010101100 0111000000

0

i ¼ Y

3; 2:r(2) , ! q(3) ¼ r(3) ¼

0 0; 101011000 (restoring)

0101011000 0111000000

0

i ¼ Y

4; 2:r(3) ¼ ! q(4) ¼

0 1;

1010110000 0111000000 011110000

r(4) ¼ 011110000

0

i ¼ Y

5; 2:r(4) ¼ ! q(5) ¼

0 1;

0111100000 0111000000 000100000

r(5) ¼ 000100000

0

i ¼ Y

6; 2:r(5) , ! q(6) ¼ r(6) ¼

0 0; 001000000 (restoring)

0001000000 0111000000

0

i ¼ Y

7; 2:r(6) , ! q(7) ¼ r(7) ¼

0 0; 010000000 (restoring)

0010000000 0111000000

0

i ¼ Y

8; 2:r(7) , ! q(8) ¼ r(8) ¼

0 0; 100000000 (restoring)

0100000000 0111000000

0

i ¼ Y

9; 2:r(8) ¼ ! q(9) ¼

0 1;

1000000000 0111000000 001000000

r(9) ¼ 001000000

0

i ¼ Y

10; 2:r(9)

, ! q(10) ¼ r(10) ¼

0 0; 010000000

0010000000 0111000000

Thus Q ¼ R ¼

0001100010 (98), 010000000 (128),

so that ¼ þ ¼ þ

4 6

43:2 98:7 128=2 , that is, 43 (98=16):7 (2=16):

0 0

Y 2 Y

6

Observe that is a multiple of 2 , so that the subtraction 2.r(i) is performed

0

Y ; the other bits of

with the four (highlighted) most signiﬁcant bits of 2.r(i) and

2.r(i) are just propagated to the next step.

Another observation is that all numbers are even. A better solution is to substitute

0

Y Y ¼ Y.2 X X/2 ¼ X ¼

5

by and by (Comment 6.1). Then 2.r(0) 000101011. The

computation is the same as before without the ﬁnal bit (always equal to 0). The

ﬁnal result is Q ¼ R ¼

0001100010 (98), 01000000 (64),

so that ¼ þ ¼ þ

4 5

43:2 98:7 64=2 , that is, 43 (98=16):7 (2=16):

Let us point out that most scientiﬁc hand calculators feature integer binary

operations. Therefore, to produce a nonzero quotient, the initial multiplication of

p is compulsory.

the dividend by 2 115

6.1 NATURAL NUMBERS

Algorithm 6.3 is quite unpractical. A better solution consists in looking ﬁrst for a

q.

q of This process assumes that a normalization procedure sets both

tentative value t n n21

a b n-digit a , b B . b B

and as natural numbers such that and ; that is, the

b

leftmost digit of is non-zero. The input parameters of the division_step

a b. a b a b

procedure are and One deﬁnes the truncated values and of and as

t t

follows (/ stands for integer division):

n3 n2

¼ a=B b ¼ b=B :

a and (6:7)

t t

n23 n n23 n n22

, b/B , B /B ¼ B b , B /B ¼ B

a , b, a 3 2

; and

Observe that, as t t

n21 n22

B /B ¼ B.

b t q, q , is computed from

The tentative value of that is, t

a ¼ q :b þ r r , b :

, with

t t t t t t

A 5-input (B-ary) look-up table (LUT) may be used as a fast computing device for

that purpose. n-digit X, n-digit Y, r(i)

Given an dividend an divisor and a remainder

Lemma 6.1

such that (/ stands for integer division)

r(i) , Y, r(0) ¼ X, Y = 0,

n1

n2 n3

r ¼ B:r(i)=B ¼ r(i)=B

(i) , (6:8)

t n2

Y ¼ Y=B ,

t

q as

and deﬁning t ¼ r

q (i)=Y , (6:9)

t t t q

q ¼ B.r(i)/Y q or – 1, that is,

then the correct value of is either t t

B:r(i)=Y q :

q 1 (6:10)

t t n21 n

Y B 2

B 1, while

From relations (6.8) and (6.9), it is obvious that

q B 2 1.

t k Y

If the most signiﬁcant digits of a given divisor are equal to 0, then a previous

k k n21

Y.B B r(i)

Y Y.B so that , while

(normalizing) step will require substituting by

n-digit n-digit

may be assumed an number with any number of zeros upfront. If an

X n-digit Y,

dividend is given greater then the divisor then a previous (normalizing)

## X X/B.

step will require substituting by

Proof The rightmost part of inequality (6.10) (/ stands for real division) is derived

from n2 n2

a

þ :B

B:r(i)=Y (i):B )=Y , (6:11)

(r t t

116 ARITHMETIC OPERATIONS: DIVISION

n22 n22

a a

B.r(i) 2r , B

where stands for (i).B ; so and

t

B:r(i)=Y , þ bB:r(i)=Yc q :

(r (i) 1)=Y and (6:12)

t t t

The leftmost part of inequality (6.10) is proven as follows.

Equation (6.8) can be written n2 B:r(i),

r (i):B (6:13)

t

while (6.9) can be written q :Y

r (i) 0: (6:14)

t t t

Using (6.13) in (6.14), b

:(Y

B:r(i) q ) 0, (6:15)

t

n22 n22

b b

Y 2 Y , B q B 2

where stands for .B ; so , then, as 1

t t

n1

b

q : , B Y, (6:16)

t

allowing (6.15) to be written B:r(i) .

(q 1):Y 0, (6:17)

t

or B:r(i)=Y . q bB:r(i)=Yc q

1 then 1, (6:18)

t t

which completes the proof.

Algorithm 6.4 Restoring Base-B Division Step

at:=a/B**(n-3); bt:=b/B**(n-2);

qt:=at/bt;

remainder:=B*a-qt*b;

remainder<0 q:=qt-1; r:=remainder+b;

if then

q:=qt; r:=remainder;

else end if;

In this case, the restoring operation consists of adding the divisor to the remainder

whenever the latter is negative. Then the remainder is iteratively shifted as in the

basic division algorithm. 117

6.2 INTEGERS Compute 752024/876544 (base 10):

Example 6.2

q(0) ¼ r(0) ¼

0, 752024;

i ¼ 1

a ¼ b ¼ q ¼ ¼

752, 87, 752=87 8,

t t t

¼ ¼ .

remainder 10 752024 8 876544 507888 0,

q(1) ¼ r(1) ¼

8, 507888;

i ¼ 2

a ¼ b ¼ q ¼ ¼

507, 87, 507=87 5,

t t t

¼ ¼ .

remainder 10 507888 5 876544 696160 0,

q(2) ¼ r(2) ¼

5, 696160;

i ¼ 3

a ¼ b ¼ q ¼ ¼

696, 87, 696=87 8,

t t t

¼ ¼ 50752 ,

remainder 10 696160 8 876544 0,

q(3) ¼ r(3) ¼ 50752 þ ¼

7, 876544 825792;

i ¼ 4

a ¼ b ¼ q ¼ ¼

825, 87, 825=87 9,

t t t

¼ ¼ .

remainder 10 825792 9 876544 369024 0,

q(4) ¼ r(4) ¼

9, 369024;

i ¼ 5

a ¼ b ¼ q ¼ ¼

369, 87, 369=87 4,

t t t

¼ ¼ .

remainder 10 369024 4 876544 184064 0,

q(5) ¼ r(5) ¼

4, 184064

So ¼ þ

5

752024:10 85794 876544 184064 or

¼ þ

752024 0:85794 876544 1:84064:

6.2 INTEGERS

6.2.1 General Algorithm

## X Y Y . Q R

Let be an integer and a natural number with 0. Deﬁne and respectively

quotient remainder X Y, p

as the and the of the division of by with an accuracy of

fractional base-B digits: p

B :X ¼ Q:Y þ R,

118 ARITHMETIC OPERATIONS: DIVISION

Q R 2Y R , Y sign(R) ¼ sign(X).

where and are integers, and In other words,

p p

X ¼ þ

(Q:B ):Y (R:B ), with (6:19)

p p p p

Y:B R:B , Y:B sign(R:B ¼ sign(X),

and ) 2p 2p

## Q.B R.B

so that the unit in the least signiﬁcant position (ulp) of and is equal to

2p

B p ¼

. In the particular case where 0, that is,

X ¼ Q:Y þ R, Y R , Y sign(R) ¼ sign(X),

with and (6:20)

Q R quotient remainder integer division X Y.

and are the and the of the of by

## X Y

The basic algorithm applies to operands and such that

Y X , Y: (6:21)

2Y X , Y,

In the general case, to ensure that a previous alignment step is

X m-digit B’s

necessary. Assume that is an reduced complement number, that is,

m21 m21

X , B

2B ; then

0 m21 0 m21 0 m21

¼ B .Y, Y B . X 2Y 2 B X;

Y Y so that .1 and .1

substitute by 0 0

## Q R X Y

compute the quotient and the remainder of the division of by , with an

p þ m21

accuracy of fractional base-B digits, that is,

pþm1 0 0 0 0 0 0

:X ¼ Q:Y þ R Y R , Y sign(R ¼ sign(X),

B , with and )

that is,

p 0 m1

B :X ¼ Q:Y þ R, Y R ¼ R =B , Y sign(R) ¼ sign(X):

with and

p Y,

Comment 6.2 X X.B is an exact multiple of for example,

If is negative and

p

X.B ¼ K.Y, then the remainder must be negative, so that

Q ¼ þ R ¼ Y:

(K 1) and

Nevertheless, the desired result was (probably)

Q ¼ K R ¼

and 0:

An alternative deﬁnition of the quotient and remainder could be

p :X ¼ Q:Y þ R, Y , R , Y sign(R) ¼ sign(X) R =

B with and if 0,

that is, Deﬁnition 2.4. 119

6.2 INTEGERS

The digit-recurrence algorithms are based on the following sequence of

equations: X ¼ r(0),

B:r(0) ¼ q(1):Y þ r(1), Y r(1) , Y,

B:r(1) ¼ q(2):Y þ r(2), Y r(2) , Y, (6:22)

...

B:r( p ¼ q( p):Y þ r( p), Y r( p) , Y:

1) p p21 p22

## B B B

Multiply the ﬁrst equation by , the second by , the third by , and so on.

p þ

Then sum up the 1 equations. The following relation is obtained:

p p1 p2

B :X ¼ þ q(2):B þ þ q( p):B þ r( p),

0

(q(1):B ):Y (6:23)

Y r( p) , Y:

with

So p1 p2

Q ¼ q(1):B þ q(2):B þ þ q( p):B R ¼ r( p):

0 and (6:24)

þ r(i þ

As a matter of fact, the 2-unknowns (q(i 1), 1)) system

B:r(i) ¼ q(i þ þ r(i þ Y r(i þ , Y,

1):Y 1), with 1)

2Y r(i þ , Y r(i þ

has two solutions as the interval 1) includes two values of 1)

r(i þ ; B.r(i) Y.

such that 1) mod Thus different algorithms can be deﬁned accord-

r(i þ q(i þ q(i þ

ing to the way the values of 1) and 1) are chosen. The range of 1) is

given by the following inequalities:

q(i þ ¼ r(i þ , þ Y)=Y ¼ þ

1) (B:r(i) 1))=Y (B:Y (B 1),

q(i þ ¼ r(i þ . Y)=Y ¼ (B þ

1) (B:r(i) 1))=Y (B:Y 1);

thus B q(i þ B::

1) (6:25)

Robertson diagram) B.r(i),

The diagram of Figure 6.1 (the gives, for every value of

r(i þ q(i þ

the two possible values of 1) along with the corresponding value of 1).

q(i)

According to (6.25) the values of computed with (6.22) are not necessarily

2B B

base-B digits. If the minimum and maximum values and are not used, then

q(i) is a signed base-B digit. An easy way to transform the obtained signed-digit

120 ARITHMETIC OPERATIONS: DIVISION

r(i+1)

Y B –1 B

q (i+1) -B -(B-1) -2 -1 0 1 2

-B.Y -(B-1).Y -2.Y -Y Y 2.Y B.Y

... ... (B–1).Y B.r (i )

-Y r(i þ q(i þ

Robertson diagram: computation of 1) and 1).

Figure 6.1 B’s

representation into a nonsigned one (e.g., a complement representation) consists

ith and :

in deﬁning, at each step, the component of two new variables q _ pos q _ neg

¼ q(i), ¼

q(i) . 0,

if 0 then q _ pos q _ neg

q(i) ¼ ¼ ¼

if 0 then 0, 0,

q _ pos q _ neg

q(i) , ¼ ¼ 2q(i),

if 0 then 0,

q _ pos q _ neg

p-digit

and are base-B numbers. It remains to compute

so that both q _ pos q _ neg

q ¼

q pos q neg: B’s

As a matter of fact, the conversion from the signed-digit representation to a

complement one can also be performed on the ﬂy ([ERC1987], [ERC1992],

[OBE1997]). sign(R) ¼ sign(X)

A ﬁnal step is necessary in order that the condition be satisﬁed:

R , X R R þ Y Q Q 2

if 0 and 0 then substitute by and by 1;

R X , R R 2 Y Q Q þ

if 0 and 0 then substitute by and by 1.

Comment 6.3 Y

If a previous alignment is necessary, instead of substituting by

0 m21 0

Y ¼ B .Y, X X ¼ X/B Y

an alternative option is to substitute by and by

0 m22 0

## ¼ B .Y, Q R

Y and to compute the quotient and the remainder of the division

0 0

Y p þ m21

X by , with an accuracy of fractional base-B digits, that is,

of pþm1 0 pþm2 0 0 0 0 0

B :X ¼ B :X ¼ Q:Y þ R Y R , Y

, with and

0 0

sign(R ¼ sign(X

) )

so that

p 0 m2

:X ¼ Q:Y þ R; Y R ¼ R =B , Y sign(R) ¼ sign(X):

B with and

0

X X is equivalent to the substitution of the ﬁrst

Observe that the substitution of by

algorithm step, namely,

B:X ¼ q(1):Y þ r(1), Y r(1) , Y 121

6.2 INTEGERS r (i +1)

## Y

q(i +1) B –1

0 1 2

....

Y 2.Y (B –1).Y B.Y B.r (i )

Restoring algorithm.

Figure 6.2

by X ¼ q(1):Y þ r(1), Y r(1) , Y:

6.2.2 Restoring Division Algorithm r(i þ

A simple way of choosing between the two possible values of 1) is to add the

condition sign(r(i þ ¼ sign(r(i)),

1)) X. X

so that all remainders have the same sign as Assume that is nonnegative. Then

the diagram of Figure 6.1 is reduced to the right upper quarter (Figure 6.2). The cor-

responding algorithm is the classical restoring algorithm of Section 6.1.

6.2.3 Base-2 Nonrestoring Division Algorithm

In the binary case, the diagram of Figure 6.1 (without the minimum and maximum

22

values and 2) is reduced to the diagram of Figure 6.3a. The way the values of

r(i þ q(i þ

1) and 1) are chosen is shown in Figure 6.3b. The main difference

with respect to the recovering algorithm is that the decision about the values of

r(i þ q(i þ r(i),

1) and 1) only depends on the sign of and it is no longer necessary

## Y:

to compare 2.r(i) with q(i þ ¼ r(i þ ¼ 2 Y;

if 2.r(i) is nonnegative, then choose 1) 1 and 1) 2.r(i)

q(i þ ¼ 21 r(i þ ¼ þ Y.

if 2.r(i) is negative, then choose 1) and 1) 2.r(i)

The corresponding algorithm is the following:

Algorithm 6.5 Nonrestoring Division, First Version

r(0):=X;

i 0..p-1

for in loop

r(i)<0 q_pos(i+1):=0; q_neg(i+1):=1;

if then

r(i+1):=2*r(i)+Y;

q_pos(i+1):=1; q_neg(i+1):=0; r(i+1):=2*r(i)-Y;

else

end if;

end loop;

122 ARITHMETIC OPERATIONS: DIVISION

r(i+1)

## Y

q(i +1) –1 0 1 2.r (i )

–2

.Y –Y 2.Y

## Y

## –Y

(a) r (i+1)

## Y

–1 1

q(i +1) 2.r (i )

–2

.Y –Y Y 2.Y

## –Y

(b)

Base-2 nonrestoring algorithm.

Figure 6.3

Q:=q _ pos-q _ neg;

R:=r(p);

X>=0 and R<0 R:=R+Y; Q:=Q-1;

if then

X<0 and R>=0 R:=R-Y; Q:=Q+1;

elsif then

end if;

Nevertheless, a better algorithm, including an implicit on-the-ﬂy conversion, can be

q(1), q(2),

used. At each step, instead of computing and so on, the following values

are computed:

0 0 0

q ¼ þ q(1))=2, q ¼ þ q(2))=2, . . . q p ¼ þ q( p))=2:

(0) (1 (1) (1 , ( 1) (1 (6:26)

Observe that these new coefﬁcients are bits:

0 2 ¼

q(i) ¼ q (i 1) 1;

if 1, then 0 2 ¼

q(i) ¼ 21, q (i 1) 0.

if then 123

6.2 INTEGERS

According to (6.24) and (6.26),

p1 p2

Q ¼ q(1):2 þ q(2):2 þ þ q( p):2 0

0 p1 0 p2 0

¼ þ þ þ p 0

(2:q (0) 1):2 (2:q (1) 1):2 (2:q ( 1) 1):2 (6:27)

0 0 p2 0 p

¼ þ q þ þ q p

0

2(q (0):2 (1):2 ( 1):2 ) (2 1)

0 p 0 p1 0

¼ (1 q þ q þ þ q p þ :

1 0

(0)):2 (1):2 ( 1):2 1:2

Thus the vector 0 0 0

q q q p

(1 (0)) (1) ( 1) 1

## Q.

is the 2’s complement representation of

The corresponding algorithm is the following:

Algorithm 6.6 Nonrestoring Division, Second Version

r(0):=X;

i 0..p-1

for in loop

r(i)<0 Q(i):=0; r(i+1):=2*r(i)+Y;

if then

Q(i):=1; r(i+1):=2*r(i)-Y;

else

end if;

end loop;

Q(0):=1-Q(0); Q(p):=1; R:=r(p);

X>=0 and R<0 R:=R+Y; Q:=Q-1;

if then

X<0 and R>=0 R:=R-Y; Q:=Q+1;

elsif then

end if; Q

Observe that, before the ﬁnal sign correction, the obtained quotient is always odd.

Examples 6.3 212/15

1. Compute with an accuracy of 8 fractional bits:

r(0) ¼ 12,

q(0) ¼ r(1) ¼ 24 þ ¼ 9,

0, 15

q(1) ¼ r(2) ¼ 18 þ ¼ 3,

0, 15

q(2) ¼ r(3) ¼ 6 þ ¼

0, 15 9,

q(3) ¼ r(4) ¼ ¼

1, 18 15 3,

q(4) ¼ r(5) ¼ ¼ 9,

1, 6 15

q(5) ¼ r(6) ¼ 18 þ ¼ 3,

0, 15

q(6) ¼ r(7) ¼ 6 þ ¼

0, 15 9,

q(7) ¼ r(8) ¼ ¼

1, 18 15 3:

124 ARITHMETIC OPERATIONS: DIVISION

Q ¼ ¼ 2205, R ¼ ¼ þ

8

So 100110011 3, and 2 .(212) (2205).15 3. The sign

Q ¼ þ ¼ 2204,

correction generates the ﬁnal result: (2205) 1

R ¼ 2 ¼ 212, ¼ þ

8

3 15 so that 2 .(212) (2204).15 (212).

X X ,

2. (Integer division) Given a 9-bit integer (2256 256) and a positive

## Y, X Y Y

integer the integer division of by is computed as follows. The divisor is sub-

0

Y ¼ Y.256, p þ m 2 ¼ þ 2 ¼

stituted by the accuracy is equal to 1 0 9 1 8 bits,

0 0

R ¼ R /256.

R will be substituted by As an example,

and the ﬁnal remainder

X ¼ 2247 Y ¼

assume that and 15:

0 ¼ ¼

Y 15:256 3840,

r(0) ¼ 247,

q(0) ¼ r(1) ¼ 494 þ ¼

0, 3840 3346,

q(1) ¼ r(2) ¼ ¼

1, 6692 3840 2852,

q(2) ¼ r(3) ¼ ¼

1, 5704 3840 1864,

q(3) ¼ r(4) ¼ ¼ 112,

1, 3728 3840

q(4) ¼ r(5) ¼ 224 þ ¼

0, 3840 3616,

q(5) ¼ r(6) ¼ ¼

1, 7232 3840 3392,

q(6) ¼ r(7) ¼ ¼

1, 6784 3840 2944,

q(7) ¼ r(8) ¼ ¼

1, 5888 3840 2048:

Q ¼ ¼ 217, R ¼ ¼ 2247 ¼ þ

So, 111101111 2048/256 8, and (217).15 8. The

Q ¼ þ ¼ 216, R ¼ 2 ¼

sign correction generates the ﬁnal result: (217) 1 8 15

27, 2247 ¼ þ

so that (216).15 (27).

The same operation can be performed taking into account Comment 6.3:

0

Y ¼ ¼

15:128 1920,

r(0) ¼ 247=2,

q(0) ¼ r(1) ¼ 247 þ ¼

0, 1920 1673,

q(1) ¼ r(2) ¼ ¼

1, 3346 1920 1426,

q(2) ¼ r(3) ¼ ¼

1, 2852 1920 932,

q(3) ¼ r(4) ¼ ¼ 56,

1, 1864 1920

q(4) ¼ r(5) ¼ 112 þ ¼

0, 1920 1808,

q(5) ¼ r(6) ¼ ¼

1, 3616 1920 1696,

q(6) ¼ r(7) ¼ ¼

1, 3392 1920 1472,

q(7) ¼ r(8) ¼ ¼ 1024:

1, 2944 1920

Q ¼ ¼ 217, R ¼ ¼ 2247 ¼ þ

So, 111101111 1024/128 8, and (217).15 8.

Q ¼ þ ¼ 216, R ¼ ¼

The sign correction generates the ﬁnal result: (217) 1 8215

27, 2247 ¼ þ

so that (216).15 (27). X Y

3. Given a 7-bit 2’s complement integer and a 6-bit natural number belonging

22 X , X/Y p ¼

3 3

to the interval .Y 2 .Y, compute with an accuracy of 5.

125

6.2 INTEGERS 0

2Y X , Y, Y ¼ 2

To ensure that the divisor is substituted by 2 .Y, the dividend by

0 ¼ X/2, p ¼ þ ¼

X the division is performed with an accuracy of 5 3 8, and the

X ¼

2

ﬁnal remainder will be divided by 2 . Assume that 1010101 (243) and

Y ¼ 000111 (7):

0 ¼ X ¼ ,

r(0) ¼ X ; 2:r(0) 1010101 0;

i ¼ 0 0

q(0) ¼ þ Y ¼ r(1) ¼ þ ¼ ,

0; 2:r(0) 1110001 0

1010101 0011100

i ¼ 1 0

q(1) ¼ þ Y ¼ r(2) ¼ þ ¼ ,

0; 2:r(1) 1111110 0

1100010 0011100

i ¼ 2 0

q(2) ¼ þ Y ¼ r(3) ¼ þ ¼

0; 2:r(2) 0011000 0

1111100 0011100

i ¼ 3 0

q(3) ¼ Y ¼ r(4) ¼ ¼

1; 2:r(3) 0010100 0

0110000 0011100

i ¼ 4 0

q(4) ¼ Y ¼ r(5) ¼ ¼

1; 2:r(4) 0001100 0

0101000 0011100

i ¼ 5 0

q(5) ¼ Y ¼ r(6) ¼ ¼ ,

1; 2:r(5) 1111100 0

0011000 0011100

i ¼ 6 0

q(6) ¼ þ Y ¼ r(7) ¼ þ ¼

0; 2:r(6) 0010100 0

1111000 0011100

i ¼ 7 0

q(7) ¼ Y ¼ r(8) ¼ ¼

1; 2:r(7) 0001100 0

0101000 0011100

i ¼ 8

q(8) ¼ 1

Q ¼ R ¼ X , r(8) .

Thus 100111011 and 0001100. Since 0 and 0, a correction has to

be made:

Q ¼ þ ¼ ¼ 196); R ¼

100111011 1 100111100 ( 0001100 0011100

¼ ¼ 16)

1110000 (

¼ þ 243 ¼ þ

so that (243).32 (2196).7 (216/4), that is, (2196/32).7 (24/32).

0 0

Y + Y

2

Observe that is a multiple of 2 , so that the operation 2.r(i) is performed

0

Y ; the other bits of 2.r(i)

with the ﬁve (boldface) most signiﬁcant bits of 2.r(i) and

are just propagated to the next step. X Y

4. Given a 7-bit 2’s complement integer and a 6-bit natural number belong-

22 X , X/Y p ¼

4 4

ing to the interval .Y 2 .Y, compute with an accuracy of 4.

0 ¼

2Y X , Y, Y 3

2 .Y, the dividend

To ensure that the divisor is substituted by

0 ¼ X/2, p ¼ þ ¼

X the division is performed with an accuracy of 4 4 8,

by

126 ARITHMETIC OPERATIONS: DIVISION

X ¼

3

and the ﬁnal remainder will be divided by 2 . Assume that 00101011 (43) and

Y ¼ 000101 (5):

0

r(0) ¼ X ¼ X ¼

; 2:r(0) 0101011 0;

i ¼ 0 0

q(0) ¼ Y ¼ r(1) ¼ ¼

1; 2:r(0) 0000011 0

0101011 0101000

i ¼ 1 0

q(1) ¼ Y ¼ r(2) ¼ ¼ ,

1; 2:r(1) 1011110 0

0000110 0101000

i ¼ 2 0

q(2) ¼ þ Y ¼ r(3) ¼ þ ¼ ,

0; 2:r(2) 1100100 0

10111100 0101000

i ¼ 3 0

q(3) ¼ þ Y ¼ r(4) ¼ þ ¼ ,

0; 2:r(3) 1110000 0

1001000 0101000

i ¼ 4 0

q(4) ¼ þ Y ¼ r(5) ¼ þ ¼

0; 2:r(4) 0001000 0

1100000 0101000

i ¼ 5 0

q(5) ¼ þ Y ¼ r(6) ¼ ¼ ,

1; 2:r(5) 1101000 0

0010000 0101000

i ¼ 6 0

q(6) ¼ þ Y ¼ r(7) ¼ þ ¼ ,

0; 2:r(6) 1111000 0

1010000 0101000

i ¼ 7 0

q(7) ¼ þ Y ¼ r(8) ¼ þ ¼

0; 2:r(7) 0011000 0

1110000 0101000

i ¼ 8

q(8) ¼ 1

Q ¼ ¼ R ¼ ¼ X

Thus 010001001 ( 137) and 0011000 ( 24). Since 0 and

r(8) ¼ þ 3

0, no corrections have to be made, so that 43.16 137.5 24/2 , that is,

¼ þ

43 (137/16).5 (3/16).

6.2.4 SRT Radix-2 Division

Consider again the diagram of Figure 6.3a. There are two overlapping areas such that

2Y q(i þ 21

if 2.r(i),0 then 1) can be chosen equal to either or 0;

, Y q(i þ

if 0 2.r(i) then 1) can be chosen equal to either 0 or 1.

The initial goal of the SRT-2 procedure was to reduce the number of additions or

q(i þ ¼ r(i þ ¼

subtractions, choosing 1) 0 and 1) 2.r(i) as often as possible.

Another advantage of the existence of overlapping areas is that, within particular

q(i þ

conditions of allowed range on the remainder, the choice of 1) can be done

Y r(i).

as a function of the truncated values of and The drawback is the nonunique

form of the ﬁnal quotient because of the use of a redundant quotient-digit set,

namely, {21, 0, 1}. This is solved through a ﬁnal conversion process. 127

6.2 INTEGERS q(i þ ¼

If the strategy consists in selecting 1) 0 whenever possible, the selection

is achieved according to the following rule (Figure 6.3a):

, 2Y, q(i þ ¼ 21;

if 2.r(i) then 1)

2Y , Y, q(i þ ¼

if 2.r(i) then 1) 0;

Y, q(i þ ¼

if 2.r(i) then 1) 1. q(i þ ¼ 2Y

In practice, detecting the situation that allows 1) 0 (i.e.,

, Y)

2.r(i) is not straightforward, unless through a trial subtraction, an operation

to be avoided whenever possible to make the savings effective. Sweeney, Robertson,

and Tocher ([SWE1957], [ROB1958], [TOC1958]) suggest an alternative solution

2Y r(i) , Y

to this question. Instead of allowing the range for the partial

remainders, a restricted range is enforced.

Y n-bit

Let be an natural number whose most signiﬁcant bit is equal to 1, that is,

n1 n

Y , 2 ,

2

## X

and an integer belonging to the range

n1 n1

X , ;

2 2

n-bit 2Y X , Y.

(an 2’s complement number) so that Then the system (6.22),

B ¼

with 2, is substituted by the following:

X ¼ r(0), n1 n1

¼ q(1):Y þ r(1), 2 r(1) ,

2:r(0) 2 ,

n1 n1

¼ q(2):Y þ r(2), 2 r(2) , (6:28)

2:r(1) 2 ,

... n1 n1

¼ q( p):Y þ r( p), 2 r( p) , :

2:r(p 1) 2

The corresponding graphical representation is shown in Figure 6.4a.

q(i þ r(i þ

The selection of 1) and 1) is done as follows (Figure 6.4b):

n21

, 22 q(i þ ¼ 21, r(i þ ¼ þ Y;

if 2.r(i) , then 1) 1) 2.r(i)

n21 n21

22 , q(i þ ¼ r(i þ ¼

if 2.r(i) 2 , then 1) 0, 1) 2.r(i);

n21

q(i þ ¼ r(i þ ¼ 2 Y.

if 2.r(i) 2 , then 1) 1, 1) 2.r(i)

n n

w ¼ 22 w ,

Observe that 2.r(i) belongs to the interval 2 . If it is represented as

n21

þ is very simple and

an (n 1)-bit 2’s complement number, the comparison with 2

can be done with the two most signiﬁcant bits: See Table 6.1.

128 ARITHMETIC OPERATIONS: DIVISION

r (i +1)

## Y

n –1

2

–1

q(i+1) 0 1

n–1

–Y 2

n n

–2 2

n–1

–2 2.r(i)

–2.Y Y 2.Y

n–1

–2

## –Y

(a) r(i +1)

## Y

n –1

2

q (i+1) –1 0 1

n n

–2 2

n–1

–Y 2

n–1

–2 2.r (i)

–2

.Y Y 2.Y

n–1

–2

## –Y

(b)

SRT-2 division algorithm.

Figure 6.4

The corresponding algorithm is the following:

Algorithm 6.7 SRT-2 Division

r(0):=X;

i 0..p-1

for in loop

2.r(i)<-(2**(n-1)) q _ pos(i+1):=0; q _ neg(i+1):=1;

if then

r(i+1):=2*r(i)+Y;

2.r(i)>=2**(n-1) q _ pos(i+1):=1; q _ neg(i+1):=0;

elsif then

r(i+1):=2*r(i)-Y;

q _ pos(i+1):=0; q _ neg(i+1):=0; r(i+1):=2*r(i);

else

end if;

end loop;

Q:=q _ pos-q _ neg;

R:=r(p);

X>=0 and R<0 R:=R+Y; Q:=Q-1;

if then

X<0 and R>=0 R:=R-Y; Q:=Q+1;

elsif then

end if; q(i 1

TABLE 6.1 SRT-2 Algorithm: Selection of 1)

w q(i þ

w 1)

n n21

0 0 0

0 1 1

21

1 0

1 1 0 129

6.2 INTEGERS

Examples 6.4 X X ,

1. Given an 8-bit 2’s complement integer (2128 128) and an 8-bit posi-

Y Y ,

tive integer whose most signiﬁcant bit is equal to 1 (128 256), compute the

## X Y.

quotient and the remainder of the division of by The range of possible values of

w 2256 w , w , 2128,

is 256. This range is partitioned into three intervals:

2128 w , w, 21,

128 and 128 to which correspond the values 0, and 1 for

q(i þ X ¼ 284, Y ¼ p ¼

1). Assume that 247 and 8:

r(0) ¼ 84,

, 128 q(1) ¼ 1, r(1) ¼ þ Y ¼ 168 þ ¼

2:r(0) 2:r(0) 247 79,

, q(2) ¼ r(2) ¼ Y ¼ ¼ 89,

128 2:r(1) 1, 2:r(1) 158 247

, 128 q(3) ¼ 1, r(3) ¼ þ Y ¼ 178 þ ¼

2:r(2) 2:r(2) 247 69,

128 , q(4) ¼ r(4) ¼ ¼

2:r(3) 128 0, 2:r(3) 138,

, q(5) ¼ r(5) ¼ Y ¼ ¼

128 2:r(4) 1, 2:r(4) 276 247 29,

128 , q(6) ¼ r(6) ¼ ¼

2:r(5) 128 0, 2:r(5) 58

128 , q(7) ¼ r(7) ¼ ¼

2:r(6) 128 0, 2:r(6) 116,

128 , q(8) ¼ r(8) ¼ ¼

2:r(7) 128 0, 2:r(7) 232,

¼ ¼ ¼ Q ¼ 2 ¼

So 01001000 (¼ 72), 10100000 ( 160), 72 160

q _ pos q _ neg

288, R ¼ Q ¼ þ

232. The sign correction generates the ﬁnal result: (288)

¼ 287, R ¼ 2 ¼ 215, ¼ þ

1 232 247 so that (284).256 (287).247 (215).

X ¼

2. Let 0101001101 (¼ 333) be a 2’s complement 10-bit integer and

Y ¼ 1101000000 (¼ 832) a 10-bit natural number whose most signiﬁcant bit is

X/Y p ¼ w

equal to 1. Compute with an accuracy of 8. At each step will be

þ ¼

represented in the form of a 2’s complement number with 10 1 11 bits. For

## Y,

adding or subtracting a 2’s complement 11-bit representations will be used:

Y ¼ 01101000000,

Y ¼ 10011000000:

The step-by-step procedure is described as follows.

q_pos q_neg

Step # Remainder Computation

i ¼ w ¼ ¼

0 2.X 01010011010

w ¼ ! q(1) ¼

w 01 1 1 0

10 9

w 01010011010

Y 10011000000

r(1) 1101011010

i ¼ w ¼ ¼

1 2.r(1) 11010110100

w ¼ ! q(2) ¼

w 11 0 0 0

10 9

130 ARITHMETIC OPERATIONS: DIVISION

q_pos q_neg

Step # Remainder Computation

w 11010110100

0 00000000000

r(2) 1010110100

i ¼ w ¼ ¼

2 2.r(2) 10101101000

w ¼ ! q(3) ¼ 21

w 10 0 1

10 9 w 10101101000

Y 01101000000

r(3) 0010101000

i ¼ w ¼ ¼

3 2.r(3) 00101010000

w ¼ ! q(4) ¼

w 00 0 0 0

10 9 w 00101010000

0 00000000000

r(4) 0101010000

i ¼ w ¼ ¼

4 2.r(4) 01010100000

w ¼ ! q(5) ¼

w 01 1 1 0

10 9 w 01010100000

Y 10011000000

r(5) 1101100000

i ¼ w ¼ ¼

5 2.r(5) 11011000000

w ¼ ! q(6) ¼

w 11 0 0 0

10 9 w 11011000000

0 00000000000

r(6) 1011000000

i ¼ w ¼ ¼

6 2.r(6) 10110000000 0 1

w ¼ ! q(7) ¼ 21

w 10

10 9 w 10110000000

Y 01101000000

r(7) 0011000000

i ¼ w ¼ ¼

7 2.r(7) 00110000000

w w ¼ ! q(8) ¼

00 0 0 0

10 9 w 00110000000

0 00000000000

r(8) ¼

0110000000 ( 384) 131

6.2 INTEGERS Y Q

As the ﬁnal remainder is positive, doesn’t need to be added, and is given by:

¼ ¼ ¼

Q ¼ 10001000 00100010 01100110 ( 102):

pos Q neg

## Q

The overall operation can be resumed as

¼ þ

8

333:2 102:832 384:

6.2.5 SRT Radix-2 Division with Stored-Carry Encoding

The most time-consuming operation, at each step of algorithm 6.7, is clearly the com-

r(i þ n-bit

putation of the new remainder 1): addition. The key idea for saving this time

is to perform a carry-save sum, that is, a reduction of three operands to two (stored-

r(i)

carry encoding, Chapter 4). So every remainder will be expressed in the form of

a sum of two 2’s complement numbers. The SRT-2 carry-save algorithm departs

from Algorithm 6.7 by the range allowed for the successive remainders and by the

q(i þ r(i þ r(i).

way the values of 1) and 1) are chosen as functions of

Y n-bit

Let be an natural number whose most signiﬁcant bit is equal to 1, that is,

n1 n

Y , 2 , (6:29)

2

X 2Y X , Y,

and let be an integer belonging to the range so that

n n

, X ,

2 2

þ B ¼

(an (n 1)-bit 2’s complement number). Then the system (6.22), with 2, gen-

## Q R X Y

erates the quotient and the remainder of the division of by with an accuracy

p q(i þ r(i þ

of fractional bits. The selection of 1) and 1) must be done as shown in

Figure 6.3a. r(i) 2Y r(i) , Y, r(i)

Every remainder belongs to the range so that satisﬁes the

following inequalities n n

, Y r(i) , Y ,

2 2 ,

w ¼

and 2.r(i) belongs to the interval

nþ1 nþ1

2 , w , :

2 (6:30)

n þ

In 2’s complement, with 3 bits,

, w ,

110000 0 010000 0:

w

All along the algorithm execution, will be represented in stored-carry form, that is,

in the form w ¼ s þ c

s c þ

where and are (n 3)-bit numbers (Figure 6.5a).

132 ARITHMETIC OPERATIONS: DIVISION

(a) (b) (c)

n +2 n +1 n n –1 n +2 n +1 n n –1 ...

... s s

s t

... ...

c

c c t

... ...

w w w ' w ''

w ¼

Stored-carry representation of 2.r(i).

Figure 6.5

0

w w ¼ j),

Deﬁne as being the truncated value of 2.r( namely (Figure 6.5b)

0 n1

w ¼ bw=2 c:

According to (6.30), n1

4 , w=2 , 4:

Thus 0

4 w 3: (6:31)

0 n21 n21

w w

The maximum difference between and .2 is smaller than 2 , that is,

n1 0 n1

w , w :2 w:

2 (6:32)

s c s c

Deﬁne the truncated values and of and as

t t

n1 n1

s ¼ bs=2 c c ¼ bc=2 c,

and

t t

00

w s c

and as being the result of adding and (Figure 6.5c). The difference between

t t

0 00

w

w and is the possible carry from the rightmost positions, so that

0 00 0

w w w :

1 (6:33)

Thus, from (6.31) and (6.33), 00

5 w 3, (6:34)

that is, in 2’s complement, 00

w

1011 0011, 133

6.2 INTEGERS

and from (6.32) and (6.33), n 00 n1

, w :2 w,

w (6:35)

2

that is, 00 n1 00 n1 n

:2 w , w :2 þ :

w 2 (6:36)

q(i þ

The selection of 1) is done as follows (see Figure 6.3a):

00 00

25 w , 21, 25 w 2 w ,

if that is, 2, then (6.36) 0 and

q(i þ ¼ 21;

1)

00 00 n21

21 w , w ¼ 21, 2Y 22 w ,

if 0, that is, then (6.36) and (6.29)

n21 Y, q(i þ ¼

and 1) 0;

2 00

w w, q(i þ ¼

if 0 3, then (6.36) 0 and 1) 1.

The corresponding selection rules are show in Table 6.2.

Assume that is a function that expresses the sum of three integers in

carry_save

the form of two integers (stored-carry encoding, Chapter 4). Then deﬁne an

procedure:

srt_step srt_step (s, c, Y: integer; q _ pos, q _ neg:

procedure in out

bit; next_s, next_c: integer)

out is

begin

w’’:=s(n+2..n-1)+c(n+2..n-1);

w’’

case is

0000|0001|0010|0011|=>

when

q _ pos:=1; q_neg:=0; (next_s, next_c):=carry_save(s, c,

## -Y);

1011|1100|1101|1110=>

when

q _ pos:=0; q _ neg:=1; (next_s, next_c):=carry_save(s, c, Y);

others=>

when

q _ pos:=0; q _ neg:=0; (next_s, next_c):=carry_save(s, c, 0);

end case;

srt_step;

end 0 0

c s/2 c/2):

The SRT-2 algorithm can now be stated as follows (s and stand for and

Algorithm 6.8 SRT-2 Division with Stored-Carry Encoding

s’(0):=x; c’(0):=0;

i 0..p-1

for in loop

srt_step(2*s’(i), 2*c’(i), y, q _ pos(p-1-i), q _ neg(p-1-i),

s’(i+1), c’(i+1));

end loop;

r:=s’(p)+c’(p);

q:=q _ pos-q _ neg;

x>=0 and r<0 r:=r+y; q:=q-1;

if then

x<0 and r>=0 r:=r-y; q:=q+1;

elsif then

end if;

134 ARITHMETIC OPERATIONS: DIVISION

TABLE 6.2 SRT-2 Algorithm with Stored-Carry

q(i 1

Encoding: Selection of 1)

w00 q(i þ 1)

0000 1

0001 1

0010 1

0011 1

0100 —

0101 —

0110 —

0111 —

1000 —

1001 —

1010 —

21

1011 21

1100 21

1101 21

1110

1111 0 n21

Comment 6.4 X 2Y X ,

When belongs to the interval 2 , it has been

00 00

25 w

w never reaches 3, that is, 2. Then

observed ([SUT2004]) that

Table 6.2 can be modiﬁed as shown Table 6.3.

00 00

w w

The most signiﬁcant bit of , that is, , is no longer necessary and Table 6.3 is

3

reduced Table 6.4. q(i 1

TABLE 6.3 Modiﬁed 1) Selection Table

w00 q(i þ 1)

0000 1

0001 1

0010 1

0011 —

0100 —

0101 —

0110 —

0111 —

1000 —

1001 —

1010 —

21

1011 21

1100 21

1101 21

1110

1111 0 135

6.2 INTEGERS q(i 1

TABLE 6.4 Reduced 1) Selection Table

00 00 00

w w w q(i þ 1)

2 1 0

000 1

001 1

010 1

21

011 21

100 21

101 21

110

111 0

Examples 6.5

X ¼

1. Let 001110111011 (¼955) be a 12-bit 2’s complement integer and

Y ¼ X/Y

11010101101 (¼1709) an 11-bit positive integer. Compute with an

p ¼ s c

accuracy of 8. At each step and will be represented in the form of 2’s

þ ¼ Y,

complement integers with 11 3 14 bits. For adding or subtracting 2’s

complement 13-bit representations will be used:

Y ¼ 0011010101101,

Y ¼ 1100101010011: no operation):

The step-by-step procedure is described as follows (nop stands for

Step # Carry-Save Remainder Computation q_pos q_neg

i ¼ s ¼ ¼ s ¼

0 2.X 00011101110110; 0001

t

¼

c ¼ c 0000

00000000000000; t

00

w ¼ ! q(0) ¼ ! Y

0001 1 subtract 1 0

s 00011101110110

c 00000000000000

Y 1100101010011

0

s

next 1111000100101

0

c

next 0001010100100

¼

i ¼ s ¼ s 1111

1 11110001001010; t ¼

c ¼ c 0001

00010101001000; t

00 ¼ ! q(1) ¼ ! Y

w 0000 1 subtract 1 0

s 11110001001010

c 00010101001000

Y 1100101010011

0

next s 0000001010001

0

next c 1101010010100

136 ARITHMETIC OPERATIONS: DIVISION

Step # Carry-Save Remainder Computation q_pos q_neg

i ¼ s ¼ s ¼

2 00000010100010; 0000

t

c ¼ c ¼

11010100101000; 1101

t

00

w ¼ ! q(2) ¼ 21 ! Y

1101 add 0 1

s 00000010100010

c 11010100101000

Y 0011010101101

0

s

next 1001100100111

0

c

next 0100101010000

¼

i ¼ s ¼ s 1001

3 10011001001110; t ¼

c ¼ c 0100

01001010100000; t

00

w ¼ ! q(3) ¼ 21 ! Y

1101 add 0 1

s 10011001001110

c 01001010100000

Y 0011010101101

0

s

next 1001001000011

0

c

next 0110101011000

¼

i ¼ s ¼ s 1001

4 10010010000110; t ¼

c ¼ c 0110

01101010110000; t

00

w ¼ ! q(4) ¼ ! nop

1111 0 0 0

s 10010010000110

c 01101010110000

0

s

next 1111000110110

0

c

next 0000100000000

¼

i ¼ s ¼ s 1111

5 11110001101100; t

c ¼ c ¼

00001000000000; 0000

t

00

w ¼ ! q(5) ¼ ! nop

1111 0 0 0

s 11110001101100

c 00001000000000

0

s

next 1111001101100

0

c

next 0000000000000

i ¼ s ¼ s ¼

6 11110011011000; 1111

t ¼

c ¼ c 0000

00000000000000; t

00 ¼ ! q(6) ¼ ! nop

w 1111 0 0 0

s 11110011011000

c 00000000000000

0

next s 1110011011000

0

next c 0000000000000 137

6.2 INTEGERS

i ¼ s ¼ s ¼

7 11100110110000; 1110

t ¼

c ¼ c 0000

00000000000000; t

00 ¼ ! q(7) ¼ ! Y

w 1110 -1 add 0 1

s 11100110110000

c 00000000000000

Y 0011010101101

0

s

next 1111100011101

0

c

next 0000101000000

r(8) ¼

0000001011101 ( 93)

## Y

As the ﬁnal remainder is positive, the divisor doesn’t need to be added to the

r(8), Q

ﬁnal remainder and is given by

Q ¼ ¼

11000000 00110001;

Q pos Q neg

In 2’s complement:

Q ¼ þ ¼ ¼

011000000 111001111 010001111 ( 143):

The overall operation can be resumed as

¼ þ

955:256 143:1709 93:

## Y

2. Let be an 8-bit positive integer whose most signiﬁcant bit is equal to 1

Y , X X ,

(128 256) and let be a 9-bit 2’s complement integer (2256 255).

## X/Y X

In order to compute the with an accuracy of 4 fractional bits, is ﬁrst

divided by 2 (Comment 6.3) and the division is performed with an accuracy of 5

X ¼ Y ¼

fractional bits. Assume that 100000111 (2249) and 10010011 (147). At

s c

each step and will be represented in the form of 2’s complement integers with

þ ¼ Y,

8 3 11 bits. For adding or subtracting 2’s complement 10-bit representations

will be used: Y ¼ 0010010011,

Y ¼ 1101101101: no operation):

The step-by-step procedure is described as follows (nop stands for

Step # Carry-Save Remainder Computation q_pos q_neg

i ¼ s ¼ X ¼ s ¼

0 11100000111; 1110

t

c ¼ c ¼

00000000000; 0000

t

00 ¼ ! q(0) ¼ 21 ! Y

w 1110 add 0 1

s 11100000111

c 00000000000

Y 0010010011

0

next s 1110010100

0

next c 0000000110

138 ARITHMETIC OPERATIONS: DIVISION

Step # Carry-Save Remainder Computation q_pos q_neg

i ¼ s ¼ s ¼

1 11100101000; 1110

t

c ¼ c ¼

00000001100; 0000

t

00 ¼ ! q(1) ¼ 21 ! Y

w 1110 add 0 1

s 11100101000

c 00000001100

Y 0010010011

0

s

next 1110110111

0

c

next 0000010000

i ¼ s ¼ s ¼

2 11101101110; 1110

t

c ¼ c ¼

00000100000; 0000

t

00 ¼ ! q(2) ¼ 21 ! Y

w 1110 add 0 1

s 11101101110

c 00000100000

Y 0010010011

0

s

next 1111011101

0

c

next 0001000100

i ¼ s ¼ s ¼

3 11110111010; 1111

t

c ¼ c ¼

00010001000; 0001

t

00

w ¼ ! q(3) ¼ ! Y

0000 1 subtract 1 0

s 11110111010

c 00010001000

Y 1101101101

0

s

next 0001011111

0

next c 1101010000

¼

i ¼ s ¼ s 0001

4 00010111110; t

c ¼ c ¼

11010100000; 1101

t

00

w ¼ ! q(4) ¼ 21 ! Y

1110 add 0 1

s 00010111110

c 11010100000

Y 0010010011

0

next s 1010001101

0

c

next 0101100100

r(5) ¼ 15)

1111110001 (

## Y

As the ﬁnal remainder is negative, the divisor doesn’t need to be subtracted

r(5), Q

from the ﬁnal remainder and is given by

¼

Q ¼ 00010 11101;

pos Q neg

Q 139

6.2 INTEGERS

In 2’s complement Q ¼ þ ¼ ¼ 27):

000010 100011 100101(

The overall operation can be resumed as

¼ þ

(249=2):32 (27):147 (15)

that is, 249 ¼ þ

(27=16):147 (15=16):

## P– D

6.2.6 Diagram

Apart from the Robertson diagram, another popular graphical tool used to illustrate

P – D remainder –divisor)

the quotient selection problem is the (partial plot diagram

([FRE1961]). It is a representation of the domain where possible values of the quo-

0

Y, r(i), r(i þ w, w

tient-digit may be assigned. First deﬁne normalized values of 1), ,

00

w :

and n n þ n n

r ¼ r(i)=2 r ¼ r(i þ y ¼ w=2

d ¼ Y=2 , , 1)=2 , , (6:37)

so that r ¼ y=2 (6:38)

0 00

w w

and the normalized values of and are

0 0 00 00

r ¼ w =2, r ¼ w =2: (6:39)

Thus 0 00

d , d r , d, r r :

1 32 52 32

1, 2 , and (6:40)

2 q ¼ q(i þ

With these normalized values, the selection of 1) corresponding to

Algorithm 6.8 (SRT division with stored-carry encoding) is done as follows

(Figure 6.6): 00

r , q ¼ 1;

52 12

if , then

00

r , q ¼

12

if 0, then 0;

00

r q ¼

32

if 0 , then 1.

P –D d)

In the diagram, the coordinates (2.r, are linked to none, one, or several

q. P– D

acceptable values of Figure 6.7 displays the diagram associated to the

140 ARITHMETIC OPERATIONS: DIVISION

+

r = 2.r – q.d

d

–1 0 1

q=

q=

q= 1/2 1 3/2 2.r

–5/2 –1 –1/2

–2.d d

–d 2.d

[ [

–d r''

[ [ [ ]

q = –1 q =0 q =1

quotient–digit selection axis

Robertson diagram.

Figure 6.6

partial remainder diagram of Figure 6.6. The domain presented in Figure 6.7

d ,

d) 12 1 and

covers the (2.r, coordinates compatible with the conditions

22d ,

2.r 2.d. This domain is divided into six zones separated by the lines

¼ 22.d, ¼ 2d, ¼ ¼ d, ¼ ¼

2.r 2.r 2.r 0, 2.r and 2.r 2.d. Above the line 2.r 2.d

¼ 22.d never reached

and under the line 2.r lie two zones; they correspond to coor-

¼ 22.d ¼ 2d, 21

dinates out of range. Between the lines 2.r and 2.r the value has

q;

to be assigned to this zone corresponds to the coordinates 2.r in the interval

2 d[ ¼ 2d

[22.d, of the diagram in Figure 6.6. Between the lines 2.r and

¼ 21 q;

2.r 0, either value or 0 may be assigned to this zone corresponds to the

coordinates 2.r in the interval [2d, 0[ of the diagram in Figure 6.6. In the same

¼

way, one can show that in the positive ﬁeld, the zone between the lines 2.r 0

¼ d q ¼ d

and 2.r corresponds to in {0,1}, while the zone between the lines 2.r

¼ q ¼

and 2.r 2.d correspond to 1.

q P –D

Now the selection strategy, deﬁned in Figure 6.6, can be mapped on the

00 00

r ¼ 2 r ¼

12

plot diagram: the lines and 0, highlighted in Figure 6.7, set the limits

q ¼ 21, P –D

for choosing 0, or 1. As it appears in the following section, the plot

k k

with 2)

diagram is particularly well suited to deal with high-radix (bases 2

because of more complex quotient-digit selection rules.

## P –D

Actually, the plot diagram as shown in Figure 6.7 is the level zero of a tri-

þ

r , assuming that

dimensional (3-D) diagram whose third (vertical) axis would be

þ ¼ 2 q.d q ¼ 21,0,

d. r 2.r for and 1

the horizontal axes are 2.r and Equations ¼ 2d, ¼ ¼ d,

now represent planes crossing level zero at lines 2.r 2.r 0, and 2.r

respectively. The diagram of Figure 6.6 is the intersection, at some allowed coordi-

þ

d, r

nate of of this tridimensional ﬁgure with a plane parallel to axes , 2.r, that is,

d ¼

parallel to plane 0. Figure 6.8 shows the 3-D Robertson/P –D diagram for

q ¼ 21. ¼ 2d, ¼ ¼ d

the plane corresponding to Lines 2.r 2.r 0, and 2.r (from

þ

P – D r ¼

plot diagram at 0) are highlighted in Figure 6.8, together with line

þ ¼ 2 q.d d ¼

r 34

2.r (from the Robertson diagram at ). 141

6.2 INTEGERS

r '' 2.r [

010.000 010.000 [ Never reached

zone .d

2

=

r

2.

01.100 01.100 q = 1 [

01.000 01.000 d

=

r

2.

0.100 0.100 1

or

0

=

q

≥0; ≥0

r'' 2.r

q set to 1 n

d = Y/ 2

[ [

0.000 0.000 [ [

0.100 0.101 0.110 0.111 01.000

=r'' ≤2.r<½

–½ ; –½

q set to 0

q =

1.100 1.100 0

[ [

or –1

2.r = – d

r''<–½ ; 2.r<0

q set to –1 [

1.000 1.000 q = –1

2.

r

10.100 10.100 = – 2.d

Never reached

zone [

10.000 10.000 Note: sign–bits are bold faced

101.100 P D

– plot diagram.

Figure 6.7

142 ARITHMETIC OPERATIONS: DIVISION

+

r = 2.r – q.d

q = –1

+

r = 2.r – q.d

d = 0.11; q = –1 01.000

0.111

0.110

0.101

0.100

2.r = –d 0

1.001

2.r = d 1.010

2.r = 0 1.011

0.100 0

1

0.

0

1 0

0.101 .1

0

1

0 .0

0.110 .

D 1

0

iv .

0

is 0.111 1

or .

1

1 2.r

.

01.000

d 0 r

ainde

1

0 em

.1

1 R

0

.0 D q ¼ 21.

Tridimensional Robertson/P – diagram for

Figure 6.8

6.2.7 SRT-4 Division k

The SRT method can be extended to any base 2 with a variety of quotient-digit sets.

k

Nevertheless, the step complexity increases with as more comparisons are

involved in the quotient-digit selection process and more divisor multiples have

to be computed. The designer will consider trade-offs between cycle time and the

number of cycles. A lot of alternatives are proposed in the literature ([ERC1990],

[ERC2004], [FAN1989], [MON1994], [QUA1992], [SRI1995], [TAY1985]). An

¼ 2

SRT-4 (B 2 ) algorithm, with redundant quotient-digit sets, is presented in the

following. X Y n-bit

One assumes that and are 2’s complement integers with

n2 n1

Y , 2 ,

2 (6:41)

Y X , Y: 143

6.2 INTEGERS r (i+1) = 4.r (i ) – q.Y

## Y

–1

–2

–3 3

2

1

0 q=

q= q= q=

q= q=

q= 4.r (i )

–2Y Y 3.Y 4.Y

–4.

Y –3Y –Y 2.Y

## –Y

SRT-4 partial remainder diagram.

Figure 6.9

A ﬁrst version is given with the quotient-digit set [23, 3]. This means that the mul-

+2.Y +3.Y

tiples and have to be generated according to the possible selection of

q ¼ q(i þ ¼ +2 +3.

1) or The Robertson diagram is given in Figure 6.9.

r(i þ 2 q(i þ

The new remainder 1) is now computed as 4.r(i) 1).Y, where

q(i þ r(i þ Y[;

1) is selected in such a way that 1) can be kept in the range [2Y,

nþ1 nþ1

, 2 [. As it will appear

the maximum allowed range for 4.r(i) is now [22 Y)

later, it is no longer possible to locate a point (4.r(i), in some zone [k.Y,

þ

(k 1).Y] of Figure 6.9, only from the numerical value 4.r(i). One also needs

Y. P – D

extra information on The plot diagram presented in Figure 6.10 emphasizes

r(i)

this point and illustrates a possible quotient-digit selection strategy (r stands for

q q(i þ q-select Y-

and for 1)). Although the zones are symmetric with respect to the

q

axis, the way to select is not. This comes from the fact that, in 2’s complement

representation, truncated nonsign bits are always positive whatever the sign is. As

¼ q.Y q-select

in Figure 6.7, lines 4.r are deﬁning zones. From Figure 6.9 one can

¼ k.Y ¼ þ q

check that between the vertical lines 4.r and 4.r (k 1).Y, can be

k k þ q 23

either or 1; this does not hold for the outermost zones where has to be

or 3, according to the side; these zones are shown in Figure 6.10.

Y n 2

In Figure 6.10, the values on axis 4.r and have been truncated by 2 and

n 2 4 bits, respectively. For clarity, sign-bits appear as bold characters. The remain-

q-selection

ing bits are actually the only ones needed for algorithmic purposes. The

strategy is illustrated in Figure 6.10 where heavy lines stand for border limit lines

q.

between consecutive options for The symbols “number & arrows” and “[”,

q,

lying on a border-line, respectively, highlight the choice for the zone covered

by this choice, and the inclusion of the limit line in that choice. At coordinates

¼

4.r and horizontal border-lines still hold between regions of

010, 01, 11, 10,

q.

different options for This is no longer valid for values between [100, and

101,[

staircase

where so-called lines emphasize that the selection in that

011, 0100[, Y

case depends on too. Take a closer look at those border-lines to make sure that

the selections proposed in Figure 6.10 are correct. First, remember that the bits

## Y

deleted are always positive, so the head bits of 4.r or (truncated coordinates 4.r,

n22 n24

Y/2

Y) and ; notice that the

stand for the minimum of the nonintegers 4.r/2 Y

rightmost point of any border-line is never reached as never reaches For

01000.

144 ARITHMETIC OPERATIONS: DIVISION

4.r

n –2

/ 2

01000 [

0111 Never reached {3}

∈

q

zone Y

.

4

0110 =

r

4.

0101 }

, 3

2

{

∈

## Y

3. q

=

r

4. 3

p4

3

p3

0100 [

[ Y

2.

[ =

r

4.

2

3

011 [

p1 [ 1,2}

p2 ∈ {

q 2

2

010 [

## Y

=

4.r 1

1

01 {0,1}

[ ∈

q Y

00 n–4

[ [ / 2

0100 0101 0110 0111 01000 0

∈

q

0 {–1,0}

11 [ 4.r = –

Y –1

–1

10 [ ∈

q { –2,–1}

4.

r = –2.

## Y

–2

101 [ –2

[ –2

–2

100 4. r [

= –3.

Y q ∈ {–3, –2 }

1011 4.

r = –4. q ∈

Y {–3

1010 }

Never reached

zone

1001

1000 Note: sign–bits are bold faced

## P D

– plot diagram for SRT-4 division.

Figure 6.10 145

6.2 INTEGERS TABLE 6.5 SRT-4 Quotient-Digit Selection Table

Y q(i þ

4r(i) 1)

4 — 3

[3, 4[ [4, 6[ 3

[3, 4[ [6, 8[ 2

[2, 3[ — 2

[1, 2[ — 1

[21, 1[ — 0

21[ 21

[22, —

22[ 22

[23, —

23[ 22

[24, [6, 8[

23[ 23

[24, [4, 6[

,24 23

—

22, 21,

these two reasons, the border-lines labeled 0, 1, and 2, in Figure 6.10, may

q,

include all the edge line points in the choice for extremities and corners included.

q ¼

The situation of the line labeled 3 is somewhat different: the choice 3 for the

p1 p1

point labeled is valid because stands between two zones where this choice

p4

is allowed. The same principle holds for point that moreover is never reached

Y. p2 p3

because of the above-mentioned limit condition on Points and appear

more critical as they are corner points in between border-lines with different choices.

p3 ¼ p2

3 is valid for standing inside a zone where this choice is allowed. has to be

q ¼ p1 ! p2)

excluded from the choice 3 (line because, with full coordinates, this

q [ q [ p2

point could fall either in the zone {1,2} or in the zone {2,3}; thus is

p2 ! p3.

given the value 2 of line

The complete selection process can be resumed in Table 6.5.

Comment 6.5 n-bit

For the sake of simplicity the truncated part of the exact

r(i)

remainder has been considered as coordinate references on Figure 6.10; four

bits of the shifted remainder are required by the SRT-4 division algorithm 6.9.

The carry-save techniques are applicable in this case, as far as a sufﬁcient quantity

c

s , representation of the remainder; moreover, the ﬂexibility

of bits are saved in the t t q-selection

in the choice of the quotient-digit set, as well as that of the strategy,

allows one to consider a number of alternatives in the algorithms. As in the SRT-2

case, the carry-save computation still has to be implemented in a way that prevents

0 0

c þ

s and from exceeding the (n 2)-bit length.

each of the carry-save components

Algorithm 6.9 SRT-4 Division

(/ Stands for integer division)

yt:=y/2**n-4

r(0):=X

i 0..p-1

for in loop

146 ARITHMETIC OPERATIONS: DIVISION

4.rt(i):=4.r(i)/2**n-2

4.rt(i)<-4 q(i+1):=-3; q _ pos(i+1):=0; q _ neg

if then

(i+1):=3; r(i+1):=4.r(i)+3*Y;

4.rt(i)<-3

elsif then

yt<6 q(i+1):=-3; q_pos(i+1):=0; q_neg(i+1):=3;

if then

r(i+1):=4.r(i)+3*Y; q(i+1):=-2; q _ pos(i+1):=0;

else

q _ neg(i+1):=2; r(i+1):=4.r(i)+2*Y; end if;

4.rt(i)<-2 q(i+1):=-2; q_ pos(i+1):=0;

elsif then

q_ neg(i+1):=2; r(i+1):=4.r(i)+2*Y;

4.rt(i)<-1 q(i+1):=-1; q_ pos(i+1):=0;

elsif then

q_ neg(i+1):=1; r(i+1):=4.r(i)+Y;

4.rt(i)<1 q(i+1):=0; q_ pos(i+1):=0;

elsif then

q_ neg(i+1):=0; r(i+1):=4.r(i);

4.rt(i)<2 q(i+1):=1; q_ pos(i+1):=1;

elsif then

q_ neg(i+1):=0; r(i+1):=4.r(i)-Y;

4.rt(i)<3 q(i+1):=2; q_ pos(i+1):=2;

elsif then

q_ neg(i+1):=0; r(i+1):=4.r(i)-2*Y;

4.rt(i)<4

elsif then

yt>=6 q(i+1):=2; q _pos(i+1):=2; q _neg(i+1):=0;

if then

r(i+1):=4.r(i)-2*Y; q(i+1):=3; q _pos(i+1):=0;

else

q_ neg(i+1):=3; r(i+1):=4.r(i)+3*Y; end if;

q(i+1):=3; q _pos(i+1):=0; q _neg(i+1):=3;

else

r(i+1):=4.r(i)+3*Y; end if;

end loop;

q:=q_pos - q _neg;

r(p)>=0 R:=r(p); R:=r(p)+Y; q:=q-1;

If then else

X ¼ Y ¼

Let and be 2’s

Example 6.6 010110111100101100110 011010110

X/Y p ¼

complement numbers Compute with an accuracy of 6.

n22 n21

Y ,

X Y 2 , a ﬁrst preliminary

According to the conditions in [2Y, [, and 2

Y Y ¼ p ¼ þ ¼

scaling of is necessary: So, 6 12 18,

011010110000000000000.

n ¼ Y.

21, and two more bits will be necessary to express the multiples of

## Y

First compute the multiples of as follows

Y ¼ 00011010110000000000000

¼

2:Y 00110101100000000000000

¼

3:Y 01010000010000000000000

Y ¼ 11100101010000000000000

2:Y ¼ 11001010100000000000000

3:Y ¼ 10101111110000000000000

q-selection

For purposes one has to compute the integer division

Y=2 ¼

17 (decimal 6)

0110 147

6.2 INTEGERS nop

The step-by-step procedure is described as follows (sign-bits are bold face;

no operation; symbol/stands

stands for for integer division).

q-select

4.rt (i)

i

Step Remainder Computation (decimal) (decimal) q_pos q_neg

r(0) ¼

0 00010110111100101100110

¼ ¼

19

4.r(0) 4.r(0)/2 5 3 3 0

01011011110010110011000

23.Y ¼

5 4

10101111110000000000000

r(1) ¼ 00001011100010110011000 ¼

¼ 19 2 2 2 0

1 4.r(1) 4.r(1)/2

00101110001011001100000 [

22.Y ¼ 2 [2, 3[

11001010100000000000000

r(2) ¼ 11111000101011001100000 ¼ 22 21

¼ 19 0 1

2 4.r(2) 4.r(2)/2

11100010101100110000000 22 [ 21[

þ1.Y ¼ [22,

00011010110000000000000

r(3) ¼ 11111101011100110000000

¼ ¼ 21

19

3 4.r(3) 4.r(3)/2 0 0 0

11110101110011000000000

nop ¼ 21 [ [21, 1[

00000000000000000000000

r(4) ¼ 11110101110011000000000 ¼ 23 22

¼ 19 0 2

4 4.r(4) 4.r(4)/2

11010111001100000000000 23 [ 22[

þ2.Y ¼ [23,

00110101100000000000000

r(5) ¼ 00001100101100000000000 ¼

¼ 19 3 2 2 0

5 4.r(5) 4.r(5)/2

00110010110000000000000 [

22.Y ¼ 3 [3, 4[

11001010100000000000000 Y [

r(6) ¼ [6, 8[

11111101010000000000000 ¼ 21

¼ 19 0 0 0

6 4.r(6) 4.r(6)/2

11110101000000000000000 21 [

nop ¼ [21, 1[

00000000000000000000000

r(7) ¼ 11110101000000000000000 ¼ 23 22

¼ 19 0 2

7 4.r(7) 4.r(7)/2

11010100000000000000000 23 [ 22[

þ2.Y ¼ [23,

00110101100000000000000

r(8) ¼ 00001001100000000000000 ¼

¼ 19 2 2 2 0

8 4.r(8) 4.r(8)/2

00100110000000000000000 [

22.Y ¼ 2 [2, 3[

11001010100000000000000

r(9) ¼ 11110000100000000000000

r(9) , Y

As 0, has to be added to the ﬁnal remainder,

R ¼ 11110000100000000000000

þ 00011010110000000000000

¼ 00001011010000000000000,

and the quotient has to be reduced by one unit. This can be done by reducing

(8) to 1.

q _ pos

148 ARITHMETIC OPERATIONS: DIVISION

So the ﬁnal quotient is given by

Q ¼

Q pos Q neg,

that is,

Q ¼ ¼

(3, 2, 0, 0, 0, 2, 0, 0, 1) (0, 0, 1, 0, 2, 0, 0, 2, 0,) (3, 1, 2, 3, 2, 1, 3, 2, 1):

Q ¼

In binary, and the overall operation (accuracy 18 and

0110110111001111001,

pre-scaling 12) can be resumed as ¼

18 12

010110111100101100110:2 0110110111001111001:011010110:2

þ :

12

00001011010:2

Comment 6.6 Q ¼

As far as the ﬁnal computation time for may

Q pos Q neg

be considered negligible with respect to the whole process, it may not be necessary

to speed up the ﬁnal conversion process. The inﬂuence of the conversion on the

overall time will mainly depend on the required accuracy and the size of operands.

On the other hand, the ﬁnal correction on the last remainder, whenever negative,

may be processed in parallel with the conversion process. To minimize the

impact of the conversion time, several on-the-ﬂy conversion algorithms have been

proposed in the literature ([ERC1987], [ERC1992], [OBE1997]). Basically these

algorithms perform the conversion as the digits are produced. Prescaling the divisor

or both operands is another idea proposed in the literature ([ERC1983],

[SVO1963]); as far as an efﬁcient method can be used to make the divisor close

enough to 1, the quotient-digit selection depends on the dividend only. As time

has to be consumed for the scaling operation, the advantages are not clear in general

cases. The SRT-4 algorithm has been used in the early Pentium processors and was

Pentium bug;

the origin of the famous the error, due to ﬂaws in the look-up tables,

was not detected on the test bench because the probability of addressing the tables at

those incorrect entries was actually very weak ([EDE1997]).

6.2.8 Base-B Nonrestoring Division Algorithm

k

In bases other than 2 or 2 (high-radix), nonrestoring division algorithms have

received little attention in the literature. Nevertheless, for the sake of generality, a

more general approach on division algorithms with extended quotient-digit sets

will be developed in the following.

Deﬁnitions 6.1 fd g

1. A system of digits (weights) is said to be complete when it is able to rep-

i B; nonredundant

resent any number as a weighted sum of powers of the base a

## B B

quotient-digit system in base is deﬁned as a complete system of exactly

digits, that is, the minimum quantity of needed digit values to express the

149

6.2 INTEGERS f0, . . . B 2

quotient (or any number) as a base-B number. The set 1, , 1g is

most commonly used.

redundant

2. In a quotient-digit set, the number of allowed quotient-digits is

a a

B. f2 2 þ1, . . . . . .

greater than The most used sets are of the form , , 0, ,

a a a

2 g dB=2e,

1, with that is, a symmetric set of consecutive integers.

r r a

redundancy factor ¼ /(B 2

3. The is deﬁned as 1).

a ¼ dB=2e minimally redundant.

4. A quotient-digit set with is said to be

a r

¼ B 2 ¼ maximally redundant.

1 ( 1) is said to be

5. A quotient-digit set with a r

. B 2 . over-redundant.

6. A quotient-digit set with 1 ( 1) is said to be

The ﬁrst algorithm to be presented for base-B nonrestoring division uses a redun-

f0, . . . Bg.

dant quotient-digit set 1, 2, , A tentative quotient estimation is made from

the truncated operands-look-up tables (LUTs) can be used to speed up this phase –

then a possible correction has to be carried out in order to convert the obtained

quotient into a ﬁnal nonredundant representation. The following lemmas and

theorems justify the algorithm.

Let

Lemmas 6.2 r r . . . r r

B:R ¼ r , , , , , ,

nþ1 n n1 1 0 (6:42)

Y ¼ y y . . . y y

, , , ,

n1 n2 1 0

n B.R, Y,

be two base-B positive integers (B 3, 3). the shifted remainder, and the

divisor, comply with the following conditions:

B:R=Y , þ

(B 1), (6:43)

n1 n

B Y B 1:

Deﬁne moreover n3 n3

¼ bB:R=B c; Y ¼ bY=B c; q ¼ bB:R=Yc;

R t t (6:44)

q ¼ bR =Y c; q ¼ bR =(Y þ 1)c:

t t t t t

t

Lemma 6.2.1 B þ

q 1,

0 t (6:45)

q B,

0 t

Proof q q

The inequalities 0 and 0 , are trivial.

t t

150 ARITHMETIC OPERATIONS: DIVISION

Condition (6.43) can be written

B:R , þ

(B 1):Y;

as n3 n3

R :B B:R Y , þ

and (Y 1):B ,

t t

then n3 n3

R :B , þ þ ! R , þ þ

(B 1):(Y 1):B (B 1):(Y 1): (6:46)

t t t t

Y B 2 ! B þ , Y þ ,

B 2 3 1 1 or (B 1)/Y 1;

Deﬁnitions (6.44) imply that t t t

thus R =Y , þ þ ¼ þ þ þ

(B 1):(Y 1)=Y (B 1) (B 1)=Y ,

t t t t t

and q ¼ bR =Y c B þ 1,

t t t

which completes the proof of the ﬁrst inequality (6.45).

The second inequality (6.45) is deduced from (6.46), written as

n3 n3

R :B =(Y þ , þ ! R =(Y þ , þ

1):B (B 1) 1) (B 1):

t t t t

Then

q ¼ bR =(Y þ B,

1)c

t t

t

which completes the proof of the second inequality (6.45).

Lemma 6.2.2

q q :

q 1 (6:47)

t t

t /Y

Proof R may be written

The fundamental equation of division for t t

r r

¼ q :Y þ , Y

R with , (6:48)

t t t t

Equation (6.48) may be expressed in any of the following two forms:

r

R ¼ q :(Y þ þ q

1) , (6:49)

t t t t 151

6.2 INTEGERS

or r

R ¼ þ þ q þ þ

(q 1):(Y 1) (Y 1), (6:50)

t t t t t

r 2 q 0, (6.49) may be written

If t

r r r

R ¼ q :(Y þ þ q ¼ q ¼ q , þ

1) , with and (Y 1): (6:51)

t t t t t

t t

r 2 q , 0, and (6.50) is written

Otherwise t

r

R ¼ q :(Y þ þ q ¼ q

1) , with 1 and

t t t

t t (6:52)

r r

¼ q þ þ , þ

(Y 1) (Y 1):

t t t

r

q B þ B Y q ,Y

2

Observe that, as 1, and , then , and cannot be negative. The

t t t t

proof is now complete.

Lemma 6.2.3 q q :

q (6:53)

t

t

Proof The following inequality is straightforward

n3 n3 n3 n3

:B =(Y þ , B:R=Y þ 1):B =Y :B 1 ¼ B

R 3n

1):B (R , 1 ,

t t t t

or =(Y þ , B:R=Y þ 1)=Y

R 1) (R ,

t t t t

then

bR =(Y þ bB:R=Yc b(R þ 1)=Y c ! q q q :

1)c

t t t t t

t

Lemmas 6.2.2 and 6.2.3 may be merged and expressed in the following theorem.

Assuming (6.44),

Theorem 6.2 n3 n3

¼ bB:R=B c; Y ¼ bY=B c; q ¼ bB:R=Yc;

R t t

q ¼ bR =Y c; q ¼ bR =(Y þ 1)c,

t t t t t

t

152 ARITHMETIC OPERATIONS: DIVISION

then

q q q :

q 1 (6:54)

t t

t

R(i) Y ith

Let and be the remainder and the divisor, respectively, as

Theorem 6.3

deﬁned in (6.42). Deﬁnitions given in (6.44) hold. The initial dividend is denoted

B R(0).

. Deﬁne moreover

þ ¼ B:R(i) q :Y:

R(i þ ¼ B:R(i) q:Y; R (i 1) (6:55)

1) t

If B:R(i) , þ

0 (B 1):Y,

then

B:R þ , þ

0 (i 1) (B 1):Y: (6:56)

¼ q,

Proof q then

If t

þ ¼ R(i þ ¼ B:R(i) q:Y , Y ! B:R þ , þ

R (i 1) 1) (i 1) (B 1):Y:

Otherwise, using Theorem 6.2 (6.54)

q ¼ q q ¼ q ¼ q

and 1 1:

t t

t

This situation corresponds to the conditions of (6.52), where

r r

¼ R q :Y , q , B ! þ ¼ R þ q :Y , B Y =B:

(i) 1 1 (i) 1

t t t t t t t t

On the other hand, n3 n3 n3 n3

r

þ q :Y :B ¼ þ , Y :B =B

R(i þ , (i) 1):B ( 1):B

1) (R t t t t

Y=B: (6:57)

¼ q 21,

q

As t þ ¼ B:R(i)

R(i þ ¼ B:R(i) q:Y R (i 1) (q 1):Y,

1) and 153

6.2 INTEGERS

then

R þ ¼ R(i þ þ Y , þ

(i 1) 1) (B 1):Y=B,

which completes the proof.

The base-B nonrestoring division algorithm, presented below, applies to the

n-digit X Y

base-B numbers (dividend) and (divisor). A normalizing operation has

n21 n

Y B Y B 2

to set to comply with the second condition (6.43), namely, 1.

X ¼ B.R(0),

The ﬁrst condition (6.43), assuming is written

B:R(0)=Y , þ

(B 1) (6:58)

and is always true. Nevertheless, if ﬂoating-point representation standards are used,

X X , Y.

the dividend is set (shifted) to the greatest value such that The tentative

q þ

quotient-digits are computed by dividing the truncated remainder by (Y 1),

t t

that is, the truncated divisor augmented by 1. The redundant set of quotient-digits

## B]. B

is in the range [0, Whenever is selected a correction (þ1) has to be carried

out to the preceding quotient-digit, generating a possible carry propagation. This

on-the-ﬂy,

correction may be done or at the end of the process, as a conversion

Q Q1

step. The following algorithm generates two quotient-digit vectors and

þ B.Q1)

whose ﬁnal sum (Q is the actual quotient.

Algorithm 6.10 Nonrestoring Base-B Division Step

rt:=B*r/B**(n-3); yt:=y/B**(n-3);

qt:=rt/yt+1; qt1:=qt/B; qt0:=qt modB;

remainder:=B*r-qt*y;

## X Y X/Y

and are positive 5-digit decimal numbers Compute as

Example 6.7 p ¼ X , Y,

45598/45522 with an accuracy of 7. To ensure that a preliminary nor-

¼

malization procedure sets the operands to 045598 and 455220, respectively (n 6,

p ¼ þ ¼

7 1 8). qt1 qt0

Step # Remainder Computation

i ¼ Y þ1 ¼ B.R(0) ¼ ¼

0 456, 045598, (B.R(0)) 045,

t t

¼ ¼

q (0) 045/456 0, 0 0

t ¼ 2 ¼

Remainder 45598 0 45598

¼

i ¼ B.R(1) ¼ 455,

1 455980, (B.R(1)) t

¼ ¼

q (1) 455/456 0, 0 0

t ¼ ¼

Remainder 455980 – 0 455980

154 ARITHMETIC OPERATIONS: DIVISION

qt1 qt0

Step # Remainder Computation

i ¼ B.R(2) ¼ ¼

2 4559800, (B.R(2)) 4559,

t

q ¼ ¼

(2) 4559/456 9, 0 9

t ¼ 2 ¼

Remainder 4559800 4096980 462820

¼

i ¼ B.R(3) ¼ 4628,

3 4628200, (B.R(3)) t

¼ ¼

q (3) 4628/456 10, 1 0

t ¼ ¼

Remainder 4628200 – 4552200 76000

¼

i ¼ B.R(4) ¼ 760,

4 760000, (B.R(4)) t

¼ ¼

q (4) 760/456 1, 0 1

t ¼ 2 ¼

Remainder 760000 455220 304780

¼

i ¼ B.R(5) ¼ 3047,

5 3047800, (B.R(5)) t

¼ ¼

q (5) 3047/456 6, 0 6

t ¼ 2 ¼

Remainder 3047800 2731320 316480

¼

i ¼ B.R(6) ¼ 3164,

6 3164800, (B.R(6)) t

¼ ¼

q (6) 3164/456 6, 0 6

t ¼ 2 ¼

Remainder 3164800 2731320 433480

¼

i ¼ B.R(7) ¼ 4334,

7 4334800, (B.R(7)) t

¼ ¼

q (7) 4334/456 9, 0 9

t ¼ 2 ¼

Remainder 4334800 4096980 237820

¼

i ¼ B.R(8) ¼ 2378,

8 2378200, (B.R(8)) t

¼ ¼

q (8) 2378/456 5, 0 5

t ¼ 2 ¼

Remainder 2378200 2276100 102100

The ﬁnal correction is expressed as

þ ¼

009016695 001000000 010016695,

and the overall operation can be resumed as

¼ þ

8

45598:10 455220:10016695 102100:

The decimal system has been used without ambiguity to represent the accuracy in

8

the multiplicative factor 10 .

The minimum redundancy of the quotient-digit set allows an easy correction

i 2 q(i) B.

procedure: adding 1 at level 1 whenever a quotient-digit reaches The

remainder computation is aided by the fact that the remainder is always positive.

The tentative quotient-digits may be extracted from look-up tables, or computed

k-digit

by a speciﬁc circuit. Instead of computing the remainder as a full subtraction,

155

## 6.3 CONVERGENCE (FUNCTIONAL ITERATION) ALGORITHMS

a carry-save technique may be applied to store the remainder as the signed sum of

k-digit

two numbers.

## 6.3 CONVERGENCE (FUNCTIONAL ITERATION) ALGORITHMS

6.3.1 Introduction

Functional iteration algorithms represent division as a function. Numerical calculus

techniques are used to solve, for example, Newton –Raphson equations or Taylor–

MacLaurin expansions. These methods provide better than linear convergence, but

the step complexity is somewhat more important than the one involved in digit

recurrence algorithms. Since functional iteration algorithms use multiplication as

a basic operation, the step complexity will mainly depend on the performance of

the multiplication resources at hand ([FER1967], [FLY1970]). In practice, whenever

the division process is integrated in a general-purpose arithmetic processor, one of

the advantages comes from the availability of multiplication without additional

hardware cost. It has been reported ([OBE1994], [FLY1997]) that, in typical ﬂoat-

ing-point applications, sharing multipliers does not signiﬁcantly affect the overall

d

performances of the arithmetic unit. In what follows, the divisor is assumed to

d ,

be a positive and normalized number such that, for example, 1/B 1. In

most practical binary applications, IEEE normalization standards are used:

d ,

1 2.

6.3.2 Newton – Raphson Iteration Technique

Coming back to the general division equation written

D ¼ d:Q þ r, (6:59)

¼

the theoretical exact quotient (r 0) may be written

Q ¼ D=d ¼ D:(1=d): x

Actually, the Newton – Raphson method ﬁrst computes the reciprocal of the divisor

d, D.

with the required precision, and then the result is multiplied by the dividend

x ¼

Using 1/d as a root target, the priming function

f ¼ d,

(x) 1=x (6:60)

f ¼ f ¼

may be considered for root extraction {xj (x) 0}. To solve (x) 0, the following

x:

equation is iteratively used to evaluate 0

x ¼ x f (x )=f (x ), (6:61)

iþ1 i i i

0

f f x

where (x ) stands for (df/dx) , that is, the ﬁrst derivative of (x) at point .

i i i

156 ARITHMETIC OPERATIONS: DIVISION

f x

( ) f x

( )

i f x

( )

i+1 f x

( )

i+2

α x

x

x x i+2

i i+1

Newton – Raphson convergence graph.

Figure 6.11

The ﬁrst steps of (6.61) are depicted in Figure 6.11. Equality (6.61) is readily

inferred from 0 a

¼ ¼ f x

f (x ) tan ( ) (x )=(x ),

i i i iþ1

x x-axis f x ¼ x

that points to as the intersection of the with the tangent to (x) at .

iþ1 i

Function (6.60) is continuous and derivable in the area (close enough to the root)

B, d

where the successive approximations are processed. In base assuming in

x B]

[1/B, 1[, any value of in ]1, could be chosen as a ﬁrst approximation. To

speed up the convergence process, a look-up table (LUT) is generally used for a

x

ﬁrst approximation. Let (=0) be this ﬁrst approximation; then

0 ¼ d,

f ) 1=x

(x

0 0

½df =dx ¼ 1=x :

2

0

0

Using (6.61): ¼ x :(2 d:x

x ),

1 0 0

and recursively x ¼ x :(2 d:x ),

2 1 1

... (6:62)

x ¼ x :(2 d:x ):

iþ1 i i 157

## 6.3 CONVERGENCE (FUNCTIONAL ITERATION) ALGORITHMS

d

In base 2, if is initially set to d ,

1 1, (6:63)

2

then ,

1 1=d 2:

, x

x within the range 1 2 will ensure a quadratic convergence.

Selecting 0 0

¼ þ 1 1

x 1/d , error can be evaluated, according to

Actually, assuming i i iþ1

(6.62), as þ 1 ¼ þ 1 d:(1=d þ 1

1=d (1=d ):(2 )), (6:64)

iþ1 i i

ending at j1 j ¼ d:1 2 , (6:65)

iþ1 i

which means that, at each step, the number of relevant digits is multiplied by two. If

t-digit p

x is drawn from a precision LUT, the minimum precision will be reached

0 p=te

k ¼ dlog steps.

after 2

Algorithm 6.11 Reciprocal Computation

x(0):=LUT (d);

i 0..k-1

for in loop

x(i+1):=x(i)*(2-d*x(i));

end loop;

Q:=x(k); d ¼

Let 0.1618280 (base 10) and compute 1/d with precision 32.

Example 6.8

Assume a look-up table with 4-digit precision.

x ¼ 6:179(LUT),

0 ¼ ¼

d:x 0:1618280 6:179 0:99993521,

0

x ¼ ¼

6:179 (2 0:99993521) 6:1794003,

1

d:x ¼ ¼

0:1618280 6:1794003 0:9999999917484,

1 ¼ ¼

x 6:1794003 (2 0:9999999917484) 6:179400350989939,

2

d:x ¼ ¼

0:1618280 6:179400350989939 0:999999999999999848492,

2

x ¼

6:179400350989939 (2 0:999999999999999848492),

3 31

¼ + :

6:1794003509899399362285883777837 10

In Example 6.8, one can observe that the error is always by default (as

.

2 dx 1). This is in agreement with the monotony of the convergence

2 i

(Figure 6.11). Nevertheless, the practical convergence could appear different

according to the way the rounding on the last digit is made: by excess or by default.

158 ARITHMETIC OPERATIONS: DIVISION

Two dependent multiplications and one subtraction are needed at each computation

## B

step. In base the subtraction can be substituted by (i) setting the integer part to 1

2

and (ii) a digitwise (B 1)’s complement operation then (iii) adding 1 at the last

digit level; the add-1 operation can be skipped to speed up the process at the cost

of a slower convergence rate. In base 2 the subtraction is actually a 2’s complement

operation, which in turn can be replaced by a bitwise complementation at the cost of

a slower convergence rate. Example 6.9 illustrates the algorithm in base 2.

d ¼

Let 0.1010111 in base 2; compute 1/d with precision 32.

Example 6.9

Assume a look-up table with 4-digit (rounded) precision.

x ¼ 1:100 (LUT);

0

d:x ¼ ¼

0:1010111 1:100 1:0000010100;

0 ¼

d:x 0:1111101100;

2 0

x ¼ ¼

1:100 0:1111101100 1:0111100 8 bits rounded;

1

d:x ¼ ¼

0:1010111 1:0111100 0:11111111100100;

1 d:x ¼

2 1:00000000011100;

1

x ¼ ¼

1:0111100 1:00000000011100 1:011110001010010

2 16 bits rounded;

d:x ¼ ¼

0:1010111 1:011110001010010 0:1111111111111111011110;

2 ¼

d:x 1:0000000000000000100010;

2 2

x ¼

1:011110001010010 1:0000000000000000100010

3 31

¼ +

1:0111100010100100110010000001011 2

The same example is now treated replacing the 2’s complement operation by

bitwise complementation.

x ¼ 1:100 (LUT),

0

d:x ¼ ¼

0:1010111 1:100 1:0000010100;

0 d:x ¼

not 0:1111101011;

0

x ¼ ¼

1:100 0:1111101011 1:0111100 8 bits rounded;

1 ¼ ¼

d:x 0:1010111 1:0111100 0:11111111100100;

1 d:x ¼

not 1:00000000011011;

1

¼

x 1:0111100 1:00000000011011

2 ¼

1:011110001001111 16 bits rounded;

¼ ¼

d:x 0:1010111 1:011110001001111 0:1111111111111011011001;

2 ¼

d:x 1:0000000000000100100110;

not 2

x ¼

1:011110001001111 1:0000000000000100100110

3 20

¼ + other next bits irrelevant

1:01111000101001001100 2 159

## 6.3 CONVERGENCE (FUNCTIONAL ITERATION) ALGORITHMS

6.3.3 MacLaurin Expansion—Goldschmidt’s Algorithm

Taylor expansions are well-known mathematical tools for the numerical calculus

of functions. The MacLaurin series, as the Taylor expansion of the function

þ x), Goldschmidt’s algorithm

1/(1 is the base of a division algorithm called

([GOL1964]). p

Consider the classical Taylor expansion at point

i i i

f ¼ S ½(x p) =i!:(d f

(x) (x)=dx ) , (6:66)

x¼p

0im

applied to the function f ¼ þ x):

(x) 1=(1 (6:67)

x ¼

At the point 0, the following MacLaurin expansion is established:

þ x) ¼ x þ x x þ x x þ

2 3 4 5

1=(1 1 , (6:68)

or equivalently,

þ x) ¼ x):(1 þ x þ x þ x þ x

2 4 8 16

1=(1 (1 ):(1 ):(1 ):(1 ) , (6:69)

This is the foundation of Goldschmidt’s algorithm. As the Newton – Raphson

method does, the series expansion technique consists of computing the reciprocal

d. þ x) x ¼

1/d of the divisor The series 1/(1 at 0 is easier to compute than 1/x

x ¼ D/d Q/1,

at 1. The problem at hand is now to let converge toward which is

readily achieved through successive multiplications by the factors of MacLaurin

expression (6.69).

For this purpose, the following is assumed:

x ¼ d 1,

d ,

1=B 1 (base-B normalized form): (6:70)

The procedure is then carried out as follows:

Q ¼ D=d ¼ D:½1=(1 þ x) þ x þ x þ x

¼ D:(1 x):(1 þ x 2 4 8 16

):(1 ):(1 ):(1 ) , (6:71)

160 ARITHMETIC OPERATIONS: DIVISION

d(0) D(0) d D

and are set to and respectively; a look-up table evaluation procedure

could reﬁne this ﬁrst approximation to get a better convergence rate. Then

d(1) ¼ d(0):(1 x) ¼ d(0):(2 d(0)),

D(1) ¼ D(0):(2 d(0)),

d(2) ¼ d(1):(1 þ x ¼ d(1):(2 d(1)),

2 )

D(2) ¼ D(1):(2 d(1)), (6:72)

d(i) ¼ d(i d(i

1):(2 1)),

D(i) ¼ D(i d(i

1):(2 1)):

Algorithm 6.12 Goldschmidt’s Algorithm

d(0):=LUT (divisor); DD(0):=LUT (dividend);

i 1..k-1

for in loop

d(i):=d(i-1)*(2-d(i-1));

DD(i):=DD(i-1)*(2-d(i-1));

end loop;

Q:=DD(k-1);

Comments 6.7

1. Each step is made up of one subtraction (2’s complement in base 2) and two

multiplications.

i, d(i) d(i) ¼ 2 x exp(i)

2. At step the computed value of is given by 1 where

i d , 2 B)/B x ,

¼ ; since (6.70) 1/B 1, then (1 0, as initial pre-

exp(i) 2

d D

scaling of and is carried out, a fast quadratic convergence is ensured: each

þ x exp(i) ) will duplicate the precision.

correction factor (1

3. The complexity of the Newton– Raphson method is similar to the one

involved in Goldschmidt’s algorithm. Nevertheless, Goldschmidt’s method

directly provides the quotient while Newton – Raphson computes the

reciprocal ﬁrst, then a further multiplication is needed. Moreover, in

Newton– Raphson, the two multiplications are dependent so they have to

be processed sequentially while parallelism is allowed in Goldschmidt’s

method.

4. Newton Raphson’s convergence method is self-correcting in the sense that

any error (i.e., taking bitwise complement instead of 2’s complement) is cor-

rected in the following steps. On the contrary any error in Goldschmidt’s

algorithm will never be corrected. The ﬁnal result will be the quotient

D(j)/d(j), j

of assuming step is the step where the last errors have been

committed.

5. Goldschmidt’s algorithm has been used in the IBM System/360 model 91

([AND1967]) and, more recently, in the IBM System/390 ([SCHW1999])

161

6.4 BIBLIOGRAPHY

and AMD-K7 microprocessor ([OBE1999]). Another algorithm based on

series expansions has been implemented in IBM’s Power3 ([SCHM1999]).

6. Goldschmidt’s algorithm is suited for combinatorial implementations; factors

of (6.72) can be calculated in parallel.

D ¼ d ¼ D/d

Let 0.152525, 0.161828 (base 10); compute with

Example 6.10

precision 32.

A look-up table with four-digit precision would approximate 1/d at 6.179, so

d D d(0) D(0)

multiplying both and by this value allows presetting and while

reducing the number of steps:

D(0) ¼ ¼

0:152525 6:179 0:942451975,

d(0) ¼ ¼

0:161828 6:179 0:999935212,

d(1) ¼ ¼

0:999935212 (2 0:999935212) 0:999999995802515056,

D(1) ¼ ¼

0:942451975 (2 0:999935212) 0:9425130345785563

d(2) ¼

0:999999995802515056 (2 0:999999995802515056)

¼ 0:99999999999999998238112014489332,

D(2) ¼

0:9425130345785563 (2 0:999999995802515056)

¼ 0:94251303853474057216724145450635

d(3) ¼ 0:99999999999999998238112014489332

(2 0:99999999999999998238112014489332)

¼ 0:99999999999999999999999999999999,

D(3) ¼ 0:94251303853474057216724145450635

(2 0:99999999999999998238112014489332)

¼ 0:94251303853474058877326544232143

Comment 6.8 An important question about convergence algorithms is the evalu-

ation of the exact (i.e., minimum) amount of bits necessary for any prescaling or

intermediate calculations, to ensure a correct result within the desired precision.

Actually, the accurate calculus of rounding errors (LUT data as well as outputs

from intermediate operations) is not a straightforward matter. This mathematical

problem has been treated extensively in the literature ([DAS1995], [COR1999],

[EVE2003]). Using extrabits is a safe and easy way to ensure correctness;

nevertheless, a careful error computation can lead to signiﬁcant savings

([EVE2003]).

6.4 BIBLIOGRAPHY

[AND1967] S. F. Anderson, J. G. Earle, R. E. Goldschmidt, and D. M. Powers, The

IBM J. Res. Dev.,

IBM System/360 Model 91: Floating-point execution unit. (Jan.):

11

34 – 53 (1967).

162 ARITHMETIC OPERATIONS: DIVISION

[ATK1968] D. E. Atkins, Higher-radix division using estimates of the divisor and partial

IEEE Trans. Comput.,

remainders. 925 – 934 (1968).

C-17(10):

[BRI1993] W. S. Briggs and D.W. Matula, A 17 69 bit multiply and add unit with redun-

Proceedings of the 11th IEEE Sym-

dant binary feedback and single cycle latency. In:

posium on Computer Arithmetic, July 1993, pp. 163 – 170.

[COR1999] M. A. Cornea-Hasegan, R. A. Golliver, and P. Markstein, Correctness proofs out-

line for Newton –Raphson based ﬂoating-point divide and square root algorithms: In:

Proceedings of the 14th IEEE Symposium on Computer Arithmetic, 1999, pp. 96 – 105.

[DAS1995] D. DasSarma and D. W. Matula, Faithfull bipartite ROM reciprocal tables. In

Proceedings of the 12th IEEE Symposium Computer Arithmetic, 1995 pp. 17 – 28.

SIAM Rev.

[EDE1997] A. Edelman, The mathematics of the Pentium division bug. 39(1):

54 – 67 (1997).

[ERC1983] M. D. Ercegovac, A higher radix division with simple selection of quotient

Proceedings of the 6th IEEE Symposium Computer Arithmetic,

digits. In July 1983,

pp. 94 – 98.

[ERC1987] M. D. Ercegovac and T. Lang, On-the-ﬂy conversion of redundant into

IEEE Trans. Comput.,

conventional representations. 895 – 897 (1987).

36(7):

[ERC1990] M. D. Ercegovac and T. Lang, Simple radix-4 division with operands scaling.

IEEE Trans. Comput., 1204– 1207 (1990).

39(9): IEEE Trans. Comput.

[ERC1992] M. D. Ercegovac and T. Lang, On-the-ﬂy rounding. 41,

(12):1497– 1503 (1992). Digital Arithmetic,

[ERC2004] M. D. Ercegovac and T. Lang, Morgan Kaufmann, San

Francisco, CA, 2004.

[EVE2003] G. Even, P.-M. Seidel, and W. E. Ferguson, A parametric error analysis of Gold-

Proceedings of the 16th IEEE Symposium on Computer

schmidt’s division algorithm. In:

Arithmetic, June 2003.

[FAN1989] J. Fandrianto, Algorithm for high-speed shared radix-8 division and radix-8

Proceedings of the Ninth IEEE Symposium on Computer Arithmetic,

square root. In

July 1989, pp. 68– 75. IEEE Trans. Comput.,

[FER 1967] D. Ferrari, A division method using a parallel multiplier.

224 – 226, (April 1967).

16: IEEE Trans. Comput.

[FLY1970] M. J. Flynn, On division by functional iteration. (August

19:

1970). Modern Research in Computer Arithmetic,

[FLY1997] M. J. Flynn, Stanford Architecture and

Arithmetic Group, Stanford Technical Report, 1997.

Advanced Computer Arithmetic Design,

[FLY2001] M. J. Flynn and S. F. Oberman, Wiley-

Interscience, Hoboken, NJ, 2001. IRE Proc.

[FRE1961] C. V. Freiman, Statistical analysis of certain binary division algorithms.

91 – 103 (1961).

49: Applications of Division by Convergence.

[GOL1964] R. Goldschmidt, Master’s thesis, MIT,

June 1964.

[MON1994] P. Montuschi and L. Ciminiera, Radix-8 division with over-redundant digit set.

J. VLSI Signal Processing (3): 259– 270 (1994).

7

[NAD1956] M. Nadler, A high speed electronic arithmetic unit for automatic computing

Acta Tech. (Prague),

machines. 464– 478 (1956).

6: 163

6.4 BIBLIOGRAPHY

[OBE1994] S. F. Oberman and M. J. Flynn, Design issues in ﬂoating point division. Technical

Report No. CSL-TR-94-647, Computer Systems Laboratory, Stanford University, Decem-

ber 1994.

[OBE1995] S. F. Oberman and M. J. Flynn, An analysis of division algorithms and implemen-

tations. Stanford Technical Report CSL-TR-95-675, July 1995. IEEE

[OBE1997] S. F. Oberman and M. J. Flynn, Division algorithms and implementations.

Trans. Comput. 833– 854 (Aug. 1997).

46:

[OBE1999] S. F. Oberman, Floating point division and square root algorithms and implemen-

Proceedings of the 14th IEEE Symposium on

tation in the AMD-K7 microprocessor. In:

Computer Arithmetic, April 1999, pp. 106– 115.

Computer Arithmetic, Algorithms and Hardware Designs,

[PAR1999] B. Parhami, Oxford

University Press, New York, 1999.

[QUA1992] N. Quach and M. Flynn, A radix-64 ﬂoating-point divider. Technical Report

CSL-TR-92 – 529, Computer Systems Laboratory, Stanford University, June 1992.

IRE Trans. Electron.

[ROB1958] J. E. Robertson, A new class of digital division methods.

Comput., 218– 222 (Sept. 1958).

EC-7:

[SCHM1999] M. S. Schmookler, R. C. Agarwal, and F. G. Gustavson, Series approximation

Proceedings of the

methods, for divide and square root in the Power3 Microprocessor. In:

14th IEEE Symposium Computer Arithmetic, April 1999, pp. 116 – 123.

[SCHW1999] E. M. Schwarz, R.M. Smith, and C. A. Krygowski, The S/390 G5 ﬂoating point

Proceedings of the 14th IEEE Symposium

unit supporting hex and binary architectures. In:

on Computer Arithmetic, April 1999, pp. 258 – 265.

[SRI1995] H. Srinivas and K. Parhi, A fast radix-4 division algorithm and its architecture.

IEEE Trans. Comput. 826–831 (1995).

44(6):

[SUT2004] G. Sutter, G. Bioul, and J.-P. Deschamps, Comparative study of SRT-dividers

Lecture Notes in Computer Science,

in FPGA. In: Vol. 3203, Springer-Verlag, 2004,

pp. 209 – 220. Stroje na Zpracovani Informaci,

[SVO1963] A. Svoboda, An algorithm for division. 25– 34

9:

(1963).

[SWE1957] J. Cocke and D. W. Sweeney, High speed arithmetic in a parallel device. IBM

Technical Report, February 1957.

[TAY1985] G. S. Taylor, Radix 16 SRT dividers with overlapped quotient selection stages.

Proceedings of the Seventh IEEE Symposium on Computer Arithmetic,

In: June 1985,

pp. 64 – 71.

[TOC1958] T. D. Tocher, Techniques of multiplication and division for automatic binary

Q. J. Mech. Appl. Math.,

computer. 364– 384 (1958).

2:

7

## OTHER ARITHMETIC OPERATIONS

This chapter is devoted to arithmetic functions and operations other than the

four basic ones. Number representation systems conversion procedures are ﬁrst

analyzed; they play a prominent role in arithmetic processes since a variety of

algorithms are designed for a wide-ranging number of systems and/or bases

(radices). Further on, this chapter reviews classical methods for approximating log-

arithmic, exponential, and trigonometric functions. Polynomial approximation,

Taylor– MacLaurin series, and convergence methods are described with a special

attention to CORDIC algorithms and their applications to trigonometric functions.

Square rooting algorithms founded on digit recurrence and convergence methods

are ﬁnally surveyed.

A common feature of a number of modern algorithm implementations is

the increased use of look-up tables (LUTs), a practice fully compatible with the

evolution of the ROM technology toward larger size and lower cost. The main

consequence of LUT-based techniques is then a low-cost speed-up of the overall

procedures.

7.1 BASE CONVERSION

Given the representation of a number in a speciﬁc system, conversion operations

consist of ﬁnding the representation of this number in another system. Arithmetic

Synthesis of Arithmetic Circuits: FPGA, ASIC, and Embedded Systems

By Jean-Pierre Deschamps, Géry J. A. Bioul, and Gustavo D. Sutter

#

Copyright 2006 John Wiley & Sons, Inc. 165

166 OTHER ARITHMETIC OPERATIONS

algorithms deal with a diversity of systems such as base-B for naturals or signed

systems for integers; the most common signed systems are sign-magnitude,

B’s complement, excess-k, or signed-digit systems such as Booth coding. Finally,

special attention is paid to ﬂoating-point representations for computer applications.

Redundant systems are also important in arithmetic operations, among others in

multiplication (Booth algorithm, Chapter 5) or division (SRT, Chapter 6). Booth

coding and redundant base-B coding are generally related to speciﬁc algorithms;

the conversion techniques are therefore developed in the sections devoted to

the respective algorithms. Floating-point conversion is reviewed in Chapter 16.

The most classic problem to deal with is the base conversion for base-B unsigned rep-

B , ﬁnd its corresponding

resentations: given a number by its representation in base 1

B .

representation in base 2 x

(source system) representation of a natural number be given by

Let the base-B

1 n1 n2

:B þ x :B þ þ x :B

x ¼ x 01 , (7:1)

n1 n2 0

1 1

## B.

weighted sum of powers of The problem at hand is to compute the base-B 2

x

(target system) representation of

m1 m2

:B þ y :B þ þ y :B :

x ¼ y 02 (7:2)

m1 m2 0

2 2

A ﬁrst simple solution consists of computing (7.1) in the target system. Expression

(7.1) may be written under the Hörner form (Chapter 5) as

x ¼ þ x þ x þ þ x

(( (0:B ):B ):B ):B , (7:3)

n1 n2

1 1 1 1 0

B . The following algorithm generates the value

then iteratively computed in base 2 x . . . x

x from its base-B representation (x , , , ):

of in base-B n-1 n-2

2 1 0

2 2

acc:=0; i 0..n-1 acc:=acc*B1+x(n 1 i);

for in loop

x:=acc;

end loop; x,

In order to generate the base-B representation of the preceding algorithm is

2

B acc.B þ x

executed in base . The base-B representation of is generated

n212i

2 2 1

by the following set of integer divisions:

acc :B þ x ¼ B :q þ r ,

n1i

0 1 2 1 0

:B þ q ¼ B :q þ r

acc ,

1 1 1 2 2 1

acc :B þ q ¼ B :q þ r ,

2 1 2 2 3 2

...

acc :B þ q ¼ B :q þ r :

m1 m1 m m1

1 2

acc i acc B

where is the digit of expressed in base .

i 2 167

7.1 BASE CONVERSION

From the preceding system the following equality is deduced:

m m1 m2

acc:B þ x ¼ B :q þ r :B þ r :B þ þ r :B :

02

n1i m m1 m2

1 0

2 2 2

n m

B q ¼

m B . In the last equality 0.

Choose the value of in such a way that m

1 2 þ x B

acc:=acc.B in base :

The following algorithm executes the assignation n212i

1 2

q:=x(n-1-i);

j 0..m-1 q:=(acc(j)*B1+q)/B2;

for in loop

acc(j):=(acc(j)*B1+q) mod B2; end loop;

The complete conversion algorithm is then given as follows:

Algorithm 7.1 Base Conversion Algorithm

– acc:=0;

j 0..m-1 acc(j):=0;

for in loop end loop;

i 0..n-1

for in loop

--acc:=acc*B1+x(n-1-i);

q:=x(n-1-i);

j 0..m-1 q:=(acc(j)*B1+q)/B2;

for in loop

acc(j):=(acc(j)*B1+q) mod B2; end loop;

end loop;

--x:=acc;

j 0..m-1 x(j):=acc(j);

for in loop end loop;

The basic computation primitive calculates

þ þ

¼ þ q)=B acc j) ¼ þ q) B

q (acc(j):B , ( (acc(j):B mod (7:4)

1 2 1 2

where þ þ

[ . . . B acc(j), acc [ . . . B

q, q {0, 1, , 1}, (j) {0, 1, , 1}:

1 2

Examples 7.1

1. Compute the hexadecimal representation of the decimal number (9128) .

10

, , m ¼

3 4 4

10 16 , one selects 4.

Observing that 16

2. Compute the decimal representation of the hexadecimal number (23A8) .

16

, , m ¼ m ¼

4 4 5

16 10 , one should select 5; actually, 4

Observing that 10 m ¼

is sufﬁcient for this particular case so 4 is selected to reduce the

number of useful steps.

168 OTHER ARITHMETIC OPERATIONS

Problem 2 is the back-conversion of the result of problem 1; both examples are

presented in parallel columns of the following table.

! !

(9128) base 16 (23A8) base 10

10 16

acc ¼ ¼

(0 0 0 0) acc (0 0 0 0)

þ ¼ þ þ ¼ þ

0.10 16.0 9 0.16 10.0 2

9 2

þ ¼ þ þ ¼ þ

0.10 0 16.0 0 0.16 0 10.0 0

acc ¼ acc ¼

(0 0 0 9) (0 0 0 2)

þ ¼ þ þ ¼ þ

9.10 16.5 11 2.16 10.3 5

1 3

þ ¼ þ þ ¼ þ

0.10 5 16.0 5 0.16 3 10.0 3

þ ¼ þ þ ¼ þ

0.10 0 16.0 0 0.16 0 10.0 0

acc ¼ acc ¼

(0 0 5 11) (0 0 3 5)

þ ¼ þ þ ¼ þ

11.10 16.7 0 5.16 10.9 0

2 10

þ ¼ þ þ ¼ þ

5.10 7 16.3 9 3.16 9 10.5 7

þ ¼ þ þ ¼ þ

0.10 3 16.0 3 0.16 5 10.0 5

þ ¼ þ þ ¼ þ

0.10 0 16.0 0 0.16 0 10.0 0

acc ¼ acc ¼

(0 3 9 0) (0 5 7 0)

þ ¼ þ þ ¼ þ

0.10 16.0 8 0.16 10.0 8

8 8

þ ¼ þ þ ¼ þ

9.10 0 16.5 10 7.16 0 10.11 2

þ ¼ þ þ ¼ þ

3.10 5 16.2 3 5.16 11 10.9 1

þ ¼ þ þ ¼ þ

0.10 2 16.0 2 0.16 9 10.0 9

acc ¼ acc ¼

(2 3 10 8) (9 1 2 8)

¼ ¼

(9128) (23A8) (23A8) (9128)

10 16 16 10

Decimal-to-binary and binary-to-decimal conversions may readily be handled by

Algorithm 7.1. According to the prominent role of those particular cases in digital

system implementations, special attention has been given to them in the literature.

Basically three methods are more commonly described for decimal-to-binary con-

## R

version. The ﬁrst one consists of subtracting from the successive remainders the

p R. p

greatest power of 2 (2 ) inferior or equal to Successive exponents identify

i

the bit-1 positions of the desired binary expression. Step of a nonrestoring version

(n212i) according to the sign of the preced-

of this algorithm would add or subtract 2

(n212i) to a negative remainder and conversely. The second

ing remainder: adding 2

method consists of computing the remainders (parity) of the integer division by 2 of

the successive decimal quotients, starting from the decimal number to be converted.

The third method is the adaptation of Algorithm 7.1. At the implementation level,

the complexity would depend on the form decimal digits are provided. Decimal

169

7.1 BASE CONVERSION

numbers are generally assumed represented in BCD form (binary coded decimal,

Chapter 3). Then algorithms are implemented as a sequence of binary operations.

A close look at Example 7.2(3) shows that the ﬁrst step of the third method

(Algorithm 7.2) actually sets the leftmost decimal digit to BCD; so this step can

be skipped if the decimal number to convert is already BCD coded. In this algorithm,

the successive steps iteratively perform the multiplication of the intermediate result

acc) ¼

(stored in by the base (10) (1010) , and then add the next (binary coded)

10 2 N

decimal digit. Let us point out moreover that multiplying a number by (1010) 2

þ þ

can readily be implemented as 2.N 8.N or 2.(N 4.N), that is, a simple

þ m n

1-shifted(N 2-shiftedN) operation. Assuming and to be the number of

digits of the decimal and binary representations, respectively, the time complexity

n m.

(in number of steps) of the ﬁrst two methods is while the third one is The

step complexity has to be evaluated in the context of the implementation resources.

The BCD-to-binary conversion algorithm can be stated as follows.

Algorithm 7.2 BCD-to-Binary Conversion Algorithm

– acc:=(bcd , bcd ,..bcd );

m21 m22 0

j 0..m-1 acc(j):=bcd(j);

for in loop end loop;

bin(m-1):=acc(m-1); * *

100) 10

j 1..m-1 bin(m-1-j):=(bin(m-j)+bin(m-j)

for in loop

+acc(m-j-1); end loop;

x:=bin(0); Convert in binary (922)

Examples 7.2 10

p

1. Nonrestoring 2 subtracting. ¼ ! x ¼

9 410 1

922 2 9

¼ ! x ¼

8

410 2 154 1

8

¼ ! x ¼

7 26 1

154 2 7

¼ 38 ! x ¼

6

26 2 0

6

¼ 6 ! x ¼

38 þ 5 0

2 5

6 þ ¼ ! x ¼

4

2 10 1

4

¼ ! x ¼

3

10 2 2 1

3

¼ 2 ! x ¼

2 0

2 2 2

2 þ ¼ ! x ¼

1

2 0 1

1

¼ 1 ! x ¼

0

0 2 0

0

¼ (1110011010)

(922) 10 2

170 OTHER ARITHMETIC OPERATIONS

2. Division-by-2. ¼ þ

922=2 461 0

¼ þ

461=2 230 1

¼ þ

230=2 115 0

¼ þ

115=2 57 1

¼ þ

57=2 28 1

¼ þ

28=2 14 0

¼ þ

14=2 7 0

¼ þ

7=2 3 1

¼ þ

3=2 1 1

¼ þ

1=2 0 1

¼

(922) (1110011010)

10 2

3. Algorithm 7.2.

acc ¼ m ¼

1001, 0010, 0010; 3;

bin(2) ¼ 1001

Step 1

bin(1) ¼ þ þ ¼

(1001 100100).10 0010 1011100

Step 2

bin(0) ¼ þ þ ¼

(1011100 101110000).10 0010 (1110011010) 2

¼

(922) (1110011010)

10 2

The most intuitive method to achieve a binary-to-decimal conversion is to com-

pute the decimal representations of the relevant powers of 2, and then add them

i

x

S .2 .

together. This corresponds to the decimal computation of the expression i i

An algorithm consisting of computing the remainders of successive divisions by

ten doesn’t appear easy to implement. Algorithm 7.1 looks the most attractive as

it still reduces to a straight application of the Hörner expansion (7.3). Assuming

that a BCD representation is desired for the converted result, the binary-to-BCD

algorithmic step performs the multiplication of the intermediate result (stored in

acc) ¼ , and then adds the next bit. For this purpose, deﬁne a

by the base 2 (10) 2

procedure to multiply a BCD number by 2 (BCDx2 procedure). Note that a shifted

x x x

, , , ) (1001) can be coded in BCD

(multiplied by 2) 4-bit binary number (x 3 2 1 0

y(1) y(1) y(1) y(0) y(0) y(0) y(0)

, , , ; , , , ), applying the following

form (y(1) 3 2 1 0 3 2 1 0

simple rules:

¼ y(1) ¼ y(1) ¼ y(0) ¼

y(1) 0, (7:5)

3 2 1 0

y(1) ¼ x _ :(x _ x

(x ), (7:6)

3 2 1 0

0 y(0) y(0) y(0) ¼ ½(x x x þ y(1) y(1)

, , , ) , , , 0) (0, , , 0) mod 16: (7:7)

(y(0) 2 1 0

3 2 1 0 0 0

y(i)

operation on a single BCD digit leaves at 0; on a full BCD

Observe that the x2 0

y(i)

number will assume the value of the carry (7.6) coming from the right neighbor

0 171

7.1 BASE CONVERSION

digit; this carry will not propagate further. This feature allows a parallel digit pro-

procedure.

cessing in the BCDx2

i i þ

Steps and 1 of the procedure are deﬁned according to (7.6) to (7.7):

BCDx_step

Step i

y(i þ ¼ x(i) _ _ x(i) þ

1) : (x(i) .(x(i) )), carry to step (i 1)

0 3 2 1 0

y(i): ¼ x(i) x(i) y(i) þ y(i þ y(i þ

(x(i) , , , ) (0, 1) , 1) , 0)] mod 16.

2 1 0 0 0 0

Step i þ 1 ¼ x(i þ _ þ þ _ x(i þ þ

y(i þ : 1) (x(i 1) .(x(i 1) 1) )), carry to step (i 2),

2) 0 3 2 1 0

y(i þ ¼ þ x(i þ x(i þ y(i þ

1): (x(i 1) , 1) , 1) , 1) )

2 1 0 0

y(i þ

þ y(i þ , 2) , 0)] mod 16.

(0, 2)

0 0

As quoted above, all the steps may be executed in parallel. The above equations

deﬁne the procedure computing the BCD expression of its BCD

BCDx2_step

input multiplied by two:

BCDx2_step (bcd(i): bcd; 2bcd (i): bcd)

procedure in out

Algorithm 7.3 describes the binary-to-BCD version of Algorithm 7.1.

Algorithm 7.3 Binary-to-BCD Conversion Algorithm

--acc:=(bin , bin ,.., bin );

n-1 n-2 0

j 0..n-1 acc(j):=bin(j);

for in loop end loop;

bcd(n-1):=acc(n-1);

j 1..n-1

for in loop

BCDx2_step (bcd(n-j));

procedure

bcd(n-1-j):=2bcd(n-j)+acc(n-j-1); end loop;

x:=bcd(0); n ¼

Convert in BCD (1011101011101001) ; 16

Example 7.3 2

acc ¼ 1011101011101001

Initial step

bcd ¼

(15) 0001

Step 1 ¼

2bcd(15) 0010

bcd(14) ¼ þ ¼

0010 0 0010

Step 2 ¼

2bcd(14) 0100

bcd(13) ¼ þ ¼

0100 1 0101

Step 3 ¼

2bcd(13) 1 0000

bcd(12) ¼ þ ¼

1 0000 1 1 0001

172 OTHER ARITHMETIC OPERATIONS

Step 4 ¼

2bcd(12) 10 0010

bcd(11) ¼ þ ¼

10 0010 1 10 0011

Step 5 ¼

2bcd(11) 100 0110

bcd(10) ¼ þ ¼

100 0110 0 100 0110

Step 6 ¼

2bcd(10) 1001 0010

bcd(9) ¼ þ ¼

1001 0010 1 1001 0011

Step 7 ¼

2bcd(9) 1 1000 0110

bcd(8) ¼ þ ¼

1 1000 0110 0 1 1000 0110

Step 8 ¼

2bcd(8) 11 0111 0010

bcd(7) ¼ þ ¼

11 0111 0010 1 11 0111 0011

Step 9 ¼

2bcd(7) 111 0100 0110

bcd(6) ¼ þ ¼

111 0100 0110 1 111 0100 0111

Step 10 ¼

2bcd(6) 1 0100 1001 0100

bcd(5) ¼ þ ¼

1 0100 1001 0100 1 1 0100 1001 0101

Step 11 ¼

2bcd(5) 10 1001 1001 0000

bcd(4) ¼ þ ¼

10 1001 1001 0000 0 10 1001 1001 0000

Step 12 ¼

2bcd(4) 101 1001 1000 0000

bcd(3) ¼ þ ¼

101 1001 1000 0000 1 101 1001 1000 0001

Step 13 ¼

2bcd(3) 1 0001 1001 0110 0010

bcd(2) ¼ þ ¼

1 0001 1001 0110 0010 0 1 0001 1001 0110 0010

Step 14 ¼

2bcd(2) 10 0011 1001 0010 0100

bcd(1) ¼ þ ¼

10 0011 1001 0010 0100 0 10 0011 1001 0010 0100

Step 15 ¼

2bcd(1) 100 0111 1000 0100 1000

bcd(0) ¼ þ ¼

100 0111 1000 0100 1000 1 100 0111 1000 0100 1001

¼ ¼

(100 0111 1000 0100 1001) (47849)

(1011101011101001) 2 BCD 10

Comments 7.1 n n-bit

Algorithm 7.3 needs steps to convert an binary number

into BCD. This ﬁgure can go down by processing 2-bit, 3-bit, or 4-bit slices per

step, that is, handling the binary vector as a base-4, octal, or hexadecimal

number, respectively. Nevertheless, the step complexity will be signiﬁcantly

increased because the adding part is no longer carry-propagation free, as it is in

the binary case: adding one unit to an even number (2bcd) never generates a carry.

173

## 7.2 RESIDUE NUMBER SYSTEM CONVERSION

## 7.2 RESIDUE NUMBER SYSTEM CONVERSION

7.2.1 Introduction

The residue number system (RNS) is not widely used in practice; nevertheless, in

some speciﬁc classes of algorithms, the RNS can provide an important speed-up

by replacing operations on large numbers by parallel processing on small size

operands ([GAR1959], [SZA1967]). A residue number system is deﬁned as a

s fm g moduli,

system of natural numbers called the greatest of which is generally

i g,

m. RNS representation N, fm is

denoted The of a given integer with respect to i s

r, residues modulo-m , are the

the vector the components of which, called i

N/m . Residues are denoted

successive remainders of the integer divisions i

¼ jNjm :

r (7:8)

i i g M,

fm is the range, denoted of the

The least common multiple (lcm) of a system i

related RNS system, that is, the quantity of different integers that can be represented

g

fm consists of pairwise prime moduli, the associ-

in the system. Whenever a system i

nonredundant.

ated RNS is said to be Selection of moduli, as small as possible, maxi-

mizes the speed of RNS arithmetic operations, as operand sizes never exceed the size

m.

of greatest modulus The most important drawbacks of the RNS are the complexity

of overﬂow detection and the conversion process. Whenever those operations are not

critical in the overall complexity of the application at hand, the RNS is fully justiﬁed.

7.2.2 Base-B to RNS Conversion

The most intuitive method for base-B to RNS coding consists in performing the

m

successive mod reductions as shown in the following example (see Chapter 8

i

m).

for reduction mod g ¼ f31, N ¼ fr g

fm 17, 7, 5, 3g and (789) ; compute

Let

Example 7.4 i i

10

s ¼ 5;

r ¼ ¼

789 mod 3 0

1 ¼ ¼

r 789 mod 5 4

2

r ¼ ¼

789 mod 7 5

3 ¼ ¼

r 789 mod 17 7

4

r ¼ ¼

789 mod 31 14

5 ¼

(789) (14, 7, 5, 4, 0)

10 RNS

m B

A faster method consists in precomputing modulo the multiples of the suc-

i

B; n n B s m reductions are needed

cessive powers of for base-B digits, modulo i

## B

The RNS expressions of the relevant powers of are ﬁrst extracted from the look-up

m componentwise to achieve the RNS conversion.

table, and then added modulo i

Table 7.1 shows the precomputed residues related to the ﬁrst 5 powers of 10 with

f23,

the set of 5 moduli 17, 7, 5, 6g.

174 OTHER ARITHMETIC OPERATIONS

f23,

TABLE 7.1 RNS 17, 7, 5, 6g: Precomputed Residues for Multiples of the

First 5 Powers of 10

! ! !

Mod 23 17 7 5 6 Mod 23 17 7 5 6 Mod 23 17 7 5 6

a # a # a #

0 2 4

.10 .10 .10

i i i

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 10000 18 4 4 0 4

100 8 15 2 0 4

2 2 2 2 2 2 200 16 13 4 0 2 20000 13 8 1 0 2

3 3 3 3 3 3 300 1 11 6 0 0 30000 8 12 5 0 0

4 4 4 4 4 4 400 9 9 1 0 4 40000 3 16 2 0 4

5 5 5 5 0 5 500 17 7 3 0 2 50000 21 3 6 0 2

6 6 6 6 1 0 600 2 5 5 0 0 60000 16 7 3 0 0

7 7 7 0 2 1 700 10 3 0 0 4 70000 11 11 0 0 4

800 18 1 2 0 2

8 8 8 1 3 2 80000 6 15 4 0 2

9 9 9 2 4 3 900 3 16 4 0 0 90000 1 2 1 0 0

# a #

a 1 3

.10 .10

i i

0 0 0 0 0 0 0 0 0 0 0 0

10 10 10 3 0 4 1000 11 14 6 0 4

20 20 3 6 0 2 2000 22 11 5 0 2

30 7 13 2 0 0 3000 10 8 4 0 0

4000 21 5 3 0 4

40 17 6 5 0 4

50 4 16 1 0 2 5000 9 2 2 0 2

60 14 9 4 0 0 6000 20 16 1 0 0

70 1 2 0 0 4 7000 8 13 0 0 4

80 11 12 3 0 2 8000 19 10 6 0 2

90 21 5 6 0 0 9000 7 7 5 0 0

fm g ¼ f23, N ¼ fr g.

Let 17, 7, 5, 6g and (81148) ; compute

Example 7.5 i i

10

s ¼ ¼

5 and the range is computed as 23.17.7.5.6 82110. From Table 7.1, the

following RNS expressions (boldface lighted in the table) are extracted:

¼

80000 (06, 15, 04, 00, 02) RNS

¼

1000 (11, 14, 06, 00, 04) RNS

¼

100 (08, 15, 02, 00, 04) RNS

¼

40 (17, 06, 05, 00, 04) RNS

¼

8 (08, 08, 01, 03, 02) RNS 175

## 7.2 RESIDUE NUMBER SYSTEM CONVERSION

f23,

TABLE 7.2 RNS 17, 7, 5, 6g: Precomputed Residues for the First 22 Powers of 2

i ! i !

Mod 23 17 7 5 6 Mod 23 17 7 5 6

i i

# #

2

2

0 1 1 1 1 1 1 11 2048 1 8 4 3 2

1 2 2 2 2 2 2 12 4096 2 16 1 1 4

2 4 4 4 4 4 4 13 8192 4 15 2 2 2

3 8 8 8 1 3 2 14 16384 8 13 4 4 4

4 16 16 16 2 1 4 15 32768 16 9 1 3 2

5 32 9 15 4 2 2 16 65536 9 1 2 1 4

6 64 18 13 1 4 4 17 131072 18 2 4 2 2

7 128 13 9 2 3 2 18 262144 13 4 1 4 4

8 256 3 1 4 1 4 19 524288 3 8 2 3 2

9 512 6 2 1 2 2 20 1048576 6 16 4 1 4

10 1024 12 4 2 4 4 21 2097152 12 15 1 2 2

The component wise sums are computed as

¼ þ þ þ þ ¼

r (02 04 04 04 02) mod 6 4

1 ¼ þ þ þ þ ¼

r (00 00 00 00 03) mod 5 3

2

r ¼ þ þ þ þ ¼

(04 06 02 05 01) mod 7 4

3 ¼ þ þ þ þ ¼

r (15 14 15 06 08) mod 17 7

4

r ¼ þ þ þ þ ¼

(06 11 08 17 08) mod 23 4

5 ¼

(81148) (4, 7, 4, 3, 4)

10 RNS

B ¼

For 2, the table is simpliﬁed because just one residue expression is needed

per power of 2. Table 7.2 shows the residues of powers of 2 in the same RNS

used for Table 7.1. The complexity of Table 7.2 is lower than that of Table 7.1.

This is paid for by an increased average quantity of power-of-2 residues to be

added during the second phase of the conversion process. Observe that both

Tables 7.1 and 7.2 are easily reduced thanks to the cyclic feature of the remainder

sequences. Those tables are thus readily compacted. It is worth pointing out that a

careful choice of moduli can lower the computational complexity for a number of

applications ([GAR 1959], [SZA 1967], [SOD1986]).

fm g ¼ f23, N ¼

Let 17, 7, 5, 6g and (10011110011111100) ;

Example 7.6 i 2

fr g.

compute i

176 OTHER ARITHMETIC OPERATIONS

s ¼ ¼

5 and the range is computed as 23.17.7.5.6 82110. From Table 7.2, the

following RNS expressions are extracted

¼

16 (09, 01, 02, 01, 04)

2 ¼

13

2 (04, 15, 02, 02, 02)

¼

12

2 (02, 16, 01, 01, 04)

¼

11

2 (01, 08, 04, 03, 02)

¼

10

2 (12, 04, 02, 04, 04)

¼

7 (13, 09, 02, 03, 02)

2 ¼

6

2 (18, 13, 01, 04, 04)

¼

5 (09, 15, 04, 02, 02)

2 ¼

4

2 (16, 16, 02, 01, 04)

¼

3

2 (08, 08, 01, 03, 02)

¼

2

2 (04, 04, 04, 04, 04)

The componentwise sums are computed as

r ¼ þ þ þ þ þ þ þ þ þ þ ¼

(04 02 04 02 04 02 04 02 04 02 04) mod 6 4

1

r ¼ þ þ þ þ þ þ þ þ þ þ ¼

(01 02 01 03 04 03 04 02 01 03 04) mod 5 3

2

r ¼ þ þ þ þ þ þ þ þ þ þ ¼

(02 02 01 04 02 02 01 04 02 01 04) mod 7 4

3 ¼ þ þ þ þ þ þ þ þ þ þ ¼

r (01 15 16 08 04 09 13 15 16 08 04) mod 17 7

4

r ¼ þ þ þ þ þ þ þ þ þ þ ¼

(09 04 02 01 12 13 18 09 16 08 04) mod 23 4

5 ¼ (4, 7, 4, 3, 4)

(10011110011111100) 2 RNS

n-digit fx g N s-moduli fm g

Algorithm 7.4 converts an base-B number into an

i j

R. n-input n.s-output

RNS representation One assumes given a LUT to store the

i

B n s r x .B i [ f0, . . . n 2 j [ f1, . . . sg,

possible residues (i) of ; 1, , 1g, 2, ,

j i

[ f0, . . . B 2 r

x 1, , 1g. Let be the RNS component (i) of

LUT(x(i), i, j)

i j

i

## .B N

x . The RNS expression of is then computed as the RNS sum

i

r(1) þ r(2) þ þ r(s).

. . .

Algorithm 7.4 Base-B to RNS Conversion

i 0..n-1

for in loop

j 1..s

for in loop

(i,j):=LUT(x(i), i, j);

acc end loop;

end loop;

j 1..s

for in loop

R:=0;

i 0..n-1

for in loop 177

## 7.2 RESIDUE NUMBER SYSTEM CONVERSION

R:=(R + acc(i,j)) mod m(j); end loop;

R(j):=R;

end loop;

7.2.3 RNS to Base-B Conversion

Theorem 7.1 Chinese Remainder Theorem (CRT)

The Chinese remainder theorem (7.9), presented in Chapter 2 (Properties 2.4), is the

fm g

key theorem for some RNS decoding algorithms. It can be stated as follows. Let i

g

s fr is the RNS representation of an

be a nonredundant system of moduli, while i a m)

N stands for mod

integer in this system. Then (jaj m

mod

jNj ¼ S m jr =m j , (7:9)

i

M m M

i i

1is mod

i

where

M ¼ P m m ¼ M=m :

;

i i

i

1is

The following method for RNS to base-B conversion is a straightforward

application of (7.9). As in the preceding case, some precomputations are assumed,

g f(1/m m g. B,

fm and ) mod The algorithm consists of computing, in base

namely, i i i

m M s

s .(1/m )] mod , and one mod sum of 2-operand products. Algor-

products [r i i i

m

ithms for mod operations are detailed in Chapter 8. Algorithms 7.5 and 7.6 con-

vert a RNS expression into a base-B one. Deﬁne the following procedures for

M, fm g, f(1/m m g:

computing and ) mod and

M_procedure star_procedure.

i i i

Algorithms 7.5

Algorithm 7.5.1

M_procedure (s:in natural; m:in data_vector(1..s);

procedure

M:out natural)

is m: data_vector(1..s);

begin

M:=1;

i 1..s M:=M*m(i);

for in loop end loop;

Algorithm 7.5.2

star_procedure (s:in natural; M:in data; m:in

procedure

data_vector(1..s); mstar:out data_vector(1..s);

invmstar:out data_vector(1..s)

is m: data_vector(1..s); M: data

begin i 1..s mstar(i):=M/m(i);

for in loop

invmstar(i):=(m(i)/M)mod m(i); end loop;

star_procedure;

end

178 OTHER ARITHMETIC OPERATIONS

Algorithm 7.6 CRT RNS to Base-B Conversion

## N

Let (r ) be the RNS representation of a number in a system deﬁned by the moduli

i

fm g. After calling and , the base-B representation

M_procedure star_procedure

i

## N

of is computed according to (7.9) as follows:

i 1..s

for in loop

acc:=0; acc:=acc+(mstar(i)*((r(i)*invmstar(i)) mod m(i)))

mod M; end loop;

N:=acc;

Another method to convert the RNS to base-B consists in converting the RNS into an

mixed-radix unsigned digit system

intermediate representation system called a

mixed numeration system.

([HUA1983]) or a According to Chapter 3, a mixed

numeration system is a weighted positional integer representation system that can

be deﬁned by n n 2

1. a set of positions or ranks numbered from 1 to 0 when written from left

to right; . . . b b g,

fb , , , called radices, in one-to-one

2. a set of natural numbers n21 1 0

correspondence with the ranks;

fB B . . . B B g,

3. a set of natural numbers , , , , called weights, computed as

n n21 1 0

B ¼ 1

0

B ¼ b :B

1 0 0

...

¼ b :B ¼ P b :

B n n1 n1 i

0in1

X, 2 X

It can be shown that any integer such that (B 1) 0, has a unique

n

representation ; . . . ; x ; x

x n1 1 0

such that :x þ þ B :x þ B :x x b

X ¼ B , 0 1 (7:10)

n1 n1 i i

1 1 0 0

It is straightforward to note that the range of a mixed-radix system is the same as

g

fb as moduli. The RNS to mixed-radix

the one of a RNS system with radix set i

conversion algorithm consists of an iterative extraction of the remainders modulo

b (substep 1), subtracting this remainder from the RNS expression (substep 2)

i b

and dividing the new RNS expression by (substep 3). The sequence of remainders

i

is the desired mixed-radix representation. This procedure is resumed in Algorithm

b(i) i, r( j) j

7.7, where stands for the modulus stands for the RNS component of

N, mr(k) k.

number and stands for the mixed-radix component 179

## 7.2 RESIDUE NUMBER SYSTEM CONVERSION

Algorithm 7.7 Mixed-Radix Procedure

Digit_extraction_step

--Step 1

j 1..s

for in loop

acc(j):=r(j); end loop;

--substep1

mr(0):=acc(1);

--substep2

j 1..s

for in loop

r1(j):=(acc(1))mod b(j); acc(j):=(acc(j)-r1(j))mod b(j);

end loop;

--substep3

j 2..s

for in loop

acc(j):=(acc(j)/b(1))mod b(j);

...

--step i

--substep1

mr(i-1):=acc(i);

--substep2

j i..s

for in loop

ri(j):=(acc(i))mod b(j); acc(j):=(acc(j)-ri(j))mod b(j);

end loop;

--substep3

j i+1..s

for in loop

acc(j):=(acc(j)/b(i))mod b(j); b b

In order to speed-up the division procedure, the inverses of modulo are

i j=i

precomputed; doing so, divisions are replaced by multiplications. The process is

explained through the following simple numeric example.

f7,

Let 3, 5g be the set of moduli deﬁning a source RNS and (6, 2, 3) be

Example 7.7

## N

a number expressed in this system. The corresponding target mixed-radix system is

f7, f105,

deﬁned by the set of radices 3, 5g leading to the set of weights 15, 5, 1g. The

range is thus [0, 104], equal to that of the source RNS. Observe that the moduli (resp.

radices) do not need to be ordered by size, but the same order has to be respected for

both systems. Strictly needed precomputed inverses are

¼ j1=5j ¼ j1=5j ¼

j1=3j 5; 2; 3:

7 3 7 N:

Substep 1 of step 1 extracts as the remainder modulo 5 of the ﬁrst rightmost mixed-

3

## N

radix digit of is thus 3; substep 2 consists of subtracting 3, expressed in the RNS as

## N

(3, 0, 3), from ¼

(6, 2, 3) (3, 0, 3) (3, 2, 0),

and substep 3 consists of dividing by 5, ¼ j2:2j ¼

¼ , 2:j1=5j , 0) (j3:3j , , 0) (2, 1, 0):

(3, 2, 0)=5 (3:j1=5j 7 3 7 3

180 OTHER ARITHMETIC OPERATIONS

f7,

The problem is now reduced to the moduli 3g and the number (2, 1). The next step 2

is described as follows.

Step 2:

Substep 1: is the second mixed radix digit as the remainder modulo 3;

1 2 ¼

Substep 2: (2, 1) (1, 1) (1, 0);

¼ ¼ ¼

Substep 3: (1, 0)/3 (1.j1/3j , 0) (1.5, 0) (5, 0).

7

Step 3: Extract as the last mixed-radix digit.

5 . Using the above computed

The desired mixed-radix representation is (5, 1, 3) M-R

weights, the base-B expression can readily be computed; for example, in base 10

:

N ¼ þ þ ¼

5:15 1:5 3 (83) 10

Observe that the inverse of the leftmost modulus is not needed while the other

inverses are computed modulo the left side moduli only.

Comment 7.2 Garner’s algorithm ([GAR1959]), for RNS to base-B conversion

(Chapter 2), actually computes the mixed-radix digits within a preliminary step of a

procedure computing the base-B digits through a mixed-radix to base-B conversion.

## 7.3 LOGARITHMIC, EXPONENTIAL, AND TRIGONOMETRIC

## FUNCTIONS

Most often the computation of functions such as logarithms and exponential or

trigonometric functions are made through software-implemented algorithms applied

to ﬂoating-point representations. Hardware or microprogrammed systems are

mainly justiﬁed for special-purpose computing devices such as ASIC or embedded

systems. As it is generally not possible to get an exact result, approximation methods

have to be used together with error estimation techniques. Newton –Raphson,

Taylor– MacLaurin series, or polynomial approximations are the most common

approaches to compute these functions. For trigonometric functions, CORDIC

(linear convergence) algorithms are well suited. Arguments included in the range

[1, 2[ (ﬂoating-point IEEE standard) are suitable for most approximation methods

that need to limit the range of the argument. Whenever a speciﬁc range is imposed

on the operands, a prescaling operation may be necessary: so an initial step may be

included in the algorithmic procedure. Crucial questions for approximation methods

are error estimation and effective rounding techniques; these problems start from

table design (ﬁrst approximation LUT) up to the ﬁnal result. Numerous methods,

algorithms, and implementations are proposed in the literature ([SPE1965],

[CHE1972], [ERC1977], [COD1980], [KOS1991], [MAR1990], [TAN1991],

[ERC1995], [PAL2000], [CAO2001]). As for the basic operations, the choice will

depend on the speed/cost compromises and other constraints imposed on the

designer. Approximation methods usually assume the availability of the four

181

## 7.3 LOGARITHMIC, EXPONENTIAL, AND TRIGONOMETRIC FUNCTIONS

basic operations as arithmetic primitives at hand, together with look-up tables for a

ﬁrst “reasonably good” approximation to start from.

7.3.1 Taylor – MacLaurin Series

Taylor expansion a)

The classical (at point formula can be written

i i i

a) =i!):d f þ R :

S ((x (x)=dx ) (7:11)

n

x¼a

0in x,

When this expansion converges over a certain range of that is, when

R ¼

lim 0, (7:12)

n

n!1

expansion Taylor series; a ¼ MacLaurin

the is referred to as the if 0, it is called the

series (Chapter 2).

Consider the convergence behavior of the exponential functions expressed by the

x ¼

following series at 0:

x i

e ¼ S =i!) þ R

(x , (7:13)

n

0in

x i

¼ S =i!) þ R

e ((x) , (7:14)

n

0in

x x: i

a ¼ e ¼ S =i!) þ R :

ln (a) ((x: ln (a)) (7:15)

n

0in

The above expressions can be factorized (Hörner scheme) to suggest a simple

n ¼

iterative computational step; for example, (7.13) for 8 can be written

þ þ þ þ þ þ

(( (x=8 1):x=7 1):x=6 1):x=5 1):x=4 1):x=3 1): (7:16)

x=2 þ þ þ R :

1):x 1 8

Formula (7.16) can be computed in 8 divide-and-add steps with an error inferior to

þ /9! . R x 1 x ¼

9

(1 0.1).x , that is, for 0 1/2, an error (maximum for 1/2)

8

given by 9

1 , =9! , :

9

(1:1):(1=2) 6:10 (7:17)

max x ¼

Actually, (7.16), computed at 1/2, yields (rounded down) 1.6487212650; ten-

e 1/2 yields (rounded up) 1.6487212708, that is,

decimal precision computation of

an error inferior to the value computed at (7.17). Obviously, the convergence is

x

faster as becomes smaller, so a prescaling operation could be necessary if the

number of algorithmic steps is ﬁxed. Expression (7.14) can be factorized (Hörner

scheme) as

þ þ þ

(( (x=8 1):x=7 1):x=6 1):x=5 1):x=4 1):x=3 1): (7:18)

x=2 þ þ R :

1):x 1 8

Formula (7.18) is computed in the same way as (7.16), but because of the alternating

x /9!.

1 9

is somewhat smaller, actually

signs, the absolute value of the error max

182 OTHER ARITHMETIC OPERATIONS

Expression (7.15) is quite similar to (7.14) excepted for the prescaling: the range of

x.ln x.

(a) has to be considered instead of just that of

Logarithmic function series are also easy to compute, in particular, ln (x) can be

written iþ1 i

:(x =i þ R , x

¼ S ( ) 1) , 0 2 (7:19)

ln (x) n

1in

n ¼

or, for 10 (Hörner scheme),

x)=10 þ

(( ((1 1=9):(x 1) 1=8):(x 1)

þ þ þ R :

1=7):(x 1) 1=2):(x 1) 1):(x 1) (7:20)

8

A close analysis of (7.19) shows that in the range [1, 2] the convergence rate can be

x ¼ 1

very slow; typically, for 2, the error is expressed as

max

j , þ

j1 1=2:(n 1): (7:21)

max n

If the range is restricted to [0, 1/2], the maximum error after steps will be

n

j , :(n þ

j1 1=2 1), (7:22)

max

24

x ¼ after 10 steps.

that is, for 1/2, less than 10

Prescaling the argument of logarithmic functions, together with the

corresponding result adjustment, does not involve a signiﬁcant increase in the

k,

overall complexity. Actually, if the argument has been initially divided by a

þln

correction term (k) has to be added to adjust the ﬁnal result. For exponential

functions the general prescaling problem is generally not straightforward. The par-

## N N

ticular case of the function 2 , with expressed in ﬂoating point, can be processed

k N; m k

m.2 be the ﬂoating-point representation of is the mantissa and

as follows. Let N

the exponent. The scaling process consists of reducing as

k k

bm:2 c ¼ m

Ns ¼ m:2 0:m (7:23)

(kþ1) (kþ2)

k c,

E ¼ bm.2 one can write

So, assuming N NsþE Ns E

¼ ¼ :2

2 2

2

Ns N E

where 2 is the mantissa of 2 and the exponent. So the scaling process reduces

N Ns

to that of 2 .

the computation of 2

Trigonometric functions are also readily expressed as series, in particular,

i

¼ S :(x) =(2:i þ þ R 1 , x 1

2:iþ1

sin (x) () 1)! , (7:24)

n

0in

n ¼

or, for 6 (Hörner scheme)

=156 =110 þ =72 =42 þ =20 =6

2 2 2 2 2 2

(( (x 1):x 1):x 1):x 1):x 1)x

þ þ R :

1):x (7:25)

8

To get a fast convergence rate, scaling is also required in this case. Since trigono-

metric functions are periodic, the scaling can be a straight reduction of the argument

p p

/2, þ /2], using trigonometric equivalence

to, for example, the range [2 183

## 7.3 LOGARITHMIC, EXPONENTIAL, AND TRIGONOMETRIC FUNCTIONS

relations; this can thus be processed without calling for any postcorrection to the

p p

/2, þ /2] 1

holds, the convergence rate is fair:

ﬁnal result. If the range [2 max

p

¼ /2)

(forjxj can be expressed as p

j1 j , =2) =(2:n þ

2:nþ3

( 3)!: (7:26)

max

29

n ¼ j1 j ,

That is, for 6, 10 .

max

7.3.2 Polynomial Approximation P(x)

Basically, polynomial approximation methods consist of building a polynomial of

n, n

degree that matches the target function at points. The principle is general for con-

tinuous functions. Taylor–MacLaurin series are particular cases. The choice of the

matching points is not ﬁxed, neither is the degree of the polynomial nor the quantity

of them (e.g., several polynomials may approximate the same function in separate

ranges). A number of methods and algorithms have been proposed in the literature.

Moreover, prescaling is often recommended to speed-up the convergence. A classical

numerical calculus technique is used to compute the coefﬁcients of the approximation

fx g

polynomial, once the degree and the desired matching points are selected; it basi-

k

g,

nþ1 fc of the form

cally consists of resolving equations, linear in i

¼ f

P(x ) (x ), (7:27)

k k

i

¼ Sc f

P(x ) .x and (x ) are, respectively, the degree-n polynomial and the func-

where k i k k x . Several alternative methods exist to obtain

tion to approximate, both computed at k

these coefﬁcients without actually solving (7.27). The polynomial coefﬁcients are

computed once, then stored; the way the polynomial is then computed for a given

value of the argument deﬁnes the algorithm computing the approximated function at

this argument. A Hörner-type factorization is a ﬁrst approach to deﬁne an iterative

computation step.

Comment 7.3 Hörner Scheme Revisited In Sections 7.3.1 and 7.3.2, the Hörner

scheme was mentioned as a possible iterative way to implement algorithms.

Whenever pipelined or parallel arithmetic operators are available, this approach

could be signiﬁcantly improved; for instance, if one can take advantage of multiple

multiply-and-add and squaring units, a useful generalized Hörner scheme may be

deﬁned as follows.

Let P(x) ¼ þ c þ c þ þ c þ c

(( (x:c ):x ):x ):x (7:28)

k1 k2 k3 1 0

P(x).

be the Hörner expansion of the polynomial

Let ¼ x:c þ c :

C 2 (x) (7:29)

i 2iþ1 2i

184 OTHER ARITHMETIC OPERATIONS

An extended Hörner expression can be written

P(x) ¼ þ C þ C þ þ C þ C

2 2 2 2 2 2 2 2 2

(( (C (x):x (x)):x (x)):x ):x (x):

k1 k2 k3 1 0 (7:30)

Assuming available operators computing (7.29) and squaring units, the number of

steps is roughly divided by two. Expressions (7.29) and (7.30) can be generalized

further; assuming

m m1

C ¼ x :c þ þ x:c þ c

(x) miþm1 miþ1 mi

i

¼ :x þ c þ þ c þ c

( (c ):x ):x , (7:31)

miþm1 miþm2 miþ1 mi

one can write m m m m m m

P(x) ¼ þ C þ C

(( (C (x):x (x)):x (x)):x

k1 k2 k3

m m m

þ þ C þ C

):x (x), (7:32)

1 0

generalized Hörner expansion

called the (GHE), which takes advantage of higher

level primitive polynomial operators. Expansion (7.32) corresponds to a polynomial

k.m 2

of degree 1. Therefore, thanks to the availability of parallel and/or pipelined

operators, multiple level Hörner computation schemes can be built to speed-up

the overall process. For instance, a degree-63 polynomial could be computed in 9

multiply-and-add steps if a 3-level scheme is used. First, 16 degree-3 polynomials

can be computed (3 steps); four degree-15 polynomials are then worked out using

the degree-3 polynomials as primitives (3 steps), and another 3 steps are ﬁnally

needed to compute the degree-63 polynomial using the degree-16 ones as primitives.

A practical implementation of this circuit is shown in Chapter 14.

7.3.3 Logarithm and Exponential Functions Approximation

by Convergence Methods

Convergence methods consist of two parallel processes on two related sequences;

normalization)

typically, one sequence converges to 1 (multiplicative or 0 (additive

normalization) while the other one converges to the function to approximate. Division

using Goldschmidt’s algorithm is an example of multiplicative normalization: while

the divisor sequence converges to 1, the dividend converges to the desired quotient.

7.3.3.1 Logarithm Function Approximation by Multiplicative Normalization

Deﬁne i

c(i) ¼ þ a :2 a [

1 , {1, 0, 1} (7:33)

i i

multiplicative normalizing function, a is selected in such a way that the

as the where i

sequence n

x(i þ ¼ x(i):c(i) x(i) [ B(2

1) (auxiliary sequence), ) (7:34)

185

## 7.3 LOGARITHMIC, EXPONENTIAL, AND TRIGONOMETRIC FUNCTIONS

converges toward 1. Then the sequence

y(i þ ¼ y(i) c(i)

1) ln (7:35)

y(0) x(0)

can be set to converge toward the result ln (x). If and are, respectively, set to

x, x(p) ﬃ

0 and to the argument and assuming 1, one can write

x( p) ¼ x:P c(i) ﬃ ! ﬃ P c(i);

1 1=x

i i

y( p) ¼ y S c(i) ¼ P c(i) ¼

ln ln ln (x): (7:36)

i i x

To make the convergence of (7.34) possible, the argument needs to be in a range

such that

P c(i) x: P c(i)

x: 1 and max lim 1

min lim 1ip 1ip

p!1 p!1

that is, . .

i i

x P x P þ

1 lim (1 2 ) and 1 lim (1 2 ),

1ip 1ip

p!1 p!1

x

that is, 0:42 3:45: (7:37)

x

This means that the argument could need to be prescaled to fall in the range (7.37).

x

An argument in the range [1, 2[ (such as, e.g., a ﬂoating-point mantissa) ﬁts per-

0

x x

fectly; otherwise use a straightforward prescaling operation that replaces by such

0 s 0 0

x ¼ x .2 (x in [1, 2[). The algorithm computes ln (x ), then a ﬁnal additive cor-

that s.ln

rection of (2) is completed. Observe that the lower bound of (7.37) can be low-

þ 0 ) can be accepted as a ﬁrst normalizing factor for computing

ered to 0.21, as (1 2

x(1).

In practical implementations of this algorithm, look-up tables are used to read out

2i

+ y(i þ x

the successive values of ln (1 2 ), needed to compute 1) of (7.35). For in

a can be selected according to the following rules:

[1/2, 2[, i ¼

a 0, (7:38)

0 x(i) . a ¼ x i

if 1, (i), 1, (7:39)

i i

x(i) , a ¼ þx i

if 1, (i):not(x (i)), 1: (7:40)

i i i1

The above rules are justiﬁed by the following two lemmas, also showing that the

p p

convergence rate reaches precision after steps (linear convergence).

Let

Lemma 7.1 k k n

x(k) ¼ þ þ 1, 1 k n,

1 2 0 2 2 , (7:41)

186 OTHER ARITHMETIC OPERATIONS

n-bit k;

be the auxiliary sequence vector at step then

2k k k

x(k):(1 , þ :

2 ) 1 2 (7:42)

1 2

Proof 1 ¼

The left inequality is trivial, it corresponds to 0. The right inequality is

2k 2k 2n

1 2

x(k).(1 2 ) for maximum, 2 2 .

deduced from the computation of 2 x(k þ

The practical interpretation of (7.42) is the impact of rule (7.39) on 1) when-

x(k) 2

ever is greater than one with a fractional part made up of a (k 1)-zero string

k. x(k þ

and a one at position Then 1) will be either greater than one, exhibiting a

þ ¼

similar pattern with at least one zero more, or inferior to one (x (k 1) 0) with

0

at least 2k ones as the header of the fractional part. In both cases, the target value

x(p) ¼ x(k þ

1 is approximated by 1) with at least one bit more.

Let

Lemma 7.2 k k n

x(k) ¼ þ 1, 1 k n,

1 2 0 2 2 , (7:43)

n-bit k,

be the auxiliary sequence vector at step then

2k k k

x(k):(1 þ , þ :

2 ) 1 2 (7:44)

1 2

Proof 1 ¼

The right inequality is trivial, it corresponds to 0. The left inequality

2k

x(k).(1 þ 1

is deduced from the computation of 2 ) for maximum, that is,

2k 2n

2 2 .

2 x(k þ

The practical interpretation of (7.44) is the impact of rule (7.40) on 1)

x(k) k-one

whenever is less than one with a fractional part made up of a string and a

k þ x(k þ

zero at position 1. Then 1) will be either less than one, exhibiting a similar

pattern with at least 2k ones as the header of the fractional part, or greater than one

þ ¼ k þ

(k 1) 1) with at least 1 zeros as the header of the fractional part. In both

(x

0 x(p) ¼ x(k þ

cases, the target value 1 is approximated by 1) with at least one bit more.

Comment 7.4

1. The selection (7.38) is justiﬁed by the fact that a decision about multiplying by

2i

a þ1

.2 (7.33) cannot be made before knowing the next bit. Actually,

i x

considering bit only (either 1 or 0) one cannot know whether the sequence

0

x(i) is already 1 (end of convergence process) or not.

x(i) .

2. When 1, the strategy described by (7.39) consists of detecting the ﬁrst

2i þ x(i) .

x(i) 1). When 1, Lemma

nonzero bit of then multiplying by (22

i, x (i) are all zeros.

7.1 shows that, at step bits 2k .2i

x(i) ,

3. When 1, the strategy described by (7.40) consists of detecting the last

2i

x(i) þ x(i)

nonzero bit of then multiplying by (2 1). When 1, Lemma 7.2

i, x

shows that, at step bits (i) are all ones.

2k.2i 187

## 7.3 LOGARITHMIC, EXPONENTIAL, AND TRIGONOMETRIC FUNCTIONS

Algorithm 7.8 Logarithm Computation by Multiplicative Normalization

x x ¼ x(0).x(1) x(2) x(n). xx(i, j)

. . .

The argument is in [1/2, 2[: Let be the com-

j xx(i) ¼ xx(i, xx(i, xx(i, n). lut(i) ¼

. . .

ponent of 0).xx(i, 1) 2) Let ln

2i

þ a(i).2 ) read from the table.

(1

a(0):=0; c(0):=1; xx(1):=x; yy(1):=0;

i 1..p-1

for in loop

xx(i)=1

if then exit; end if;

xx(i)>1 a(i):=-xx(i,i)

if then else

a(i):=xx(i,i)*not(xx(i,i+1)); end if;

c(i):=1+a(i)*2**(-i); xx(i+1):=xx(i)*c(i);

yy(i+1):=yy(i)-lut(i);

end loop; x(i)

In this example the auxiliary sequence is computed in the binary

Example 7.8 y(i)

system, while, for readability, the sequence is computed in decimal; the

2i

+ ) are assumed

precision is then readily veriﬁed. The functional values ln (1 2

x

given by look-up tables. is in [1, 2[.

Let x ¼ x(0) ¼ x :x x x x x ¼ ¼

1:10111 (1:71875)

1 2 3 4 5

0 10

y(0) ¼ 0 p ¼

Compute ln (x) with precision 8.

c(i) x þ y(i þ

(i 1) 1)

2i

a

i a þ x(i).c(i) y(i)–ln c

.2 1 ln c (i) (i)

i i x(0) ¼ y(0) ¼

— — — 1.10111 — 0

20 þ 1 (1.10111).(1)

0.2

c(0) ¼ x(1) ¼

¼

a 1 1.1011100 0 0

0

0 0 21 þ

22 1 (1.1011100).(0.1)

c(1) ¼ x(2) ¼ 20.69314718

a ¼ 21 0.1 0.11011100 0.69314718

1 1 22 þ 1 (0.11011100).(1.01)

2

c(2) ¼ x(3) ¼

a ¼ 1.01 1.00010011 0.223143551 0.470003628

2 1

2 23 þ 1 (1.00010011).1

0.2

a c(3) ¼ x(4) ¼

¼

3 1 1.00010011 0 0.470003628

0

3 24 þ

22 1 (1.00010011).(0.1111)

c(4) ¼ x(5) ¼ 20.064538521

a ¼ 21 0.1111 1.00000010 0.534542149

4 4 25 þ 1 (1.00000010).1

0.2

c(5) ¼ x(6) ¼

a ¼ 1 1.00000010 0 0.534542149

5 0

5 26 þ 1 (1.00000010).1

0.2

c(6) ¼ x(7) ¼

a ¼ 1 1.00000010 0 0.534542149

6 0

6 27 þ

22 1 (1.00000010).(0.1111111)

a c(7) ¼ x(8) ¼ 20.007843177

¼ 21

7 0.1111111 1 (rounded up) 0.542385326

7

188 OTHER ARITHMETIC OPERATIONS

29

The actual decimal value of ln (1.71875) is 0.541597282+10 , the difference from

24 210

, 2 .

the computed result is less than 8.10 a ¼ i

As it appears in the preceding example, whenever 0, the only effect of step

i

on the computation process consists of incrementing the step number; both

x(i) y(i)

sequences and remain unchanged. So, by detecting strings of 0 or 1 in

x(i), one could readily jump to the next nontrivial computation step. The following

example illustrates this feature.

As in the preceding example the auxiliary sequence x(i) is computed

Example 7.9 y(i)

in the binary system, while sequence is computed in decimal The functional

2i

+ x

values ln (1 2 ) are given by look-up tables. is now in [1/2, 2[. Strings . .

00

or . . are highlighted by bold type.

11

Let :x x x x x ¼ ¼

x ¼ x(0) ¼ x 0:10011 (0:59375)

1 2 3 4 5

0 10

y(0) ¼ 0 p ¼

Compute ln (x) with precision 10 (see Table 7.3). 29

20.521296923 +

The actual decimal value of ln (0.59375) is 10 , the

26 210

, 2 .

difference from the computed result is less than 4.10

7.3.3.2 Exponential Function Approximation by Additive Normalization

Deﬁne i

:2 a [

c(i) ¼ þ a ), { 1, 0, 1} (7:45)

ln ln (1 i i

additive normalizing function, a

as the where is selected in such a way that the

i

sequences x(i þ ¼ x(i) c(i)

1) ln (auxiliary sequence) (7:46)

sequence)

converges toward 0. Then the sequence (main

y(i þ ¼ y(i):c(i)

1) (7:47)

x y(0) x(0)

e . If and are, respectively, set to 1

can be set to converge toward the result

x, x( p) ﬃ

and to the argument and assuming 0, one can write

x( p) ¼ x S c(i) ﬃ ! x ﬃ S c(i) ¼ P c(i);

ln 0 ln ln

i i i

x

y(p) ¼ y:P c(i) ¼ P c(i) ¼ e ¼ e :

ln (Pc(i))

i i x

To make the convergence of (7.46) possible, the argument needs to be in a range

such that

S c(i) x S c(i)

x ln ;0 and max lim ln 0,

min lim 1ip 1ip

p!1 p!1

that is,

i i

x S x S þ

lim ln (1 2 ) and lim ln (1 2 ) 0, (7:48)

1ip 1ip

p!1 p!1

20.523248143

20.405465108 20.521293108

(i) 0

1) c ¼

ln — —

þ y(0)

2

y(i y(i) 0 0.117783035

0.405465108 20.001955035

(i) — —

—

c

ln 0 (1.0000000010).(0.111111111) up)

1.0000000010

0.1110010000

0.1001100000

0.1001100000 (0.1001100000).(1.1) (0.111001).(1.001) (rounded

(0.10011).(1) x(2) x(4)

1) x(i).c(i)

þ — —

¼ ¼

(i x(9)

x(3) 1

x ¼

¼

¼ ¼

¼ x(10)

x(4)

x(2)

x(1)

x(0) 0.111111111

1.001

1.1 1

1 1 1

1 1 þ

þ þ þ

¼ þ

c(i) ¼ — —

— 29

2i 20 23 ¼

21

c(0) c(1)

.2 22

0.2 1.2 c(3)

2

i ¼

a c(9)

21

8

0 0 1

1 a 0

¼ ¼ ¼ ¼ !

—

i

a ¼ ¼

0 1 2 3 4

a a

a

a 9

a a

7.3

TABLE 8

!

— 4

2 3 9

1

0

i 189

190 OTHER ARITHMETIC OPERATIONS

or 1:242 x 0:869: (7:49)

þ 0

Observe that the upper bound of (7.48) can be raised to 1.562, as ln (1 2 ) can be

x(1).

accepted as the ﬁrst normalizing factor for computing This extends the range

x

(7.49) to]21.242, 1.562]. Here again, the argument could need to be prescaled to

fall in the (extended) range (7.49). The range [1, 2[ (ﬂoating-point mantissa) doesn’t

x

ﬁt any more; the range [21, 1[ does. A possible prescaling operation can replace by

0

0 0 s 0 x x

x x ¼ x e e

such that .2 (x in [21, 1[). The algorithm ﬁrst computes , then is pro-

0

x

s-time s , e . In particu-

vided after an squaring (square rooting if 0) correction on

0 0

! x

x ¼ x .2 in [1/2, 1[;

lar, a single shift reduces the range [1, 2[to [1/2, 1[:

0

x x

e

e provides .

squaring

In the practical implementations of this algorithm, look-up tables are used to read

2i x(i þ x

+ ), needed to compute 1) of (7.46). For

out the successive values of ln (1 2

a can be selected according to the following rule:

in [21, 1[, i x(i),

a holds the same sign as then

whenever nonzero, i i

j ¼ j þ a :2 ja j ¼

ja 1 if ln (1 )j 2:jx(i)j, 0 otherwise: (7:50)

i i i

jx(i)j

This simple rules ensures that, at each step, will either decrease or stay

a ¼ x(i) p p

unchanged; 0 prevents from increasing. Precision is reached after steps.

i

An interesting feature of the method comes from the following relation:

i i

þ ¼ :

ln (1 2 ) 2 (7:51)

lim

i!1

2i is the ﬁrst term of the Taylor –MacLaurin series (7.19). The approximation

2 2i 2i 22i 220

þ ﬃ

ln (1 2 ) 2 produces an error bounded by (2 ), that is, less than 2

after 10 steps. This very fast convergence behavior (see Table 7.3) can be used to

p

speed-up the algorithm. If the desired precision is great enough, the algorithmic

p/2

procedure can be stopped after iteration steps; at this point one can write

i

x(p=2) ¼ x x ¼ S x :2

0:00 0x ,

p=2þ1 p=2þ2 n p=2þ1in i

i i

x(p) ﬃ x( p=2) S x :2 ﬃ x(p=2) S þ x :2 ﬃ

ln (1 ) 0,

p=2þ1ip i p=2þ1ip i (7:52)

a ¼ x 8i p/2.

corresponding to the selection ,

i i x(p)

Actually (7.52), the convergence of to zero, is settled by subtracting

2i 2i

x x(p/2); p/2 x

S .2 from for great enough .2 can be approximated

p/2þ1ip i i

2i p/2).

þ x .2 ), (i

by ln (1 i 2i

p

On the other hand, for great enough (2 small enough),

i i

þ x :2 ﬃ þ S x :2 ﬃ þ x( p=2)):

P (1 ) (1 ) (1 (7:53)

p=2þ1ip i p=2þ1ip i 191

## 7.3 LOGARITHMIC, EXPONENTIAL, AND TRIGONOMETRIC FUNCTIONS

p/2

As a consequence, the ﬁnal iteration steps can be replaced by

y( p) ¼ y( p=2):(1 þ x(p=2)): (7:54)

x(p/2), x( p) ¼

The auxiliary sequence has been built up to then settled to 0 by

y(p) y(p/2)

(7.52); the main sequence can be computed from in just one compu-

tation step (7.54). Algorithm 7.9 resumes the general procedure. Chapter 14

describes an implementation including the above-mentioned one-step simpliﬁcation

p/2

for the last steps.

Algorithm 7.9 Exponential Computation by Additive Normalization

2i 2i

x lutn(i) ¼ 2 lutp(i) ¼ þ

The argument is in [21, 1[. Let (1 2 ), (1 2 ),

2i 2i

lutlnn(i) ¼ lutlnp(i) ¼ þ

ln (1-2 ) and ln (1 2 ) be read from look-up tables.

xx(0):=x; yy(0):=1;

i 0..p-1

for in loop

xx(i)=0

if then exit, end if;

xx(i)<0

if then

abs(lutlnn(i))<=abs(2*xx(i))

if then

xx(i+1):=xx(i)-lutlnn(i); yy(i+1):=yy(i)*lutn(i);

xx(i+1):=xx(i); yy(i+1):=yy(i);

else end if;

else abs(lutlnp(i))<=abs(2*xx(i))

if then

xx(i+1):=xx(i)-lutlnp(i); yy(i+1):=yy(i)*lutp(i);

xx(i+1):=xx(i); yy(i+1):=yy(i);

else end if;

end if;

end loop; For the sake of readability, the following example is treated

Example 7.10 2i 2i

2

þ ) and ln (1 2 ) for

in decimal. Table 7.4 exhibits the values of ln (1 2

i ¼ . . . x

0, 1, , 15. This table is used to compute the successive values of (i).

232

e 0.65625 with precision 2 . The ﬁrst 16 steps

The problem at hand is to compute

are displayed in Table 7.5. The desired precision is reached through a ﬁnal single

p ¼

step computing (7.54) for 16. The result is 1.92755045011, while the exact

¼

e 0.65625 1.92755045016. This makes a differ-

result rounded to the 11th decimal is

211 232

,

ence around 5.10 2 . y

Comment 7.5 x

The general exponentiation function may be computed as

y y

¼ e

x ln (x) , (7:55)

which may be calculated by logarithm computation and multiplication. The particu-

lar cases of integer powers (squaring, cubing, etc.) may be calculated through cus-

tomized multiplication schemes. Convergence methods, such as the one described

above, are most often used to implement exponentiation algorithms. An important

12

particular case is the square root (power ) for which a classical digit recurrence

algorithm, very similar to division, can easily be adapted. Square rooting is reviewed

in Section 7.4. 20.693147180559945

20.287682072451781

20.133531392624523

20.064538521137571

20.031748698314580

20.015748356968139

20.007843177461026 20.001955034835803

20.000977039647827

20.000488400498109

20.003913899321136 20.000244170432174

20.000122077763687

20.000061037018971

20.000030518043796

)

2i

2

2

(1

ln

¼

lutn(i) 2

)

2i 0.693147180559945 0.117783035656383

0.223143551314210

0.405465108108164 0.015504186535965 0.007782140442055

0.060624621816435 0.030771658666754 0.001951220131262

0.003898640415657 0.000488162079501

0.000976085973055 0.000244110827527 0.000122062862526 0.000061033293681 0.000030517112473

2

þ

(1

ln

¼

lutp(i) 0.999969482421875

0.99993896484375

0.9998779296875

0.999755859375

0.99951171875

0.9990234375

0.998046875

0.99609375

0.9921875

0.984375

0.96875

2i 0.9375

2 0.875

0.75

2 0.5

0

1

)

i

2

+ 1.000030517578125

1.00006103515625

ln(1 1.0001220703125

1.000244140625

1.00048828125

1.0009765625

Table 1.001953125

1.00390625

1.0078125

1.015625

1.03125

2i

Look-Up 1.0625

1.125

2 1.25

þ 1.5

1 2

7.4

TABLE 10 11 12 13 14 15

0 1 2 3 4 5 6 7 9

8

i

192 1.9275685068

)

) )

212

210 214

) )

) )

24 28

20 25 2 2

2

1) 2

2 2

2 2

þ þ

2

2

þ

þ

þ y(12).(1

y(10).(1 y(14).(1

¼

y(5).(1 y(8).(1

y(0).(1 y(4).(1

y(i y(13)

y(11) y(15)

y(1) y(6)

y(2) y(7) y(9)

y(3)

1

¼ ¼ ¼

¼

¼ ¼

¼

¼ ¼ ¼

¼ ¼ ¼

¼ ¼

¼ ¼

y(12) y(16)

y(10) y(14)

y(13)

y(11) y(15)

y(0) y(2) y(7)

y(6)

y(3) y(8)

y(1) y(9)

y(4)

y(5) 1.92755045011

) 1.92755045016

2i

.2 0.000783581232008 0.000051665691127

20.003130318089128

20.036897180559945 0.027641340577626 20.003130318089128 20.000192504741047 20.000009367602554

20.036897180559945 20.003130318089128

20.036897180559945 20.036897180559945 20.000192504741047 20.000009367602554

0.000783581232008 0.000051665691127

i 0.6562500000

a

þ

(1 ¼

ln 0.9999906324

– ¼

x(i) 0.65625

¼

¼ x(0)

1) e

þ decimal:

x

x(i 1.9275685068 th

11

1 0 0 21 0 0

1 0 0

0 0 21

0 1 1

21

i

a the

to

¼ rounded

x(16))

21) 20.693147180559945 20.015748356968139 20.000488400498109 20.000030518043796

20.031748698314580 20.001955034835803 20.000122077763687

20.287682072451781 20.007843177461026

20.133531392624523 20.003913899321136 20.000244170432174

20.000977039647827 20.000061037018971

20.064538521137571

¼ þ

i result

(a y(16).(1

—

)

2i

2 Exact

2 ¼

(1 y(32)

ln

1) 0.000488162079501 0.000030517112473

0.000122062862526

0.000244110827527

0.405465108108164 0.015504186535965

0.693147180559945 0.030771658666754 0.001951220131262

0.223143551314210 0.007782140442055

0.117783035656383 0.003898640415657 0.000976085973055 0.000061033293681

0.060624621816435

¼

i

(a

0:65625 )

2i

2

þ

e (1

7.5 ln

TABLE 11 15

13

12

10 14

1 6

0 5 9

2 7

3 8

4

i 193

194 OTHER ARITHMETIC OPERATIONS

7.3.4 Trigonometric Functions—CORDIC Algorithms

CORDIC algorithms ([VOL1959], [ERC1987], [DEL1989], [VOL2000]) belong to

the class of linear convergence methods. They are particularly well suited to evalu-

ation of trigonometric functions, although they apply to a number of other functions

such as the logarithm, exponential, or square root. Historically, the Coordinate

Rotation Digital Computer had been designed as a special-purpose digital computer

for airborne computation in real-time. The CORDIC techniques were then extended

for solving trigonometric relationships involved in plane coordinate rotation and

conversion from Cartesian to polar coordinate systems. The CORDIC algorithms

converge linearly (one bit per cycle) and are rather simple to implement in practice.

Conceptually, the method consists of computing the new coordinates of a given

a 2

vector after rotation by a given angle ; after rotation, the unit vector (0, 0) (1, 0)

a a

, sin ).

would get its new end point coordinates as (cos a

y 2 y

2 , ) be the vector (0, 0) (x , ) rotated by an angle

Let (0, 0) (x

1 1 0 0

(Figure 7.1), the basic rotation formulas are given by

a a

¼ x y

x cos sin (7:56)

1 0 0

a a

y ¼ x þ y

sin cos (7:57)

1 0 0

The CORDIC algorithm deﬁnes, as the auxiliary sequence, the residual rotation

i,

remaining to be completed after step namely,

a

a ¼ a : (7:58)

iþ1 i i

a

If is the desired rotation angle, the auxiliary sequence will be set to converge

0

to zero while the main sequence converges toward the function to evaluate.

The method replaces rotations as deﬁned by formulas (7.56) and (7.57) by

pseudorotations as illustrated in Figure 7.2. Pseudorotations expand coordinates

a a

¼ þ i

2 1/2

by a factor of 1/cos (1 tan ) ; (7.56) and (7.57) for step are replaced by

i i

a a a a

¼ x y ¼ y þ

x 2 1=2

tan (x cos sin ):(1 tan ) (7:59)

iþ1 i i i i i i i i

and a a a a

y ¼ x þ y ¼ þ y þ :

2 1=2

tan (x sin cos ):(1 tan ) (7:60)

iþ1 i i i i i i i i

x y x y, p

Assuming and to be the initial coordinates and after rotations by angle

0 0

a , the recursive use of (7.59) and (7.60) leads to

i (Sa (Sa a

x ¼ y þ 2 1=2

(x cos ) sin )):P(1 tan ) (7:61)

p i i i

and (Sa (Sa a

y ¼ þ y þ :

2 1=2

(x sin ) cos )):P(1 tan ) (7:62)

p i i i

a

On the other hand, assuming to be the overall rotation angle, that is, the argument

of the trigonometric function to be evaluated, then the auxiliary sequence is written

a

a ¼ a S : (7:63)

p i 195

## 7.3 LOGARITHMIC, EXPONENTIAL, AND TRIGONOMETRIC FUNCTIONS

a

f g

The set is selected in order to make (7.61) and (7.62) easy to calculate and, in

i a a

f g

P(1 þ 2 1/2

) . For this sake, is

particular, to be able to precompute the factor tan i i

pre-set except for the sign; then, convergence will be achieved by a suitable strategy

on sign selection. In the following, an algorithm is developed for computing sine and

cosine functions.

a

f g

Let the set be such that

i i

a ¼ s s [

tan 2 , { 1, 1}: (7:64)

i i

Equations (7.59) and (7.60) can then be written i

¼ x y :s

x 2 (7:65)

iþ1 i i i

and i

y ¼ x :s þ y

2 , (7:66)

iþ1 i i i

while (7.58) becomes 1 i

a ¼ a tan (s 2 ): (7:67)

iþ1 i i Sa

p a a ¼ y x

After iterations, is assumed close enough to zero, and (7.63) ; and ,

p i p p

computed iteratively by (7.65) and (7.66), are introduced in (7.61) and (7.62),

respectively, leading to ¼ k:(x a y a)

x cos sin (7:68)

p

and y ¼ k:(x a þ y a),

sin cos (7:69)

p

y x ,y

α α 1 1

= x sin +y cos

y

1 0 0 ,y

x

0 0 x'

α α x

0 α

= x cos – y sinα

x

1 0 0

Basic rotation formulas.

Figure 7.1

196 OTHER ARITHMETIC OPERATIONS

y α, α

x' , y' = x /cos y /cos

i+1 i+1 i+1 i+1

x , y

i+1 i+1 x , y

i i

α i x

0 Pseudorotations.

Figure 7.2

a a

k ¼ P(1 þ 2 1/2 2

where the factor tan ) can be precomputed since tan is not depen-

i i

a x ¼ x ¼ y ¼ y ¼

. Then, setting the initial values 1/k and 0,

dent on the sign of i 0 0

y

x and , respectively.

the cosine and sine functions are given by p p

21 2i

(s 2 ) are stored in a look-up table. The size of the

Precomputed values of tan i 21 2i

p.

look-up table sets the precision Table 7.6 shows the ﬁrst 25 values of tan (s 2 )

i

a i

k ¼ P(1 þ 2 1/2

) : for 20,

together with the fast converging values of tan i

¼ x ¼ ¼

k ¼ x 1/k 0.607252935009. The strategy for

1.646760258121 and 0 a

s is trivial. Since each angle (Table 7.6) is smaller than half

selecting the sign i i

a s ¼

, the convergence may be achieved by selecting sign

the preceding angle i21 i

(a ). The domain of convergence is greater than [2908, 908], so any trigonometric

i21

value can be computed (with a prospective use of trigonometric relations for angle

+

reduction). More precisely, for 25-step precision, the range is [299.888, 99.888],

a

the sum of the listed in Table 7.6. Algorithm 7.10 describes the CORDIC pro-

i a a.

cedure for computing sin and cos Without loss of generality, the decimal

system has been used for a better readability.

Algorithm 7.10 Sine and Cosine CORDIC Procedure

21 2i

a

lut(i) ¼

Let be tan (2 ) read from the table.

i

x(0):=0.607252935009; y(0):=0; a(0):=a;

a(0)=0 x(0):=1; y(0):=0;

if then exit; end if;

i 0..p-1

for in loop

a(i)=0

if then exit; end if;

a(i)>0 s(i):=1 s(i):=-1;

if then else end if;

x(i+1):=x(i)-(y(i)*s(i)*2**(-i));

y(i+1):=y(i)+(x(i)*s(i)*2**(-i));

a(i+1):=a(i)-(s(i)*lut(i));

end loop; 197

## 7.3 LOGARITHMIC, EXPONENTIAL, AND TRIGONOMETRIC FUNCTIONS

1 i

TABLE 7.6 Look-Up Table for tan (2 )

1 i 1 i

a a

¼ ¼

tan (2 ) tan (2 )

i i

2i a a

i k ¼ P(1 þ

þ 1=2 1=2

2 2

2 ) tan )

(rad) (deg) (1 tan i i

0 1.0000000000 0.7853981634 45.0000000000 1.41421356 1.4142135623731

1 0.5000000000 0.4636476090 26.5650511771 1.11803399 1.5811388300842

2 0.2500000000 0.2449786631 14.0362434679 1.03077641 1.6298006013007

3 0.1250000000 0.1243549945 7.1250163489 1.00778222 1.6424840657522

4 0.0625000000 0.0624188100 3.5763343750 1.00195122 1.6456889157573

5 0.0312500000 0.0312398334 1.7899106082 1.00048816 1.6464922787125

6 0.0156250000 0.0156237286 0.8951737102 1.00012206 1.6466932542736

7 0.0078125000 0.0078123411 0.4476141709 1.00003052 1.6467435065969

8 0.0039062500 0.0039062301 0.2238105004 1.00000763 1.6467560702049

9 0.0019531250 0.0019531225 0.1119056771 1.00000191 1.6467592111398

10 0.0009765625 0.0009765622 0.0559528919 1.00000048 1.6467599963756

11 0.0004882813 0.0004882812 0.0279764526 1.00000012 1.6467601926847

12 0.0002441406 0.0002441406 0.0139882271 1.00000003 1.6467602417620

13 0.0001220703 0.0001220703 0.0069941137 1.00000001 1.6467602540313

14 0.0000610352 0.0000610352 0.0034970569 1.00000000 1.6467602570986

15 0.0000305176 0.0000305176 0.0017485284 1.00000000 1.6467602578655

16 0.0000152588 0.0000152588 0.0008742642 1.00000000 1.6467602580572

17 0.0000076294 0.0000076294 0.0004371321 1.00000000 1.6467602581051

18 0.0000038147 0.0000038147 0.0002185661 1.00000000 1.6467602581171

19 0.0000019073 0.0000019073 0.0001092830 1.00000000 1.6467602581201

20 0.0000009537 0.0000009537 0.0000546415 1.00000000 1.6467602581208

21 0.0000004768 0.0000004768 0.0000273208 1.00000000 1.6467602581210

22 0.0000002384 0.0000002384 0.0000136604 1.00000000 1.6467602581211

23 0.0000001192 0.0000001192 0.0000068302 1.00000000 1.6467602581211

24 0.0000000596 0.0000000596 0.0000034151 1.00000000 1.6467602581211

25 0.0000000298 0.0000000298 0.0000017075 1.00000000 1.6467602581211

i-bit

In binary, the operations involved in one step of Algorithm 7.10 are: two

x(i þ

shifts, one table look-up, and three signed additions. Computation of 1),

y(i þ a(i þ

1) and 1) are independent; hardware implementations can thus proﬁt

from the potential parallelism. In this case, the elementary step delay is that of a

table look-up (or a shift—whatever operation is slower) plus a signed addition.

Algorithm 7.10 is illustrated in Example 7.11.

a ¼ a a

Let 758; compute the binary expression of cos and sin

Example 7.11

Table 7.6 displays the ﬁrst steps of the algorithm. Since the binary system is at

x, x(0) ¼

hand, the initial value of namely 0.607252935009 , has to be expressed

10

in binary, with the appropriate precision. In the example, 13 bits have been used

¼

(x(0) 0.1001101101110) to set the procedure for a 12-bit precision. The results

¼ x(5) ¼

after 5 steps (Table 7.7) are cos 758 0.0100001011011 and sin

¼ y(5) ¼

758 0.1111011011111. The ﬁrst seven bits are correct but this precision

is irrelevant because too few steps have been completed. The average convergence

198 OTHER ARITHMETIC OPERATIONS

7588 7588

TABLE 7.7 cos sin : First Five Computational Steps

x(0) ¼ y(0) ¼ a ¼

0:1001101101110 0 758

21 2i x(i þ ¼

lut(i) ¼ y(i þ ¼ a(i þ ¼

1)

tan 2 1) 1)

2i 2i 2i

i s(i) x(i) 2 y(i).s(i).2 y(i) þ x(i).s(i).2 a(i) 2 s(i).lut(i)

2 (deg)

0 1.0000000000 45.000000000 1 0.1001101101110 0.1001101101110 30.000000000

1 0.1000000000 26.565051177 1 0.0100110110111 0.1110100100110 3.434948823

210.601294645

2 0.0100000000 14.036243468 1 0.0001001101101 0.1111110010011

21 23.476278296

3 0.0010000000 7.125016349 0.0011001100110 0.1111101000110

21

4 0.0001000000 3.576334375 0.0100001011011 0.1111011011111 0.100056079

rate (1 bit per step) is reached after a sufﬁcient number of steps, namely, whenever

21 2i 2i 21 2i

i ¼

2 is close enough to 2 ; Table 7.6 shows that for 10, tan 2 may be

tan i 27

with an error inferior to 4.10 .

set to 2

The next decimal example runs 28 computational steps (Table 7.8). In

¼ x(28) ¼ ¼

this example, the results are cos 308 0.8660254031 and sin 308

29 228

O(2

y(28) ¼ O(10 ), that is, ).

0.5000000012. In both cases, the error is

7.4 SQUARE ROOTING

Square rooting deserves special attention because of its frequent use in a number of

applications ([ERC1994], [OBE1999]). Although square rooting can be viewed as a

particular case of the exponential operation, the similarity with division is a more

important consideration for the choice of algorithms. Several techniques in base-B

and in binary systems are reviewed in this section.

7.4.1 Digit Recurrence Algorithm—Base-B Integers

Let X ¼ x x . . . x x x [ . . . B

, , , , , {0, 1, , 1} (7:70)

i

2n1 2n2 1 0

be the 2n-digit base-B radicand.

## Q R

The square root and the remainder are denoted

q . . . q q q [ . . . B

Q ¼ q , , , , {0, 1, , 1} (7:71)

n1 n2 i

1 0

and r . . . r r r [ . . . B

R ¼ r , , , , {0, 1, , 1} (7:72)

n n1 i

1 0

respectively.

The remainder R ¼ X Q 2 (7:73)

21 21 21 22 22 22 22 24 23 23 24 25 24 24 25 25 25 26 26 26 27 27 27 28

1.490189970.10 1.844002211.10 1.364549189.10

1.827527850.10 7.085479160.10 9.136742266.10 3.941004264.10 1.208928597.10 5.259096796.10

22.648613858.10 21.307455292.10 26.080439449.10 23.977220642.10 21.571092374.10 27.173187274.10

24.105088547.10 22.583382598.10

28.348541711.10 21.791560108.10

26.987298407.10

21,523147071.10 22.904319042.10

27.698849268.10

27.124209252.10

s(i).lut(i)

¼ 4.653824058

22.471192291 1.077489683

308 11.56505118

1)

¼

þ

2

a

a(i

a(i) -15

2i 0.5039928950 0.4997746615 0.5001975889 0.5000919018 0.5000060116 0.5000000237 0.4999999721 0.4999999979 0.5000000108

0.4972149222 0.5006190824 0.4989283309 0.4999861848 0.5000390470 0.5000126187 0.4999994043 0.5000027079 0.5000010561 0.5000002302 0.4999998173 0.4999999205 0.5000000044 0.5000000012

0.3036264675 0.4269747199 0.4833116621

0.5313463181

0.6072529350 0.5106461982

x(i).s(i).2

¼

0

¼ 1)

y(0)

þ

þ

y(i

y(i)

0.607252935 2i 0.8636960212 0.8661552804 0.8659112498 0.8659723349 0.8660219330 0.8660253901 0.8660254199 0.8660254050 0.8660253975

0.8675805128 0.8656647435 0.8666425151 0.8660333684 0.8660028581 0.8660181181 0.8660257477 0.8660238403 0.8660247940 0.8660252709 0.8660255093 0.8660254497 0.8660254013 0.8660254031

0.9108794025 0.9013910754 0.8747051554

0.8349727856

0.6072529350 0.8596016660

y(i).s(i).2

¼

1)

þ

2

x(i

¼ x(i)

x(0) 1 1 1 1 1 1 1 1 1

21 21

21 21 21

21

21 21

21

21

21 21 21

21

21 21 1

1

1 1

s(i) 21 21 21 21 22 22 22 23 23 23 24 24 24 24 25 25 25 26 26 26 27 27 27

2i 5.5952891894.10 2.7976452617.10 1.3988227142.10 6.9941136754.10 3.4970568507.10 1.7485284270.10 8.7426421369.10 4.3713210687.10 2.1856605344.10 1.0928302672.10 5.4641513360.10 2.7320756680.10 1.3660378340.10 6.8301891700.10 3.4150945850.10 1.7075472925.10 8.5377364625.10 4.2688682313.10 2.1344341156.10

8.9517371021.10 4.4761417086.10 2.2381050037.10 1.1190567707.10

2 7.12501634890 3.57633437500 1.78991060825

21 14.0362434679

45.0000000000 26.5650511771

tan (deg)

¼

lut(i)

3088 25 25 26 26 26 27 27 27 27 28 28 28 29 29

sind 24 25 3.0517578125.10 1.5258789063.10 7.6293945313.10 3.8146972656.10 1.9073486328.10 9.5367431641.10 4.7683715820.10 2.3841857910.10 1.1920928955.10 5.9604644775.10 2.9802322388.10 1.4901161194.10 7.4505805969.10 3.7252902985.10

0.000244140625 1.220703125.10 6.103515625.10

0.00048828125

0.0009765625

0.0156250000 0.0078125000 0.0039062500 0.0019531250

0.2500000000 0.1250000000 0.0625000000

1.0000000000 0.5000000000 0.0312500000

,

3088 2i

2

cos

7.8

TABLE 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

6 7 8 9

2 3 4

0 1 5

i 199

200 OTHER ARITHMETIC OPERATIONS

complies with the condition R 2Q, (7:74)

Q ¼ bX c:

1=2

ensuring that

The classical pencil and paper method, described in what follows, assumes that

21) 2

2 (B 1) look-up table). Allow-

all roots of 2-digit numbers are available ((B

X n

ing the ﬁrst digit of to be zero, the radicand can always be sliced into 2-digit

## X.

groups. Algorithm 7.11 computes the square root of the 2n-digit integer The

/

stands for integer multiplication, while ** and stand for integer exponen-

symbol P(i, k)

tiation and division respectively. One deﬁnes the function as

P(i, k) ¼ B Q(n i) þ k) k B i))

(2 (2 (n (7:75)

## B

Algorithm 7.11 Integer Square Rooting in Base

R(0):=X; Q(n-1):=0;

q(n-1):=SQR (R(0)/B**(2*n-2));

Q(n-2):=q(n-1);

R(1):=R(0)-(q(n-1)**2)*(B**(2*n-2));

i 2..n

for in loop

P(i, B-1)<=R(i-1) R(i):=R(i-1)- P(i, B-1);

if then

q(n-i):=B-1;

Q(n-i-1):=B*Q(n-i)+q(n-i);

P(i, B-2)<=R(i) R(i):=R(i-1)- P(i, B-2);

elsif then

q(n-i):=B-2;

Q(n-i-1):=B*Q(n-i)+q(n-i);

elsif

... P(i, 1) <=R(i) R(i):=R(i-1)- P(i, 1); q(n-i):=1;

elsif then

Q(n-i-1):=B*Q(n-i)+q(n-i);

R(i):=R(i-1); q(n-i):=0; Q(n-i-1):=B*Q(n-i)+q(n-i);

else

end if;

end loop; X ¼

Compute the square root of (591865472)

Example 7.12 10

¼ X ¼ R(0) ¼

Slicing (n 5) gives 05 ’91 ’86 ’54 ’72

Step 1 Q(4) ¼ 0

Q(3) ¼ q(4) ¼ b5 c ¼

1=2 2

R(1) ¼ 0591865472

0400000000

¼ 191865472 201

7.4 SQUARE ROOTING

Step 2 P(2,9) ¼ . R(1)

6

49:9:10

P(2,8) ¼ . R(1)

6

48:8:10

...

P(2,4) ¼ ¼ , R(1)

6

44:4:10 176000000

R(2) ¼ 191865472

176000000

¼ 015865472

q(3) ¼ Q(2) ¼ þ ¼

4; 10:2 4 24

Step 3 ...

P(3,3) ¼ ¼ , R(2)

4

483:3:10 14490000

R(3) ¼ 15865472

14490000

¼ 01375472

q(2) ¼ Q(1) ¼ þ ¼

3; 10:24 3 243

Step 4 ...

P(4,2) ¼ ¼ , R(3)

2

4862:2:10 972400

R(4) ¼ 1375472

972400

¼ 0403072

q(1) ¼ Q(0) ¼ þ ¼

2; 10:243 2 2432

Step 5 ... ¼ , R(4)

P(4,8) ¼ 0 389184

48648:8:10

R(5) ¼ 403072

389184

¼ :

013888 final remainder

q(0) ¼ Q( ¼ Q ¼ :

8; 1) 24328 integer square root of 591865472:

X ¼ Q þ R(5) : ¼ þ

2 2

591865472 24328 13888:

Comments 7.6 The process can be carried on further, up to the desired quantity of

digits after the decimal point.

202 OTHER ARITHMETIC OPERATIONS

P(i, k)

The function (7.75) of Algorithm 7.11 is used to compute the greatest value of

k P(i, k) R 2

verifying (i 1).

k

Actually, can be deﬁned algebraically by the formula

:Q i) þ R(i c,

k ¼ bB:Q(n i) þ 2 2 2(ni) 1=2

(n 1)=B ) (7:76)

(B 0

k [ R

the integer part of the solution to such that

0

R(i ¼ P(i, k ), (7:77)

1)

where P(i, k) R(i , P(i, k þ

1) 1): (7:78)

Obviously formula (7.76), using the square root, is useless for algorithmic

k

implementation. Other methods must therefore be derived to get verifying (7.78).

B .

Techniques for square rooting in base 2 are quite similar to those for base-B

division. The recurrence formula for the remainder is given by

R(i) ¼ R(i P(i, q(n i)),

1) (7:79)

with P(i, q(n i)) ¼ i) þ q(n i)):q(n i):B 2(ni) , (7:80)

(2B:Q(n

for 2n-digit integers.

7.4.2 Restoring Binary Shift-and-Subtract Square Rooting Algorithm

P(i, k)

In base 2, the computation of the function is obviously more straightforward,

k. P(i, ¼ P(i,

since just 0 or 1 have to be considered for Moreover, since 0) 0, just 1),

P(i),

hereafter denoted is computed,

P(i) ¼ i) þ :

2(ni)

(4:Q(n 1):2 (7:81)

Deﬁning (7.82)

P(i)=(4*Q(n-i)+1)*2**(2*(n-i)),

Algorithm 7.11 can be simpliﬁed as follows.

Algorithm 7.12 Integer Binary Square Rooting; Restoring

R(0):=X; Q(n-1):=0; q(n):=0; P(1):=2**(2*(n-1));

i 1..n,

for in loop

R(i-1)-P(i)>=0

if R(i):=R(i-1)-P(i); q(n-i):=1;

then

Q(n-i-1):=2*Q(n-i)+q(n-i);

R(i):=R(i-1); q(n-i):=0; Q(n-i-1):=2*Q(n-i)+

else

q(n-i); 203

7.4 SQUARE ROOTING

end if;

end loop; Compute the square root of

Example 7.13 X ¼ ¼

101101100011 (n 6)

Step 1 P(1) ¼ 10000000000

R(0) P(1) ¼ 101101100011

10000000000

¼ ¼ R(1)

011101100011 0

q(5) ¼ Q(4) ¼

1; 1

Step 2 P(2) ¼ 10100000000

R(1) P(2) ¼ 011101100011

10100000000

¼ ¼ R(2)

001001100011 0

q(4) ¼ Q(3) ¼

1; 11

Step 3 P(3) ¼ 1101000000

R(2) P(3) ¼ 1001100011

1101000000

, 0

R(3) ¼ R(2) ¼ 1001100011

q(3) ¼ Q(2) ¼

0; 110

Step 4 P(4) ¼ 110010000

R(3) P(4) ¼ 1001100011

110010000

¼ ¼ R(4)

0011010011 0

q(2) ¼ Q(1) ¼

1; 1101

204 OTHER ARITHMETIC OPERATIONS

Step 5 P(5) ¼ 11010100

R(4) P(5) ¼ 11010011

11010100

, 0

R(5) ¼ R(4)

q(1) ¼ Q(0) ¼

0; 11010

Step 6 P(6) ¼ 1101001

R(5) P(6) ¼ 11010011

¼ 1101001

¼ ¼ R(6)

01101010 0

q(0) ¼ Q( ¼ Q ¼

1; 1) 110101

X ¼ Q þ R(6):101101100011 ¼ þ

2 2

(110101) 1101010

Comments 7.7 X;

1. Generality is not lost assuming 2n digits for the radicand a ﬁrst digit zero

can always be assumed. q(n-1)

2. Step 1 of Algorithm 7.12 actually computes as the integer square root of

X. X q(n21)

the leftmost two bits of the radicand If is not headed by “00”, will

always be one.

7.4.3 Nonrestoring Binary Add-and-Subtract Square Rooting Algorithm

As well as in the classical binary division algorithm, restoring the remainder,

whenever negative, is not necessary since the following operation can cope with a

negative remainder and perform in a single operation the restoring process together

with the next step operation. i,

Considering a current step notice that when

R(i P(i)

1) 0, (7:83)

the nonrestoring algorithm proceeds like the restoring one.

Whenever R(i P(i) ,

1) 0, (7:84)

P(i) q(n 2 i)

the restoring process would add back to the partial result (7.84), set to

P(i þ R(i 2

zero, then subtract 1) from restored 1). These operations can be

substituted by adding, to the partial result (7.84), the following expression

P þ ¼ P(i) P(i þ

(i 1) 1) (7:85)

i þ

at the next step 1. 205

7.4 SQUARE ROOTING

i q(n 2 i) ¼ q ¼

Since, from step (7.84), 0, one can write:

n2i 2(ni)

zﬄﬄﬄﬄﬄ}|ﬄﬄﬄﬄﬄ{

P(i) ¼ q q q

0 1 000 0 (7:86)

n1 niþ2 niþ1

and 2(ni1)

zﬄﬄ}|ﬄﬄ{

q q q

P(i þ ¼ q

1) 0 0 1 0 0

n1 niþ2 niþ1 n1

#

set to 0 (7:87)

then 2(ni1)

zﬄﬄ}|ﬄﬄ{

þ ¼ P(i) P(i þ ¼ q q q

P (i 1) 1) 0 1 1 0 0 (7:88)

n1 niþ2 niþ1

Assuming ¼ q q

Q ,

ni1 n1 ni

(7.88) can be written

P þ ¼ P(i) P(i þ ¼ ½(4:Q þ i

2(ni1)

(i 1) 1) ) 11:2 2: (7:89)

ni1

The nonrestoring binary square rooting method is described in Algorithm 7.13,

and are deﬁned as follows

where P(i) Pstar(i) (7.90)

P(i):=(4*Q)n-i)+1)*2**(2*(n-i)), (7.91)

Pstar(i):=(4*Q(n-i)+11)*2**)2*(n-i)).

Algorithm 7.13 Integer Binary Square Rooting; Nonrestoring

R(0):=X; Q(n-1):=0; q(n):=0; P(1):=2**(2*(n-1));

R(1):=R(0)- P(1);

R(1)>=0

if q(n-1):=1; Q(n-2):=1;

then q(n-1):=0; Q(n-2):=0;

else

end if;

i 2..n

for in loop

R(i-1)>=0

if R(i):=R(i-1)- P(i);

then R(i):=R(i-1)+Pstar(i);

else

end if;

R(i)>=0

if q(n-i):=1; Q(n-i-1):=2*Q(n-i)+q(n-i);

then q(n-i):=0; Q(n-i-1):=2*Q(n-i)+q(n-i);

else

end if;

end loop;

206 OTHER ARITHMETIC OPERATIONS

As well as for nonrestoring division algorithms, a ﬁnal correction may be necessary.

Whenever the ﬁnal remainder is negative, it needs to be adjusted to the previous

positive one. Note: 2’s complement notation is used; the sign bit is in boldface

Example 7.14

type

Compute the square root of

X ¼ 0 ¼

1 1 0 0 1 1 0 1 0 0 0 0 1 (n 7)

Step 1

P(1) ¼ 0 1 0 0 0 0 0 0 0 0 0 0 0 0;

P(1) ¼ 1 1 0 0 0 0 0 0 0 0 0 0 0 0

R(1) ¼ R(0) P(1) ¼ 0 1 1 0 0 1 1 0 1 0 0 0 0 1

1 1 0 0 0 0 0 0 0 0 0 0 0 0

0 ¼ R(1)

1 0 0 1 1 0 1 0 0 0 0 1 0

R(1) ) q(6) ¼ Q(5) ¼

0 1; 1

Step 2

P(2) ¼ 0 1 0 1 0 0 0 0 0 0 0 0 0 0;

P(2) ¼ 1 0 1 1 0 0 0 0 0 0 0 0 0 0

R(2) ¼ R(1) P(2) ¼ 0 1 0 0 1 1 0 1 0 0 0 0 1

1 0 1 1 0 0 0 0 0 0 0 0 0 0

1 ¼ R(2) ,

0 1 0 1 1 0 1 0 0 0 0 1 0

R(2) , ) q(5) ¼ Q(4) ¼

0 0; 10

Step 3

P ¼ 0

(3) 1 0 1 1 0 0 0 0 0 0 0 0

R(3) ¼ R(2) þ P ¼ 1

(3) 0 1 0 1 1 0 1 0 0 0 0 1

0 1 0 1 1 0 0 0 0 0 0 0 0

0 ¼ R(3)

1 0 1 0 0 0 0 1 0

R(3) ) q(4) ¼ Q(3) ¼

0 1; 101 207

7.4 SQUARE ROOTING

Step 4 P(4) ¼ 0 1 0 1 0 1 0 0 0 0 0 0 ;

P(4) ¼ 1 0 1 0 1 1 0 0 0 0 0 0

R(4) ¼ R(3) P(4) ¼ 0 0 0 0 1 0 1 0 0 0 0 1

1 0 1 0 1 1 0 0 0 0 0 0

1 ¼ R(4) ,

0 1 1 0 1 1 0 0 0 0 1 0

R(4) , ) q(3) ¼ Q(2) ¼

0 0; 1010

Step 5 ¼ 0

P (5) 1 0 1 0 1 1 0 0 0 0

R(5) ¼ R(4) þ P ¼ 1

(5) 0 1 1 0 1 1 0 0 0 0 1

0 0 1 0 1 0 1 1 0 0 0 0

1 ¼ R(5) ,

0 0 0 0 1 0 0 0 1 0

R(5) , ) q(2) ¼ Q(1) ¼

0 0; 10100

Step 6 ¼ 0

P (6) 1 0 1 0 0 1 1 0 0

¼ 1

R(6) ¼ R(5) þ P (6) 0 0 0 0 1 0 0 0 1

0 1 0 1 0 0 1 1 0 0

1 ¼ R(6) ,

0 1 0 1 1 1 0 1 0

R(6) , ) q(1) ¼ Q(0) ¼

0 0; 101000

Step 7 ¼ 0

P (7) 1 0 1 0 0 0 1 1

¼ 1

R(6) þ P (7) 0 1 0 1 1 1 0 1

0 1 0 1 0 0 0 1 1

0 ¼ R(7) ¼

0 0 0 0 0 0 0 0 0

R(7) ¼ ) q(0) ¼ Q ¼

0 1; 1010001,

the exact square root.

208 OTHER ARITHMETIC OPERATIONS

Comments 7.8 The square rooting methods developed in the preceding sections

are classic and easy to implement. The main component involved in the step

complexity is the signed sum. One digit is obtained at each step (digit recurrence),

p, p n-digit

so, for a desired precision adding stages are involved, combinationally or

sequentially implemented. To speed-up the process, fast adders may be used. SRT

type algorithms have been developed for square rooting, using the feature of

carry-save redundant adders ([ERC2004]). On the other hand, convergence algor-

ithms have been developed, such as Newton – Raphson, reviewed in the following

section.

7.4.4 Convergence Method—Newton – Raphson

The priming function for square root computation could be

f ¼ x X,

2

(x) (7:92)

x=X 1/2

which has a root at .

f

To solve (x)=0, the same equation used for inverse computation can be used:

0

¼ x f

x (x )=f (x ) (7:93)

iþ1 i i i

or x ¼ þ X=x

1=2(x ): (7:94)

iþ1 i i

Formula (7.94) involves a division and an addition at each step, which makes

Newton– Raphson’s method apparently less attractive in this case. Nevertheless,

## X,

1/2 , then multiplying by the radicand

computing the inverse square root 1/X

leads to a more effective solution.

The priming function is now f ¼ X,

2

(x) 1=x (7:95)

x=1/X 1/2 , and the equation of convergence is given by

with root ¼ ðx =2Þ:ð3 x : x Þ;

x 2 (7:96)

iþ1 i i

which involves three multiplications, one subtraction (3’s complement), and one

division by 2 (shift in base 2). A ﬁnal multiplication is needed: the inverse square

root by the radicand.

7.5 BIBLIOGRAPHY

[CAO2001] J. Cao, B. W. Wei, and J. Cheng, High-performance architecture for elementary

Proceedings of the 15th IEEE Symposium on Computer Arith-

functions generation. In:

metic, 2001, pp. 136– 144.

[CHE1972] T. C. Chen, Automatic computation of exponentials, logarithms, ratios, and

IBM J. Res. Dev.,

square roots. 380– 388 (1972).

16: 209

7.5 BIBLIOGRAPHY Software Manual for the Elementary Functions,

[COD1980] W. J. Cody and W. Waite,

Prentice-Hall, Englewood, Cliffs, NJ, 1980. SPIE Adv.

[DEL1989] J.-M. Delosme, CORDIC algorithms, theory and extensions.

Algorithms Architecture Signal Processing IV 131 – 145 (1989).

1152:

[ERC1977] M. D. Ercegovac, A general hardware-oriented method for evaluation of func-

IEEE Trans. Comput.,

tions and computations in a digital computer. 667 – 680

26(7):

(1977).

[ERC1987] M. D. Ercegovac and T. Lang, Fast cosine/sine implementation using on-line

Proceedings of the 21st Asilomar Conference on Signals, Systems, Compu-

CORDIC. In:

ters, 1987, pp. 222– 226. Division and Square-Root: Digit-Recurrence

[ERC1994] M. D. Ercegovac and T. Lang,

Algorithms and Implementations, Kluwer Academic Publishers, Norwell, MA, 1994.

[ERC1995] M. D. Ercegovac, FPGA implementation of polynomial evaluation algorithms. In:

Proceedings of SPIE Photonics East ’95 Conference, Vol. 2607, pp. 177 – 188, 1995.

Digital Arithmetic.

[ERC2004] M. D. Ercegovac and T. Lang, Morgan Kaufmann, San

Francisco, CA, 2004. IRE Trans. Electron. Comput.,

[GAR1959] H. L. Garner, The residue number system. EC 8:

140 – 147 (1959).

[HUA1983] C. Huang, A fully parallel mixed-radix conversion algorithm for residue number

IEEE Trans. Comput.

applications. 398 – 402 (1983).

32(4): IEEE

[KOS1991] D. K. Kostopoulos, An algorithm for the computation of binary logarithms.

Trans. Comput., 1267 –1270 (1991).

40(11):

[MAR1990] P. W. Markstein, Computations of elementary functions on IBM RISC System/

IBM J. Res. Dev.,

6000 processor. 111– 119 (1990).

[OBE1999] S. F. Oberman, Floating point division and square root algorithms and

Proceedings of the 14th IEEE

implementation in the AMD-K7 microprocessor. In:

Symposium Computer on Arithmetic, 1999, pp. 106 – 115.

[PAL2000] V. Paliouras, K. Karagianni, and T. Stouraitis, A ﬂoating-point processor for fast

IEEE Trans. Circuits Systems II: Analog and Digital

and accurate sine/cosine evaluation.

Signal Processing, (5): pp. 441– 451 (2000).

47 21

[SPE1965] W. H. Specker, A class of algorithms for ln (x), exp (x), sin (x), cos (x), tan (x)

21 IEEE Trans. Electron. Comput.,

and cot (x). 85 – 86 (1965).

EC 14:

Residue Arithmetic and Its Applications to Computer

[SZA1967] N. S. Szabo and R.I. Tanaka,

Technology. McGraw-Hill, New York, 1967.

[TAN1991] P. K. Tang, Table look-up algorithms for elementary functions and their error

Proceedings of the 10th IEEE Symposium on Computer Arithmetic,

analysis. In: 1991,

pp. 232 – 236. IRE Trans.

[VOL1959] J. E. Volder, The CORDIC trigonometric computing technique.

Electron. Comput., 330– 334 (1959).

EC 8: J. VLSI Signal Processing Syst.,

[VOL2000] J. E. Volder, The birth of CORDIC. 101 – 105

25:

(2000).

8

## FINITE FIELD OPERATIONS

Finite ﬁeld operations are used as computation primitives for executing numerous

cryptographic algorithms, especially those related with the use of public keys (asym-

metric cryptography). Classical examples are ciphering deciphering, authentication,

RSA-type elliptic curve

and digital signature protocols based on or algorithms. Other

error correcting codes residue number

classical applications of ﬁnite ﬁelds are and

systems. This chapter proposes algorithms allowing the execution of the main arith-

## Z

metic operations (addition, subtraction, multiplication) in ﬁnite rings and poly-

m

## Z

Z [x]/f (x). In the case of , an exponentiation algorithm based on

nomial rings p m

Montgomery multiplication p f

the concept is also described. If is prime and (x) is

n

Z GF(p), GF(p

Z , [x]/f (x), and ) are ﬁnite ﬁelds

an irreducible polynomial, then p p

for which inversion algorithms are proposed.

## Z

8.1 OPERATIONS IN m

m . Z ¼ f0, . . . m 2

Given a natural number 1, the set 1, , 1g is a ring whose oper-

m

m

ations are deﬁned modulo (Chapter 2):

ðx þ yÞ m; ðx yÞ m; ðx:yÞ m:

mod mod and mod

Synthesis of Arithmetic Circuits: FPGA, ASIC, and Embedded Systems

By Jean-Pierre Deschamps, Géry J. A. Bioul, and Gustavo D. Sutter

#

Copyright 2006 John Wiley & Sons, Inc. 211

212 FINITE FIELD OPERATIONS

8.1.1 Addition x y x, y , m,

Given two natural numbers and belonging to the interval 0 compute

z ¼ þ y) m.

(x mod Taking into account that

x þ y ,

0 2:m,

z x þ y x þ y 2 m.

must be equal to either or The corresponding algorithm is the

following. m

Algorithm 8.1 Modulo Addition

2

z1:=x+y; z2:=z1 m;

z2>=0 z:=z2; z:=z1;

if then else end if;

n21 n

B , m B x y n-digit

Assume now that and that and are base-B numbers.

Consider three cases: n n n

x þ y , m x þ y , B þ y) þ 2 m) , B z ¼ x þ y;

if then , (x (B ,

n n n

m x þ y , B þ y) þ 2 m) B z ¼ x þ y 2 m ¼ þ y)

if then (x (B , ((x

n n

2 m)) B

þ mod ;

## (B

n n n

B x þ y þ y 2 B þ 2 m) ¼ x þ y 2 m ¼ z.

if then (x ) (B

So Algorithm 8.1 can be substituted by the following one where all operands have

n digits. B m

Algorithm 8.2 Base Modulo Addition

z1:=(x+y) mod B**n; c1:=(x+y)/B**n;

z2:=(z1+B**n - m) mod B**n; c2:=(z1+B**n - m)/B**n;

c1=0 and c2=0 z:=z1; z:=z2;

if then else end if;

n

B ¼ n ¼ m ¼ B 2 m ¼

Assume that 10, 3, 750, so that 250:

Example 8.1 n n

þ 2 m) ¼ þ

x ¼ y ¼ x þ y ¼ þ y) B ) (B 638

if 247 and 391 then 638, ((x mod n

¼ c ¼ c ¼ z ¼ þ y) B ¼

250 888, so that 0 and (x mod 638;

1 2 n n

x ¼ y ¼ x þ y ¼ þ y) B þ 2 m) ¼ þ

if 247 and 597 then 844, ((x mod ) (B 844

n n

¼ c ¼ z ¼ þ y) B þ 2m))

¼ c 0, 1, and (((x mod ) (B

250 1094, so that 1 2

n

B ¼

mod 094; n n

þ 2m) ¼

x ¼ y ¼ x þ y ¼ þ y) B ) (B

if 247 and 912 then 1159, ((x mod n

¼ c ¼ z ¼ þ y) B þ

þ ¼ c 1, 0, and ((x mod )

159 250 409, so that 1 2

n n

2m)) B ¼

(B mod 409.

Z 213

8.1 OPERATIONS IN m

8.1.2 Subtraction x y x, y , m,

Given two natural numbers and belonging to the interval 0 compute

z ¼ 2 y) m.

(x mod Taking into account that

m , x y , m,

z x 2 y x 2 y þ m.

must be equal to either or The corresponding algorithm is the

following. m

Algorithm 8.3 Modulo Subtraction

2

z1:=x y; z2:=z1+m;

z1<0 z:=z2; z:=z1;

if then else end if;

n21 n

B , m B x y n-digit

If , and and are base-B numbers, consider two cases:

n n n n n

2 m , x þ 2 y) , B x þ 2 y) þ m . B

2m , x 2 y,0 B (B , (B ,

if then n n

z ¼ x 2 y þ m ¼ þ 2 y) þ m) B

(x (B mod ;

n n n n

, x 2 y B , x þ 2y), z ¼ x 2 y ¼ þ 2y)) B

if 0 then (B (x (B mod .

Algorithm 8.3 can be substituted by the following algorithm where all operands

n

have digits. B m

Algorithm 8.4 Base Modulo Subtraction

2 2

z1:=(x+B**n y) mod B**n; c1:=(x+B**n y)/B**n;

z2:=(z1+m) mod B**n;

c1=1 z:=z1; z:=z2; end if;

if then else

B ¼ n ¼ m ¼

Assume that 10, 3, 750:

Example 8.2 n n

2 y ¼ xþ 2 y) ¼ þ ¼

x ¼ y ¼ B 609, (B 247 609 856, so

if 247 and 391 then n n n

¼ z ¼ 2 y)) B þ m) B ¼ þ

c 0 and (((xþ (B mod ) mod (856 750)

that 1 ¼

mod 1000 606; n n

x ¼ y ¼ B 2 y ¼ xþ 2 y) ¼ þ ¼

if 391 and 247 then 753, (B 391 753 1144, so

n n

¼ z ¼ 2 y)) B ¼

c 1 and (xþ (B mod 144.

that 1

8.1.3 Multiplication

x y [ Z ¼ f0, . . . m 2 z ¼ x.y m. x, y,

Given and 1, , 1g, compute mod Assume that

m n n

m ¼ B m

m B m , B (if the modulo reduction

and are represented in base and that

is trivial).

214 FINITE FIELD OPERATIONS

8.1.3.1 Multiply and Reduce x

The ﬁrst algorithm consists of (1) multiplying by

y, p, p m.

obtaining a 2.n-digit intermediate result and (2) reducing modulo The fol-

lowing multiplication and division procedures must have been deﬁned:

multiply (x, y: digit_vector (0..n-1); z:

procedure in out

digit_vector (0..2*n-1));

divide (x: digit_vector (0..2*n-1); y:

procedure in in

digit_vector (0..n-1); q: digit_vector (0..n-1); r:

out

digit_vector (0..n-1));

out y, z ¼ x.y,

Given two natural numbers x and the ﬁrst procedure generates the product

q r x ¼ q.y þ r,

and the second one the quotient and the remainder such that with

r , y. For that purpose any one of the multiplication (Chapter 5) and division

(Chapter 6) algorithms can be used. The following algorithm is based on the property:

z ¼ x.y m z , m q

mod if, and only if, and there exists a natural number such that

x.y ¼ q.m þ z. B m

Algorithm 8.5 Base Modulo Multiplication, Multiply and Reduce

multiply (x, y, p);

divide (p, m, q, z);

8.1.3.2 Modiﬁed Shift-and-Add Algorithm An alternative solution consists of

m

using Algorithm 5.1 and reducing modulo in every step.

m

Algorithm 8.6 Modulo Shift-and-Add Algorithm

p(n):=0;

i 0..n-1

for in loop

p(n-1-i):=(p(n-i)*B+x(n-1-i)*y) mod m;

end loop;

z:=p(0); n21 n

B , m B x y n-digit

Assume now that and that and are base-B numbers.

Observe that

p(n i):B þ x(n i):y þ

1 (m 1):B (B 1):(m 1)

n

¼ , (8:1)

(2:B 1):(m 1) (2:B 1):B

p(n 2 i).B þ x(n 2 2 i).y þ

so that 1 is an (n 2)-digit number and

p(n i):B þ x(n i):y ¼ q:m þ r

1 (8:2)

Z 215

8.1 OPERATIONS IN m

where q ,

2:B 1 (8:3)

is a 2-digit number. In order to execute Algorithm 8.6, two procedures must be

x.B þ a.y, x y n-digit

deﬁned: the ﬁrst one computes where and are numbers and

a is a digit: shift_and_add (x, y: digit_vector (0..n-1); a:

procedure in in

digit; z: digit_vector(0..n+1));

out

the second one is a division procedure:

divide (x: digit_vector (0..n+1); y: digit_

procedure in in

vector (0..n-1); q: digit_vector (0..1); r:

out out

digit_vector (0..n-1));

Algorithm 8.6 can be substituted by the following algorithm where all operands have

n digits: m

Algorithm 8.7 Base-B Modulo Shift-and-Add Algorithm

p(n):=0;

i 0..n21

for in loop

shift_and_add (p(n-i), y, x(n-1-i), z1);

divide (z1, m, q, p(n-1-i));

end loop;

z:=p(0);

B ¼

In base 2 the execution of the main operation of Algorithm 8.6, namely,

p(n i) ¼ i):2 þ x(n i):y) m,

1 (p(n 1 modulo

can be performed in a slightly different way. According to (8.2) and (8.3)

p(n i):2 þ x(n i):y ¼ q:m þ r

1

q , q

where 3, so that is either 0, 1, or 2.

Algorithm 8.8

p1:=p(n-i)*2; p2:=p1+x(n-i-1)*y-m;

p2<0 p3:=p2+m; p(n-1-i):=p3;

if then

else

p3:=p2-m;

p3<0 p(n-1-i):=p2; p(n-1-i):=p3;

if then else end if;

end if;

216 FINITE FIELD OPERATIONS

p2 p3

Algorithm 8.8 can be simpliﬁed. On the one hand and cannot be

simultaneously negative:

p2 ¼ i) þ x(n i m,

2:p(n 1):y

2m p2 , p2,0 p3 ¼ p2 þ m 2 m þ m ¼

so that 2.m; if then 0. On the other

hand, instead of computing

p2 ¼ p1 þ x(n i m,

1):y

k ¼ m 2 y p2

the value of can be precalculated (outside the main loop) so that is

p1 2 m x(n 2 i 2 ¼ p1 2 k ¼ p1 2 m þy

equal to either if 1) 0 or if

x(n 2 i 2 ¼

1) 1. The modiﬁed algorithm is the following.

m

Algorithm 8.9 Base-2 Modulo Shift-and-Add Algorithm

p(n):=0; k:=m-y;

i 0..n-1

for in loop

x(n-i-1)=0 w:=m; w:=k;

if then else end if;

* 2; p2:=p1-w;

p1:=p(n-i)

p2<0 p3:=p2+m; p3:=p2-m;

if then else end if;

p3<0 p(n-1-i):=p2; p(n-1-i):=p3;

if then else end if;

end loop;

z:=p(0);

8.1.3.3 Montgomery Multiplication Montgomery

In some cases the use of the

product concept ([MON1985]) allows one to reduce the computation complexity.

Only the binary case (B¼2) will be studied. The corresponding algorithm is based

n-bit x, y, m,

on the fact that, given three natural numbers and such that

m x , m, y , m,

odd, and

z , m

it is relatively easy to ﬁnd a natural number such that

n m ¼ x:y m:

(z:2 ) mod mod (8:4)

n

m m

As is an odd number, the greatest common divisor of 2 and is 1, so that there

2n 2n n ¼ m,

, such that 2 . 2 1 mod and the preceding

exits a natural number, denoted 2

relation can be written in the form n

z ¼ x:y:2 m:

mod (8:5)

x y.

Relation (8.5) deﬁnes the Montgomery product of by The following algorithm

z.

computes Z 217

8.1 OPERATIONS IN m

Algorithm 8.10 Montgomery Product

r(0):=0;

i 1..n

for in loop

a:=r(i-1)+x(i-1)*y;

r(i):=(a+a(0)*m)/2;

end loop;

r(n)<m z:=r(n);

if then

z:=r(n)-m;

else end if;

It is based on the following lemmas.

Lemma 8.1

i i1 i2

r(i):2 ; þ x(i þ þ x(0):2 m, 8i .

0

1):2 2):2 mod 0:

(x(i ):y

Proof m a þ a(0).m

First observe that if is odd (m(0)¼1) then at each step is even:

þ a(0):m) ¼ þ a(0):m(0)) ¼ þ a(0)) ¼

(a mod 2 (a(0) mod 2 (a(0) mod 2 0:

Then the property is demonstrated by induction. At the ﬁrst execution of the

iteration, a ¼ r(0) þ x(0):y ¼ x(0):y;

r(1):2 ¼ a þ a(0):m ¼ x(0):y þ a(0):m ; x(0):y m:

mod

i,

At step number

a ¼ r(i þ x(i

1) 1):y,

i i1 i1 i1 i1 i1

r(i):2 ¼ a:2 þ a(0):2 :m ¼ r(i þ x(i :y þ a(0):2 :m

1):2 1):2

i2 i1

; þ þ x(0):2 þ x(i :y m

0

(x(i 2):2 ):y 1):2 mod

i1

; þ þ x(0):2 m:

0

(x(i 1):2 ):y mod

Lemma 8.2 r(i) , 2:m:

Proof The property is demonstrated by induction. At the ﬁrst execution of the

iteration, a ¼ r(0) þ x(0):y ¼ x(0):y y , m,

r(1) ¼ þ a(0):m)=2 þ m)=2 , þ m)=2 ¼ m:

(a (a (m

218 FINITE FIELD OPERATIONS

i,

At step number a ¼ r(i þ x(i , þ y ,

1) 1):y 2:m 3:m,

r(i) ¼ þ a(0):m)=2 , þ m)=2 ¼

(a (3:m 2:m:

A direct consequence of Lemmas 8.1 and 8.2 is that

n

r(n):2 ; x:y m r(n) ,

mod and 2:m,

z r(n) r(n) 2 m.

so that is either or has been deﬁned:

Assume that the procedure Montgomery_product

Montgomery_product (x, y, m: in bit_vector

procedure

2 2

(0..n 1); z: out bit_vector (0..n 1));

and that the value of ¼ m

2:n

2 mod (8:6)

2n

exp z ¼ x.y m

has been previously computed. Then mod can be computed as follows:

n n n n

:2 m ¼ m:

z ¼ 2:n

):2 mod (x:y:2 ):( exp 2n):2 mod

(x:y:2

Algorithm 8.11 Modular Product Based on the Montgomery Product

Montgomery_product (x, y, m, z1);

Montgomery_product (z1, exp_2n, m, z);

n ¼ m ¼ x ¼ y ¼ x ¼

8, 239, 217, 189; in base 2, 11011001;

Example 8.3 ¼

exp_2n ¼ 16 mod 239 50.

2 x y:

First compute the Montgomery product of and

r(0) ¼ 0,

a ¼ r(0) þ x(0):y ¼ r(1) ¼ þ ¼

189; (189 239)=2 214;

a ¼ r(1) þ x(1):y ¼ r(2) ¼ ¼

214; 214=2 107;

a ¼ r(2) þ x(2):y ¼ r(3) ¼ þ ¼

107; (107 239)=2 173;

a ¼ r(3) þ x(3):y ¼ þ ¼ r(4) ¼ ¼

173 189 362; 362=2 181;

a ¼ r(4) þ x(4):y ¼ þ ¼ r(5) ¼ ¼

181 189 370; 370=2 185;

a ¼ r(5) þ x(5):y ¼ r(6) ¼ þ ¼

185; (185 239)=2 212;

a ¼ r(6) þ x(6):y ¼ þ ¼ r(7) ¼ þ ¼

212 189 401; (401 239)=2 320;

Z 219

8.1 OPERATIONS IN m

a ¼ r(7) þ x(7):y ¼ þ ¼ r(8) ¼ þ ¼

320 189 509; (509 239)=2 374;

¼ ¼

z 374 239 135;

1 z ¼

in base 2 10000111;

1 exp_2n:

z and

then compute the Montgomery product of 1

r(0) ¼ 0,

a ¼ r(0) þ x(0):y ¼ r(1) ¼ ¼

50; 50=2 25;

a ¼ r(1) þ x(1):y ¼ þ ¼ r(2) ¼ þ ¼

25 50 75; (75 239)=2 157;

a ¼ r(2) þ x(2):y ¼ þ ¼ r(3) ¼ þ ¼

157 50 207; (207 239)=2 223;

a ¼ r(3) þ x(3):y ¼ r(4) ¼ þ ¼

223; (223 239)=2 231;

a ¼ r(4) þ x(4):y ¼ r(5) ¼ þ ¼

231; (231 239)=2 235;

a ¼ r(5) þ x(5):y ¼ r(6) ¼ þ ¼

235; (235 239)=2 237;

a ¼ r(6) þ x(6):y ¼ r(7) ¼ þ ¼

237; (237 239)=2 238;

a ¼ r(7) þ x(7):y ¼ þ ¼ r(8) ¼ ¼

238 50 288; 288=2 144;

z ¼ 144; ¼

conclusion: 217 189 mod 239 144.

In the case of multioperand modular products an elegant presentation—not

## T Z Z

always an effective one—is based on the deﬁnition of a mapping from into :

m m

n

T(x) ¼ x:2 m:

mod (8:7)

Use the following notation for representing the Montgomery product:

n

MP(x,y) ¼ x:y:2 m:

mod (8:8)

Then the following properties are evident:

T(x) ¼ MP(x, exp 2n), (8:9)

1

T ¼ MP(x,

(x) 1), (8:10)

MP(T(x), T(y)) ¼ T(x:y m),

mod (8:11)

8 x y Z .

and in m T m

According to (8.11), the transformation replaces the mod product by the Mon-

tgomery product. The following algorithm computes the product

z ¼ x :x : :x m:

mod

k

1 2

220 FINITE FIELD OPERATIONS

Algorithm 8.12 Multioperand Modular Product Based on the

Montgomery Product

i 1..k Montgomery_product(x(i), exp_2n, m,

for in loop

y(i)); end loop;

p(1):=y(1);

i 2..k Montgomery_product(p(i-1), y(i), m,

for in loop

p(i)); end loop;

z:=Montgomery_product(p(k), 1, m, z); k

The preceding algorithm includes 2.k Montgomery products, instead of modular

products if a classical multioperand product algorithm were used. Generally, the

shorter computation time of the Montgomery product does not compensate the mul-

tiplication by 2 of the number of primitive operations. This drawback disappears if

many operands are known to be identical, as is the case if an exponential function

k

x

such as is computed (Section 8.1.4).

8.1.3.4 Speciﬁc Ring m

In the preceding algorithms is a parameter whose value is

m,

any natural number greater than 1. For some particular values of speciﬁc algor-

k

m ¼ B 2 c c, m

ithms can be deﬁned. As an example, if for some small the modulo

x n-digit

reduction is easier. Assume that is a 2.n-digit number (the product of two

n n

m ¼ B 2 c, c B x

numbers) and with . Then can be decomposed in the form

n n

þ x x x B

x ¼ x .B , with and smaller than , so that

1 0 1 0 n

:B þ x m ¼ :c þ x m,

x m ¼ ) mod (x )mod

mod (x 1 0 1 0

where 0 n

x ¼ x :c þ x x :B þ x ¼ x:

1 0 1 0 0

x m, x

So instead of reducing modulo the ﬁrst operation consists of computing , and

0 0 n

x B

x . If is still greater than , the same transformation can be per-

then reducing 0 n 00

0 0 0 0

¼ x þ x x ¼ x þ x z

x .B , .c , and so on. Eventually a number

formed, that is, 1 0 1 0

n x m ¼ z m.

z , B and mod mod

is obtained such that m

Algorithm 8.13 Modulo Reduction

z:=x; z>=B**n

while loop

z1:=z/B**n; z0:=z mod B**n; z:=z1*c+z0;

end loop;

z:=z mod m; n21

B ¼ m

In base 2, with 2 , the last instruction is replaced by

z >=m z:=z2m;

if then end if;

Z 221

8.1 OPERATIONS IN m

Example 8.4

B ¼ n ¼ m ¼ x ¼ y ¼

2, 8, 239, 217, 189;

z ¼ x.y p ¼ x.y ¼ p

In order to compute mod 239, ﬁrst compute 41013; then reduce

¼ 2

8

mod 239 2 17: ¼ þ ; þ ¼

41013 160:256 53 160:17 53 2773;

¼ þ ; þ ¼

2773 10:256 213 10:17 213 383;

¼ þ ; þ ¼

383 1:256 127 1:17 127 144;

Z ¼ 144:

Even more speciﬁc algorithms can be used.

B ¼ n ¼ m ¼ ¼ 2 x

8

Assume again that 2, 8, 239 2 17 and that is a

Example 8.5 x

2.n-bit number. The computation of mod 239 can be performed as follows:

þ x þ x

x x ¼ x 12 8

.2 .2 ;

decompose in the form 2 1 0 0

¼ ¼ x ¼ x þ

12 12 8 8

replace 2 by 33 2 mod 239, and 2 by 17 2 mod 239, so that .33

2

þ x

x .17 ;

1 0

0 0 0 0

x x ¼ x þ x

8

is a 10-bit number that can be decomposed in the form .2 ;

1 0

00 0 0

x ¼ x þ x

8

replace 2 by 17 so that .17 ;

1 0

00

x þ ¼ x

is a 9-bit number, smaller than 3.17 256 307, so that mod 239 is equal to

00 00

x 2

x or 239.

either

x ¼

If 41013 then

¼ þ þ ; þ þ ¼

41013 10:4096 0:256 53 10:33 0:17 53 383;

¼ þ ; þ ¼

383 1:256 127 1:17 127 144:

8.1.4 Exponentiation x

x y [ Z ¼ f0, . . . m 2 e ¼ y m. x, y,

Given and 1, , 1g, compute mod Assume that

m n

m m , . Then

and are represented in base 2 and that 2 n1 :x(n

x ¼ x(0) þ þ þ 1),

2:x(1) 2

e

and can be written in the form (a so-called Horner scheme)

x(n1) x(n2) x(1) x(0)

:y :y :y :y m:

e ¼ 2 2 2 2 2

) ) ) ) mod

(( ((1

The corresponding algorithm is the following.

222 FINITE FIELD OPERATIONS

Algorithm 8.14

e:=1;

i 1..n

for in loop

e:=(e*e) mod m;

x(n-i)=1 e:=(e*y) mod m;

if then end if;

end loop; m

This algorithm includes a lot of mod products. Nevertheless, all the operands

y,

are either 1, or a previously obtained value (e), so that an alternative solution is the

use of the Montgomery product (Section 8.1.3.3, relations (8.7) to (8.11)). The com-

putation is performed as follows: n m T(y) ¼

y T(1) ¼ mod and

1. Substitute the initial operands 1 and by 2

MP(y, exp_2n). m

2. Execute the main loop of Algorithm 8.14 substituting the mod products by

Montgomery products.

21 ¼ MP(e,

T (e) 1).

3. Compute n m exp_2n ¼ m

exp_n ¼ 2n

mod and 2 mod have been previously

Assume that 2 x m:

e ¼ y mod

computed. The following algorithm computes

Algorithm 8.15

e_transformed:=exp_n;

Montgomery_product (y, exp_2n, m, y_transformed);

i 1..n

for in loop

Montgomery_product (e_transformed, e_transformed, m,

e_transformed);

x(n-i)=1

if then

Montgomery_product (e_transformed, y_transformed, m,

e_transformed); end if;

end loop;

Montgomery_product (e_transformed, 1, m, z);

GF( p)

8.2 OPERATIONS IN GF(p),

p Z is the Galois ﬁeld and every nonzero element

If is a prime number, then p

21

y Z y p

of has an inverse . Unless is small—in which case all inverses could have

p 21 p

z ¼ x mod

been previously computed and stored in a table—the computation of

is based on the extended Euclidean algorithm (Chapter 2, Section 2.1.2), which

x

allows the expression of the greatest common divider of two natural numbers

y

and in the form gcd(x, y) ¼ b:x þ c:y

GF( p) 223

8.2 OPERATIONS IN 21

b c x p, z ¼ x p

where and are integers. Given and the computation of mod is made

up of a sequence of integer divisions:

r(0) ¼ p c(0) ¼ 0

r(1) ¼ x c(1) ¼ 1

r(0) ¼ r(1):q(1) þ r(2) c(2) ¼ c(0) c(1):q(1)

r(1) ¼ r(2):q(2) þ r(3) c(3) ¼ c(1) c(2):q(2)

r(2) ¼ r(3):q(3) þ r(4) c(4) ¼ c(2) c(3):q(3)

and so on. r(i) ¼ b(i).p þ c(i).x,

It has been demonstrated (Chapter 2, Section 2.1.2) that so that

r(i) ; c(i):x p:

mod

Taking into account that r(3) , r(2) , x , p,

and gcd(r(2), r(3)) ¼ gcd(r(1), r(2)) ¼ gcd(r(0), r(1)) ¼ gcd(p, x) ¼ 1,

r(i þ

after some ﬁnite number of steps, a remainder 2) is obtained such that

r(i þ ¼ gcd(r(i þ r(i þ ¼ r(i þ ¼

2) 0 and 1), 2)) 1) 1;

so ; c(i þ p

1 1):x mod

and z ¼ c(i þ p:

1) mod

The corresponding algorithm is the following.

## Z

Algorithm 8.16 Inversion in p

r_i:=p; r_iplus1:=x; c_i:=0; c_iplus1:=1;

r_iplus1>1

while loop

q:=r_i/r_iplus1; r_iplus2:=r_i mod r_iplus1;

c_iplus2:=(c_i-q*c_iplus1) mod p;

r_i:=r_iplus1; r_iplus1:=r_iplus2; c_i:=c_iplus1;

c_iplus1:=c_iplus2;

end loop;

z:=c_iplus1;

224 FINITE FIELD OPERATIONS

2p/2 , c(i) , p/2

As a matter of fact, it can be demonstrated that so that, in the

p

preceding algorithm, it is not necessary to perform the mod reduction at each

step. The reduction can be performed at the end of the computation, substituting

the last instruction by

c_iplus1<0 z:=c_iplus1+p; z:=c_iplus1;

if then else end if;

Compute the inverse of 114 mod 239:

Example 8.6

r(0) ¼ r(1) ¼ c(0) ¼ c(1) ¼

239, 144, 0, 1

q(1) ¼ ¼ r(2) ¼ ¼ c(2) ¼ ¼ 1

239=144 1, 239 mod 144 95, 0 11

q(2) ¼ ¼ r(3) ¼ ¼ c(3) ¼ þ ¼

144=95 1, 144 mod 95 49, 1 1:1 2

q(3) ¼ ¼ r(4) ¼ ¼ c(4) ¼ 1 ¼ 3

95=49 1, 95 mod 49 46, 2:1

q(4) ¼ ¼ r(5) ¼ ¼ c(5) ¼ þ ¼

49=46 1, 49 mod 46 3, 2 3:1 5

q(5) ¼ ¼ r(6) ¼ ¼ c(6) ¼ 3 ¼ 78

46=3 15, 46 mod 3 1, 5:15

z ¼ 78 þ ¼

239 161:

## Z

8.3 OPERATIONS IN [x]/f(x)

p

Given a polynomial n1 n

þ f :x þ f :x þ þ f :x þ f :x

f ¼ f 2

(x) n1 n

0 1 2

n f = Z Z /f

of degree ( 0) whose coefﬁcients belong to (p prime), the set [x] (x)

n p p

n, f

of polynomials of degree less than modulo (x), is a ﬁnite ring (Chapter 2, Section

2.2.2).

8.3.1 Addition and Subtraction

Given two polynomials n1

a(x) ¼ a þ a :x þ a :x þ þ a :x

2 and

n1

0 1 2 n1

b(x) ¼ b þ b :x þ b :x þ þ b :x

2 ,

n1

0 1 2

the addition and the subtraction are deﬁned as follows:

a(x) þ b(x) ¼ þ b þ þ b þ þ b þ

2

(a ) (a ):x (a ):x

0 0 1 1 2 2

n1

þ þ b

(a ):x , (8:12)

n1 n1

a(x) b(x) ¼ b þ b þ b þ

2

(a ) (a ):x (a ):x

0 0 1 1 2 2

n1

þ b

(a ):x , (8:13)

n1 n1

þ b a 2 b p.

a and are computed modulo Assume that two procedures

where i i i i

Z 225

8.3 OPERATIONS IN [x]/f(x)

p

modular_addition (a, b: coefﬁcient; m:

procedure in in

module; c: coefﬁcient);

out

modular_subtraction (a, b: coefﬁcient; m:

procedure in in

module; c: coefﬁcient);

out þ b) m 2 b) m

have been deﬁned. They compute (a mod and (a mod (see Sections

8.1.1 and 8.1.2). Then the addition and subtraction of polynomials are performed

componentwise.

Algorithm 8.17 Addition of Polynomials

i 0..n-1

for in loop

modular_addition (a(i), b(i), p, c(i));

end loop;

Algorithm 8.18 Subtraction of Polynomials

i 0..n-1

for in loop

modular_subtraction (a(i), b(i), p, c(i));

end loop;

8.3.2 Multiplication

Given two polynomials n1

a(x) ¼ a þ a :x þ a :x þ þ a :x

2 and

n1

0 1 2 n1

b(x) ¼ b þ b :x þ b :x þ þ b :x

2 ,

n1

0 1 2

z(x)¼a(x).b(x)

their product can be computed as follows: n1

z(x) ¼ a :b(x) þ a :b(x):x þ a :b(x):x þ þ a :b(x):x

2 n1

0 1 2

¼ þ a :b(x)):x þ a :b(x)):x þ þ a :b(x)):x

( ((0:x n1 n2 1

þ a :b(x): (8:14)

0

The corresponding formal algorithm is the following.

Algorithm 8.19

z:=zero;

i 1..n z:=z*x+a(n-i)*b;

for in loop end loop;

The computation primitives necessary for executing Algorithm 8.19 are:

x,

the multiplication of a polynomial by

the multiplication of a polynomial by a coefﬁcient,

the addition of polynomials.

226 FINITE FIELD OPERATIONS

The addition is performed componentwise (Algorithm 8.17). The multiplication of a

polynomial by a coefﬁcient is also computed componentwise. Assume that a

procedure modular_product (a, b: coefﬁcient; m: module;

procedure in in

c: coefﬁcient);

out c ¼ a.b m

has been deﬁned. It computes mod (see Section 8.1.3). Then the following

a(x) b:

procedure computes the product of by a coefﬁcient

by_coefﬁcient (a: polynomial; b: coefﬁcient;

procedure in in

p: module; c: polynomial)

in out

is begin

i 0..n-1 modular_product(a(i), b, p, c(i));

for in loop

end loop;

end procedure;

It remains to generate a procedure for computing the multiplication of a

a(x) x.

polynomial by First observe that n1 n

f þ f :x þ f :x þ þ f :x þ f :x ; f

2 0 mod ,

n1 n

0 1 2

so that n n1

; r þ r :x þ r :x þ þ r :x f

x 2 mod , (8:15)

n1

0 1 2

where ¼ f =f p:

r mod (8:16)

i i n

a(x).x:

Compute now n1

þ a :x þ a :x þ þ a :x ¼ a :x þ a :x þ

2 2

):x

(a n1

0 1 2 0 1

n1 n

þ a :x þ a :x

n2 n1 n1 n1

; a :x þ a :x þ þ a :x þ ðr þ r :x þ r :x þ þ r :x Þ

2 2

n2 n1

0 1 0 1 2

¼ a :r þ þ a :r þ þ a :r þ

2

(a ):x (a ):x

n1 n1 n1

0 0 1 1 2

n1

þ þ a :r :

(a ):x

n2 n1 n1

The corresponding procedure is

by_x (a: polynomial; p: module; b:

procedure in in

polynomial)

out is

begin

modular_product (a(n-1), r(0), p, b(0));

i 1..n-1

for in loop

Z 227

8.3 OPERATIONS IN [x]/f(x)

p

modular_product (a(n-1), r(i), p, c);

modular_addition (a(i-1), c, p, b(i));

end loop;

end procedure;

Thus Algorithm 8.19 is equivalent to the following one.

Algorithm 8.20 Multiplication of Polynomials, First Version

i 0..n-1 z(i):=0;

for in loop end loop;

i 1..n

for in loop

by_x(z, p, z1);

by_coefﬁcient(b, a(n-i), p, z2);

j 0..n-1 modular_addition(z1(j), z2(j),

for in loop

p, z(j)); end loop;

end loop;

The preceding algorithm can be decomposed at the coefﬁcient level. The oper-

ations corresponding to the main loop are the following:

next_z(0)=(z(n-1).r(0)+b(0).a(n-i)) mod p,

next_z(1)=(z(0)+z(n-1).r(1)+b(1).a(n-i)) mod p,

next_z(2)=(z(1)+z(n-1).r(2)+b(2).a(n-i)) mod p,

...

next_z(n-1)=(z(n-2)+z(n-1).r(n-1)+b(n-1).a(n-i)) mod p,

z=next_z. r ¼ 2f /f p

The complete algorithm is the following (the values of mod should

i i n

have been previously computed).

Algorithm 8.21 Multiplication of Polynomials, Second Version

i 0..n-1 z(i):=0;

for in loop end loop,

i 1..n

for in loop

modular_product (z(n-1), r(0), p, c(0)); -- c =z .r mod p

0 n-1 0

=a .b mod p

modular_product (a(n-i), b(0), p, d(0)); -- d 0 n-i 0

modular_addition (c(0), d(0), p, next_z(0));

-- next_z =z .r +a .b mod p

0 n-1 0 n-i 0

i 1..n-1

for in loop =z .r mod p

modular_product (z(n-1), r(i), p, c(i)); -- c i n-1 i

modular_product (a(n-i), b(i), p, d(i)); -- d =a .b mod p

i n-i i

modular_addition (c(i), d(i), p, e(i)); -- e =z .r +a .b mod p

i n-1 i n-i i

modular_addition (z(i-1), e(i), p, next_z(i));

=z +z .r +a .b mod p

-- next_z

i i-1 n-1 i n-i i

end loop;

z:=next_z;

end loop;

228 FINITE FIELD OPERATIONS

a 2 a(n 2 . . .

Instead of addressing a new coefﬁcient of at each step (a(n 1), 2), ,

a(0)), an alternative solution is to use a procedure

right_rotate polynomial)

procedure(a: inout n21

a þ a þ a þ þ a a þ a þ a þ þ

2 2

. . . . . .

that substitutes .x .x .x by .x .x

n21 n21

0 1 2 0 1

n21

a .x .

n22

Algorithm 8.22 Multiplication of Polynomials, Third Version

i 0..n-1 z(i):=0;

for in loop end loop;

i 1..n

for in loop

modular_product (z(n-1), r(0), p, c(0));

modular_product (a(n-1), b(0), p, d(0));

modular_addition (c(0), d(0), p, next_z(0));

i 1..n21

for in loop

modular_product (z(n-1), r(i), p, c(i));

modular_product (a(n-1), b(i), p, d(i));

modular_addition (c(i), d(i), p, e(i));

modular_addition (z(i-1), e(i), p, next_z(i));

end loop;

z:=next_z;

righ_rotate(a);

end loop; n

GF( p

8.4 OPERATIONS IN ) n

GF( p

f Z [x]/f (x) is the Galois ﬁeld ), so

If is an irreducible polynomial then p 21

a(x) a

that every nonzero polynomial has a multiplicative inverse (x). Given

two polynomials n1

a ¼ a þ a :x þ a :x þ þ a :x

2 and

n1

0 1 2 n1 n

f ¼ f þ f :x þ f :x þ þ f :x þ f :x

2 ,

n1 n

0 1 2

a variant of the extended Euclidean algorithm (Chapter 2, Section 2.1.2) allows the

f a

expression of the greatest common divider of and in the form

gcd( f a) ¼ b:a þ c:f :

,

21

gcd( f, a) ¼ a ¼ b(x) f.

In particular, if 1 then (x) mod a

returns the degree of and

The following formal algorithm, in which degree(a)

21

a b, z(x) ¼ a

interchanges and computes (x).

swap(a, b) n

GF( p 229

8.4 OPERATIONS IN )

Algorithm 8.23

u:=f; v:=a; c:=0; e:=1;

m:=degree(u); t:=degree(v);

-1

;

t=0 result:=(v(0))

if then

else t>0

while loop

m<t swap(u,v); swap(c,e); swap(m,t);

if then -1 m-t

q:=u(m)*(v(t)) *x ; r:=u-(v*q); cc:=c-(e*q);

u:=v; v:=r; c:=e; e:=cc;

m:=t; t:=deg(v);

end loop;

-1

z:=e*(v(0)) ;

end if;

Example 8.7 a(x) ¼ þ x

p ¼ n ¼ f ¼ þ x þ x 4 2

, 1 ;

2, 4, 1

(x)

p ¼ u(m) ¼

As 2, Algorithm 8.23 can be simpliﬁed; in particular,

21 ¼ v(0) ¼

v (t) 1.

21

a

Compute (x):

u ¼ þ x þ x v ¼ þ x c ¼ e ¼

4 2

1 , 1 , 0, 1

m ¼ t ¼

4, 2

q ¼ x r ¼ þ x þ x þ x ¼ þ x þ x cc ¼ ¼ x

2 4 2 2 2 2 2

, 1 (1 ):x 1 , 0 1:x

u ¼ þ x v ¼ þ x þ x c ¼ e ¼ x

2 2 2

1 , 1 , 1,

m ¼ t ¼

2, 2 þ x þ x ¼ x, cc ¼ x :1 ¼ þ x

q ¼ r ¼ þ x 2 2 2 2

(1 ):1 1 1

1, 1

u ¼ þ x þ x v ¼ x, c ¼ x e ¼ þ x

2 2 2

1 , , 1

m ¼ t ¼

2, 1

q ¼ x, r ¼ þ x þ x x:x ¼ þ x, cc ¼ x þ x ¼ x þ x þ x

2 2 2 2 3

1 1 (1 ):x

u ¼ x, v ¼ þ x, c ¼ þ x e ¼ x þ x þ x

2 2 3

1 1 ,

m ¼ t ¼

1, 1

q ¼ r ¼ x þ x):1 ¼ cc ¼ þ x þ x þ x ¼ þ x þ x

2 2 3 3

1, (1 1, 1 (x ):1 1

u ¼ þ x, v ¼ c ¼ x þ x þ x e ¼ þ x þ x

2 3 3

1 1, , 1

m ¼ t ¼

1, 0

z ¼ þ x þ x :

3

1

230 FINITE FIELD OPERATIONS

Effectively,

þ x þ x þ x ¼ þ x þ x þ x þ x þ x ¼ þ x þ x þ x

2 3 3 2 3 5 2 5

(1 ):(1 ) 1 1

¼ þ x þ x þ ¼ þ x þ x

4 4

(1 ):x 1 1 mod (1 ):

In order to execute Algorithm 8.23, the following procedures must be deﬁned:

the procedure

. degree (a: polynomial; deg: natural);

procedure in out

deg a;

computes the degree of

the procedure

. invert (a: coefﬁcient; p: module; b:

procedure in in out

coefﬁcient);

21

a p;

computes mod Algorithm 8.16 could be used;

the procedure has already been deﬁned;

by_coefﬁcient

. the procedure

. add (a, b: polynomial; p: module; c:

procedure in in out

polynomial);

computes the sum of two polynomials; Algorithm 8.17 could be used;

the procedure

. sub (a, b: polynomial; p: module; c:

procedure in in out

polynomial);

computes the difference of two polynomials; Algorithm 8.18 could be used;

the procedure

. shift (a: polynomial; k: natural; c:

procedure in in out

polynomial); k

c(x) ¼ a(x).x k-position

computes ; it is equivalent to a right-shift of the

a, k

coefﬁcients of with the lower-degree coefﬁcients set to 0.

The following algorithm is deduced from Algorithm 8.23 and from the previous

one

procedure deﬁnitions (zero and stand for the polynomials 0 and 1, respectively):

n

GF(p

Algorithm 8.24 Inversion in )

u:=f(0..n-1); f_n:=f(n); v:=a; c:=zero; e:=one;

degree(v,t);

t=0

if then

invert(v(0), p, result(0)); i 1..n-1

for in

result(i):=0;

loop end loop;

n

GF( p 231

8.4 OPERATIONS IN )

else

j:=n-t; --the initial value of m is deg(f)=n

-1

invert(v(t), p, inverted_v_t); --(v(t)) -1

k:=(f_n*inverted_v_t) mod p; --f_n.(v(t)) -1

by_coefﬁcient(v, k, p, k_v); --v.f_n.(v(t))

-1 n-t

shift(k_v, j, shifted_v); --v.f_n.(v(t)) .x =v.q

sub(u, shifted_v, p, r); --r=u - v.q -1

by_coefﬁcient (e, k, p, e_v); --e.f_n.(v(t))

-1 n-t

shift(e_v, j, shifted_e); --e.f_n.(v(t)) .x =e.q

sub(c, eq, p, cc); --cc=c - e.q

degree(r, deg_v);

j:=t - deg_v;

j>=0 u:=v; v:=r; c:=e; e:=cc; m:=t; t:=deg_v;

if then

u:=r; c:=cc; m:=deg_v;

else end if;

t>0

while loop

j:=m-t; -1

invert(v(t), p, inverted_v_t); --(v(t)) -1

k:=(u(m)*inverted_v_t) mod p; --u(m).(v(t)) -1

by_coefﬁcient (v, k, p, k_v); --v.u(m).(v(t))

-1 n-t

shift(k_v, j, shifted_v); --v.u(m).(v(t)) .x =v.q

sub(u, shifted_v, p, r); --r=u - v.q -1

by_coefﬁcient(e, k, p, e_v); --e.u(m).(v(t))

-1 n-t

shift(e_v, j, shifted_e); --e.u(m).(v(t)) .x =e.q

sub(c, eq, p, cc); --cc=c - e.q

degree(r, deg_v);

j:=t - deg_v;

j>=0 u:=v; v:=r; c:=e; e:=cc; m:=t; t:=deg_v;

if then

u:=r; c:=cc; m:=deg_v;

else end if;

end loop; -1

invert(v(0), p, inverted_v_t); --(v(0)) -1

by_coefﬁcient(e, inverted_v_t, p, result); --e. (v(0))

end if;

A different method, based on a modiﬁcation of the Itoh – Tsujii algorithm

f

([ITO1988]), can be used if (x) is a binomial ([WOO2000], [BAI2001]). First

n21

þ þ p

r ¼ þ p þ p 2 r

. . . , then (Chapter 2, Section 2.2.4) (a(x))

observe that if 1

## Z

is an element of (a 0-degree polynomial). As

p 1 r1 r

¼ =(a(x))

(a(x)) (a(x)) , n

GF(p ) and to

the problem is reduced to the computation of exponential functions in

GF(p).

the inversion in 21

z(x) ¼ a (x).

The following formal algorithm computes

Algorithm 8.25

b:=1;

i 1..n-1

for in loop r(i) i

d:=a**(p**i); --d=a where r(i)=p

r(1) r(i-1) r(i)

b:=b*d; --b=1.a .....a .a

r-1

--b=a

end loop;

232 FINITE FIELD OPERATIONS

r

g:=b*a; --g=a r

k:=(1/g(0)) mod p; --k=1/a

r-1 r -1

z:=b*k; --z=a /a =a

In order to execute the preceding algorithm the following procedures must be

deﬁned:

the procedure

. multiply (a, b, f: polynomial; p: module;

procedure in in

z: polynomial);

out a b f;

computes the product of by modulo Algorithm 8.20, 8.21, or 8.22 could be

used;

the procedures and have already been deﬁned;

by_coefﬁcient invert

. the procedure

. procedure (a, f: polynomial; p:

exponentiation in in

module; i: natural; b: polynomial);

in out

r(i) i

a f, r(i) ¼ p

computes modulo where .

It remains to generate the preceding procedure. First recall (Chapter 2, Section

n

a b g

. . . GF(p

, , , are elements of ), then

2.2.5) that if p p p p

a b g a b g

þ þ þ ¼ þ þ þ :

) (8:17)

( i

r(i) ¼ p , then

More generally, if r(i) r(i) r(i) r(i)

a b g a b g

þ þ þ ¼ þ þ þ :

( ) Z

a (an element of ), then

Observe also that, given a coefﬁcient k p

p

a ¼ a ,

k

k

and, more generally, r(i)

a ¼ a : (8:18)

k

k

From (8.17) and (8.18) the following relation is deduced:

r(i) r(i)

¼ a þ a :x þ a :x þ þ a :x :

2:r(i) (n1):r(i)

(a(x)) n1

0 1 2

f

Assume now that is a binomial n c

f ¼ x

(x)

n

GF( p 233

8.4 OPERATIONS IN )

n p 2 p n ¼

and that divides 1 ( mod 1), so that

i

p ¼ q(i):n þ 1:

Then k:r(i) k:q(i):n k n k:q(i) k k:q(i) k

x ¼ x :x ¼ :x ¼ c :x :

(x )

The values of k:q(i)

f ¼ c p

mod (8:19)

ki f

can be computed in advance (an algorithm for computing is given in Appendix

ki

8.1), so that r(i) r n1

¼ a þ f :a :x þ f :a :x þ þ f :a :x :

2

(a(x)) (8:20)

n1

0 1i 1 2i 2 (n1)i

The corresponding procedure is the following:

exponentiation

procedure (a, f: polynomial; p: module;

exponentiation in in

i: natural; b: polynomial)

in out is

begin

b(0):=a(0);

k 1..n21 modular_product (f(k,i), a(k), p, b(k));

for in loop

end loop;

end procedure;

The complete inversion algorithm is deduced from Algorithm 8.25.

Algorithm 8.26 Inversion, Second Version

b:=one;

i 1..n-1

for in loop r(i)

exponentiation (a, f, p, i, d); --d=(a(x)) r(i)

multiply (b, d, f, p, e); --e:=b.(a(x))

r-1

--e=(a(x))

end loop; r

multiply (e, a, f, p, g); --g=(a(x))

h:=g(0); -1

invert (h, p, k); --k=h r-1 r

by_coefﬁcient (e, k, p, z); --z=(a(x)) /(a(x))

In order to reduce the number of calls to the procedure the

exponentiation

following property can be used.

234 FINITE FIELD OPERATIONS

k l

s ¼ þ p þ p þ þ p t ¼ þ p þ p þ þ p k

2 2

. . . . . .

If 1 and 1 , where

Property 8.1

l ¼ 2

is odd and (k 1)/2, then s t t:u

¼ :((a(x))

(a(x)) (a(x)) ,

k2l

u ¼ p .

where

Proof l kl kl klþ1 k

t:u ¼ ð1 þ p þ p þ þ p Þ:p ¼ p þ p þ þ p

2 ,

where k l ¼ k ¼ þ ¼ l þ

(k 1)=2 (k 1)=2 1,

so that k

þ þ p :

t þ t:u ¼ þ p þ p 2

1 m m21

2 l ¼ 2

kþ1 k ¼ 1, then 2 1, and the same

If is a power of 2, that is, 2

decomposition can be recursively applied. The following algorithm computes

s k m

s ¼ þ p þ p þ þ p k ¼ 2

z(x) ¼ 2 . . .

, where 1 with 2 1.

(a(x))

Algorithm 8.27

b(0):=a;

j 0..m-1

for in loop

exponentiation (b(2*j), f, p, 2**j, b(2*j+1));

multiply (b(2*j), b(2*j+1), f, p, b(2*(j+1)));

end loop;

z:=b(2*m); i

k ¼ m ¼ r(i) p

7, 3; in the following computation scheme stands for ,

Example 8.8

r(i).r( j) ¼ r(i þ j).

so that b(0) ¼ a,

r(1)

b(1) ¼ a ,

b(2) ¼ b(0):b(1) ¼ a 1þr(1) ,

r(2) r(2)þr(3)

b(3) ¼ ¼ a

(b(2)) ,

b(4) ¼ b(3):b(4) ¼ a 1þr(1)þr(2)þr(3) ,

r(4) r(4)þr(5)þr(6)þr(7)

¼ a

b(5) ¼ ,

(b(4))

b(6) ¼ b(4):b(5) ¼ a 1þr(1)þr(2)þr(3)þr(4)þr(5)þr(6)þr(7) ,

z ¼ b(6): n

GF( p 235

8.4 OPERATIONS IN )

n21

Assume now that is a power of 2, that is,

m

n ¼ þ

2 1: (8:21)

Then n1 kþ1 m

þ þ p ¼ þ p þ p þ þ p k ¼

r ¼ þ p þ p 2 2

1 , where 2 1,

1

and r1 s:p k

¼ s ¼ þ p þ p þ þ p :

2

(a(x)) , where 1

(a(x))

The preceding algorithm can be used for computing

s

b(2:m) ¼ :

(a(x))

It remains to compute p s:p r1

e ¼ ¼ ¼

(b(2:m)) (a(x)) (a(x))

and r

g ¼ e:a ¼ :

(a(x))

The complete inversion algorithm, when

n m

f ¼ x c, p n ¼ n ¼ þ

(x) mod 1, 2 1, (8:22)

is the following.

Algorithm 8.28 Inversion, Third Version

b(0):=a;

j 0..m-1

for in loop

exponentiation (b(2*j), f, p, 2**j, b(2*j+1));

multiply (b(2*j), b(2*j+1), f, p, b(2*(j+1))); s

--b(2.m)=(a(x))

end loop; s.p r-1

exponentiation (b(2*m), f, p, 1, e); --e=(a(x)) =(a(x))

r

multiply (e, a, f, p, g); --g=(a(x))

h:=g(0); -1

invert(h, p, k); --k=h r-1 r

by_coefﬁcient (e, k, p, z); --z=(a(x)) /(a(x))

m

m n 2 ¼

Observe that the main iteration is executed times instead of 1 2 times

as in Algorithm 8.26.

236 FINITE FIELD OPERATIONS

p ¼ f ¼ x 2

17

If 239 and (x) 2, then Algorithm 828 can be applied:

Example 8.9 ¼ þ n ¼ ¼ þ

4

239 14:17 1, 17 2 1;

f can be computed with Algorithm A 8.1.

the coefﬁcients ki x 22

6

Another example is the binomial with

p ¼ ¼ 32 387;

42,798,677,629 2

¼ þ

42,798,677,629 7,133,112,938:6 1:

n 2

As 1 is not a power of 2, Algorithm 8.28 must be slightly modiﬁed.

8.5 BIBLIOGRAPHY

[BAI2001] D. V. Bailey and C. Paar, Efﬁcient arithmetic in ﬁnite ﬁeld extensions with appli-

J. Cryptol.,

cation in elliptic curve cryptography. 153 – 176 (2001).

14(3):

[ITO1988] T. Itoh and S. Tsujii, A fast algorithm for computing multiplicative inverses in

m

GF(2 Math. Computation

) using normal bases. 519 – 521 (1985).

44(4): Math. Compu-

[MON1985] P. Montgomery, modular multiplication without trial division.

tation 519– 521 (1985).

44(4): Elliptic Curve Cryptography.

[ROS1999] M. Rosing, Manning Publications, Greenwich, CT,

1999.

[WOO2000] A. D. Woodbury, Elliptic curve cryptography on smart cards without coproces-

## IFIP CARDIS,

sors. 71– 92 (2000). f

APPENDIX 8.1 COMPUTATION OF ki

i

q(i) p ¼ q(i).n þ

First compute the value of such that 1.

Lemma A8.1 8i . ¼ p:q(i þ q(1):

1:q(i) 1) (A8:1)

Proof By induction,

i i1

¼ p:p ¼ p:(q(i þ ¼ p:q(i þ p ¼ p:q(i þ q(1):n þ

p 1):n 1) 1):n 1):n 1

¼ p:q(i þ q(1)):n þ

( 1) 1,

so that q(i) ¼ p:q(i þ q(1):

1)

q(i)

c p.

Then compute mod f 237

APPENDIX 8.1 COMPUTATION OF ki

Lemma A8.2 q(i) i:q(1)

c p ¼ c p:

mod mod (A8:2)

Proof By induction,

q(i) p:q(i1)þq(1) q(i1) p q(1) q(i1) q(1)

p ¼ c p ¼ :c p ¼ c :c p

c mod mod (c ) mod mod

q(1) i:q(1)

¼ c :c p ¼ c p:

(i1):q(1) mod mod

k.q(i)

¼ c

f .

It remains to compute ki

Lemma A8.3 k:i q(1)

f ¼ b p, b ¼ c p:

mod where mod (A8:3)

ki

Proof According to (A8.2),

k:q(i) i:q(1):k q(1) k:i

c p ¼ c p ¼ p:

mod mod (c ) mod

(Complete Ada source code available.) Consider the following

Example A8.1

case: c ¼ p ¼ n ¼

2, 239, 17:

¼

First observe that 239 14.17þ1 so that ¼

q(1) ¼ b ¼ 14 mod 239 132;

14 and 2

then compute f ¼ 132,

11

f ¼ ¼

132:132 mod 239 216,

12 ¼ ¼

f 216:132 mod 239 71,

13

...

f ¼ f ¼ ¼

2

( ) mod 239 132:132 mod 239 216,

21 11

f ¼ f ¼ ¼

2

( ) mod 239 216:216 mod 239 51,

22 12

f ¼ f ¼ ¼

2

( ) mod 239 71:71 mod 239 22,

23 13

...

238 FINITE FIELD OPERATIONS

f ¼ f ¼ ¼

3

( ) mod 239 216:132 mod 239 71,

31 11

f ¼ f ¼ ¼

3

( ) mod 239 51:216 mod 239 22,

32 12

f ¼ f ¼ ¼

3

( ) mod 239 22:71 mod 239 128,

33 13

and so on. f

The following Ada program computes all the coefﬁcients (the complete source

ki

code is available). f

Algorithm A8.1 Ada Program for Computing ki

procedure frobenius is

type frobenius_matrix is array (0.n-1, 0.n-1) of

coefﬁcient;

f: frobenius_matrix;

q, qq: polynomial;

quotient, power: coefﬁcient;

cr: character;

begin

quotient:=p/n;

for i in 1..n-1 loop

q(i):=(p*q(i-1)+quotient) mod p;

end loop;

power:=(2**quotient) mod p;

qq(0):=1;

for i in 1..n-1 loop

qq(i):=(power*qq(i-1)) mod p;

end loop;

for i in 1..n-1 loop

f(0,i):=1;

for k in 1..n-1 loop

f(k,i):=(f(k-1, i)*qq(i)) mod p;

end loop;

end loop;

for k in 1..n-1 loop

for i in 1..n-1 loop

put("f("); put(k); put(","); put(i); put(")=");

put(f(k,i));

new_line;

end loop;

get(cr);

end loop;

end frobenius;

9

## HARDWARE PLATFORMS

This chapter is devoted to the hardware platforms available to implement the algor-

ithms described in the preceding chapters. In the ﬁrst section, some generalities in

electronic system design are presented. The hardware platforms are then classiﬁed

as instruction-set processor, ASIC based, and reconﬁgurable hardware. Special

emphasis is given to FPGA technologies.

## 9.1 DESIGN METHODS FOR ELECTRONIC SYSTEMS

integrated circuit

With the passing of time, (IC) technology has provided a variety

of implementation formats for system designers. The implementation format deﬁnes

the technology to be used, how the switching elements are organized and how the

system functionality will be materialized. The implementation format also affects

the way systems are designed and sets the limits of the system complexity. Today

complementary metal-oxide semiconductor

the majority of IC systems are based on

(CMOS) technology. In modern digital systems, CMOS switching elements are

prominent in implementing basic Boolean functions such as AND, OR, and NOT.

regularity granularity

With respect to the organization of switching elements, and

of elements are essential parameters. The regularity has a strong impact on the

design effort, because the reusability of a fairly regular design can be very

simple. The problem raised by the regularity is that the structure may limit the

usability and the performances of the resource. The granularity expresses the

level of functionality encapsulated into one design object. Examples of ﬁne-grain,

Synthesis of Arithmetic Circuits: FPGA, ASIC, and Embedded Systems

By Jean-Pierre Deschamps, Géry J. A. Bioul, and Gustavo D. Sutter

#

Copyright 2006 John Wiley & Sons, Inc. 239

240 HARDWARE PLATFORMS

arithmetic and logic units

medium-grain, and coarse-grain are logic gates, (ALUs),

intellectual property components

and (processor, network interfaces, etc.), respect-

ively. The granularity affects the number of required design objects and, thereby, the

required design or integration effort.

Depending on how often the structure of the system can be changed, the three main

dedicated systems, reconﬁgurable

approaches for implementing its functionality are

systems, programmable systems.

and In a dedicated system, the structure is ﬁxed at

application-speciﬁc integrated circuits

the design time, as in (ASICs). In program-

mable systems, the data path of the processor core, for example, is conﬁgured by

every instruction fetched from memory during the decode-phase. The traditional

microprocessor-based computer is the classical example. In reconﬁgurable systems,

the structure of the system can be altered by changing the conﬁguration data, as in

ﬁeld programmable gate arrays (FPGAs).

9.1.1 Basic Blocks of Integrated Systems

The basic building blocks for digital ICs are input, output, data path, memory, and

control, as in a common computer (Figure 9.1). Additionally, a communication net-

work is necessary to interconnect the blocks. The implementation format of each

basic block can, at least theoretically, be any combination of the previous classes.

The consists of regular elements, as in reconﬁgurable arrays, or dedi-

data path

cated pipelined blocks, as in superscalar processors. The granularity of data path

elements can vary from single gates to processor arrays in multiprocessor architec-

tures. A typical data path consists of an interconnection of basic logic (AND, OR,

etc.), arithmetic operators (adders, multipliers, shifters, complement), and registers

to store the intermediate results.

The can be characterized by size, number of ports, latency, and

memory

bandwidth. Latency is the access delay of a randomly chosen data element and

bandwidth is the data rate. Memory components are typically two-dimensional

regular structures, and latency is inversely proportional to the size of memory.

The bandwidth depends on the memory buses, the internal organization of the

Memory Input

Control Output

Datapath

Components of a generic digital processor; arrows represent interconnection

Figure 9.1

network. 241

## 9.1 DESIGN METHODS FOR ELECTRONIC SYSTEMS

memory, and the access logic. The memory is typically organized hierarchically.

The faster and more expensive memories are near the data path; slower, bigger

and cheaper memories are used to store less-frequently accessed data.

The main two memory classes are volatile memories, such as, for example, static

random-access memory (SRAM) or dynamic random-access memory (DRAM), and

nonvolatile memories, such as read-only memory, (ROM), and FLASH memory.

SRAM is a fast memory, but typical implementations need six transistors per bit.

DRAM is a dense memory, with only one transistor per bit, but the latency

(i.e., access delay) is high. The FLASH memory also suffers from high latencies.

The DRAM and FLASH memories are internally asynchronous and have different

latencies for random and sequential accesses. For DRAM, in particular, a variety

of solutions have been proposed for speeding up the overall performances, among

them fast page mode accesses, synchronous interfaces, and intelligent control

interfaces.

The is another important component of an electronic

communication network

system. Communication channels can be divided into dedicated channels (signals)

and shared channels (buses and networks). The buses connect subsystems, and net-

works connect full systems according to the classical deﬁnition. The dedicated chan-

nels may be static point-to-point connections or dynamic switched connections. The

buses can be further divided into data path memory and input/output (I/O) buses,

parallel and serial buses, or synchronous and asynchronous buses, according to

their purpose or physical implementation.

The determines what actions are carried out by the data path at

control module

any time, when and how memory, I/O modules, and data path are communicated or

related. A controller is implemented as a ﬁnite state machine (FSM). The logic of a

FSM can be implemented in different ways, with basic logic gates (AND, OR,

NOT), using arrays of programmable logic devices (PLDs), or programming a

memory (microprogramming). The way to implement FSM registers depends on

the selected technology.

The are used to connect the system to the outside world.

input/output modules

These modules are most often slower than the other system parts. The throughput

necessary in the I/O deﬁnes the communication network and the whole system

organization.

9.1.2 Recurring Topics in Electronic Design

In the electronics industry, competition of one form or another leads to smaller,

faster, cheaper, and better products and related manufacturing techniques. Gordon

Moore’s ([MOO1965]) insight, that the density of chips would double every 18

months, has proved incredibly accurate, and there is no end in sight. Nowadays, elec-

tronics industry leaders apply this principle to forecast three generations ahead. This

competition and rapid growth create outstanding electronic design challenges.

9.1.2.1 Design Challenge: Optimizing Design Metrics The obvious design goal

is to construct and implement within the desired functionality, but the key design

### DESCRIZIONE APPUNTO

Appunti in **inglese **di **Architetture Sistemi Elaborazione **del prof. Mazzocca su **Synthesis of Arithmetic Circuits** - **FPGA**, **ASIC **and **Embedded Systems**: Introduction, Mathematical Background, Number Representation, Arithmetic Operations (Addition and Subtraction, Multiplication, Division), Other Arithmetic Operations, Finite Field Operations, Hardware Platforms, Circuit Synthesis: General Principles, Floating-Point Unit.

I contenuti di questa pagina costituiscono rielaborazioni personali del Publisher valeria0186 di informazioni apprese con la frequenza delle lezioni di Architetture Sistemi Elaborazione e studio autonomo di eventuali libri di riferimento in preparazione dell'esame finale o della tesi. Non devono intendersi come materiale ufficiale dell'università Napoli Federico II - Unina o del prof Mazzocca Nicola.

Acquista con carta o conto PayPal

Scarica il file tutte le volte che vuoi

Paga con un conto PayPal per usufruire della garanzia Soddisfatto o rimborsato