/* 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;
}
}
Fine. Go improve mine by improving load time optimization: put in an array of numbers to tell you how many times each symbol appears in a row. Like +++>>----< would in memory be:
>>41
If you're going for load time optimization, you can speed up the loops even more if, instead of using a stack, determine during load time which address to jump to for each [ and ] instruction.
Name:
The Amazing Anus!Anus3nMVO22009-09-03 14:44
All times are averages after several runs.
$ time bf bench.bf
real 0.649 secs
$ time qdbf bench.bf
real 0.220 secs
$ time bfanon34 bench.bf
real 0.186 secs
Ok, it seems that I can't do anything to get >>38-chan's interpreter to accept my bench.bf program. It also hangs forever if I try to run the quine on it... same with the program that prints the Fibonacci numbers.
The only thing that actually works is "Hello, World!" I get corrupted stack and segmentation fault when I try to run the Fibonacci program on it. I'll see if I can find out what its problem is.
Here are some typographical errors, spotted by various readers. Page numbers refer to the online versions (which differ from the printed version).
• Page 128, second to last paragraph of section 3.7, first sentence. "addis" should be "is a".
• Page 134, the code for i'. The "n" under the big brace symbol should say "n pairs".
• Page 135. Equation 3.36 should have "NConstr", not "Constr".
• Page 230, second paragraph. "There one final gloss..." should be "There is one final gloss...".
• Page 278, the definition of nfib. Replace "n==0" by "n<=1".
>>43
I didn't test it, just got it to compile (actually it compiled straight away) and checked the Hello, World program on Wikipedia. That being said it runs your fibs program fine, how are you compiling/running? If you post the line number of the segfault I'll have a look.
Name:
Anonymous2009-09-04 1:47
>>51
I compiled it with gcc -o bfanon38 bfanon38.c
Ok, now I am able to successfully run the Fibonacci thing... But bench.bf still causes yours to hang. I could use a better benchmarking program, but it works fine on the other 3 so I'm not sure what it is about yours.
>>57
yes there is... or was.
i remember several times in which i have been banned because evidently i am a spambot. each time it happened i was posting four or five urls in the same post, so i just put two and two together.
Name:
Anonymous2009-09-04 6:33
There is an autobanner and it's apparently broken. I was deemed a spambot when I was making a regular posts. No URLs, no repetition.
>>60
Almost; I got an offer from Firefox to save a file with such name.
Name:
Anonymous2009-09-04 17:56
I think that bfanon38 hanging is something to do with not supporting cell wrap-around. Working on a better bench.bf at the moment 'cause the last one is shit.
>>43
The time given under "user" is a far better description of how fast a program runs than "real". Anyway, If you want fast, try beef or the program that beef stole its code from. It matches brackets while the code is loading by loading it into a binary tree. Run-time bracket matching is slower.
Name:
Anonymous2009-11-14 9:41
Why do people write brainfuck interpreters? The thing is so simple that you can write brainfuck in brainfuck. If you're using C, compile the damn code already
>>68
There has been more than one brainfuck compiler posted, It's probably personal preference
Name:
Anonymous2009-11-14 10:30
>>68
C++ is so simple that you can write C++ in C++.
Name:
Anonymous2009-11-14 11:03
//`'''```,
o // LISP `.,
,....OOo. .c;.',,,.'``.,,.`
.' ____.,'.//
/ _____ \___/.'
| / || \\---\|
|| || \\ ||
co co co co
Name:
Anonymous2009-11-15 19:06
#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_;
>>73 C:\Dev-Cpp\projects\bfpp.cpp:35: error: a function call cannot appear in a constant-expression
C:\Dev-Cpp\projects\bfpp.cpp:36: error: missing `>' to terminate the template argument list
C:\Dev-Cpp\projects\bfpp.cpp:36: error: missing `>' to terminate the template argument list
C:\Dev-Cpp\projects\bfpp.cpp:36: error: template argument 2 is invalid
C:\Dev-Cpp\projects\bfpp.cpp:36: error: expected unqualified-id before '{' token
C:\Dev-Cpp\projects\bfpp.cpp:39: error: `>>' should be `> >' within a nested template argument list
C:\Dev-Cpp\projects\bfpp.cpp: In member function `block<block<B, I>, insn<c, 1> > block<B, I>::operator,(const insn<c, 1>&)':
C:\Dev-Cpp\projects\bfpp.cpp:40: error: `>>' should be `> >' within a nested template argument list
C:\Dev-Cpp\projects\bfpp.cpp: At global scope:
C:\Dev-Cpp\projects\bfpp.cpp:50: error: `>>' should be `> >' within a nested template argument list
C:\Dev-Cpp\projects\bfpp.cpp: In member function `block<block<void, void>, insn<c, 1> > block<void, void>::operator,(const insn<c, 1>&)':
C:\Dev-Cpp\projects\bfpp.cpp:50: error: `>>' should be `> >' within a nested template argument list
C:\Dev-Cpp\projects\bfpp.cpp: At global scope:
C:\Dev-Cpp\projects\bfpp.cpp:59: error: missing `>' to terminate the template argument list
C:\Dev-Cpp\projects\bfpp.cpp:59: error: template argument 1 is invalid
C:\Dev-Cpp\projects\bfpp.cpp:59: error: missing `>' to terminate the template argument list
C:\Dev-Cpp\projects\bfpp.cpp:59: error: template argument 2 is invalid
C:\Dev-Cpp\projects\bfpp.cpp:75: error: missing `>' to terminate the template argument list
C:\Dev-Cpp\projects\bfpp.cpp:75: error: template argument 1 is invalid
C:\Dev-Cpp\projects\bfpp.cpp:75: error: missing `>' to terminate the template argument list
C:\Dev-Cpp\projects\bfpp.cpp:75: error: template argument 2 is invalid
C:\Dev-Cpp\projects\bfpp.cpp:161: error: ISO C++ forbids declaration of `static_assert' with no type
C:\Dev-Cpp\projects\bfpp.cpp:161: error: expected `;' before '(' token
C:\Dev-Cpp\projects\bfpp.cpp:170: error: ISO C++ forbids declaration of `static_assert' with no type
C:\Dev-Cpp\projects\bfpp.cpp:170: error: expected `;' before '(' token
C:\Dev-Cpp\projects\bfpp.cpp: In function `int main()':
C:\Dev-Cpp\projects\bfpp.cpp:187: error: ISO C++ forbids declaration of `prog' with no type
C:\Dev-Cpp\projects\bfpp.cpp:188: error: expected `)' before "BFCODE"
C:\Dev-Cpp\projects\bfpp.cpp:189: error: cannot convert `block<void, void>' to `int' in initialization
C:\Dev-Cpp\projects\bfpp.cpp:190: error: `prog' cannot be used as a function
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.