• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.

Archived

This topic is now archived and is closed to further replies.

Clash Rocker

{word dword} 8 bytes?

12 posts in this topic

    
#include <iostream.h>

typedef unsigned long  DWORD;
typedef unsigned short WORD;

typedef struct
{
    WORD m_w;
    DWORD m_d;
}AAA;

typedef struct
{
    DWORD m_d1;
    DWORD m_d2;
}BBB;

typedef struct
{
    WORD m_w1;
    WORD m_w2;
}CCC;

int main(void)
{
    cout<<"sizeof(WORD): "<<sizeof(WORD)<<endl;
    cout<<"sizeof(DWORD): "<<sizeof(DWORD)<<endl;
    cout<<"sizeof(AAA): "<<sizeof(AAA)<<" WORD + DWORD"<<endl;
    cout<<"sizeof(BBB): "<<sizeof(BBB)<<" DWORD + DWORD"<<endl;
    cout<<"sizeof(CCC): "<<sizeof(CCC)<<" WORD + WORD"<<endl;

    return 0;
}    
OK, why does sizeof(AAA) report 8 bytes? That was under g++ (2.x) and MSVC 6.0 omf.com Edited by - Clash Rocker on June 18, 2001 1:05:59 AM
0

Share this post


Link to post
Share on other sites
The compiler adds an extra 4bytes to align the data structure.
Memory transfers are considerably faster if just falls on 64/128/256/512bit boundries; I don''t know the exact technical reason why, it has to do with cache page sizes, bus bit widths, and the memory gating/multiplexing address lines.

You can turn it off in the project settings, and with a compiler directive, pack something, I never use it...

Magmai Kai Holmlor
- The disgruntled & disillusioned
0

Share this post


Link to post
Share on other sites
In MSVC: Go to project settings, into "C/C++" tab and choose "code generaion". Then check "struct member aligment". It''s 8 bytes for default. You can choose another aligment (2 bytes, for example), but i think the program will run faster with 8 bytes struct aligment
0

Share this post


Link to post
Share on other sites
You could also swap the two entries and have the DWORD first, and as it aligns to a dword it will not need to add in packing bytes to the structure (not in between the values anyway)



Dæmin
(Dominik Grabiec)
sdgrab@eisa.net.au
0

Share this post


Link to post
Share on other sites
I saw something like this... The way it worked was if it was on a 1/2/4/8/16/32/64(on the pent 4 only) byte boundary, it could move the whole thing around with one instruction. However, if by chance it ended up as 7/31/63 byte boundary, you get stuck with like 8 instructions. In fact, if it''s on a 32 byte boundary (or 64 byte on the pent 4) it''ll go even faster. No idea why, though, but it really makes its mark when writing to the agp bus like in DDraw. Saw this in some x86 journal, I think, or maybe it was game developer mag...
0

Share this post


Link to post
Share on other sites
The reason is because Intel processors (since the days of the 386) had a 32-bit wide bus. That is, when transferring data from memory into cache/registers/other memory, it does it 32 bits at a time. Even if you only want 8-bits transferred, you still had to transfer the full 32 bits and then mask the data you want. Luckily, the masking is not difficult, but it''s a bit slower than accessing the full 32-bits. Also, accessing the lower 8- or 16- bits of a number is quite fast (since the 32-bit eax register is also accessed with the 16-bit ax registor or the 8-bit ah and al registers) at least compared to accessing the upper 16 bits of a number (there''s no axh or whatever register)


War Worlds - A 3D Real-Time Strategy game in development.
0

Share this post


Link to post
Share on other sites
This issue is even worse on RISC processors. Accessing unaligned data isn''t just slow - it crashes. The OS can catch the crash and fix things up for you but that amounts to making a call to the OS every time you want to read (unaligned) data.

-Mike
0

Share this post


Link to post
Share on other sites
related question:
is there a way to declare a single structure as "packed"?

I ran into this alignment problem once when writing a lib that loaded BMPs... a book I have gives you the structure to read in the header info, which starts with the bytes "bm". The book has that header in the structure, but when I was trying to use it, my program was reading in four bytes instead of two (apparently because of this alignment issue)... and thus corrupting the rest of the struct''s data... so is there a way to declare the struct that would fix this?

fyi, the way I fixed this was to read in the header seprately, then the header struct

--Tr][aD--
0

Share this post


Link to post
Share on other sites
oh, for the curious it seems this works...

#pragma pack(1) // 1-byte structure packing.
struct MyStruct {
// whatever members.
}
#pragma pack() // return to default packing.


found on another post... this is why it''s always a good idea to do a search before posting a question... chances are it''s been asked already

--Tr][aD--
0

Share this post


Link to post
Share on other sites
Its the same way on the PS2, except its 128 bits aligned because it has a 128bit proccessor. However, there are a lot of instructions that are 64bit, so it is possible (and faster) to feed it two of the 64bit instructions at the same time if you micro code it correctly. It runs faster and is really neat.

Things will always be faster if you pass the proccessor what I wants most. Which is why most bools are ints, because it is a lot faster. You could write a class to use just 1 bit, but then I''d reccommend making it a whole int and just representing each bit as its own bool. Oh well I''m just rambling now. :p
0

Share this post


Link to post
Share on other sites
So as a general tip, it''s good to tell the compiler to align for 32 bits? Or do you get errors with non- Intel cpu''s then?

I think Lamoth says something about this in Tricks of the Game Programming Gurus, but I''m not sure



Humanity''s first sin was faith; the first virtue was doubt
0

Share this post


Link to post
Share on other sites
As a general tip, it''s good to let the compiler do what it thinks is best. Most compilers will align to "natural alignment" by default. This is whatever alignment is best for the particular structure.

Unless you have a specific reason to change it, just leave the default as-is. For the vast majority of people the only reason to change it is because you need to match the memory layout of some structure that you didn''t define.

-Mike
0

Share this post


Link to post
Share on other sites