changed: 64bit asm code didn't want to compile

there were used 'lahf' and 'sahf' instructions
         probably they are supported in a new version of GCC
         with '-msahf' option but GCC 4.2.1 has no such option
         at the moment I'm using opcodes:
            lahf -> 9f
            sahf -> 9e
         Warning! these instructions are not on all 64bit processors
         from: http://gcc.gnu.org/onlinedocs/gcc/i386-and-x86_002d64-Options.html
         "Early Intel CPUs with Intel 64 lacked LAHF and SAHF instructions supported
          by AMD64 until introduction of Pentium 4 G1 step in December 2005."
changed: UInt::Add on 64bit platform
changed: UInt::Add on 32bit platform (a little)


git-svn-id: svn://ttmath.org/publicrep/ttmath/trunk@80 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
Tomasz Sowa 2008-10-22 18:56:04 +00:00
parent f8f324f98f
commit ca51020fe6
7 changed files with 423 additions and 417 deletions

View File

@ -1,31 +1,31 @@
CC = g++ CC = g++
o = main.o uinttest.o o = main.o uinttest.o
CFLAGS = -Wall -pedantic CFLAGS = -Wall -pedantic
ttmath = /d/roboczy/prog/ttmath ttmath = ..
name = tests name = tests
.SUFFIXES: .cpp .o .SUFFIXES: .cpp .o
.cpp.o: .cpp.o:
$(CC) -c $(CFLAGS) -I$(ttmath) $< $(CC) -c $(CFLAGS) -I$(ttmath) $<
all: $(name) all: $(name)
$(name): $(o) $(name): $(o)
$(CC) -o $(name) $(CFLAGS) -I$(ttmath) $(o) $(CC) -o $(name) $(CFLAGS) -I$(ttmath) $(o)
main.o: main.cpp uinttest.h main.o: main.cpp uinttest.h
uinttest.o: uinttest.cpp uinttest.h uinttest.o: uinttest.cpp uinttest.h
clean: clean:
rm -f *.o rm -f *.o
rm -f $(name) rm -f $(name)
rm -f $(name).exe rm -f $(name).exe

View File

@ -36,35 +36,33 @@
*/ */
#include <iostream> #include <iostream>
#include <ttmath/ttmath.h> #include <ttmath/ttmath.h>
#include "uinttest.h" #include "uinttest.h"
const char uint_tests_file[] = "tests.uint32"; const char uint_tests_file[] = "tests.uint32";
void test_uint() void test_uint()
{ {
UIntTest test; UIntTest test;
test.set_file_name(uint_tests_file); test.set_file_name(uint_tests_file);
test.go(); test.go();
} }
int main() int main()
{ {
using namespace ttmath; using namespace ttmath;
test_uint();
test_uint();
return 0;
}
return 0;
}

View File

@ -1,63 +1,63 @@
# Add # Add
# min_bits max_bits a b result carry # min_bits max_bits a b result carry
add 32 0 0 0 0 0 add 32 0 0 0 0 0
add 32 0 1 1 2 0 add 32 0 1 1 2 0
add 32 0 2342234 3563456 5905690 0 add 32 0 2342234 3563456 5905690 0
add 32 0 254455 3453435 3707890 0 add 32 0 254455 3453435 3707890 0
add 32 0 4294967295 0 4294967295 0 add 32 0 4294967295 0 4294967295 0
# testing a carry for add # testing a carry for add
add 32 32 4294967295 1 0 1 add 32 32 4294967295 1 0 1
add 32 32 4294967295 0 4294967295 0 add 32 32 4294967295 0 4294967295 0
add 64 64 18446744073709551615 1 0 1 add 64 64 18446744073709551615 1 0 1
add 64 64 18446744073709551615 0 18446744073709551615 0 add 64 64 18446744073709551615 0 18446744073709551615 0
add 96 96 79228162514264337593543950335 1 0 1 add 96 96 79228162514264337593543950335 1 0 1
add 96 96 79228162514264337593543950335 0 79228162514264337593543950335 0 add 96 96 79228162514264337593543950335 0 79228162514264337593543950335 0
add 128 128 340282366920938463463374607431768211455 1 0 1 add 128 128 340282366920938463463374607431768211455 1 0 1
add 128 128 340282366920938463463374607431768211455 0 340282366920938463463374607431768211455 0 add 128 128 340282366920938463463374607431768211455 0 340282366920938463463374607431768211455 0
add 160 160 1461501637330902918203684832716283019655932542975 1 0 1 add 160 160 1461501637330902918203684832716283019655932542975 1 0 1
add 160 160 1461501637330902918203684832716283019655932542975 0 1461501637330902918203684832716283019655932542975 0 add 160 160 1461501637330902918203684832716283019655932542975 0 1461501637330902918203684832716283019655932542975 0
add 192 192 6277101735386680763835789423207666416102355444464034512895 1 0 1 add 192 192 6277101735386680763835789423207666416102355444464034512895 1 0 1
add 192 192 6277101735386680763835789423207666416102355444464034512895 0 6277101735386680763835789423207666416102355444464034512895 0 add 192 192 6277101735386680763835789423207666416102355444464034512895 0 6277101735386680763835789423207666416102355444464034512895 0

