Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: another simple c question

Author: Gerd Isenberg

Date: 10:30:05 07/21/04

Go up one level in this thread


On July 21, 2004 at 13:14:59, Scott Gasch wrote:

>
>>It is an extremely bad idea, because your struct (and worse, single variables
>>within the struct) could now lie across multiple cache lines.
>>
>>anthony
>
>
>Not to mention that some architectures (not x86, though) raise exceptions when
>you access misaligned data.  That means if you have a 4 byte int it _must_ be at
>a memory address that is a multiple of 4 bytes (i.e. 0, 4, 8 ...)
>
>Scott

two notes about AMD64 from

Software Optimization Guide for
AMD Athlon™ 64 and AMD Opteron™ Processors

5.2 Natural Alignment of Data Objects

Optimization

Make sure data objects are naturally aligned. An object is naturally aligned if
it’s located at an address that is a multiple of its size.

Application

This optimization applies to:
• 32-bit software
• 64-bit software

Rationale

A misaligned store or load operation suffers a minimum one-cycle penalty in the
processor’s load-store pipeline. Also, using misaligned loads and stores
increases the likelihood of encountering a store-to-load forwarding pitfall,
especially when operating in long mode (64-bit software). (For a more detailed
discussion of store-to-load forwarding issues, see “Store-to-Load Forwarding
Restrictions” on page 93.)

In addition, if the Alignment Mask bit is set in Control Register 0 (CR0), an
unaligned memory reference may cause an alignment check exception. For more
information on this topic, see Volume 2 of the AMD64 Architecture Programmer’s
Manual (order# 24593).

Locate this type of object At an address evenly divisible by
Word 2
Doubleword 4
Quadword 8
Ten-byte (for example, TBYTE or REAL10) 8 (instead of 10)
Double quadword 16

-----------------------------------------------------------------------------
2.18 Sorting and Padding C and C++ Structures

Optimization

Sort and pad C and C++ structures to achieve natural alignment.

Application

This optimization applies to:
• 32-bit software
• 64-bit software

Rationale

In order to achieve better alignment for structures, many compilers have options
that allow padding of structures to make their sizes multiples of words,
doublewords, or quadwords. In addition, to improve the alignment of structure
members, some compilers might allocate structure elements in an order that
differs from the order in which they are declared. However, some compilers might
not offer any of these features, or their implementations might not work
properly in all situations. By sorting and padding structures at the source-code
level, if the first member of a structure is naturally aligned, then all other
members are naturally aligned as well. This allows, for example, arrays of
structures to be perfectly aligned.

Sorting and Padding C and C++ Structures

To sort and pad a C or C++ structure, follow these steps:
1. Sort the structure members according to their type sizes, declaring members
with larger type sizes ahead of members with smaller type sizes.
2. Pad the structure so the size of the structure is a multiple of the largest
member’s type size.



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.