Log in

View Full Version : Newbie questions regarding warnings and errors when compiling x64 plugins.


Reel.Deel
4th February 2022, 00:08
Hello all,

I'm creating this thread in hopes of asking some newbie questions when compiling existing plugins in x64. There's still some plugins that are x86 only (and not written in assembly). I'm hoping to compile what I can and put it up on the wiki so that they're available to everyone. I've compiled a few plugins already for personal use, and they all work but sometimes I get some warnings and/or errors that I do not know how to fix.

For example ChannelMixer (http://avisynth.nl/index.php/ChannelMixer), it compiles and works but it shows 19 warnings in MSVC 2019.

warning C4244: '=': conversion from 'double' to 'int', possible loss of data (9 WARNINGS)

// Build look-up tables
for(i = 0; i < 256; i++) {
RR[i] = 0.01 * inRR * (float)i;
RG[i] = 0.01 * inRG * (float)i;
RB[i] = 0.01 * inRB * (float)i;

GR[i] = 0.01 * inGR * (float)i;
GG[i] = 0.01 * inGG * (float)i;
GB[i] = 0.01 * inGB * (float)i;

BR[i] = 0.01 * inBR * (float)i;
BG[i] = 0.01 * inBG * (float)i;
BB[i] = 0.01 * inBB * (float)i;

warning C4101: 'tmp': unreferenced local variable (1 WARNING)
int i, tmp;

warning C4244: 'argument': conversion from 'double' to 'float', possible loss of data (9 WARNINGS)

// Calls the constructor with the arguments provied.
return new ChannelMixer(args[0].AsClip(), args[1].AsFloat(), args[2].AsFloat(), args[3].AsFloat(), args[4].AsFloat(), args[5].AsFloat(), args[6].AsFloat(), args[7].AsFloat(), args[8].AsFloat(), args[9].AsFloat(), env);

What is the proper way to get rid of these warnings? Also I read sometime ago that some warnings are harmless, is that the case here since the plugin works as intended?

wonkey_monkey
4th February 2022, 01:35
warning C4244: 'argument': conversion from 'double' to 'float', possible loss of data (9 WARNINGS)

There are two types of floating point number, 32-bit floats or 64-bit doubles. If you try to set a float variable to a double value, you'll get this warning.

If any of the variables involved are double, then the whole result gets "upgraded" to a double. In this case, the literal "0.01" is treated as a double. You can force it to a float with an f suffix:

RR[i] = 0.01f * inRR * (float)i;

Or you just cast the whole thing to a float (casting only the integer i to a float is somewhat redundant anyway);

RR[i] = (float)(0.01 * inRR * i);

It's possible that these two methods will give very slightly different results, or possibly run at very slightly different speeds, since the calculations are done with different accuracies depending on the types involved.

Generally speaking though, this warning is safe to ignore.

warning C4101: 'tmp': unreferenced local variable (1 WARNING)

This one's probably just a forgetful/lazy developer. Just remove tmp and it should be fine.

warning C4244: 'argument': conversion from 'double' to 'float', possible loss of data (9 WARNINGS)

Avisynth's AsFloat() actually returns a double, which is then being forced to convert to float because ChannelMixer::ChannelMixer(...) is expecting floats. Change all the instances of AsFloat() to AsFloatf() which will return a float instead.

Reel.Deel
4th February 2022, 07:59
Thanks for the information wonkey_monkey. Removing tmp and changing AsFloat() to AsFloatf() took care of those warnings.

I messed up and copied the wrong warning for the first example, my bad.
The warning is warning C4244: '=': conversion from 'double' to 'int', possible loss of data instead of what I wrote in the 1st post.
I tried both of your suggestions and I still get the same warning.

DTL
4th February 2022, 08:37
It looks the computation
0.01 * inRR * (float)i;
is in double because 0.01 is defined as double and compiler raises precision to highest used component in formula.

And RR[i] possibly int. So compiler warn - conversion of double formula calculation result to int output memory value.

So to fix warning - make explicit conversion of computation result to int:

RR[i] = (int)(0.01f * inRR * (float)i);

Also use 0.01 as float32 that may make computing faster. I hope float32 is enough in that use case.