View File

@ -36,206 +36,206 @@
*/ */
#include "uinttest.h" #include "uinttest.h"
void UIntTest::set_file_name(const std::string & f) void UIntTest::set_file_name(const std::string & f)
{ {
file_name = f; file_name = f;
} }
int UIntTest::read_int() int UIntTest::read_int()
{ {
int result = 0; int result = 0;
skip_white_characters(); skip_white_characters();
for( ; *pline>='0' && *pline<='9' ; ++pline ) for( ; *pline>='0' && *pline<='9' ; ++pline )
result = result * 10 + (*pline - '0'); result = result * 10 + (*pline - '0');
return result; return result;
} }
template<unsigned int type_size> template<unsigned int type_size>
void UIntTest::test_add() void UIntTest::test_add()
{ {
using namespace ttmath; using namespace ttmath;
UInt<type_size> a,b,result, new_result; UInt<type_size> a,b,result, new_result;
int min_bits = read_int(); int min_bits = read_int();
int max_bits = read_int(); int max_bits = read_int();
if( min_bits != 0 && type_size * TTMATH_BITS_PER_UINT < (unsigned int)min_bits ) if( min_bits != 0 && type_size * TTMATH_BITS_PER_UINT < (unsigned int)min_bits )
return; return;
if( max_bits != 0 && type_size * TTMATH_BITS_PER_UINT > (unsigned int)max_bits ) if( max_bits != 0 && type_size * TTMATH_BITS_PER_UINT > (unsigned int)max_bits )
return; return;
a.FromString(pline, 10, &pline); a.FromString(pline, 10, &pline);
b.FromString(pline, 10, &pline); b.FromString(pline, 10, &pline);
result.FromString(pline, 10, &pline); result.FromString(pline, 10, &pline);
int carry = read_int(); int carry = read_int();
std::cerr << '[' << row << "] Add<" << type_size << ">: "; std::cerr << '[' << row << "] Add<" << type_size << ">: ";
skip_white_characters(); skip_white_characters();
if( *pline!='#' && *pline!= 0 ) if( *pline!='#' && *pline!= 0 )
{ {
std::cerr << "syntax error" << std::endl; std::cerr << "syntax error" << std::endl;
return; return;
} }
new_result = a; new_result = a;
int new_carry = new_result.Add(b); int new_carry = new_result.Add(b);
bool ok = true; bool ok = true;
if( new_carry != carry ) if( new_carry != carry )
{ {
std::cerr << "Incorrect carry: " << new_carry << " (expected: " << carry << ")" << std::endl; std::cerr << "Incorrect carry: " << new_carry << " (expected: " << carry << ")" << std::endl;
ok = false; ok = false;
} }
if( new_result != result ) if( new_result != result )
{ {
std::cerr << "Incorrect result: " << new_result << " (expected: " << result << ")" << std::endl; std::cerr << "Incorrect result: " << new_result << " (expected: " << result << ")" << std::endl;
ok = false; ok = false;
} }
if( ok ) if( ok )
{ {
std::cerr << "ok" << std::endl; std::cerr << "ok" << std::endl;
} }
} }
int UIntTest::upper_char(int c) int UIntTest::upper_char(int c)
{ {
if( c>='a' && c<='z' ) if( c>='a' && c<='z' )
return c - 'a' + 'A'; return c - 'a' + 'A';
return c; return c;
} }
bool UIntTest::is_white(int c) bool UIntTest::is_white(int c)
{ {
if( c==' ' || c=='\t' || c==13 ) if( c==' ' || c=='\t' || c==13 )
return true; return true;
return false; return false;
} }
void UIntTest::skip_white_characters() void UIntTest::skip_white_characters()
{ {
while( is_white(*pline) ) while( is_white(*pline) )
++pline; ++pline;
} }
bool UIntTest::read_method() bool UIntTest::read_method()
{ {
skip_white_characters(); skip_white_characters();
if( *pline == '#' ) if( *pline == '#' )
return false; return false;
method.clear(); method.clear();
for(int c = upper_char(*pline) ; c>='A'&& c<='Z' ; c = upper_char(*pline) ) for(int c = upper_char(*pline) ; c>='A'&& c<='Z' ; c = upper_char(*pline) )
{ {
method += c; method += c;
++pline; ++pline;
} }
if( method.empty() ) if( method.empty() )
{ {
skip_white_characters(); skip_white_characters();
if( *pline == 0 ) if( *pline == 0 )
return false; return false;
else else
{ {
std::cerr << '[' << row << "] "; std::cerr << '[' << row << "] ";
std::cerr << "syntax error" << std::endl; std::cerr << "syntax error" << std::endl;
return false; return false;
} }
} }
return true; return true;
} }
void UIntTest::test_method() void UIntTest::test_method()
{ {
const char * p = pline; const char * p = pline;
if( method == "ADD" ) if( method == "ADD" )
{ {
pline = p; test_add<1>(); pline = p; test_add<1>();
pline = p; test_add<2>(); pline = p; test_add<2>();
pline = p; test_add<3>(); pline = p; test_add<3>();
pline = p; test_add<4>(); pline = p; test_add<4>();
pline = p; test_add<5>(); pline = p; test_add<5>();
pline = p; test_add<6>(); pline = p; test_add<6>();
pline = p; test_add<7>(); pline = p; test_add<7>();
pline = p; test_add<8>(); pline = p; test_add<8>();
pline = p; test_add<9>(); pline = p; test_add<9>();
} }
else else
{ {
std::cerr << '[' << row << "] "; std::cerr << '[' << row << "] ";
std::cerr << "method " << method << " is not supported" << std::endl; std::cerr << "method " << method << " is not supported" << std::endl;
} }
} }
bool UIntTest::check_line() bool UIntTest::check_line()
{ {
std::getline(file, line); std::getline(file, line);
pline = line.c_str(); pline = line.c_str();
if( read_method() ) if( read_method() )
test_method(); test_method();
if( file.eof() ) if( file.eof() )
return false; return false;
return true; return true;
} }
void UIntTest::go() void UIntTest::go()
{ {
file.open(file_name.c_str()); file.open(file_name.c_str());
if( !file ) if( !file )
{ {
std::cerr << "I can't open the input file" << std::endl; std::cerr << "I can't open the input file" << std::endl;
return; return;
} }
row = 1; row = 1;
while( check_line() ) while( check_line() )
++row; ++row;
} }

