How to implement binary128 in C?

247 views Asked by At

I started to try and implement new data types, specifically floating-point values, using C. I came up with this sort of way on implementing binary128 using longer_int which I haven't fully understood.

The way how I started was to implement it via copying the code and modifying it. However, the code is C++, and C doesn't support OOP, so what I have to do is to try and translate the code.

I can't find the C version for longer_int because this question I once owned (the same account but now deleted) is now deleted and I can't view the links.

With the help of these C++ files, how can I implement binary128 using C?

And just so this question won't get closed, I want to know how to translate the OOP format in longer_int.h.

class longer_int {
private:
    char num_str[];
public:
    longer_int();
    longer_int(const int &num);
    longer_int(const char &num[]);
    longer_int(const longer_int &num);

// Sample code from it, now translated to C, but still OOP
3

There are 3 answers

2
xanatos On BEST ANSWER

You should begin with something like this: a struct instead of a class, ignore the public/private keywords, the constructors are methods that return the my_int128. There is no overloading in C, so each different constructor must have a different name. Another school of thought would say that you don't need specialized constructors, but simply special method setters that can copy data from an int, from a char[] and from a my_int128 (and I prefer this school of thought). I give an example setter at the end.

#include <stdio.h>
#include <string.h>

typedef struct 
{
    char num_str[16]; /* fixed to int128 */
} my_int128;

/* Don't need this: my_int128 li = { 0 }; is enough!
my_int128 create_int128()
{
    my_int128 li = { 0 };
    return li;
}
*/

my_int128 create_int128_from_num(int num)
{
    my_int128 li = { 0 };
    /* TODO: set li with num */
    return li;
}

my_int128 create_int128_from_char_array(const char num[16])
{
    my_int128 li = { 0 };
    memcpy(li.num_str, num, 16);
    return li;
}

my_int128 create_int128_from_int128_ptr(const my_int128 *num)
{
    return create_int128_from_char_array(num->num_str);
}

The setter:

void set_from_int(my_int128 *li, int num)
{
    /* TODO: set li with num */
}
1
Skult On

Your best shot using C would be implementing binary128 as a structure type

struct binary128 {
    unsigned char data[16];
};

Mind that there is no operator overloading in C, so you will need to implement all interaction with the type (like assignment from another type, arithmetic operations, so on) as functions.

Follow link in comments posted by Shawn to see more general case: implementation of integers of arbitrary width.

1
Serge Ballesta On

I assume that you have some C++ object code and you want to rewrite it in plain C. Once you think that first C++ compilers were just pre-processors that generate C code, it should not be too hard.

First the caveats:

  • C has no provision for encapsulation: functions can only have translation unit visibility or global visibility
  • C has no provision for function overwriting. One function can only have one set of parameters: use different names

That being said, to translate a C++ class, you build a C struct to hold the data members, and replace methods with plain functions. For that last part, you just replace the hidden this with an explicit pointer as an additional parameter.