Neo Geo Dev: Fixed Point Numbers
The article explains fixed point numbers for the Neo Geo, enabling decimal-like calculations using integers. It discusses their advantages, drawbacks, and practical coding examples for game development, emphasizing precision management.
Read original articleThe article discusses the implementation of fixed point numbers in programming for the Neo Geo, a vintage gaming console that lacks native support for decimal numbers. Unlike modern computers that utilize floating point arithmetic for decimal calculations, the Neo Geo operates solely with integers. To work around this limitation, developers can use a fixed point system, where the decimal point's position is predetermined, allowing for fractional values while still using integer arithmetic. The article explains the advantages of fixed point systems, such as speed and simplicity, as well as their drawbacks, including limited precision and flexibility. It emphasizes the importance of fixed point numbers in game development, particularly for sprite movement, where fractional pixel movement can enhance gameplay fluidity. The author provides a basic framework for creating a fixed point system, including macros for conversion and functions for multiplication and division, while also addressing potential overflow issues. The article concludes with a discussion on the need for precision adjustments in fixed point systems as game development progresses.
- Fixed point numbers allow for decimal-like calculations on systems that only support integers.
- The Neo Geo uses fixed point arithmetic to manage sprite movement and other calculations requiring fractional values.
- Developers must carefully choose the number of bits allocated for integer and decimal parts in fixed point systems to balance precision and range.
- Overflow management is crucial when performing arithmetic operations with fixed point numbers.
- The article provides practical coding examples for implementing fixed point arithmetic in game development.
Related
Y292B Bug
The Y292B bug is a potential timekeeping issue in Unix systems due to a rollover in the year 292,277,026,596. Solutions involve using dynamic languages or GNU Multiple Precision Arithmetic Library in C, emphasizing the need for kernel-level fixes.
Neo Geo Architecture: A practical analysis
The Neo Geo system by SNK featured dual processors, advanced I/O handling, and sophisticated graphics capabilities. It supported arcade and home markets with MVS and AES variants, offering rich gaming experiences.
SNES Graphics: 186.2ns or Bust
The article explains the SNES graphics rendering, detailing real-time pixel generation, VRAM access limitations, internal memory optimization, various video modes, and efficient sprite rendering using a line buffer.
Another variable-length integer encoding
The article presents two encoding schemes for small integers in binary formats: metric varint and imperial varint, highlighting their efficiency, advantages, and the use of zig-zag encoding for signed integers.
Floating Point Math
Floating point math in computing can cause inaccuracies in decimal calculations due to binary representation limitations. Different programming languages manage this with varying precision, affecting results like 0.1 + 0.2.
So you'd take
A.a * B.b and split it into A*B + A*b + a*B + a*b
Or out = ((A >> fixedbits) * (B >> fixedbits) << fixedbits)
+ ((A >> fixedbits) * b)
+ ((B >> fixedbits) * a)
+ ((a * b) >> fixedbits);
If you can get away with a little less precision and smaller whole numbers, you can avoid some of the multiplications by just doing this, which is quite common: (A.a >> (fixedbits / 2)) * (B.b >> (fixedbits / 2))
Since this article is talking about more precisely positioning sprites in a 2D world, it could practically be a one-liner: "instead of tracking positions/velocities in pixels, track them in half pixels". Everything falls out of that intuition.
https://en.wikipedia.org/wiki/Fixed-point_arithmetic#Softwar...
I spent so much of the early stage of my career doing early mobile stuff I practically still think in fixed point, and always have to adjust to floats, for example, fixed point results can be compared exactly while with floats that is not a great idea. TeX uses fixed point entirely because it was reproducible across machines in an era where floating point was not.
The SNES and GBA both supported affine transformations, where the elements of the multiplication matrix are fixed point numbers. The Playstation's geometry coprocessor (GTE) used fixed point matrices with quite low 16 bit precision. An emulator feature called PGXP is able to perform these calculations at higher precision.
Other than on retro systems, fixed point is still useful in smaller microcontrollers.
At that time, such games were written in assembler, and you had to be very careful to place the instructions for scaling and descaling in the right places, not only to get the final result in the right units (i.e., screen coordinates), but also in intermediate calculations to preserve precision.
Then your code doesn't ever need to be aware of the special fixed point math and horrible syntax everywhere and everything just works?
int temp = a * b;
ngassert(
abs(temp) >= abs(a),
"overflow!"
);
I wonder what stops the compiler from optimizing away the assert, as it's clearly based on an undefined behavior.Related
Y292B Bug
The Y292B bug is a potential timekeeping issue in Unix systems due to a rollover in the year 292,277,026,596. Solutions involve using dynamic languages or GNU Multiple Precision Arithmetic Library in C, emphasizing the need for kernel-level fixes.
Neo Geo Architecture: A practical analysis
The Neo Geo system by SNK featured dual processors, advanced I/O handling, and sophisticated graphics capabilities. It supported arcade and home markets with MVS and AES variants, offering rich gaming experiences.
SNES Graphics: 186.2ns or Bust
The article explains the SNES graphics rendering, detailing real-time pixel generation, VRAM access limitations, internal memory optimization, various video modes, and efficient sprite rendering using a line buffer.
Another variable-length integer encoding
The article presents two encoding schemes for small integers in binary formats: metric varint and imperial varint, highlighting their efficiency, advantages, and the use of zig-zag encoding for signed integers.
Floating Point Math
Floating point math in computing can cause inaccuracies in decimal calculations due to binary representation limitations. Different programming languages manage this with varying precision, affecting results like 0.1 + 0.2.