low level programming and computer architecture - lecture 5 · low level programming and computer...
TRANSCRIPT
Low Level Programming and Computer ArchitectureLecture 5
Carl Henrik Ek{chek}@csc.kth.se
Royal Institute of Technology
March 10, 2013
Ek KTH
Low Level Programming and Computer Architecture
Last Lecture• Abstraction• File formats• Structured Data
Ek KTH
Low Level Programming and Computer Architecture
Abstraction of Data
Ek KTH
Low Level Programming and Computer Architecture
Abstraction of Data
Representing Images• Indexed Images• Truecolour• HAM - Hold And Modify
• Image sizeI 640× 480× 3 =
921600bytes = 900KB
• PBM, PGM, PPM, PNM, BMP• executable
Ek KTH
Low Level Programming and Computer Architecture
Compression
Defintion
f : X → Yg : Y → X
• RAW - uncompressed data• Lossless
I x = g (f (x))• Lossy
I d(x,g (f (x))) 6= 0I What is d(·, ·)
Ek KTH
Low Level Programming and Computer Architecture
Compression: Lossless
Lossless• Run-length encoding• Frequency dependency
I Huffman encoding
• LHA, LZX, ZIP, TAR,GZIP, . . .
Ek KTH
Low Level Programming and Computer Architecture
Compression: Lossless
Huffman encoding• David A. Huffman, 1952
MIT• Entropy encoding• Claude Shannon
I Father of Information theory
Ek KTH
Low Level Programming and Computer Architecture
Compression: Lossless
Dictionary Coding• Dictionary• LZW• Original Unix compress
Ek KTH
Low Level Programming and Computer Architecture
Compression: Lossy
Images• Lots of different formats• Lossless
I TIFF, GIF, BMP
• LossyI JPEG
• Loss function
Ek KTH
Low Level Programming and Computer Architecture
Compression: Lossy
JPEGa
aWikipedia
• Change-of-Basis
f (t) =∫ ∞−∞
f (x)e−2πixtdx
• perception• what is the loss function?
Ek KTH
Low Level Programming and Computer Architecture
This Lecture• Abstraction
I Left-overs from last time
• Programming• Structured Data
Ek KTH
Low Level Programming and Computer Architecture
Communication
Parity• Check Bit• Even/Odd
I number of set bits needs to beEven/Odd
I Parity bit compensates
• Checksum: CRC• ASCII, TCP/IP traffic
Ek KTH
Low Level Programming and Computer Architecture
Communication
Parity• Check Bit• Even/Odd
I number of set bits needs to beEven/Odd
I Parity bit compensates
• Checksum: CRC• ASCII, TCP/IP traffic
Ek KTH
Low Level Programming and Computer Architecture
Communication
Parity• Check Bit• Even/Odd
I number of set bits needs to beEven/Odd
I Parity bit compensates
• Checksum: CRC• ASCII, TCP/IP traffic
Ek KTH
Low Level Programming and Computer Architecture
Communication
Error Correcting Codes• Detect error• Correct• Distance between codes
Copyright © 2012 Pearson Education, Inc. 0-0
Figure 1.29 An error-correcting code
Ek KTH
Low Level Programming and Computer Architecture
Communication
Copyright © 2012 Pearson Education, Inc. 0-0
Figure 1.30 Decoding the pattern 010100using the code in Figure 1.30
Ek KTH
Low Level Programming and Computer Architecture
Recapitulation
Program Execution• Code is just data• OS Loads executable
I Lecture 7 May 3rd
Ek KTH
Low Level Programming and Computer Architecture
Recapitulation
Ek KTH
Low Level Programming and Computer Architecture
Recapitulation
Ek KTH
Low Level Programming and Computer Architecture
Recapitulation
Ek KTH
Low Level Programming and Computer Architecture
Disassembly• Machine Code and Assembler• Recover Code from
executable• Intel
• Motorla 68k
• AMD Reference Manual
Ek KTH
Low Level Programming and Computer Architecture
Executable
Executable• Mac
I Mach-OI machine.h
• PE - Windows, ELF - Linux• Extended Hunk Format -
AmigaOS• Library
I Static, Dynamic
Ek KTH
Low Level Programming and Computer Architecture
Abstraction II
Programming Languages• Generate Instructions• Levels of abstraction• “Generations”
Ek KTH
Low Level Programming and Computer Architecture
Abstraction II
Second Generation• Assembly• Mnemonics• Injective mapping
(one-to-one)• Assembler
19/3/13 10:16 Parallel byte-wise maximum
Page 1 of 3http://ada.untergrund.net/forum/index.php?action=vthread&forum=4&topic=870
- Forums - Reply - Search - Statistics -
Amiga Demoscene Archive Forum / Coding / Parallel byte-wise maximum
Author Message
BlueberryForums Member
#1 - Posted: 25 Feb 2013 10:04 - Edited by: BlueberryReply Quote
Hi!
It has been discussed in here a couple of times how to do byte-wise saturated add one longword at a time.
But how about byte-wise maximum? If we have byte values up to 127 in all four bytes of D0 and D1, we can perform abyte-wise maximum like this:
move.l #$80808080,d2 sub.l d1,d0 ; byte-wise differences with overflows add.l d2,d0 ; overflows negated, msb of each byte is 1 iff difference is positive and.l d0,d2 ; 128 for bytes where difference is positive, 0 for the others move.l d2,d3 lsr.l #7,d3 sub.l d3,d2 ; 127 for bytes where difference is positive, 0 for the others and.l d2,d0 ; only positive differences add.l d1,d0 ; original + positive differences = maximum
With appropriate pairing, these 9 instructions result in an overhead (compared to simply using one of the sources) of1.125 cycles per byte. One of the sources (D1) can be in memory for no extra cost.
Can it be done better? Can we get down to 1 cycle? :)
Suppose we need, say, 6 bits of precision on our byte values, but those 6 bits have to be placed in the upper bits of eachbyte (in the memory operand and the destination). We can do this by a move (from memory) and an lsr #1 at thebeginning and an lsl #1 at the end, resulting in 3 more instructions (1.5 cycles per byte total). Is there perhaps somebetter way to do it?
jamie2010Forums Member
#2 - Posted: 25 Feb 2013 14:14Reply Quote
Hey,
Do you need to do this sort of operation several times by pixel?
jamie2010Forums Member
#3 - Posted: 25 Feb 2013 14:17Reply Quote
By Pixels i mean for 4 pixels.
daltonForums Member
#4 - Posted: 26 Feb 2013 08:43Reply Quote
I can't think of a quicker way to perform this operation. What bothers me is that so many cycles are spent on creating abit mask from the sign bits (x-x>>7). Another way of doing it would be (x>>7)*127. It saves one line of code and oneregister. But I don't think it's any faster =(
move.l #$80808080,d2 sub.l d1,d0 ; byte-wise differences with overflows add.l d2,d0 ; overflows negated, msb of each byte is 1 iff difference is positive and.l d0,d2 ; 128 for bytes where difference is positive, 0 for the others
Ek KTH
Low Level Programming and Computer Architecture
Abstraction II
Third Generation• Higher-level
I not injective
• Machine independent• Compiled/Interpreted
Ek KTH
Low Level Programming and Computer Architecture
Paradigms
Imperative• Traditional algorithm• Define procedure• State changes• Machine Language, Pascal,C, C++, Python
Ek KTH
Low Level Programming and Computer Architecture
Paradigms
Functional• Express as functional evaluation• Recursion• Avoid state changes• Haskell, Lisp
• Beautiful but slowI academic
Ek KTH
Low Level Programming and Computer Architecture
Paradigms
Declarative• Defines logic• Does not define flow• SQL, Prolog
Ek KTH
Low Level Programming and Computer Architecture
Copyright © 2012 Pearson Education, Inc. 0-0
Figure 6.2 The evolution ofprogramming paradigms
Ek KTH
Low Level Programming and Computer Architecture
Types
Compiled• Translates whole program• Executes program• Intermediate stages
I ParserI Compile timeI Run time
Ek KTH
Low Level Programming and Computer Architecture
Types
Interpreted• Translates and executes• Matlab
• Fast development• Hard to Optimise
Ek KTH
Low Level Programming and Computer Architecture
Types
Typing• Metadata• Strongly Typed
I JAVA, Python, C++
• Weakly TypedI C
• UntypedI Assembler
• Static, Dynamic
Ek KTH
Low Level Programming and Computer Architecture
Virtual Machine• Abstraction of Hardware• Hard to make efficient• Convinient distribution• API
Ek KTH
Low Level Programming and Computer Architecture
Feynman
“You can know the name of abird in all the languages of theworld, but when you’re finished,you’ll know absolutely nothingwhatever about the bird... Solet’s look at the bird and seewhat it’s doing– that’s whatcounts. I learned very early thedifference between knowing thename of something and knowingsomething.”
Ek KTH
Low Level Programming and Computer Architecture
Summary• Imperative• Compiled/Interpreted• Virtual Machine
Ek KTH
Low Level Programming and Computer Architecture
Misc
Turing Completness• Simluate Turing Machine
I States, memory, aritmetic
• All Imperative languages• SQL, HTML
Ek KTH
Low Level Programming and Computer Architecture
Misc
Esoteric Languages• Brainfuck
I Only uses > < + { . , []
I code
• MalbolgeI Eight circle of HellI code
• ShakespeareI code
• Esolang
• IOCCC
Ek KTH
Low Level Programming and Computer Architecture
Next Lecture• April 23th• C Programming• Practical Session April 17th
Ek KTH
Low Level Programming and Computer Architecture
e.o.f.
Ek KTH
Low Level Programming and Computer Architecture