View File

@ -36,55 +36,55 @@
*/ */
#ifndef headerfileuinttest #ifndef headerfileuinttest
#define headerfileuinttest #define headerfileuinttest
#include <string> #include <string>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <ttmath/ttmath.h> #include <ttmath/ttmath.h>
class UIntTest class UIntTest
{ {
std::string file_name; std::string file_name;
// current line from the file // current line from the file
std::string line; std::string line;
const char * pline; const char * pline;
std::ifstream file; std::ifstream file;
std::string method; std::string method;
int row; int row;
public: public:
void set_file_name(const std::string & f); void set_file_name(const std::string & f);
void go(); void go();
template<unsigned int type_size> template<unsigned int type_size>
void test_add(); void test_add();
int upper_char(int c); int upper_char(int c);
bool is_white(int c); bool is_white(int c);
void skip_white_characters(); void skip_white_characters();
bool read_method(); bool read_method();
void test_method(); void test_method();
bool check_line(); bool check_line();
int read_int(); int read_int();
}; };
#endif #endif

View File

@ -265,9 +265,9 @@ public:
xor eax,eax xor eax,eax
sub eax,[c] sub eax,[c]
lahf // flags -> AH (flags: SF ZF AF CF) PF? lahf // flags -> AH (flags: SF ZF AF PF CF)
p: p:
sahf // AH -> flags (flags: SF ZF AF CF) PF? sahf // AH -> flags (flags: SF ZF AF PF CF)
mov eax,[ebx] mov eax,[ebx]
adc eax,[edx] adc eax,[edx]
mov [ebx],eax mov [ebx],eax
@ -276,7 +276,7 @@ public:
add ebx,4 add ebx,4
add edx,4 add edx,4
sub ecx,1 dec ecx
jnz p jnz p
// checking carry from the last word // checking carry from the last word
@ -318,7 +318,7 @@ public:
"add $4,%%ebx \n" "add $4,%%ebx \n"
"add $4,%%edx \n" "add $4,%%edx \n"
"subl $1,%%ecx \n" "decl %%ecx \n"
"jnz 1b \n" "jnz 1b \n"
"test $1,%%ah \n" "test $1,%%ah \n"

