What is endianness?

What does it mean for the byte order to be little-endian versus big-endian? Basically, it’s how the computer interprets multi-byte values. For example, we know that a single byte can take on the values of 0-254 inclusive. The arrangement of the bits within that single byte is pretty standard.

(1)2^7 + (0)2^6 + (0)2^5 + (0)2^4 + (1)2^3 + (0)2^2 + (0)2^1 + (1)2^0 = 137

Looking at the above and only looking at the ones and zeroes we have 10001001 = 137 in decimal.
We usually have the location of the smallest bit (least significant bit) on the right going to the largest bit (most significant bit) on the left. So this is for a single byte. What about multi-byte values?

First lets discuss some memory addressing issues. When you have a pointer that points to a memory address: char* p = ……; Something like that (we are assuming a char is a single byte), then p points to a single byte.

If we increment it like this: ++p, then p will point to the next byte in memory. It’s important to note that addresses in memory are the location of a single byte. You can never address bits. Although by using bit shift operators and such, you can manipulate the bits within that single byte.

Let’s expand on this a little bit. Lets say we have an array of characters:
charletters[]={‘a’,’b’,’c’,’d’};

Read the rest of this entry »

Beginning win32 multithreading

In an attempt to understand windows multithreading a little more thoroughly, I’ve decided to write about the simple concepts associated with multithreading. This is mainly to clarify it in my own head but maybe it will help someone else out too. If any of this information is wrong, don’t yell at me.:-)Please just post a comment and I’ll look into it and/or fix it. So here we go.

Thread: I’m not going to define a thread. Just know that a thread is a kernel object, which means that when a thread finishes executing, it gets signaled. If you use “WaitForSingleObject();” for example to wait for it to finish executing, it is safer and doesn’t eat up CPU time like a spin lock would.

Signaled: Kernel objects get signaled (threads, mutexes, etc.). When a kernel object gets signaled, “WaitForSingleObject()” wakes up. The same goes for other “Wait*()” functions. In the case of threads: running = nonsignaled, when a thread terminates = signaled.

Atomic Operation: A single operation that the processor can not perform a context switch in between.

Context Switch: When the processor halts execution of a thread, and then continues executing another thread from where it left off previously. The cause of race conditions when multithreading, but a necessary feature on processors.

Spin Lock: Eats CPU time, not atomic. Basically the predecessor to critical sections. Basically a “for” loop that waits around until a thread returns. This is the worst way to do multithreading, don’t use spin locks at all.

Race Condition: The easiest way to conceptualize a race condition is to think of it this way: A thread writes data and soon after, another thread blindly overwrites the data that was just written by the first thread. Critical Sections resolve this.

Critical Section: If you only have one resource that needs sharing between multiple threads, you use a “Critical Section.” Critical sections are not kernel objects and prevent race conditions between multiple threads. The reason you use critical sections is to make certain areas of your code / data atomic so that a context switch doesn’t fowl up your data. Note: You can specify different areas of your code as the same critical section.

Mutex: If you have multiple resources that need to be shared among threads, you do NOT use a critical section. This can result in a dead lock where one thread waits for the other thread to release a resource, but that thread is waiting on the first thread to release a resource it needs, so both end up waiting forever. So instead you use a mutex. A mutex is a kernel object and can be shared among threads as well as processes. Basically, a mutex says: “Execute this code if both (or however many the mutex was defined with) resources are available.” If that requirement isn’t satisfied, then the code isn’t run. For example: If only one resource was available but the mutex was defined for two resources, then the code would not run.

Note: Mutexes introduce race conditions now, the very thing that critical sections fixed. Also, it’s worth noting that you can still get a dead lock with mutexes if you use “WaitForSingleObject().” Instead, you must use “WaitForMultipleObjects()” to avoid dead locks.

Win32 functions associated with multithreading
General Win32 Thread Functions: You can look up the parameters yourself on msdn.
CreateThread();
GetExitCodeThread();
ExitThread();
WaitForSingleObject();
WaitForMultipleObjects();
MsgWaitForMultipleObjects();

Critical Section: Sharing one resource
InitializeCriticalSection();
DeleteCriticalSection();
EnterCriticalSection();
LeaveCriticalSection();

Mutex: Sharing multiple resources
CreateMutex();
OpenMutex();
WaitForSingleObject();
WaitForMultipleObjects();
MsgWaitForMultipleObjects();
ReleaseMutex();
CloseHandle();

Notes on library files

A few notes regarding library files.

