PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] Crazy Bytes Algorithm-VBForums
Results 1 to 7 of 7

Thread: [RESOLVED] Crazy Bytes Algorithm

  1. #1

    Thread Starter
    Android OpenGL ES Guru Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,233

    Resolved [RESOLVED] Crazy Bytes Algorithm

    Hey guys. Long time no see.

    Here is my problem. My problem is not in VB.Net, but the solution I can convert from VB.Net code to the current language I'm working on. If I were to post this in any other forum, the odds on me finding an answer will be slim to none. But lucky for you all, I know many programming languages. So conversion is no problem.

    ... Probably shouldn't have said what I said so I'll keep the language I'm using out of it.

    Anyways straight to the point. The language I'm using is using a library that rely's on a constant buffer in the increments of 16 bytes. So whatever structure you feed it, for example:

    VB.NET Code:
    1. Public Structure MyBuffer
    2.     Public value as Integer
    3. End Structure

    instead of the 4 bytes it is, it will convert it to 16 bytes through padding. If it were slightly bigger, such as:

    VB.NET Code:
    1. Public Structure MyBuffer
    2.     Public value1 as Integer
    3.     Public value2 as Integer
    4.     Public value3 as Integer
    5.     Public value4 as Integer
    6.     Public value5 as Integer
    7. End Structure

    it will then be 32 bytes instead of the 20, and so on and so forth. So this means, I ran into a programming problem where I need to come up with a method that no mater what value you feed it, it should convert it in increments of 16. The math behind it is giving me a headache. I need to do it to end up with values like so:

    Code:
        '(0 - 16)   = 16
        '(17 - 32) = 32
        '(33 - 48) = 48
        '(49 - 64) = 64
        ' Etc etc etc
    I tried doing something like this:

    VB.NET Code:
    1. Private Function Convert_To_Buffer_Bytes(value As Integer) as Integer
    2.         Dim result As Integer
    3.         result = value / 16
    4.         Return result * 16
    5.     End Function
    6.  
    7.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    8.         Dim i As Integer
    9.  
    10.         For i = 0 To 64
    11.             ListBox1.Items.Add(Convert.ToString(i) & ": " & Convert_To_Buffer_Bytes(i))
    12.         Next i
    13.     End Sub

    but I wasn't the results I was looking for. Any help is appreciated in this "probably so easy to solve" programming problem that I'm having difficulty with at the moment because my brain stopped working for some reason. Thanks in advance

  2. #2

    Thread Starter
    Android OpenGL ES Guru Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,233

    Re: Crazy Bytes Algorithm

    Ok, I don't know why, but after playing around, I accidentally solved it:

    VB.NET Code:
    1. Private Function Convert_To_Buffer_Bytes(value As Integer) As Integer
    2.         Dim result As Integer
    3.  
    4.         If value > 0 Then
    5.             If value Mod 16 = 0 Then
    6.                 result = value / 16
    7.             Else
    8.                 result = Math.Floor(Convert.ToSingle(value) / 16.0!) + 1
    9.             End If
    10.         ElseIf value = 0 Then
    11.             result = 1
    12.  
    13.         Else
    14.             Return 0
    15.         End If
    16.  
    17.         Return result * 16
    18.     End Function

    Well, so much for that

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    40,658

    Re: [RESOLVED] Crazy Bytes Algorithm

    It looks like your brain woke up, as that seems valid.

    Note that for VB at least (I don't know about your target language), this section of the code:
    Code:
                If value Mod 16 = 0 Then
                    result = value / 16
                Else
                    result = Math.Floor(Convert.ToSingle(value) / 16.0!) + 1
                End If
    ...can be simplified to:
    Code:
                result = value \ 16 + If(value Mod 16 = 0, 0, 1)
    Using \ for division (rather than / ) gives the integer portion of the result, without decimal places. A less efficient alternative is to use: Int(Value/16)

  4. #4

    Thread Starter
    Android OpenGL ES Guru Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,233

    Re: [RESOLVED] Crazy Bytes Algorithm

    It was actually C++ with DirectX11. The constant buffers are fed through the shader in increments of 16 bytes. So in order to put in the size of the buffer, you have to put in a value divisible by 16. Any other value will crash the program. This method I created will prevent that.

  5. #5
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,905

    Re: [RESOLVED] Crazy Bytes Algorithm

    I usually just add X - 1 to the value and do the integer divide.
    In your case X is 16, so add 15 to the value and do the integer divide.
    Code:
        Private Function Convert_To_Buffer_Bytes(value As Integer) As Integer
            If value >= 0 then 
                If value =  0 Then
                    Return 16
                 Else
                    Return ((value + 15) \ 16) * 16
                 End If
            Else
                Return 0
            End If
        End Function

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    9,143

    Re: [RESOLVED] Crazy Bytes Algorithm

    Without division or multiplication

    Code:
        Private Function Convert_To_Buffer_Bytes(value As Integer) As Integer
            Const msk As Integer = -16
            Dim rv As Integer = (value + 15) And msk
            If rv = 0 Then rv = 16
            Return rv
        End Function
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,905

    Re: [RESOLVED] Crazy Bytes Algorithm

    Yes that is better.
    That is what I do when the value desired is a power of 2. Should have recognized it, but at the time was thinking about a general function that could handle any value of X could be written, so distracted myself from the fact that is was a power of 2.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width