PERFORMANCE Problem Solving in C: Floating Point Bit Manipulation

Поділитися
Вставка
  • Опубліковано 30 вер 2024

КОМЕНТАРІ • 25

  • @nileriversoftware4070
    @nileriversoftware4070 2 роки тому +7

    Unlike most tech bros, this guy actually knows what he's talking about.

    • @ZygalStudios
      @ZygalStudios  2 роки тому +1

      🤣 can't tell if that's sarcasm or not.
      Sorry humor tends to go over my head.
      Either way, thanks!

  • @michaelanderson3958
    @michaelanderson3958 3 роки тому +5

    I felt so big brain knowing the solution would be the sign bit from IEEE

    • @ZygalStudios
      @ZygalStudios  3 роки тому +1

      EZ Peezy lemon squeezey! Copy the sign bit and call it a day! :)

  • @saraweber312
    @saraweber312 2 роки тому +2

    Took me a while of staring at it, but once I figured out why you like dereferenced then casted a pointer, I was extremely satisfied. A very clever, very C solution :)

  • @shkhamd
    @shkhamd 2 роки тому +1

    The moment I saw the function name and that if else block, I felt like, jeez dude, just cast the variable, shift the sign bit, OR it, re-cast and return.
    Knowing that I haven't coded C in years and right now wouldn't even qualify in a interview for a C developer, this gave me a little moment of good feeling.
    May be all hope is not lost for me.

  • @aperture147
    @aperture147 Рік тому

    the evil bit hack like the one used in Q_rsqrt in quake

  • @zzco
    @zzco 3 роки тому +1

    DOOM did something similar.

    • @ZygalStudios
      @ZygalStudios  3 роки тому +3

      Yes! Carmack came up with the fast inverse square root by approximating the division using newton's method!
      Their solution was much more elegant and complicated than mine lol. I'm just copying a bit

    • @zzco
      @zzco 3 роки тому +1

      @@ZygalStudios No worries! It just took a bit (hahaha) for it to be understood.

    • @ZygalStudios
      @ZygalStudios  3 роки тому +1

      @@zzco 🤣😂😅

    • @CKDEV
      @CKDEV 3 роки тому +3

      DOOM actually came before most computers had floating point support, so it was entirely fixed point. It was Quake (2?) that implemented the fast inverse square root trick.

    • @zzco
      @zzco 3 роки тому +1

      @@CKDEV Oh, right! Oops, haha.

  • @camjackson5977
    @camjackson5977 3 роки тому

    Any particular reason you type out a constant for your bitmask in the first part of the OR and shift left logical in the second? Seems odd to show them in two different ways on the same line. For instance couldn't you replace "0x8000000" with another "(1

    • @ZygalStudios
      @ZygalStudios  3 роки тому

      The only real reason was it was the first way it came to mind. Lol
      It's awesome you cared enough to comment about this and have a discussion about it. This is exactly what I was hoping for with this video! So thank you for that.
      Expanding on your point, I definitely see where you are coming from, but here's my take on the situation.
      Of course this is an opinion, so take it with a grain of salt.
      1. It seems like you were clearly able to grasp this code fast enough to the point where you already found another way to get the same instructions. Wouldn't that mean it's already clear enough? I hope i didn't sound like an imbecile (might be a bit crass), but usually that's a thumbs up from me. As long as it fits inside the project coding standards.
      2. Why waste engineering resources on doing something like this if functionally it's already where you want it to be? Having the urge to be pedantic is a helpful tool, but it can also waste alot of time in some cases for marginal benefit. There's no purpose in making a function this obscure any clearer in my opinion, as long as this is outlined in comments. If they result in the same instructions and this is a utility function, who cares? If this was something that would potentially need to be refactored in the future and dealt with more than just a float result, I definitely would agree with you, but since it's just a utility, I'm totally OK with it. If there are instructional improvements; however, now we are talking. Otherwise, it's totally OK.
      That's my take. :)

    • @camjackson5977
      @camjackson5977 3 роки тому

      @@ZygalStudios Ah, gotcha. Definitely didn't mean to criticize your coding style or anything, and I apologize if that was how it came off. I was mainly just wondering for my own knowledge if there was some quirk in C that I didn't know about that made it necessary to do it that way. You seem to have a lot more experience working at a near hardware level than I do so I figured you might know something I don't. Thanks for the response. Love the videos

    • @ZygalStudios
      @ZygalStudios  3 роки тому

      @@camjackson5977 didn't take it that way :)
      Like I said your comment was awesome and these are the types of comments I love to see. You asked a great question and took the time to understand the technical parts.
      I just tried to expand on the comment with how I would normally approach this from an engineering perspective.
      Something I try to do too is put my ego aside. Whether that is for UA-cam, or professional work. I don't know everything and you can always learn something from someone else's take, or analysis.

    • @shkhamd
      @shkhamd 2 роки тому

      @@ZygalStudios Until I saw the assembly, I would have actually used the 0x8000 0000 instead of shifting, thinking that the shift operation would take extra cycles where the constant would do the job. Probably boils down to personal choice.
      But agree with you, too much neat picking at these levels often blindfolds the programmer.

  • @A31Chris
    @A31Chris 3 роки тому +3

    I have a suggestion. For myself personally videos about programming when they show the computer screen like this I cannot see the code on the screen at all really no matter how i jack the resolution up. I think it would be better done in some other way to enlarge the text greatly somehow. Or presented in a similar way that the code was presented in the 'what is a stack' video that you made. I am sorry to suggest extra work but this sort of presentation is really miserable for me to follow.
    Just my opinion and thoughts.

    • @Anteksanteri
      @Anteksanteri Рік тому +1

      Simply pressing Ctrl-+ also works in my experience.

  • @A31Chris
    @A31Chris 3 роки тому +2

    Can you do a video on floating point/fixed point and how it relates to games? Quake used floating point and was a challenge porting over to fixed point systems.

  • @A31Chris
    @A31Chris 3 роки тому +1

    I love your videos. Just grab popcorn