/* 64kb -1b each
*
* The Brainfuck spec says that memory should be 30000 bytes, but I prefer to
* use a whole 64kb for both code and memory.
*
*/
#define BRAINF_CODESIZE 65535
#define BRAINF_MEMSIZE 65535
FILE* fp;
unsigned char* code, *memory;
short unsigned int codeptr, memptr;
int main(int argc, char* argv[])
{
if (argc != 2)
exit(1);
while (!feof(fp))
{
fread(code, BRAINF_CODESIZE -1, 1, fp);
}
/*** Interpreting begins ***/
while (code[codeptr])
{
switch (code[codeptr])
{
case '>':
++memptr;
++codeptr;
break;
case '<':
--memptr;
++codeptr;
break;
case '+':
++memory[memptr];
++codeptr;
break;
case '-':
--memory[memptr];
++codeptr;
break;
case '.':
putchar(memory[memptr]);
++codeptr;
break;
case ',':
memory[memptr] = getchar();
++codeptr;
break;
/* :NOTE: Some kind of error handling should take
* place if no ']' is found.
*/
case '[':
if (!memory[memptr])
{
while ((code[codeptr] != ']') && (code[codeptr]))
{
++codeptr;
}
}
else
++codeptr;
break;
/* :NOTE: Some kind of error handling should take
* place if no '[' is found.
*/
case ']':
if (memory[memptr])
{
while ((code[codeptr] != '[') && (codeptr))
--codeptr;
}
else
++codeptr;
break;
/* invalid instruction
* For now we will just continue executing when this happens.
* It should ignore characters 10, 13, & 32 anyways.
*/
default:
++codeptr;
break;
}
}
Dev-Cpp Compiling Sepples0x with a Sepples2003 compiler
( ≖‿≖)
Name:
The Amazing Anus2009-11-15 22:42
Ok, I am using the bench.bf provided by >>73 (thx btw)
I compile each one with gcc -s -Os.
This is how they match up with the "real" provided by time:
bf 13.291 seconds (My own, as in >>29 )
qdbf 4.774 seconds (QDBF, forget where I found it)
bfanon34 6.287 seconds (Provided by >>34 )
bfanon38 12.527 seconds (Provided by >>38 )
I can't bench >>73 because I still can't get it to compile.
Name:
Anonymous2009-11-15 22:43
>>81
Yeah, I know. What do you recommend? Is gcc 4.4.2 (or whatever the newest stable build is) capable of building it?
Name:
Anonymous2009-11-16 17:32
>>77 Why thank you, Dear! I'm very fond of you too.
Here's a version that should compile w/o any trickery.
#include <iostream>
using std::cin;
using std::cout;
using std::cerr;
using std::endl;
#define MS (65536)
struct state {
unsigned char * tape;
size_t head;
static state s;
private:
state() : tape(new unsigned char[MS]), head() {}
};
state state::s;
state & s = state::s;
template <char c, int times = 1>
struct insn {
enum { C = c };
enum { T = 0 };
void operator () () const {}
};
template <typename B, typename I>
struct block : B {
typedef B base;
typedef I insn_;
Compile w/ this to get the benchmark previously mentioned: g++ -O2 -DBFCODE=,r,p,p,b,l,p,p,p,p,p,p,p,p,p,p,p,p,p,r,m,e,l,b,b,r,p,r,p,l,l,m,e,r,b,l,p,r,m,e,p,p,p,p,p,p,p,p,b,r,p,p,p,p,p,p,p,p,l,m,e,r,d,b,m,e,l,l,r,p,p,p,p,p,p,p,p,p,p,b,r,p,p,p,p,p,p,p,p,p,p,b,r,p,p,p,p,p,p,p,p,p,p,b,r,p,p,p,p,p,p,p,p,p,p,b,r,p,p,p,p,p,p,p,p,p,p,b,r,p,p,p,p,p,p,p,p,p,p,b,r,p,p,p,p,p,p,p,p,p,p,b,m,e,l,m,e,l,m,e,l,m,e,l,m,e,l,m,e,l,m,e,l,m,e,p,p,p,p,p,p,p,p,p,p,d
I reckon there's a bug hiding somewhere in there, as it emits a different/wrong result when compiled with cl.exe, than it does when compiled with g++. (I used the 20091112 snapshot of g++-4.5.)
Name:
Anonymous2009-11-16 17:36
what is that -DBFCODE bullshit for?
Name:
The Amazing Anus2009-11-16 17:44
>>84
I don't know why this one doesn't even take a file as input but it runs in 1.560 seconds.
Now let's see who can write the fastest Brainfuck interpreter. I want a harder benchmark program, and I want >>84 to take a file as input before I accept it into the contest.
A CL "compiler" for bf could be done using macros or just COMPILE, add some declarations and it might even produce some decent code on some implementations.
... but I doubt it would exceed the Perl to C compiler's performance, if at all. A better solution might be to go platform specific and generate machine code (or assembly) directly, which might just be faster, especially if one makes it an optimizing compiler...
>>92 ~ % perl bfcc.pl bench.bf|gcc -O3 -ffast-math -xc -std=gnu99 -xc -o bf1 -
~ % time ./bf1
ZYXWVUTSRQPONMLKJIHGFEDCBA
./bf1 0.04s user 0.00s system 98% cpu 0.039 total
~ % runghc bf2c.hs <bench.bf |gcc -O3 -ffast-math -xc -std=gnu99 -xc -o bf2 -
~ % time ./bf2
ZYXWVUTSRQPONMLKJIHGFEDCBA
./bf2 0.01s user 0.00s system 97% cpu 0.013 total
Name:
Anonymous2009-11-17 19:42
I HATE women. I never had a girlfriend and never will. The only times I got laid was when I paid a woman or promised her something. I'm never going to hold hands with a chick, kiss a girl intimately because we're in love, or any of the other shit that human beings were made to do. I guess that I'm suppose to be happy masturbating every fucking night. I'm a man with sexual urges and can't get with a female. I'm suppose to be alright with that? THERE IS A FUCKING CURSE ON MY LIFE. A CURSE THAT PREVENTS ANY FEMALE FROM LIKING ME. Oh I forgot, I do get interest from fat chicks and I'm not attracted to fat chicks.
I don't give a fuck anymore. I'm going to become the biggest asshole in the world. I tried the whole being considerate thing and it got me nowhere. If people can't handle my newfound harshness, then bring it on. BECAUSE I DON'T GIVE A FUCK. I DON'T GIVE A FUCK. I DON'T GIVE A FUCK.
I get happy when I hear about some college slut getting murdered or injured in a hit and run. "oh she was a beautiful and talented girl, how could this happen." I don't know but I'm glad it did.