lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It looks like gcc on the powerpc isn't implementing IEEE 754 rounding
modes correctly or consistently with -O2. If I recompile Lua without
optimisation then the negative zeroes go away.

foot:Code fanf$ uname -a
Darwin foot.local 9.8.0 Darwin Kernel Version 9.8.0: Wed Jul 15 16:57:01 PDT 2009; root:xnu-1228.15.4~1/RELEASE_PPC Power Macintosh
foot:Code fanf$ ./lua-5.1.4/src/lua -e 'print(0%1)'
-0
foot:Code fanf$ cat fegetround.c
#include <fenv.h>
#include <math.h>
#include <stdio.h>

#define luai_nummod(a,b) ((a) - floor((a)/(b))*(b))

int main() {
	printf("FE_DOWNWARD = %d\n", FE_DOWNWARD);
	printf("FE_TONEAREST = %d\n", FE_TONEAREST);
	printf("FE_TOWARDZERO = %d\n", FE_TOWARDZERO);
	printf("FE_UPWARD = %d\n", FE_UPWARD);
	printf("fegetround() = %d\n", fegetround());
	double x = 0.0, y = 9.0;
	printf("luai_nummod(%f,%f) = %f\n", x, y, luai_nummod(x,y));
	fesetround(FE_DOWNWARD);
	printf("luai_nummod(%f,%f) = %f\n", x, y, luai_nummod(x,y));
	return(0);
}
foot:Code fanf$ gcc -Wall -Wextra -o fegetround fegetround.c && ./fegetround
FE_DOWNWARD = 3
FE_TONEAREST = 0
FE_TOWARDZERO = 1
FE_UPWARD = 2
fegetround() = 0
luai_nummod(0.000000,9.000000) = 0.000000
luai_nummod(0.000000,9.000000) = -0.000000
foot:Code fanf$ gcc -O2 -Wall -Wextra -o fegetround fegetround.c && ./fegetround
FE_DOWNWARD = 3
FE_TONEAREST = 0
FE_TOWARDZERO = 1
FE_UPWARD = 2
fegetround() = 0
luai_nummod(0.000000,9.000000) = 0.000000
luai_nummod(0.000000,9.000000) = 0.000000

Here are a few tests using a Lua modified to add wrappers for fegetround()
and fesetround() and compiled with optimization. This shows how weirdly it
can behave.

foot:Code fanf$ ./lua-5.1.4/src/lua -e 'print(0%1, math.getround())'
-0	0
foot:Code fanf$ ./lua-5.1.4/src/lua -e 'for i=0,4 do math.setround(i); print(0%1); end'0
0
0
0
0
foot:Code fanf$ ./lua-5.1.4/src/lua -e 'math.setround(0); print(0%1)'
0
foot:Code fanf$ ./lua-5.1.4/src/lua -e 'math.setround(1); print(0%1)'
-0
foot:Code fanf$ ./lua-5.1.4/src/lua -e 'math.setround(2); print(0%1)'
-0
foot:Code fanf$ ./lua-5.1.4/src/lua -e 'math.setround(3); print(0%1)'
-0

Tony.
-- 
f.anthony.n.finch  <dot@dotat.at>  http://dotat.at/
HUMBER THAMES DOVER WIGHT PORTLAND: NORTH BACKING WEST OR NORTHWEST, 5 TO 7,
DECREASING 4 OR 5, OCCASIONALLY 6 LATER IN HUMBER AND THAMES. MODERATE OR
ROUGH. RAIN THEN FAIR. GOOD.