IvantheDugtrio / veclib

Vector library for porting SSE2 instructions to other architectures
MIT License
14 stars 3 forks source link

Either inline your functions, move them in a class, or move them to a .c file #1

Closed nfischer closed 7 years ago

nfischer commented 7 years ago

I noticed you currently have all your function implementations in a .h file. While this may seem simpler, this can cause big issues when depending on your project.

If two different .c files include your header (even though you have #pragma once), you'll doubly define your function. This is not allowed in either C or C++.

The standard way of getting around this is to move your implementations into a .c file. You'll keep your declarations in the .h. So it'll look a bit like this:

// -------------------------------
// in your .h file
#pragma once

// your includes go here (don't forget your .h file!)

__m128i _mm_load_si128 (__m128i const* address);
__m128i _mm_loadu_si128 (__m128i const* address);
// etc.

// -------------------------------
// in your .c file

// your includes go here

__m128i _mm_load_si128 (__m128i const* address)
{
    return vec_load1q (address);
}

__m128i _mm_loadu_si128 (__m128i const* address)
{
    return vec_loadu1q (address);
}

In C++, you get some extra options to work around the issue

  1. inline your functions. This doesn't really fix anything, but compilers ignore the problem when you do this. C has support for inlining, but I tried it and it didn't seem to avoid the issue.
  2. Move your functions into a class declaration, and implement them within the class body. This actually inlines your functions, so it's really the same as #1.

Helpful resources

IvantheDugtrio commented 7 years ago

e2657c5 fixes #1

nfischer commented 7 years ago

@IvantheDugtrio I think it was actually fixed in https://github.com/IvantheDugtrio/veclib/commit/e2657c5cceb18fbe6ad70d4ed5849cdfaa864bbf

Thanks for getting to this so quickly!