News for hackers. Stuff that matters.
Re:How does your programming language handle “minus zero” (-... by linkdd on Friday March 05, 2021 @09:30AM (#26357209)
In some game engines, Fixed Point Arithmetic is used instead of Floating Point, because it's faster, and the approximation is generally good enough for a game.
Re:How does your programming language handle “minus zero” (-... by maximilianburke on Friday March 05, 2021 @11:06AM (#26358346)
It might be a little faster for addition and subtraction in synthetic benchmarks -- and with the use of pipelining might end up being slower overall -- but multiplication and division are considerably faster with floating point.
Re:How does your programming language handle “minus zero” (-... by dnautics on Friday March 05, 2021 @01:25PM (#26360088)
division, yes, but multiplication is faster, but it's not huge. The limiting factor for multiplication is that it's O(N^2) in multiplied digits; so a 32-bit fixed point has 32x32 and IEEE fp32 multiplication has 23x23.
Re:How does your programming language handle “minus zero” (-... by krackers on Friday March 05, 2021 @05:19PM (#26362920)
That's assuming that you're implementing multiplication in software though, right? Since CPUs have dedicated multipliers, aren't both around a single cycle?
Re:How does your programming language handle “minus zero” (-... by dnautics on Friday March 05, 2021 @05:50PM (#26363258)
They are not in general (iirc 64b mult is down to 3 cycles now), but usually in an x86 cpu it can run microinstructions out of order enough so that the multiplication looks like it's finished in one cycle.
Re:How does your programming language handle “minus zero” (-... by quelsolaar on Friday March 05, 2021 @09:38AM (#26357294)
I dont think this is true. Integer math is used in some game engines because they need to be deterministic, between networked computers (and different CPUs rounds floating point numbers differently). I have written an engine like that, and i know that Starcraft 2 is all integer for the same reason. No one does it because its faster or easier. Its a pain.
Re:How does your programming language handle “minus zero” (-... by neurocline on Friday March 05, 2021 @11:59AM (#26359069)
Starcraft II uses floats, but for the game simulation portion it uses an internal software floating-point library to ensure consistency.
Re:How does your programming language handle “minus zero” (-... by quelsolaar on Friday March 05, 2021 @12:29PM (#26359417)
You are right, all the graphics are full of floats. Writing "all" was a bit of an overreach.
Re:How does your programming language handle “minus zero” (-... by sandworm101 on Friday March 05, 2021 @09:45AM (#26357354)
It depends on which 0 you are talking about. The 1-1=0 or the 1/infinity zero. Somewhere deep in a physics department there is an equation where the distinction either confirms or denies the multiverse.
Re:How does your programming language handle “minus zero” (-... by namibj on Saturday March 06, 2021 @12:06PM (#26368774)
Actually, the rounding for basic add/multiply are strictly defined and configurable (some algorithms need other rounding rules than the default "round to nearest even").

If you only use those (maybe including division, I'm not sure), and don't rely on hardware support for sqrt, sin/cos/tan, approximate-inverse, etc., you can totally use them deterministically between different architectures.

Re:How does your programming language handle “minus zero” (-... by linkdd on Friday March 05, 2021 @09:41AM (#26357324)
I never said it was easier and I never said ALL game engines.

And computing integers IS faster than computing floats, at least on a CPU.

Re:How does your programming language handle “minus zero” (-... by quelsolaar on Friday March 05, 2021 @09:59AM (#26357510)
>And computing integers IS faster than computing floats, at least on a CPU.

In the abstract yes. In reality not so much. A lot of what floats are used for in games is vector math (you are often in a 2D/3D world), and vector math in fixed point requires a lot more work in integer math since you constantly need to shift down things in order to avoid overflow. Overflow bugs are a constant problem when doing dot products in fixed point integer math.

Another common operation in vector math is square root (to normalize vectors) Modern CPU's do that very fast, and you can also use clever approximations that use the way floating point's are represented in hardware.

Most of whats done in 32 bit floats in games, needs to be done with 64bit integers to manage these problems if you decide to use integers, and that means that your data grows, more cache misses, and things slow down.

On top of this everything you do in a game needs to be drawn on the GPU so you need to convert everything to floats anyway to show them on screen, and converting between integers and floats is also slow.

Re:How does your programming language handle “minus zero” (-... by linkdd on Friday March 05, 2021 @10:08AM (#26357613)
I never use the square root in games, I always compare squared distances (since the properties are the same).

If you try to do with Fixed Point Arithmetic what was intended to be done with Floating Point, you're the problem.

A 32 bits integer can hold values up to 4 billions, if I have that kind of value in a simple game, then yes i will switch to Floating Point Arithmetic, but when does that use case happen if you're not writing a physic simulation game ?

Re:How does your programming language handle “minus zero” (-... by quelsolaar on Friday March 05, 2021 @11:09AM (#26358393)
> A 32 bits integer can hold values up to 4 billions, if I have that kind of value in a simple game, then yes i will switch to Floating Point Arithmetic, but when does that use case happen if you're not writing a physic simulation game ?

All the time. Let me give you an example from a game I made. During early R&D I used 32 bit numbers and a fixed point of 11 bits. That means that a normalized vector is between 1024 and -1023. You can do 2 dot products between vectors before it breaks it breaks. (10 + 10 + 10 bits plus one sign bit). That means that you have to do a lot of shifting down. the world can only be 20 bits large, because you need to be able to multiply world coordinates with vectors without getting overflow.

20 bis is very low resolution for a real-time world. You get problems with things not being able to move slow enough at high frame rates. (this was in 2D). Switching to 64 bit was the right move.

Re:How does your programming language handle “minus zero” (-... by adwn on Friday March 05, 2021 @10:33AM (#26357943)
It appears you have a very niche definition of "game", one which excludes basically all 3D games.
Re:How does your programming language handle “minus zero” (-... by linkdd on Friday March 05, 2021 @10:35AM (#26357967)
Yes, that's exactly why I wrote:

> some game engines

and

> simple games

A 3D game is not a simple game.

Re:How does your programming language handle “minus zero” (-... by adwn on Friday March 05, 2021 @10:43AM (#26358063)
Ugh, do you really have to be this nitpicky? Fine, you wrote:

> the approximation is generally good enough for a game

No, it's not generally good enough for a game – only for some games, and not good enough for any game with 3D graphics.

Re:How does your programming language handle “minus zero” (-... by yvt on Saturday March 06, 2021 @12:28AM (#26365454)
Technically, 32-bit fixed-point numbers have 8-bit more precision compared to 32-bit floating-point numbers, but the real problem is it's quite cumbersome to fully utilize the precision offered by fixed-point numbers as you'd have to keep in mind their numerical range all the time.
Re:How does your programming language handle “minus zero” (-... by Qwertious on Friday March 05, 2021 @12:43PM (#26359596)
Speaking of nitpicky, I thought the gamecube's GPU was integer-driven? And it has plenty of 3D games.
Re:How does your programming language handle “minus zero” (-... by edflsafoiewq on Friday March 05, 2021 @01:06PM (#26359882)
The biggest problem with fixed point is reciprocals behave very poorly; ie. x·(1/x)=1 and 1/(1/x)=x fail very badly. In order for these to work you obviously need the same amount of numbers above 1 as below 1, like in floating point.

Using fixed point with 11 bits after the decimal, with numbers as low as 100 (18 bits) we already have errors as large as 100·(1/100)=0.977.

Re:How does your programming language handle “minus zero” (-... by namibj on Saturday March 06, 2021 @12:03PM (#26368744)
Modern CPUs, like Zen2, have vastly more f64 multiplier throughput than 64 bit integer multiplier throughput.

This goes so far, that Intel added an AVX512 instruction to expose this 52/53 bit multiplier for integer operations, to accelerate bigint cryptography calculations like RSA and ECC.

Re:How does your programming language handle “minus zero” (-... by bjourne on Friday March 05, 2021 @11:05AM (#26358336)
Which modern general-purpose CPUs do not follow IEEE 754? I don't think what you're saying is true.
Re:How does your programming language handle “minus zero” (-... by quelsolaar on Friday March 05, 2021 @11:24AM (#26358617)
They all follow IEEE 754. But that defines the representation of the bits in a floating point number, not what happens when you operate on them. Different implementations do rounding differently, and there are arguments about how to do this correctly. There can be multiple ways to represent the same number even (this is a reason not to ever do a == compare on floating point number that have been operated on). Not only have there been differences between CPU makers, but also between different CPU of the same vendor.

A separate, complicating factor is that floating point math often don't yield the same results with different levels of optimization turned on. The reason to use integer math for games is because you want it to be deterministic so can be an issue.

Re:How does your programming language handle “minus zero” (-... by zokier on Friday March 05, 2021 @12:47PM (#26359639)
IEEE 754 does also specify operations and their behavior. E.g. section 5.1 (from 2008 edition):

> All conforming implementations of this standard shall provide the operations listed in this clause for all supported arithmetic formats, except as stated below. Each of the computational operations that return a numeric result specified by this standard shall be performed as if it first produced an intermediate result correct to infinite precision and with unbounded range, and then rounded that intermediate result, if necessary, to fit in the destination’s format (see 4 and 7).

Re:How does your programming language handle “minus zero” (-... by quelsolaar on Friday March 05, 2021 @04:10PM (#26362162)
Well the fact is that different hardware rounds it differently, so its the world we live in. My guess is that actually implementing something in hardware " to infinite precision and with unbounded range" might not be realistic.