| We hope you enjoy your visit. You're currently viewing the Ultimate 3D Community as a guest. This means that you can only read posts, but can not create posts or topics by yourself. To be able to post you need to register. Then you can participate in the community active and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free. Join our community! If you are already a member please log in to your account to access all of our features: |
| Oop - if-less programming; For games | |
|---|---|
| Tweet Topic Started: May 19 2018, 01:13 PM (45 Views) | |
| Despellanion | May 19 2018, 01:13 PM Post #1 |
|
Forum God
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]()
|
Lately I've become more and more interested in programming without if-conditionals, switch statements and ternary operators. I can grasp the basic principle of "code as data", but how worthwhile is it for game development and is it a common practice? |
![]() |
|
| Dr. Best | May 20 2018, 12:52 PM Post #2 |
|
Administrator
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]()
|
If you write data-parallel code for SIMD architectures, especially for GPUs, you want to avoid all sorts of branching (if, for, while, switch, etc.) that could be divergent across threads. Threads run in groups (on NVIDIA GPUs they are called warps and consist of 32 threads). If one of these threads executes an instruction, all threads in the warp pay the cost for this instruction, whether they execute it or not. Thus, under these circumstances it is a common practice. However, if you do not have thousands of threads running in parallel to process similar data, the benefits should be much smaller. Conditionals do incur some overhead. If you can avoid them without doing much more work overall, that is often a good idea. But don't go out of your way to avoid conditionals in code that is not performance sensitive. Also, avoiding conditionals is one of very many things that you should be doing if you write cryptographic code that must not be susceptible to timing attacks. |
![]() |
|
| Despellanion | May 20 2018, 02:22 PM Post #3 |
|
Forum God
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]()
|
Thanks, that puts it into better perspective. Also, there are some instances in game development where it's more or less impossible to not use conditionals - at least to my knowledge. |
![]() |
|
| Dr. Best | May 24 2018, 11:08 AM Post #4 |
|
Administrator
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]()
|
Nah, it's totally possible, just look at branchless Doom.
|
![]() |
|
| Despellanion | May 26 2018, 12:22 AM Post #5 |
|
Forum God
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]()
|
So there's basically no upsides to it except that it runs super-safe? |
![]() |
|
| Dr. Best | May 26 2018, 06:14 PM Post #6 |
|
Administrator
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]()
|
And it's funny .
|
![]() |
|
| « Previous Topic · Basics · Next Topic » |





![]](http://z1.ifrm.com/static/1/pip_r.png)



.
4:50 PM Jul 11