Re: Floating point load-store behaviour.



thisismyidentity@xxxxxxxxx writes:
I am trying to predict the behaviour of floating point load and store
operations on integer locations. I ve written a small piece of code
having some inline assembly code which I am describing here.

Why?

In standard C, attempting to read a floating-point value from an
integer object invokes undefined behavior. Anything can happen. If
you want to know what happens on your particular system, this isn't
the place to ask about it.

========================================================
#include<stdio.h>
#include<stdlib.h>

void fp_int_mem_ops(unsigned long);
void print_byte_vals(unsigned long);

static int unmatched_count = 0;

main ()

This should be "int main(void)".

{
unsigned long foo;
int i, seed;
for(i = 1000 ; i < 500000; i++){// Just to generate some random
ints.

Please avoid "//" comments in code posted to this newsgroup. They're
legal in C99, but not in C90, and as you can see they can create
problems with line-wrapping. If a "/* ... */" comment is wrapped onto
a second line, it won't create a syntax error.

seed = rand();
srand(seed);
foo = rand();
fp_int_mem_ops(foo);
}

This is a *really* bad way to generate random numbers. By repeatedly
calling srand(), you're interfering with the generator. Calling
srand() and rand() in some arbitrarily complicated way gives you
poorer random numbers, not better ones.

You should call srand() exactly once, before any call to rand(). One
common way to do this is:
srand(time(NULL));
which sets a seed based on the current time. Then call rand() once
for each random number you need.

If your system's rand() isn't good enough (it's often mediocre, and
should not be depended on for cryptographically secure random
numbers), use some system-specific random number generator; playing
tricks with rand() will only make things worse.

The only reason to call srand() more than once is to repeat the same
sequence, but I don't think that's what you need here.

printf ("\n%d mismatches\n", unmatched_count);
return 0;

}

void fp_int_mem_ops(unsigned long location)
{
unsigned long fp, fp1;

printf("\n===\n");
printf("%x\n", location);

__asm__ __volatile__ (
"flds %1;"
"fstps %0;"
:"=m"(fp)
:"m"(location), "m" (fp1)
);

__asm__ is non-standard, and we can't help you with it here. If you
have questions about this, try a newsgroup that's specific to your
system.

I don't understand your assmebly code, but if location is supposed to
be a memory location, you should declare it as a pointer, not as an
integer.


printf("%x\n", fp);

if(location != fp)
{
printf("NOT MATCHED\n");
unmatched_count++;
}



return 0;

}
========================================================
[snip]

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.



Relevant Pages

  • Re: Hunt for rand and srand implementations ;)
    ... Also I tested knoppix's rand and srand.... ... rand and srand... ... void srand(unsigned int seed); ... rand retrieves the pseudorandom numbers that are generated. ...
    (sci.crypt)
  • Re: Hunt for rand and srand implementations ;)
    ... probably have the same srand() and randroutines... ... // IBM AIX srand and rand implementation occurding to: ... int IBM_AIX_rand ...
    (sci.crypt)
  • Re: rand()
    ... Any other value for seed sets the generator to a random starting ... rand retrieves the pseudorandom numbers that are generated. ... rand before any call to srand generates the same sequence as calling srand ... appear to repeat on successive 'runs' is to use the system time, ...
    (microsoft.public.vc.mfc)
  • Re: rand and srand
    ... value from rand() by 2001, and returns the remainder - so the number can ... The C99 standard wants and int. ... automatic void of course with just as parameters. ... So is there really a need to use srand? ...
    (comp.lang.c)
  • Re: randomizing trouble
    ... number generator using gawk did not work because all 3 times it was run ... rand() returns a small decimal and srand() takes an integer. ...
    (comp.lang.awk)