0

I am trying to write a VB.NET program which deals with a lot of small bit manipulations for which I am really missing the #define macro's which can be used in C/C++.

Is there a possibility to define preprocessor macros. For example,

#define MAX(x,y) ((x) > (y) ? (x) : (y))

Above will substitute the function at runtime replacing the token in C. Is it possible to do the same in VB.NET.

I do not want to write functions as for me speed is the key and I cannot afford functions being pushed on the stack for small but repetitive tasks.

Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
Ali Habiby
  • 69
  • 1
  • 7
  • 2
    I don't believe it's possible ([this](http://stackoverflow.com/questions/4952413/vb-net-is-there-a-way-to-create-a-pre-processor-constant-that-behaves-as-a-simpl) is sort-of a dupe), but [this](http://stackoverflow.com/a/23063080/2278086) may help with performance. – Mark Jun 08 '16 at 17:32
  • Thanks a lot Mark. Your link is really useful! – Ali Habiby Jun 10 '16 at 20:30
  • There is no legitimate reason for anyone to ever write that macro in C or C++, either. So you aren't losing anything by not being able to do it in VB.NET. – Cody Gray - on strike Jun 11 '16 at 11:13
  • @Cody Gray. In my humble opinion these are actually useful as they increase code readibility when you need repetetive one line statements but donot cost the same performance as a function calling and return. Look at the source codes of Stockfish or Crafty chess engines to see how useful these can be. – Ali Habiby Jun 12 '16 at 12:31
  • Any C or C++ compiler worth using supports inline functions, which give you all of the benefits with none of the disadvantages. Either those engines you speak of were written 20 years ago when inline functions were not available, or the authors learned C at approximately the same time and have not bothered to update their knowledge. Calling a simple function like this is *free*. You don't even need the `inline` keyword; the compiler knows enough to inline it on its own. – Cody Gray - on strike Jun 12 '16 at 13:15
  • The compiler may inline it or may not. Absolute control is still with the use of these directives. And this matters for a top chess engine like stockfish which need every bit of optimization to improve performance. – Ali Habiby Jun 12 '16 at 20:34

1 Answers1

0

As per Mark's response above in the comments,

It is not possible to define preprocessor directive macros in VB.NET. However, the closest alternative is to define a function and ask the compiler to use Aggressive inlining for that function. It will be still the JIT decision to make the function inline if certain conditions are fulfilled. (code size < 32 bytes in function ...)

This is supported in .NET version 4.5 and above.

Imports System.Runtime.CompilerServices 'This is needed to define the aggressive inlining constant

Class TestClass
    <MethodImplAttribute(MethodImplOptions.AggressiveInlining)> 'This will instruct compiler to use aggressive inlining if possible. Should be just before the function definition
    Public Function MyFunc(ByVal A As Integer, ByVal B As Integer) As Integer
        Return (A * A + B * B + 2 * A * B) 'An example function which should be inlined
    End Function
End Class
Ali Habiby
  • 69
  • 1
  • 7