Our Properties: Gamasutra GameCareerGuide IndieGames Indie Royale GDC IGF Game Developer Magazine GAO
My Message close
Contents
Data Alignment, Part 1
 
 
Printer-Friendly VersionPrinter-Friendly Version
 
Latest News
spacer View All spacer
 
February 8, 2012
 
Zynga's in the black – but for how long? [3]
 
Bungie co-founder Alex Seropian leaves Disney [1]
 
Todd McFarlane brings a comic artist's eye to Kingdoms of Amalur: Reckoning
spacer
Latest Jobs
spacer View All     Post a Job     RSS spacer
 
February 8, 2012
 
THQ Montreal
Art Lead-THQ Montreal
 
THQ Montreal
Sr. Gameplay Programmer
 
THQ Montreal
Senior Technical Animator/Rigger
 
Airtight Games
Senior Game Designer
 
Lantern
3D Animator
 
Nintendo of America Inc.
Localization Writer/Editor
spacer
Latest Features
spacer View All spacer
 
February 8, 2012
 
arrow Jerked Around by the Magic Circle - Clearing the Air Ten Years Later [14]
 
arrow Building the World of Reckoning [3]
 
arrow SPONSORED FEATURE: TwitchTV - How to Build Community Around Your Game in 2012 [12]
 
arrow Happy Action, Happy Developer: Tim Schafer on Reimagining Double Fine [8]
 
arrow Building an iOS Hit: Phase 1 [11]
 
arrow Postmortem: Appy Entertainment's SpellCraft School of Magic [5]
 
arrow Talking Copycats with Zynga's Design Chief [82]
 
arrow Finnish Experiments, American Nightmare [12]
spacer
Latest Blogs
spacer View All     Post     RSS spacer
 
February 8, 2012
 
Merging Waterfall and SCRUM [2]
 
Business Post Mortem: Wolf Toss: Pre-launch Planning & Blended CAC
 
Minmaxing - Is turn-based fun anymore? [51]
 
PRICED TO DIE [4]
 
What happened with Shadow Physics: An Introduction [3]
spacer
About
spacer Editor-In-Chief/News Director:
Kris Graft
Features Director:
Christian Nutt
Senior Contributing Editor:
Brandon Sheffield
News Editors:
Frank Cifaldi, Tom Curtis, Mike Rose, Eric Caoili, Kris Graft
Editors-At-Large:
Leigh Alexander, Chris Morris
Advertising:
Jennifer Sulik
Recruitment:
Gina Gross
 
Feature Submissions
 
Comment Guidelines
Sponsor
Features
  Data Alignment, Part 1
by Noel Llopis [Programming]
6 comments Share on Twitter Share on Facebook RSS
 
 
March 6, 2009 Article Start Page 1 of 3 Next
 

I grew up on Z80 and x86 assembly. I happily moved data into my registers from anywhere in memory and back without ever giving it a second thought. Life was nice and simple.

It wasn't until years later, when I started dealing with caches, vector units, and different architectures that data alignment became a big deal. Today, data alignment is an inescapable reality, and we all have to deal with it one way or another.


Data alignment refers to where data is located in memory. All data is at least 1-byte aligned, meaning that it starts at any one byte in memory. Data that is n-byte aligned is located somewhere with a memory address that is an exact multiple of n bytes.

It's All About Performance

We care about alignment for a single reason: Performance.

In modern hardware, memory can only be accessed on particular boundaries. Trying to read data from an unaligned memory address can result in two reads from main memory plus some logic to combine the data and present it to the user.

Considering how slow main memory access is, that can be a major performance hit. Some platforms such as the PowerPC choose not to hide such an inefficient use of hardware and simply raise a hardware exception flagging the invalid memory access. Neither scenario is particularly attractive.

Even when reading from fast, level-one cache memory, unaligned access to data will use up more cache lines, making poor use of the available memory and evicting data that might be accessed again. As a result, sections of your code can thrash the cache and become major performance bottlenecks.

Vector operations usually force programmers to deal with data alignment explicitly. To squeeze more performance without increasing the frequency of CPU cores, hardware designers have been putting more emphasis on vector operations (which are just a type of SIMD instruction -- single instruction multiple data).

One vector instruction can operate on four or more data points at once, considerably improving the performance of a program that operates over each data point serially. Vector units often have a restriction that they can only work on data aligned on a particular boundary.

For example, Intel's and AMD's SSE vector instructions operate on data aligned on 16-byte boundaries, so it is the programmer's responsibility to align the data properly before feeding it to the vector units.

In general, the more you interface with the hardware directly, the more careful you will have to be about the alignment of your data.

 
Article Start Page 1 of 3 Next
 
Comments

James Munro
profile image
This is particularly relevant to me because I have just ported an existing 32-bit game engine (id Tech 2) over to native 64-bit on the Windows platform as part of my university dissertation. I'm currently assessing the impact of both performance and also the inflation of data structures due to changes in alignment and data-type sizes.

Thanks for the interesting information. With regarding to 64-bitness, I found some useful information on data-alignment here: http://developer.amd.com/documentation/articles/pages/1213200696.aspx

Harold Myles
profile image
Look like a 'typo' on page two: "For example, the following code checks if some data is aligned on a 16-byte boundary: ((uintptr_t)&data & 0x1F)".

To align on 16 byte boundary would be: ((uintptr_t)&data & 0x0F) == 0.

Cheers.

Noel Llopis
profile image
Harold, You're totally correct. Good catch! The source code provided does the computation correctly, but clearly I added an extra bit when I did it on my head for the article :-)

--Noel

Roberto Alfonso
profile image
Interesting feature. I use assembler for encryption, and need to align data before processing to lessen the performance hit. However, we haven't yet ported our application to native 64 bits, and I cringe at the amount of code I will have to review.

The code format is messed up, must be my version of Vim.

Bob Stevens
profile image
"We care about alignment for a single reason: Performance."

We also care about alignment for another reason: it affects the atomicity of operations on that data.

This is so relevant that C++0x is adding standard functionality to address it.

Bjoern Knafla
profile image
Regarding the code for aligned_malloc and aligned_free I am wondering:
Shouldn't aligned_malloc check the return value of std::malloc and not proceed if NULL is returned?
Shouldn't aligned_free only try to access the alignment bookkeeping data if the argument isn't NULL?

--Bjoern


none
 
Comment:
 




UBM Techweb
Game Network
Game Developers Conference | GDC Europe | GDC Online | GDC China | Gamasutra | Game Developer Magazine | Game Advertising Online
Game Career Guide | Independent Games Festival | Indie Royale | IndieGames

Other UBM TechWeb Networks
Business Technology | Business Technology Events | Telecommunications & Communications Providers

Privacy Policy | Terms of Service | Contact Us | Copyright © UBM TechWeb, All Rights Reserved.