Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 80123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:15:11+00:00 2026-05-10T21:15:11+00:00

I have a C library with numerous math routines for dealing with vectors, matrices,

  • 0

I have a C library with numerous math routines for dealing with vectors, matrices, quaternions and so on. It needs to remain in C because I often use it for embedded work and as a Lua extension. In addition, I have C++ class wrappers to allow for more convenient object management and operator overloading for math operations using the C API. The wrapper only consists of a header file and as much use on inlining is made as possible.

Is there an appreciable penalty for wrapping the C code versus porting and inlining the implementation directly into the C++ class? This library is used in time critical applications. So, does the boost from eliminating indirection compensate for the maintenance headache of two ports?

Example of C interface:

typedef float VECTOR3[3];  void v3_add(VECTOR3 *out, VECTOR3 lhs, VECTOR3 rhs); 

Example of C++ wrapper:

class Vector3 { private:     VECTOR3 v_;  public:     // copy constructors, etc...      Vector3& operator+=(const Vector3& rhs)     {         v3_add(&this->v_, this->v_, const_cast<VECTOR3> (rhs.v_));         return *this;     }      Vector3 operator+(const Vector3& rhs) const     {         Vector3 tmp(*this);         tmp += rhs;         return tmp;     }      // more methods... }; 
  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T21:15:12+00:00Added an answer on May 10, 2026 at 9:15 pm

    Your wrapper itself will be inlined, however, your method calls to the C library typically will not. (This would require link-time-optimizations which are technically possible, but to AFAIK rudimentary at best in todays tools)

    Generally, a function call as such is not very expensive. The cycle cost has decreased considerably over the last years, and it can be predicted easily, so the the call penalty as such is negligible.

    However, inlining opens the door to more optimizations: if you have v = a + b + c, your wrapper class forces the generation of stack variables, whereas for inlined calls, the majority of the data can be kept in the FPU stack. Also, inlined code allows simplifying instructions, considering constant values, and more.

    So while the measure before you invest rule holds true, I would expect some room for improvements here.


    A typical solution is to bring the C implementaiton into a format that it can be used either as inline functions or as ‘C’ body:

    // V3impl.inl void V3DECL v3_add(VECTOR3 *out, VECTOR3 lhs, VECTOR3 rhs) {     // here you maintain the actual implementations     // ... }  // C header #define V3DECL  void V3DECL v3_add(VECTOR3 *out, VECTOR3 lhs, VECTOR3 rhs);  // C body #include 'V3impl.inl'   // CPP Header #define V3DECL inline namespace v3core {   #include 'V3impl.inl' } // namespace  class Vector3D { ... } 

    This likely makes sense only for selected methods with comparedly simple bodies. I’d move the methods to a separate namespace for the C++ implementation, as you will usually not need them directly.

    (Note that the inline is just a compiler hint, it doesn’t force the method to be inlined. But that’s good: if the code size of an inner loop exceeds the instruction cache, inlining easily hurts performance)

    Whether the pass/return-by-reference can be resolved depends on the strength of your compiler, I’ve seen many where foo(X * out) forces stack variables, whereas X foo() does keep values in registers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a library written in C that I would like to use in
We have a Library Project that we use for all our central reused code
I have a library in C-language. is it possible to use it in C
I have had numerous bad experiences with GUI library so I would like to
i have library that is licensed under zlib/libpng and i what to use it
Of course most languages have library functions for this, but suppose I want to
I have a library I created, File mylib.c: #include <mylib.h> int testlib() { printf("Hello,
I have a library A, that I develop. When I deploy it on a
I have a library consisting of approx 100 source files. I want one of
I have a library that I would like to license and distribute. I know

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.