View File

@ -225,37 +225,36 @@ namespace ttmath
"push %%rcx \n" "push %%rcx \n"
"push %%rdx \n" "push %%rdx \n"
"movq $0, %%rax \n" "xorq %%rax, %%rax \n"
"subq %%rsi, %%rax \n" "subq %%rsi, %%rax \n"
//"lahf \n"
// in order to use this instruction one need to use -msahf option of the GCC
// but in my compiler (gcc version 4.2.1) there is no such option
// at the moment I'm using the opcode of this instruction
// In the future this can be simply change into 'lahf'
".byte 0x9f \n"
"1: \n" "1: \n"
//"sahf \n"
".byte 0x9e \n"
"movq (%%rbx),%%rax \n" "movq (%%rbx),%%rax \n"
"adcq (%%rdx),%%rax \n" "adcq (%%rdx),%%rax \n"
"movq %%rax,(%%rbx) \n" "movq %%rax,(%%rbx) \n"
"inc %%rbx \n"
"inc %%rbx \n"
"inc %%rbx \n"
"inc %%rbx \n"
"inc %%rbx \n"
"inc %%rbx \n"
"inc %%rbx \n"
"inc %%rbx \n"
"inc %%rdx \n"
"inc %%rdx \n"
"inc %%rdx \n"
"inc %%rdx \n"
"inc %%rdx \n"
"inc %%rdx \n"
"inc %%rdx \n"
"inc %%rdx \n"
"loop 1b \n"
"movq $0, %%rax \n" //"lahf \n"
"adcq %%rax,%%rax \n" ".byte 0x9f \n"
"movq %%rax, %%rsi \n"
"addq $8, %%rbx \n"
"addq $8, %%rdx \n"
"decq %%rcx \n"
"jnz 1b \n"
"test $1, %%ah \n"
"setnz %%al \n"
"movzx %%al, %%rsi \n"
"pop %%rdx \n" "pop %%rdx \n"
"pop %%rcx \n" "pop %%rcx \n"
@ -680,11 +679,14 @@ namespace ttmath
"push %%rbx \n" "push %%rbx \n"
"push %%rcx \n" "push %%rcx \n"
"lahf \n" //"lahf \n"
".byte 0x9f \n"
"1: \n" "1: \n"
"sahf \n" //"sahf \n"
".byte 0x9e \n"
"rclq $1,(%%rbx) \n" "rclq $1,(%%rbx) \n"
"lahf \n" //"lahf \n"
".byte 0x9f \n"
"addq $8,%%rbx \n" "addq $8,%%rbx \n"
@ -698,7 +700,8 @@ namespace ttmath
"jnz 2b \n" "jnz 2b \n"
"xor %%rdx,%%rdx \n" "xor %%rdx,%%rdx \n"
"sahf \n" //"sahf \n"
".byte 0x9e \n"
"setc %%dl \n" "setc %%dl \n"
"pop %%rsi \n" "pop %%rsi \n"
@ -762,13 +765,17 @@ namespace ttmath
"xorq %%rax, %%rax \n" "xorq %%rax, %%rax \n"
"subq %%rdx, %%rax \n" "subq %%rdx, %%rax \n"
"lahf \n" //"lahf \n"
".byte 0x9f \n"
"1: \n" "1: \n"
"subq $8, %%rbx \n" "subq $8, %%rbx \n"
"sahf \n" //"sahf \n"
".byte 0x9e \n"
"rcrq $1,(%%rbx) \n" "rcrq $1,(%%rbx) \n"
"lahf \n" //"lahf \n"
".byte 0x9f \n"
"subq $1,%%rcx \n" "subq $1,%%rcx \n"
"jnz 1b \n" "jnz 1b \n"
@ -781,7 +788,8 @@ namespace ttmath
"jnz 2b \n" "jnz 2b \n"
"xor %%rdx,%%rdx \n" "xor %%rdx,%%rdx \n"
"sahf \n" //"sahf \n"
".byte 0x9e \n"
"setc %%dl \n" "setc %%dl \n"
"pop %%rsi \n" "pop %%rsi \n"