General Principles of Intellectual Property: Concepts of Intellectual Proper...
Chapter 5
1. 1
1
Today: Integer Arithmetic
(H&H 5.1-5.2)
Next: Floating Point Arithmetic
(H&H 5.3)
Lecture Topics
2
Self-study module #4
Project #4 (due no later than 9/26)
Project #5 (due no later than 10/3)
Announcements
2. 2
3
Arithmetic operations:
NEG (negation)
ADD (addition)
SUB (subtraction)
MUL (multiplication)
DIV (division)
Negation is a unary operation (one operand)
Operations on Integers
4
Bitwise operations:
NOT
AND
OR
XOR
NOT is a unary operation (one operand)
Operations on Integers
3. 3
5
Shift operations:
SLL (shift left logical)
SRL (shift right logical)
SRA (shift right arithmetic)
No need for “shift left arithmetic” – same as
“shift left logical”
Operations on Integers
6
Math unit:
addition and subtraction
bitwise operations
Shift unit:
shift operations
Multiply and Divide unit:
multiplication and division
Integer Circuits
4. 4
7
C/C++ shift operations:
SLL <<
SRL >> (when value is unsigned)
SRA >> (when value is signed)
Example: examine an integer value and
count the number of bits which are 1s
(C source code on next slide).
Bitwise Operators in C/C++
8
#include <stdio.h>
int main()
{
unsigned int i, n, val = 0x7890ABCD;
for (i=0; i<32; i++)
{
n = n + ((val >> i) & 0x00000001);
}
printf( "value: %08x count: %dn", val, n );
return 0;
}
value: 7890abcd count: 16
5. 5
9
Logarithmic barrel shifter: for N bit register,
use log2 N levels of multiplexers
Example: 8 bit register ==> 3 levels
1st level – shift 4 bits
2nd level – shift 2 bits
3rd level – shift 1 bit
Shift Circuits
10
Right Logical Shifter (8 bits)
6. 6
11
Control signal (2 bits):
No shift
Shift left logical
Shift right logical
Shift right arithmetic
Combined Shift Circuits
12
Require several steps
Simple versions use sequential logic
Multiply: N bits X N bits ==> 2N bits
Divide: 2N bits / N bits ==> N bits
Multiplication and Division
8. 8
15
Unsigned Serial Division
16
Serial multiply (and divide) require sequence
of steps
Signed multiply (and divide) must account
for sign bits
More complex circuits can reduce number of
steps
Summary
9. 9
17
C is the parent of C++ (and thus a subset)
C++ features which are not part of C:
Classes (and thus no I/O classes)
Templates (and thus no STL)
One other minor difference: C does not
support local variables in "for" statements
for (int i=0; i<10; i++) // illegal
Programming in C
18
No classes, so no stream class library.
Thus, all I/O must be done via functions.
Example:
printf( "Sum: %dn", total );
Rich set of functions in the standard library;
most programmers only use a few.
I/O in C
10. 10
19
Standard Streams
Monitor
Executing
program
Keyboard
stdoutstdin
Output DeviceInput Device
stdin – standard input stream
stdout – standard output stream
20
File Streams
Executing
program
file object file object
OutputInput
Function fopen() used to create a file object, interact
with the operating system's file system.
File (on disk)File (on disk)
11. 11
21
Returns one character (one byte, converted to four
bytes) from the standard input stream. Returns -1
when end-of-file is true.
int getchar();
Sends one character (four bytes, converted to one
byte) into the standard output stream. Returns a
copy of the character (-1, if an error occurs).
int putchar( int );
Character I/O
22
#include <stdio.h>
int main()
{
int input;
for (;;)
{
input = getchar();
if (input == EOF) break;
putchar( input );
}
return 0;
}
12. 12
23
Translate (compile and link) the program. Then,
execute it and supply input from the keyboard.
<2 north:~/Examples > gcc -Wall example08.c
<3 north:~/Examples > a.out
This is the first line.
This is the first line.
And this is the last line.
And this is the last line.
^D
24
Execute it again and use input redirection to
supply input from a file.
<4 north:~/Examples > a.out < example08.c
/*********************************************************
Example #2 -- Demo the use of "getchar" and "putchar"
*********************************************************/
#include <stdio.h>
int main()
{
int input;
...
(entire contents of the file, character-by-character)
13. 13
25
I/O Buffering (input stream)
Executing
program
OS
Function getchar() waits until the buffer is not empty,
fetches the next character and updates the pointer to the
current item. OS routines fill the buffer when it is empty.
File (on disk)
F
i
r
s
t
l
…
getchar()
26
I/O Buffering (input stream)
Executing
program
OS
The only difference with the standard input stream: the
OS routines echo print the characters as they are copied
into the buffer. Copying a newline signals "buffer ready".
Keyboard
F
i
r
s
t
l
…
getchar()
14. 14
27
I/O Buffering (output stream)
Executing
program
OS
Function putchar() waits until there is room in the
buffer, then stores the next character and updates the
pointer to the current item. OS routines empty the buffer
when it is full.
File (on disk)
S
o
m
e
o
u
…
putchar()
28
I/O Buffering (output stream)
Executing
program
OS
No significant difference with the standard output stream
(some output functions flush the buffer themselves, such
as the endl manipulator in C++).
monitor
S
o
m
e
o
u
…
putchar()
15. 15
29
Reads characters from the standard input stream,
converts substrings according to the format string.
Returns the number of successful conversions.
int scanf( const char *form, ... );
Converts items into characters strings according to
the format string, sends them to the standard output
stream. Returns the number of characters sent.
int printf( const char *form, ... );
Formatted I/O
30
int main()
{
const char A = '?';
const short B = 19;
const int C = 28;
printf( "A: %2c %2d %xnn", A, A, A );
printf( "B: %2d %2x %04xnn", B, B, B );
printf( "C: %2d %2X %08Xnn", C, C, C );
}
A: ? 63 3f
B: 19 13 0013
C: 28 1C 0000001C
16. 16
31
int main()
{
const float D = 23.5;
const double E = 16.25;
const char F[] = "CSE 320";
printf( "D: %f %5.2f %e %6.4enn", D, D, D, D );
printf( "E: %f %5.2f %E %6.4Enn", E, E, E, E );
printf( "F: >%s< >%10s< >%-10s<nn", F, F, F );
}
D: 23.500000 23.50 2.350000e+01 2.3500e+01
E: 16.250000 16.25 1.625000E+01 1.6250E+01
F: >CSE 320< > CSE 320< >CSE 320 <
32
int main()
{
int Count, X;
double Y;
char Z;
printf( "Enter an integer, a real and a char: " );
Count = scanf( "%d %lf %c", &X, &Y, &Z );
if (Count > 0)
{
printf( "X: %d Y: %g Z: %cnn", X, Y, Z );
}
}
Enter an integer, a real and a char: 125 7.5e-3 A
X: 125 Y: 0.0075 Z: A
17. 17
33
Function printf() converts items from internal
representation into character strings (based on the
formatting specs) and copies them into the output
buffer.
%c – put character
%s – put character(s), stop on null byte
%d – put decimal digit(s)
%x – put hex digit(s)
Note: printf() is converting internal
representation to external represenation.
34
Example:
short int N = 67;
printf( "N: %d %x %cn", N, N, N );
N in RAM: 0000000001000011
Output buffer:
. . . n N : 6 7 4 3 C n .
Old New
18. 18
35
Function scanf() extracts characters from the
input buffer, converts them based on the formatting
specs and stores them in memory at the pointers.
%c – get character
%s – get character(s), stop on whitespace
%d – get decimal digit(s), stop on anything else
%x – get hex digit(s), stop on anything else
Function skips leading whitespace; handles special
cases gracefully (ex: formatting spec is %d, next
character is not a decimal digit).
36
Example:
Count = scanf( "%d %x %c", &X, &Y, &Z );
Input buffer:
%d – extracts " 125", converts to two's complement
%x – extracts " 7af", converts to two's complement
%c – extracts " A" (no conversion necessary)
Buffer pointer now is positioned on "B"
. 1 2 5 7 a f A B C .
Old New
20. 20
39
Nested calculation:
125 = 1 * 102 + 2 * 101 + 5 * 100
= (((((1) * 10) + 2) * 10) + 5)
7af = 7 * 162 + a * 161 + f * 160
= (((((7) * 16) + a) * 16) + f )
40
Algorithm:
answer = 0
iterate over digits in original number
answer = answer * base + current digit
Programming considerations:
digits must be converted from char to int
all values in two’s complement inside circuits
21. 21
41
Conversion: internal to external base 10
0000000001000011 (16 bits) ==> "67"
Calculation:
67 / 10 = 6 R 7
6 / 10 = 0 R 6 (halt when quotient is 0)
Process digits from bottom to top, convert each digit
to the corresponding character.
Note: 6 + “0” = “6”
42
Conversion: internal to external base 16
0000000001000011 (16 bits) ==> "43"
Calculation:
67 / 16 = 4 R 3
4 / 16 = 0 R 4 (halt when quotient is 0)
Process digits from bottom to top, convert each digit
to the corresponding character.
Note: 4 + “0” = “4”
22. 22
43
Algorithm:
value = original number
loop until value == 0
current digit = value % base
value = value / base
Programming considerations:
digits generated in reverse order
digits must be converted from int to char
all values in two’s complement inside circuits
44
Complete examples:
~cse320/Examples/example08.pdf
~cse320/Examples/example09.pdf
23. 1
1
Today: Floating Point Arithmetic
(H&H 5.3)
Next: continued
Lecture Topics
2
Self-study Module #5 (this week)
Project #5 (due no later than 10/3)
Project #6 (due no later than 10/17)
Exam #1 – Thursday, 10/10
Announcements
24. 2
3
Thursday, 10/10 during lecture
18% of course grade
Bring #2 pencils (multiple choice)
Bring MSU ID
One 8.5x11 note sheet (both sides)
Old exam on course website
Exam #1
4
In mathematics, the real numbers are infinite and
continuous.
In computing, the floating point numbers are finite
and discrete because there is a fixed number of
bits to represent values.
N bits ==> 2N bit patterns
Floating point values are a subset of the real
numbers.
Floating Point Numbers
26. 4
7
Format:
sign of fraction 1 bit
biased exponent 11 bits
fraction 52 bits
Interpretation:
+/- 1.fraction x 2(biased exponent – 0x3FF)
Double Precision
8
Interpretation:
+/- 1.fraction x 2(biased exponent – 0x3FF)
sign: 0 for positive, 1 for negative
significand: 1.fraction
true exponent: biased exponent – 0x3FF
Double Precision
27. 5
9
The decoding process:
Partition the 64 bits into the three fields
Calculate the true exponent
Use the sign, significand and true
exponent to calculate the value
Decoding DP Values
10
Internal rep: 3FFE000000000000 base 16
0011 1111 1111 1110 0000 0000 .... 0000 0000
sign bit: 0 (positive)
biased exponent: 011 1111 1111 (0x3FF)
true exponent: 0x3FF – 0x3FF = 0
significand: 1.111000000000....00000000
value: +1.111 x 20 base 2
= +1.111 base 2
= +1.875 base 10
28. 6
11
Internal rep: C019000000000000 base 16
1100 0000 0001 1001 0000 0000 .... 0000 0000
sign bit: 1 (negative)
biased exponent: 100 0000 0001 (0x401)
true exponent: 0x401 – 0x3FF = 2
significand: 1.100100000000....00000000
value: -1.1001 x 22 base 2
= -110.01 base 2
= -6.25 base 10
12
Internal rep: BFC0000000000000 base 16
1011 1111 1100 0000 0000 0000 .... 0000 0000
sign bit: 1 (negative)
biased exponent: 011 1111 1100 (0x3FC)
true exponent: 0x3FC – 0x3FF = -3
significand: 1.000000000000....00000000
value: -1.0 x 2-3 base 2
= -0.001 base 2
= -0.125 base 10
29. 7
13
The encoding process:
Express the value in binary, then
normalize it
Calculate the biased exponent
Pack the sign, fraction and biased
exponent into the three fields
Encoding DP Values
14
Value: +1.25 base 10
= +1.01 base 2
= +1.01 x 20 base 2
significand: 1.010000000000....00000000
biased exponent: 0x0 + 0x3FF = 0x3FF
= 01111111111
sign bit: 0 (positive)
0011 1111 1111 0100 0000 0000 .... 0000 0000
Internal rep: 3FF4000000000000 base 16
30. 8
15
Value: -10.75 base 10
= -1010.11 base 2
= -1.01011 x 23 base 2
significand: 1.010110000000....00000000
biased exponent: 0x3 + 0x3FF = 0x402
= 10000000010
sign bit: 1 (negative)
1100 0000 0010 0101 1000 0000 .... 0000 0000
Internal rep: C025800000000000 base 16
16
Value: +0.0625 base 10
= +0.0001 base 2
= +1.0 x 2-4 base 2
significand: 1.000000000000....00000000
biased exponent: -0x4 + 0x3FF = 0x3FB
= 01111111011
sign bit: 0 (positive)
0011 1111 1011 0000 0000 0000 .... 0000 0000
Internal rep: 3FB0000000000000 base 16
31. 9
17
Four categories of special values:
Infinity
Not-A-Number
Zero
Denormal Values
All other values called “normalized”
Special Values
18
Biased exponent field all ones
Infinity – fraction field all zeroes
7FF0000000000000
NaN – fraction field not all zeroes
7FF0000000000001
Sign bit can be 1 (for negative); set of 253
different representations of NaN
32. 10
19
Biased exponent field all zeroes
Zero – fraction field all zeroes
0000000000000000
Denormal – fraction field not all zeroes
0000000000000001
Sign bit can be 1 (for negative); set of 253
different denormal values
20
Range of DP exponents:
111 1111 1111 Special (Infinity, NaNs)
111 1111 1110 Largest (+0x3FF or +1023)
...
100 0000 0001 +2
100 0000 0000 +1
011 1111 1111 +0
011 1111 1110 -1
011 1111 1101 -2
...
000 0000 0001 Smallest (-0x3FE or -1022)
000 0000 0000 Special (Zero, Denormals)
33. 11
21
Denormal values are smaller than the
minimal normalized value – they allow us to
have values which are close to zero.
Normalized: 1.fraction x 2(Biased-0x3FF)
Denormal: 0.fraction x 2-0x3FE
All denormal values have the same true
exponent (-0x3FE or -1022)
Denormal (Subnormal) Values
22
The smallest normalized value:
0000000000010000....0000
Value: 1.0 x 2-1022
The largest denormal value:
0000000000001111....1111
Value: 0.1111….1111 x 2-1022
34. 12
23
The second largest denormal value:
0000000000001111....1110
Value: 0.1111….1110 x 2-1022
The smallest denormal value:
0000000000000000....0001
Value: 0.0000….0001 x 2-1022
24
Format:
sign of fraction 1 bit
biased exponent 8 bits
fraction 23 bits
Interpretation:
+/- 1.fraction x 2
(biased exponent – 0x7F)
Single Precision
35. 13
25
Interpretation:
+/- 1.fraction x 2
(biased exponent – 0x7F)
sign: 0 for positive, 1 for negative
significand: 1.fraction
true exponent: biased exponent – 0x7F
Single Precision
26
Range of SP exponents:
1111 1111 Special (Infinity, NaNs)
1111 1110 Largest (+0x7F or +127)
...
1000 0001 +2
1000 0000 +1
0111 1111 +0
0111 1110 -1
0111 1101 -2
...
0000 0001 Smallest (-0x7E or -126)
0000 0000 Special (Zero, Denormals)
36. 14
27
The decoding process is the same as for DP
values, except fields are 1, 8 and 23 bits
The encoding process: sizes of fields
Note: biased exponent field of 8 bits
determines biasing factor:
01111111 = 0x7F
Decoding and Encoding SP Values
28
Internal rep: 40B80000 base 16
0100 0000 1011 1000 0000 0000 0000 0000
sign bit: 0 (positive)
biased exponent: 1000 0001 (0x81)
true exponent: 0x81 – 0x7F = 2
significand: 1.01110000000000000000000
value: +1.0111 x 22 base 2
= +101.11 base 2
= +5.75 base 10
37. 15
29
Internal rep: BFC00000 base 16
1011 1111 1100 0000 0000 0000 0000 0000
sign bit: 1 (negative)
biased exponent: 0111 1111 (0x7F)
true exponent: 0x7F – 0x7F = 0
significand: 1.10000000000000000000000
value: -1.1 x 20 base 2
= -1.1 base 2
= -1.5 base 10
30
Value: +10.75 base 10
= +1010.11 base 2
= +1.01011 x 23 base 2
significand: 1.010110000000....00000000
biased exponent: 0x3 + 0x7F = 0x82
= 10000010
sign bit: 0 (positive)
0100 0001 0010 1100 0000 0000 0000 0000
Internal rep: 412C0000 base 16
38. 16
31
Value: -99.5 base 10
= -1100011.1 base 2
= -1.1000111 x 26 base 2
significand: 1.10001110000000000000000
biased exponent: 0x6 + 0x7F = 0x85
= 10000101
sign bit: 1 (negative)
1100 0010 1100 0111 0000 0000 0000 0000
Internal rep: C2C70000 base 16
32
Goldberg: What Every Computer Scientist
Should Know About Floating-Point Arithmetic
(PDF on the course website)
More depth: