Shift Right: Delphi vs C

Few weeks ago I converted a little function from C language to Delphi. I kept getting completely wrong results all the time even though I was sure the C to Pascal conversion was right (it was really just few lines). After some desperate time, I just tried replacing SHR operator by normal DIV (as A SHR 1 = A DIV 2 and so on). To my surprise, I immediately got the right results. Can Delphi's (I didn't test it in Free Pascal) SHR operator behave differently than C's >> operator?

It does in fact. SHR treats its first operand as unsigned value even though it is a variable of signed type whereas >> takes the sign bit into account. In the function I converted the operand for right shift was often negative and Delphi's SHR just ignored the value of the sign bit.

A Bit of Code

int a, b1, b2;
a = -512;
b1 = a >> 1;
b2 = a / 2;

After running this C code both b1 and b2 have a value of -256.

var A, B1, B2: Integer;
A := -512;
B1 := A shr 1;
B2 := A div 2;

This Delphi code however yields different result: B2 is -256 as expected but B1 has a value of 2147483392.

A Bit of Assembler

Assembler output of C code:

Unit1.cpp.22: b1 = a >> 1;
mov eax,[ebp-$0c]
sar eax,1
mov [ebp-$10],eax
Unit1.cpp.23: b2 = a / 2;
mov edx,[ebp-$0c]
sar edx,1
jns $00401bb9
adc edx,$00
mov [ebp-$14],edx

Assembler output of Delphi code:

Unit1.pas.371: B1 := A shr 1;
mov eax,[ebp-$0c]
shr eax,1
mov [ebp-$1c],eax
Unit1.pas.373: B2 := A div 2;
mov eax,[ebp-$0c]
sar eax,1
jns $00565315
adc eax,$00
mov [ebp-$20],eax

As you can see, asm output of C and Delphi divisions is identical. What differs is asm for shift right operator. Delphi uses shr instruction whereas C uses sar instruction. The difference: shr does logical shift and sar does arithmetic one.

The SHR instruction clears the most significant bit (see Figure 6-7 in the Intel Architecture Software Developer's Manual, Volume 1); the SAR instruction sets or clears the most significant bit to correspond to the sign (most significant bit) of the original value in the destination operand.

Quoted from: http://faydoc.tripod.com/cpu/shr.htm

DirectX 11: GPUs and headers

Few days ago I was wondering how long it will take to see DirectX 11 headers translated to Delphi/Pascal. I knew that translations of new DX components Direct2D and DirectWrite are part of Delphi 2010, but what about Direct3D and DirectCompute?

I checked out Clootie graphics pages but no luck there. Unfortunately, it's not so active anymore. After that I thought DX11 for Pascal wouldn't appear in like several months at least. Surprise, I found it about 5 minutes later. John Bladen published his DX11 translation on DirectX for Delphi blog. Thanks John!

Now all that remains is buying a DX11 GPU. New Radeons should be released sometime during this week and I'm thinking of getting 5850 model in a few months. Rumours are that NVidia's DX11 cards won't be out before 2010, so probably no big price drops for 5800 Radeons are to be expected until then.

My pile of old graphic cards will get bigger once again. I don't buy new CPU/motherboard/RAM too often (now it's Core2 from 2007 and before that AthlonXP in 2002) and I usually manage to sell the old ones or put them on server duty somewhere. However, since GPU performance as well as requirements on it rise much more faster than that of CPU, I ended up with like 5 cards in last 8 years. I managed to sell Radeon 9000 and GeForce 6600 but I still have Riva TNT2 and Radeon X1950 in my closet somewhere (and S3 Trio!). Hopefully, I'll find a buyer for my 1GB 4850 soon.

Lately, I've had bad luck buying some new stuff just before major price drop. Hopefully, this won't happen with 5850. Not that I want it to remain expensive - just want to buy it after the drop, not  a little while before it.

Block Compression, DXTC, And Imaging

Imaging supported DXT image/texture compression since one of the earliest releases. Quality of compressed images isn't very high though (at least the compression isn't too slow). For future Imaging versions I plan to ditch the current compression code and add a new one. To be precise, two new ones - fast and lower quality (still probably better than current Imaging's compression), and slow and higher quality mode. Fast one will be based on Real-Time DXT Compression by Id Soft. I'm not decided on high quality one yet, but probably something like cluster fit algorithm from Squish library.

Mainly for testing purposes during implementation of these new methods, I want to create extension for Imaging that compares two images (original and one reconstructed from compressed original) and measures PSNR and some other quality metrics.

I'm also thinking of implementing DXT5 based format using YCoCg colorspace and PVRTC (texture compression currently used in iPhone).

Few links if you're interested:

Here's some quick comparison of DXT compressors (click the image to see full size). Value in brackets is MSE (mean square error) - lesser number means compressed image is more similar to original.

DXT compressors comparison

Daggerfall for Free!

One of my all time favorite games was released as free download few weeks ago as a celebration of 15th anniversary of Elder Scrolls series. It’s 1996 title The Elder Scrolls: Daggerfall by Bethesda. You can get 150MiB zip archive with install at BethSoft's site. Getting it working on modern OS is not easy but DosBox should help here.

There were a lot of attempts at remakes of Daggerfall but all failed so far. However, a new promising one appeared recently and it’s progressing at warp speed since then (one month from wrongly textured entrance of Privateer’s Hold dungeon to rendering city blocks). Check out development blog and downloads at the homepage of DaggerXL. You need original Daggerfall data files to run the demo – no problem now when you can get it free.

Daggerfall Workshop was recently reopened as well. It’s a home of tools that let you explore Daggerfall’s game assets (textures, models, maps, music, etc.). There’s also a new exploring tool in development that looks really good, with very interesting blog posts about various Dagger’s file formats, rendering techniques, and so on.

I did my small share of dabbling in Daggerfall’s file formats too. Image and texture loaders in Object Pascal are part of ElderImagery extension of Imaging library and I’ve got BSA archive handler somewhere too. I’m now thinking of adding SKY format (backdrop images used in game for skies and far away mountains) loader, just for fun I guess.

When writing about Daggerfall, one cannot forget mentioning its music which was really excellent. Much more atmospheric and fitting than the music in recent Elder Scrolls games (there was also a lot more tracks). Check out Robert Hood’s arrangements of Eric Heberling's original music tracks.

UPDATERobert Hood's site has been dead for years but you can find his and many other great DF music remixes here: Daggerfall music.

Castle WayrestWildreness at night

WildernessMy character

My other character

First JPEG 2000 for Pascal Release

I finally released first version of JPEG 2000 for Pascal library. It’s based on translated header and precompiled OpenJpeg library that was part of Imaging for a long time – now released separately.

There’s header translation working with both Delphi and FPC and original C library precompiled to object files (Delphi) and static libraries (FPC for Win32, Linux x86/x64, and Mac OS X).

I’ve written simple TBitmap descendant that loads and saves JPEG 2000 images. It’s only for Delphi now – can’t do LCL version with just few IFDEFS, there are lot more changes between VCL and LCL TBitmaps. I plan to write separate LCL version for one of upcoming releases (using TFPCustomImageWriter and TFPCustomImageReader classes).

You can get JPEG 2000 for Pascal library at it’s project page.

APNG Update

APNG loading and animating implementation for Imaging wasn't very hard work. However, there are not that many test images to be found on the Internet and most of the available ones are very simple. They're usually not using disposal methods and are basically just collection of independent images. Big difference compared to GIF files - some of them were quite difficult to animate right. I even got most of nontrivial APNG test files by converting some more complex animated GIF files. Tools for creating APNG images are not yet as sophisticated as some GIF animation tools - there's GIF to APNG converter, APNG Anime Maker, and some web based tools that assemble simple APNG file from bunch of uploaded single PNG frames.

Now I'm gonna extend PNG saver to allow saving multi images to simple APNG files, much like MNG saving works now. There's really not a good unified way how to pass some more information to image file savers in current library design - that's one of TODOs for new Imaging architecture.

APNG Support for Imaging

I started working on support for APNG format for Imaging library. APNG is unofficial extension of PNG image file format created by two guys from Mozilla Corporation. The point of APNG is to allow storing simple animations in PNG files (hence the "A" for "Animated").

There is already PNG-like chunk based format for animations called MNG (already supported by Imaging - at least the basic features). However, MNG is quite complex format and its support among browsers and image viewers/editors is lacking. Code library supporting all MNG features is huge.

APNG on the other hand is just an extension of PNG and its implementation is not so complex. I'm going to load only the raw frames from files at first and see what will have to be done to support animating the frames next. Canvas class will have to be used here for alpha blending subsequent frames to previous ones. I'll add option to turn the animating on/off just like it is available for animated GIF files.

More info about APNG: http://www.animatedpng.com and https://wiki.mozilla.org/APNG_Specification.

New Homepage Ready

I created new web site based on WordPress at https://galfar.vevb.net/wp/ few months and I’ve been slowly transferring content there since then. It’s ready now so I’ll redirect http://galfar.vevb.net/ to the new site in a moment. Old one will still be available at http://galfar.vevb.net/cms/ but all the old content should be available here too.

Current theme is just temporary, I’ll look for something more to my liking later (going away for a week in few hours) and probably do some modifications. I’ve tried quite a few themes already but there was always some glitch (no header margins, wrong image alignment, etc.).

Working in MODULA-2

I’ve been working in Modula-2 for last few weeks. It’s a considered a dead language now, one of Pascal’s descendants designed by Niclaus Wirth. I’ve been doing some updates to quite an old project, embedded system for military usage.  I had to run Mac OS 7.5 emulator to get original 1989 compiler running. Modula-2 dialect understood by this compiler is quite strict – you cannot even mix cardinal and integer numbers without explicitly converting one of them.

First difference you may notice (compared to Pascal) is case sensitiveness of Modula-2, for instance all reserved words are upper cased. You don’t have to write so many begin-end statements though. Another C-like trait is importance of header files (definition modules) order during compiling – just change it a little bit in a large project and whole thing breaks down.

More info about Modula-2 if you’re interested: http://www.modula2.org/.

Modula-2