3

I have a C++ code that I'm trying to reuse on my C# project and I need some help. Here is the subject

 for (int i = 0; i < numOfSamples; i++)
 {
      *(((double*)m_Buffer) + i)
          = max(*(((double*)m_Buffer) + i*4), *(((double*)m_Buffer) + i*4 + 1));
 }

where m_Buffer is array of float. This part of code read each 2 "floats" of array as a one "double" and then do some manipulations (shift it, choose max etc.) The question is - how can I do the same operation in C#.

For example, I have an array [12,45,26,32,07,89,14,11] and I have to transform items in position 0 and 1 (12 and 45) so that I will get a new number (type of double) where highest (I'm not sure - maybe lowest) part of bits will be formed from 12 and lowest - from 45

Codor
  • 17,447
  • 9
  • 29
  • 56
Andriy Zakharko
  • 1,623
  • 2
  • 16
  • 37
  • To my understanding, there is no bit shifting involved. What is `numOfSamples`? Apparently, `m_buffer` must contain at least `numOfSamples*4+1` elements of type `double` (if `double` uses 4 bytes) to work. – Codor Apr 01 '16 at 07:00
  • numOfSamples - its an length of array, big enough to hold this manipulations in bounds – Andriy Zakharko Apr 01 '16 at 07:10
  • 2
    (`*(pointer + index)` is repulsive coding in stead of `pointer[index]` - double-check if you wouldn't be better off coding from scratch. – greybeard Apr 01 '16 at 07:26

2 Answers2

1

It should be something like:

for (int i = 0; i < numOfSamples; i++)
{
    m_Buffer[i] = Math.Max(m_Buffer[i * 4], m_Buffer[i * 4 + 1]);
}

Where m_Buffer must be an array of at least numOfSamples * 4 + 1 elements.

xanatos
  • 109,618
  • 12
  • 197
  • 280
  • This is what I was thinking; However I wonder about the unusual syntax in the question; why was `*(((double*)m_Buffer) + i)` used instead of `((double*)m_buffer)[i]`? I also wonder about the bigger picture of the snippet. – Codor Apr 01 '16 at 07:07
  • 2
    @Codor In C/C++ there is an equivalence between using the `ptr[x]` notation and the `*(ptr + x)` notation... The programmer that wrote it probably didn't know OR preferred the pointer notation. – xanatos Apr 01 '16 at 07:09
  • 1
    And all the various `(double*)` are perhaps because the `m_Buffer` is a `char*` – xanatos Apr 01 '16 at 07:09
  • @AndriyZak I hope not... It is being casted to `double*` everywhere... `float` and `double` [are normally different](http://stackoverflow.com/a/2386882/613130). – xanatos Apr 01 '16 at 07:18
  • @xanatos I know that they are different. Unfortunately that is the reality - I'm working with legacy code – Andriy Zakharko Apr 01 '16 at 07:29
  • @xanatos I'm not so strong in C++ so let me clarify this - does this code m_Buffer[i] = Math.Max(m_Buffer[i * 4], m_Buffer[i * 4 + 1]); take to consideration that fact that in original code we have ((double*)m_Buffer) + i) so "i" should add 2 more bytes? Maybe it should be like this - m_Buffer[2*i] = Math.Max(m_Buffer[i * 4], m_Buffer[i * 4 + 1]); – Andriy Zakharko Apr 01 '16 at 07:30
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/107925/discussion-between-andriy-zak-and-xanatos). – Andriy Zakharko Apr 01 '16 at 07:36
0

So, I got the solution. Key point here is a structure

[StructLayout(LayoutKind.Explicit)]
struct MyStruct
{
    [FieldOffset(0)]
    public double Double;

    [FieldOffset(0)]
    public float Float1;

    [FieldOffset(4)]
    public float Float2;
}

I simply create a new array and put array[2*i] to Float1 and array[2*i+1] to Float2. Then apply Math.Max to each new_array[i].Double

Andriy Zakharko
  • 1,623
  • 2
  • 16
  • 37