I'm trying to compile the following C program using GCC and I'm getting an error
on line seven because the type taus88_t*
is somehow not getting returned properly from the initializing function call called make_taus88(seed);
?
error: incompatible types when initializing type 'struct taus88_t *' using type 'taus88_t'|
I've tried using taus88_t TAUS88 = make_taus88(6346456);
but that gives more errors/warnings.
taus88main.c||In function 'main':|
taus88main.c|8|error: incompatible type for argument 1 of 'taus88u32'|
taus88.h|21|note: expected 'struct taus88_t *' but argument is of type 'taus88_t'|
taus88main.c|9|error: incompatible type for argument 1 of 'taus88f32'|
taus88.h|22|note: expected 'struct taus88_t *' but argument is of type 'taus88_t'|
taus_88_cpp\taus88main.c|9|warning: unused variable 'numberf32'|
taus_88_cpp\taus88main.c|8|warning: unused variable 'numberu32'|
The project is in 3 files below.
taus88main.c
#include "taus88.h"
#include <stdint.h>
int main()
{
taus88_t* TAUS88 = make_taus88(6346456);
u32 numberu32 = taus88u32(TAUS88);
f32 numberf32 = taus88f32(TAUS88);
return 0;
}
taus88.h
#ifndef _COMMON_TAUS88_H
#define _COMMON_TAUS88_H
#include <stdint.h>
typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef float f32;
typedef double f64;
typedef struct {u32 s1, s2, s3;} taus88_t;
taus88_t make_taus88(u32 seed);
u32 taus88u32(taus88_t *t);
f32 taus88f32(taus88_t *t);
#endif
taus88.c
#include <stdint.h>
#include "taus88.h"
taus88_t make_taus88(u32 seed)
{
taus88_t t;
t.s1 = 1243598713U ^ seed; if (t.s1 < 2) t.s1 = 1243598713U;
t.s2 = 3093459404U ^ seed; if (t.s2 < 8) t.s2 = 3093459404U;
t.s3 = 1821928721U ^ seed; if (t.s3 < 16) t.s3 = 1821928721U;
return t;
}
u32 taus88u32(taus88_t *t)
{
t->s1 = ((t->s1 & -2) << 12) ^ (((t->s1 << 13) ^ t->s1) >> 19);
t->s2 = ((t->s2 & -8) << 4) ^ (((t->s2 << 2) ^ t->s2) >> 25);
t->s3 = ((t->s3 & -16) << 17) ^ (((t->s3 << 3) ^ t->s3) >> 11);
return t->s1 ^ t->s2 ^ t->s3;
}
f32 taus88f32(taus88_t *t)
{
union {u32 i ; f32 f ;} u;
u.i = 0x3F800000 | (taus88u32(t) >> 9);
return u.f - 1.0;
}
So the main problem here is that you are returning a taus8_t
and trying to assign that to a taus88_t *
which is not valid, if you need to use pointers for reasons that are not obvious from the code then the fix is as follows:
taus88_t* TAUS88 = malloc(sizeof(taus88_t)) ;
*TAUS88 = make_taus88(6346456);
You must remember to call free
on the pointer though when you are done. A simpler approach, would be to skip using a pointer and do as follows:
taus88_t TAUS88 ;
TAUS88 = make_taus88(6346456);
u32 numberu32 = taus88u32(&TAUS88);
f32 numberf32 = taus88f32(&TAUS88);
Now you don't have to worry about calling free
anymore.
The other issue that I pointed out is that most likely taus88f32
violates strict aliasing
rules.