Shared library: Not the same as a dynamic library. Only used in linux. A .so file extension. Can be shared across processes, not loaded into a process’s address space, loaded globally. (i believe) Note: It is loaded at application startup.

Static library: Also known as an object library, contains a collection of .obj files, extension .lib

Dynamic library: .dll extension or .so. Dynamically loaded by the OS’s loader. Loaded into a process’s address space. (i think) Note: Loaded whenever you use a “load library()” call, not on application startup.

Import library: Allows your program to call entry points in the DLL, .lib file, linux does not require these as it directly links to the .so file

Sometimes a shared library and a dynamic library are said to be the same thing, but they aren’t. If there are any mistakes or I have my facts mixed up, please let me know.

In fact, if you want, there is an entire book dedicated to linkers and loaders online. And it’s free: Linkers and Loaders

The hidden default constructor in C++

Note: This posting is just completely wrong. I didn’t know C++ very well at the time, so I’m just mentioning it. I won’t bother correcting it though.

I think I’m going to start posting about general C++ stuff too and since C++ is my favorite language (aside from assembly) I thought it would be interesting to post obscure C++ things that I find. Today I’m going to talk about a blog post that I found a while ago regarding constructors.

The fellow (I forgot where I saw it) mentioned that there was a significant difference the two lines of code.

The first line will NOT initialize intrinsic data types. So they will be filled with whatever when the object is instantiated.
The second line, on the other hand, WILL initialize all intrinsic data types to zero.
There are a couple of issues to be aware of though:

1) There must NOT be a default constructor available.
2) This is very well compiler/implementation dependent. I haven’t actually checked the standard though but I don’t see why the standard would define such a thing since it could (theoretically) impose a performance risk.

Looking back at point 1, lets use an example:

Note: No default constructor or destructor. In this case, the compiler will provide them for your automatically. At least Microsoft’s compiler will. In that case, it will be kind enough to also initialize foo to zero if you specify

as opposed to


Useful links / blog roll

If you’ll look to the right side I put up a “useful links” section. It contains a lot of valuable links to current research and useful game dev sites. I’ll be updating it as time goes on.

Also, for those still looking for a game dev job, I stumbled across this guy’s blog posting regarding interviewing at game dev companies:Link

Too many white papers, too little time

I’ve been hearing quite a bit aboutO3Dfrom Google, but I’ve just sorta blown it off as another Google thing. Kind of like their Chrome Browser which I wasn’t too fond of. But I actually got a chance to check it out and I must say that I’m impressed. This demo shows some interesting bits:Beach Demo

The only thing I don’t like about it are the input controls. I couldn’t simultaneously press “w” and move the mouse at the same time. But overall it looks impressive. Eventually people will start using it for game dev. It appears they already have checkers operational. The only problem I see is that it is yet another api to learn.

On a side note, I have begun to realize, after trying to keep up with my rss feeds, that it is next to impossible to keep up to date on all the latest and coolest things. What I’m referring to are “white papers.” Some of them are fantastic but not useful, while others are very useful. So how might one go about keeping up to date with the latest, but weed out the less important white papers to avoid wasting time?

Currently, I don’t have a solution to this problem. But the best I can hope for is to only read the papers that other people recommend and/or the popular papers. Another thing to do is to avoid reading white papers all together and only read books. This presents a problem for a couple of reasons but also might be a good idea:

1) Pro: Books usually contain useful proven information (I like to call them stable solutions.) You’re much less likely to waste time if you read a book.
2) Con: You won’t get the “latest” engineering solutions. You could always engineer a solution yourself though.
3) Con: You might not be able to find what you’re looking for in books. There aren’t a whole lot of books on “point rendering” for example.

Honestly, there is too many interesting things going on to always get the best of the best. From GPGPU processing, Graphics (real-time, precomputed), volumetric rendering, etc. etc.

Linux Documentation

I’ve been using linux for years now, mainly redhat/fedora and now ubuntu. But a long time ago I found some really good linux documentation (besides the documentation installed /w linux.) It is located here:http://www.redhat.com/docs/manuals/enterprise/

It says it’s for enterprise, but it mostly doesn’t matter.

A Few Code Snippets

Periodically I’ll stumble across a small but useful piece of code. I have a small list right now, but as I stumble across them, I’ll post them here. Note: I do not take credit for any of this code.

Pausing your application

or

Convert any data type to a std::string object

Test if a float is zero or near zero

EPSILON is a #define that is something small, like 0.002f
Note, I will repost, I just realized that serendipity can’t handle angle brackets…