The constant expression optimization that happens in NXC will occur with or without whitespace in the expression at optimization level 1+, iirc. It definitely is applied at level 2, which is the level I always use.
FYI, I have changed rand() to be defined like this:
and RAND_MAX is now correctly defined like this:
I briefly flirted with changing rand to be something like the function Doc used in his code above along with a simplified form of srand but in the end I decided against that approach. With rand defined as above I get a million iterations through Doc's for loop (after removing the in-loop screen drawing) in 231089 milliseconds. This is from a modified form of his code:
Code: Select all
// 231089
j=rand()/(RAND_MAX / 16 + 1);
/*
syscall 24, __RandomArgs
add __signed_stack_001main, __RandomArgs.Result, __constVal32768
div __main_7qG2_j_7qG2_000, __signed_stack_001main, __constVal4096
*/
With Random(16) I get this:
Code: Select all
// 279698
j=Random(16);
/*
set __D0main, 16
syscall 24, __RandomArgs
add __RandomTmp, __RandomArgs.Result, __constVal32768
mul __RandomTmp, __RandomTmp, __D0main
div __RandomTmp, __RandomTmp, __constVal65536
mov __main_7qG2_j_7qG2_000, __RandomTmp
*/
And with the rand_ function I get this:
Code: Select all
// 285057
// j=rand_()%16; // customized randomization function
/*
subcall rand_, __rand__return
// the body of rand_
mul __signed_stack_001rand_, _RAND_SEED_, __constVal1103515245
add _RAND_SEED_, __signed_stack_001rand_, __constVal12345
mod __DU0rand_, _RAND_SEED_, __constVal32768
subret __rand__return
// end of the body of rand_
mod __main_7qG2_j_7qG2_000, __DU0rand_, __constVal16
*/
I also removed the extra mov from the underlying __Random macro in NXTDefs.h that spiller pointed out. As defined now, rand() still does not produce results that work with the poor %N algorithm originally used by Doc (and working fine with Doc's rand_ function). It does, however, produce great (and the fastest possible) results when you use the algorithm shown above, i.e., /(RAND_MAX / N + 1).
John Hansen