소스 검색

added ofib bench (closure + proto access + proto dispatch)

Nicolas Cannasse 10 년 전
부모
커밋
ee65be3ab8
2개의 변경된 파일74개의 추가작업 그리고 0개의 파일을 삭제
  1. 33 0
      bench/ofib/Main.hx
  2. 41 0
      bench/ofib/main.c

+ 33 - 0
bench/ofib/Main.hx

@@ -0,0 +1,33 @@
+
+class Main {
+
+    static function main() {
+		Sys.println(new Sub().mfib(40));
+    }
+	
+	var one = 1;
+	var two = 2;
+	var mfib3 : Int -> Int;
+
+	function new() {
+		mfib3 = mfib;
+	}
+
+    function mfib(x) {
+		if( x <= 2 ) return 1;
+		return mfib2(x-one) + mfib3(x-two);
+    }
+	function mfib2(x) {
+		return mfib(x);
+	}
+
+}
+
+class Sub extends Main {
+
+	override function mfib2(x) {
+		return mfib(x);
+	}
+
+}
+

+ 41 - 0
bench/ofib/main.c

@@ -0,0 +1,41 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+typedef struct vobj vobj;
+
+typedef struct {
+	int *UNUSED;
+	int (*fib)( vobj *o, int n );
+} proto;
+
+typedef struct {
+	int (*fib)( vobj *o, int n );
+	vobj *o;
+} vclosure;
+
+struct vobj {
+	proto *p;
+	int one;
+	int two;
+	vclosure *c;
+};
+
+int ofib( vobj *o, int n ) {
+	if( n <= 2 )
+		return 1;
+	return o->p->fib(o, n - o->one) + o->c->fib(o->c->o,n - o->two);
+}
+
+int main() {
+	proto *p = (proto*)malloc(sizeof(proto));
+	vclosure *c = (vclosure*)malloc(sizeof(vclosure));
+	vobj *o = (vobj*)malloc(sizeof(vobj));
+	o->p = p;
+	o->one = 1;
+	o->two = 2;
+	o->c = c;
+	c->fib = ofib;
+	c->o = o;
+	p->fib = ofib;
+	printf("%di\n",o->p->fib(o,40));
+}