0

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
  • This site isn't a "please write for me the code" (especially not a full library). If there is a piece of code you don't understand you can ask. But there must be a precise question. – xanatos Dec 23 '20 at 12:21
  • Welcome to stackoverflow.com. Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please take the [tour] and read about [ask] good questions. Lastly please read [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). It's a lot to read, but it will help you write better questions. – Some programmer dude Dec 23 '20 at 12:22
  • The link you provided doesn't work... –  Dec 23 '20 at 12:22
  • @Cowboy_Patrick Which one? –  Dec 23 '20 at 12:26
  • https://github.com/vlad-olteanu/longer-int is the only such link I see in the deleted question's comments. – Shawn Dec 23 '20 at 12:38
  • If you don't mind compiler extensions, see https://gcc.gnu.org/onlinedocs/gcc-10.2.0/gcc/Floating-Types.html – Shawn Dec 23 '20 at 12:48
  • `longer_int` is not a floating-point type. – ssbssa Dec 23 '20 at 14:38
  • This one: https://stackoverflow.com/questions/65168668/is-there-a-way-how-to-make-a-custom-floating-point-value-using-c –  Dec 23 '20 at 19:31
  • @Cowboy_Patrick Only moderators and users with >10k rep can see it. –  Dec 24 '20 at 09:08
  • @Shawn Yeah, that's what I'm looking for. The strange thing with this is that I wanted to modify it to actually make it into a floating-point, but I have no idea how. Trying `double` could've helped me, but I didn't know where to start. Thanks for the link. I needed it. –  Dec 24 '20 at 19:40

3 Answers3

0

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.

Skult
  • 46
  • 3
  • Well, I wanted a floating-point one, so I think a good form of doing so is finding more stuff to help me build the actual data type. –  Dec 24 '20 at 19:39
0

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 */
}
xanatos
  • 109,618
  • 12
  • 197
  • 280
  • Still can't figure out how to deal with the TO-DO. Trying still with the hard days of the work. –  Dec 24 '20 at 19:36
  • @MarkGiraffe That depends on how you want to "organize" your `binary128`. I wrote "TOTO" exactly for this reason. There are various ways to do it – xanatos Dec 24 '20 at 20:20
0

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.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • As far as I know, structs are the closest to classes, but I'm still confused how OOP fully works via general-purpose one. –  Dec 24 '20 at 19:37