Open Design Computer Project

オリジナルCPUから作る本格的自作コンピューター

ユーザ用ツール

サイト用ツール


software:samples:mt19937ar

メルセンヌツイスタ (mt19937ar)

コンパイル結果

mt19937ar_mist32.s
	.file	"mt19937ar.c"
	.section	.text
	.align	2
	.globl	init_genrand
	.type	init_genrand, @function
init_genrand:
	# PROLOGUE, vars= 0, regs= 0, args= 0, extra= 0
	lih	r2, hi(mt)
	wl16	r2, lo(mt)
	lih	r4, hi(1812433253)
	st32	r1, r2
	lil	r0, 1
	add	r2, 4
	wl16	r4, lo(1812433253)
.L2:
	move	r3, r1
	shr	r3, 30
	xor	r1, r3
	mull	r1, r4
	add	r1, r0
	st32	r1, r2
	inc	r0, r0
	add	r2, 4
	cmp	r0, 624
	br	.L2, #ne
	lih	r1, hi(mti)
	wl16	r1, lo(mti)
	st32	r0, r1
	b	rret, #al
	# EPILOGUE
	.size	init_genrand, .-init_genrand
	.align	2
	.globl	init_by_array
	.type	init_by_array, @function
init_by_array:
	# PROLOGUE, vars= 0, regs= 6, args= 0, extra= 0
	push	r8
	push	r9
	push	r10
	push	r11
	push	r12
	lih	r5, hi(mt)
	lih	r0, hi(19650218)
	wl16	r5, lo(mt)
	push	r13
	wl16	r0, lo(19650218)
	lih	r8, hi(1812433253)
	lil	r4, 4
	st32	r0, r5
	add	r4, r5
	lil	r3, 1
	wl16	r8, lo(1812433253)
.L6:
	move	r6, r0
	shr	r6, 30
	xor	r0, r6
	mull	r0, r8
	add	r0, r3
	st32	r0, r4
	inc	r3, r3
	add	r4, 4
	cmp	r3, 624
	br	.L6, #ne
	lih	r0, hi(mti)
	lih	r12, hi(mt)
	wl16	r0, lo(mti)
	wl16	r12, lo(mt)
	lih	r11, hi(1664525)
	lil	r10, 624
	lil	r13, 2492
	st32	r3, r0
	max	r10, r2
	ld32	r0, r5
	lil	r6, 0
	lil	r3, 1
	wl16	r11, lo(1664525)
	add	r13, r12
.L10:
	move	r4, r0
	move	r8, r3
	shr	r4, 30
	xor	r0, r4
	shl	r8, 2
	move	r4, r6
	add	r8, r5
	shl	r4, 2
	ld32	r9, r8
	mull	r0, r11
	add	r4, r1
	ld32	r4, r4
	xor	r0, r9
	add	r0, r4
	add	r0, r6
	st32	r0, r8
	inc	r3, r3
	inc	r6, r6
	cmp	r3, 623
	br	.L8, #le
	ld32	r0, r13
	st32	r0, r12
	lil	r3, 1
.L8:
	dec	r10, r10
	cmp	r2, r6
	br	.L9, #gt
	lil	r6, 0
.L9:
	cmp	r10, 0
	br	.L10, #ne
	lih	r8, hi(mt)
	wl16	r8, lo(mt)
	lih	r6, hi(1566083941)
	lil	r9, 2492
	lil	r4, 623
	wl16	r6, lo(1566083941)
	add	r9, r8
	br	.L13, #al
.L18:
	ld32	r0, r0
.L13:
	move	r2, r3
	move	r1, r0
	shr	r1, 30
	shl	r2, 2
	add	r2, r5
	xor	r0, r1
	mull	r0, r6
	ld32	r1, r2
	xor	r0, r1
	sub	r0, r3
	st32	r0, r2
	inc	r3, r3
	dec	r4, r4
	cmp	r3, 624
	br	.L11, #ne
	ld32	r0, r9
	st32	r0, r8
	lil	r3, 1
.L11:
	dec	r0, r3
	shl	r0, 2
	add	r0, r5
	cmp	r4, 0
	br	.L18, #ne
	lih	r0, hi(-2147483648)
	st32	r0, r5
	pop	r13
	pop	r12
	pop	r11
	pop	r10
	pop	r9
	pop	r8
	b	rret, #al
	# EPILOGUE
	.size	init_by_array, .-init_by_array
	.section	.rodata.cst4,"aM",@progbits,4
	.align	2
.LC1:
	.long	mt+1588
	.align	2
.LC2:
	.long	mt+2496
	.align	2
.LC3:
	.long	mt+1584
	.section	.text
	.align	2
	.globl	genrand_int32
	.type	genrand_int32, @function
genrand_int32:
	# PROLOGUE, vars= 0, regs= 5, args= 0, extra= 0
	push	r8
	push	r9
	push	r10
	push	r11
	lih	r6, hi(mti)
	push	r12
	wl16	r6, lo(mti)
	ld32	r1, r6
	cmp	r1, 623
	br	.L31, #le
	lih	r8, hi(mt)
	wl16	r8, lo(mt)
	cmp	r1, 625
	br	.L22, #eq
.L25:
	lih	r3, hi(.LC1)
	lih	r10, hi(.LC2)
	wl16	r3, lo(.LC1)
	wl16	r10, lo(.LC2)
	lih	r5, hi(mag01.2431)
	ld32	r4, r8
	ld32	r3, r3
	ld32	r10, r10
	wl16	r5, lo(mag01.2431)
	lil	r12, -1584
	lil	r11, -1588
.L23:
	move	r1, r3
	add	r1, r12
	lih	r0, hi(-2147483648)
	lih	r2, hi(2147483647)
	and	r0, r4
	wl16	r2, lo(2147483647)
	ld32	r4, r1
	and	r2, r4
	or	r2, r0
	lil	r1, 1
	and	r1, r2
	ld32	r0, r3
	shl	r1, 2
	shr	r2, 1
	add	r1, r5
	xor	r2, r0
	move	r9, r3
	ld32	r1, r1
	move	r0, r2
	add	r9, r11
	xor	r0, r1
	st32	r0, r9
	add	r3, 4
	cmp	r10, r3
	br	.L23, #ne
	lih	r4, hi(mt)
	wl16	r4, lo(mt)
	lih	r10, hi(.LC3)
	add	r4, 908
	lih	r3, hi(mt)
	wl16	r10, lo(.LC3)
	ld32	r4, r4
	wl16	r3, lo(mt)
	ld32	r10, r10
.L26:
	lil	r1, 912
	add	r1, r3
	lih	r0, hi(-2147483648)
	lih	r2, hi(2147483647)
	and	r0, r4
	wl16	r2, lo(2147483647)
	ld32	r4, r1
	and	r2, r4
	or	r2, r0
	lil	r1, 1
	and	r1, r2
	ld32	r0, r3
	shl	r1, 2
	shr	r2, 1
	add	r1, r5
	xor	r2, r0
	ld32	r1, r1
	lil	r9, 908
	move	r0, r2
	add	r9, r3
	xor	r0, r1
	st32	r0, r9
	lih	r1, hi(mag01.2431)
	add	r3, 4
	wl16	r1, lo(mag01.2431)
	cmp	r10, r3
	br	.L26, #ne
	lil	r5, 2492
	add	r5, r8
	ld32	r4, r5
	lih	r2, hi(-2147483648)
	and	r4, r2
	lih	r2, hi(2147483647)
	ld32	r0, r8
	wl16	r2, lo(2147483647)
	and	r2, r0
	or	r4, r2
	lil	r2, 1
	and	r2, r4
	lil	r3, 1584
	add	r8, r3
	shl	r2, 2
	add	r2, r1
	shr	r4, 1
	ld32	r3, r8
	ld32	r1, r2
	xor	r3, r4
	xor	r1, r3
	st32	r1, r5
	lil	r2, 1
.L21:
	move	r1, r0
	shr	r1, 11
	xor	r0, r1
	move	r1, r0
	lih	r3, hi(-1658038656)
	wl16	r3, lo(-1658038656)
	shl	r1, 7
	and	r1, r3
	xor	r0, r1
	move	r1, r0
	st32	r2, r6
	lih	r3, hi(-272236544)
	shl	r1, 15
	pop	r12
	and	r1, r3
	pop	r11
	xor	r0, r1
	pop	r10
	move	r1, r0
	shr	r1, 18
	pop	r9
	xor	r0, r1
	pop	r8
	b	rret, #al
.L31:
	lih	r2, hi(mt)
	move	r0, r1
	wl16	r2, lo(mt)
	shl	r0, 2
	add	r0, r2
	ld32	r0, r0
	inc	r2, r1
	br	.L21, #al
.L22:
	lil	r0, 5489
	lih	r4, hi(1812433253)
	lil	r2, 4
	st32	r0, r8
	add	r2, r8
	lil	r1, 1
	wl16	r4, lo(1812433253)
.L24:
	move	r3, r0
	shr	r3, 30
	xor	r0, r3
	mull	r0, r4
	add	r0, r1
	st32	r0, r2
	inc	r1, r1
	add	r2, 4
	cmp	r1, 624
	br	.L24, #ne
	br	.L25, #al
	# EPILOGUE
	.size	genrand_int32, .-genrand_int32
	.align	2
	.globl	genrand_int31
	.type	genrand_int31, @function
genrand_int31:
	# PROLOGUE, vars= 0, regs= 1, args= 0, extra= 0
	push	rret
	lih	rtmp, hi(genrand_int32)
	wl16	rtmp, lo(genrand_int32)
	movepc	rret, 8
	b	rtmp, #al
	shr	r0, 1
	pop	rret
	b	rret, #al
	# EPILOGUE
	.size	genrand_int31, .-genrand_int31
	.globl	__floatunsidf
	.globl	__muldf3
	.align	2
	.globl	genrand_real1
	.type	genrand_real1, @function
genrand_real1:
	# PROLOGUE, vars= 16, regs= 3, args= 0, extra= 0
	push	r8
	push	r9
	push	rret
	srspadd	-16
	move	r9, r1
	lih	rtmp, hi(genrand_int32)
	wl16	rtmp, lo(genrand_int32)
	movepc	rret, 8
	b	rtmp, #al
	move	r2, r0
	srspr	r1
	lih	rtmp, hi(__floatunsidf)
	wl16	rtmp, lo(__floatunsidf)
	movepc	rret, 8
	b	rtmp, #al
	srspr	r8
	srspr	r0
	add	r8, 8
	ld32	r2, r0
	ldd32	r3, r0, 4
	move	r1, r8
	lih	r4, hi(1039138816)
	lih	r5, hi(1048576)
	lih	rtmp, hi(__muldf3)
	wl16	rtmp, lo(__muldf3)
	movepc	rret, 8
	b	rtmp, #al
	ld32	r0, r8
	ldd32	r8, r8, 4
	std32	r8, r9, 4
	st32	r0, r9
	srspadd	16
	pop	rret
	move	r0, r9
	pop	r9
	pop	r8
	b	rret, #al
	# EPILOGUE
	.size	genrand_real1, .-genrand_real1
	.align	2
	.globl	genrand_real2
	.type	genrand_real2, @function
genrand_real2:
	# PROLOGUE, vars= 16, regs= 3, args= 0, extra= 0
	push	r8
	push	r9
	push	rret
	srspadd	-16
	move	r9, r1
	lih	rtmp, hi(genrand_int32)
	wl16	rtmp, lo(genrand_int32)
	movepc	rret, 8
	b	rtmp, #al
	move	r2, r0
	srspr	r1
	lih	rtmp, hi(__floatunsidf)
	wl16	rtmp, lo(__floatunsidf)
	movepc	rret, 8
	b	rtmp, #al
	srspr	r8
	srspr	r0
	add	r8, 8
	ld32	r2, r0
	ldd32	r3, r0, 4
	move	r1, r8
	lih	r4, hi(1039138816)
	lil	r5, 0
	lih	rtmp, hi(__muldf3)
	wl16	rtmp, lo(__muldf3)
	movepc	rret, 8
	b	rtmp, #al
	ld32	r0, r8
	ldd32	r8, r8, 4
	std32	r8, r9, 4
	st32	r0, r9
	srspadd	16
	pop	rret
	move	r0, r9
	pop	r9
	pop	r8
	b	rret, #al
	# EPILOGUE
	.size	genrand_real2, .-genrand_real2
	.globl	__adddf3
	.align	2
	.globl	genrand_real3
	.type	genrand_real3, @function
genrand_real3:
	# PROLOGUE, vars= 24, regs= 4, args= 0, extra= 0
	push	r8
	push	r9
	push	r10
	push	rret
	srspadd	-24
	move	r9, r1
	lih	rtmp, hi(genrand_int32)
	wl16	rtmp, lo(genrand_int32)
	movepc	rret, 8
	b	rtmp, #al
	move	r2, r0
	srspr	r1
	lih	rtmp, hi(__floatunsidf)
	wl16	rtmp, lo(__floatunsidf)
	movepc	rret, 8
	b	rtmp, #al
	srspr	r10
	srspr	r0
	add	r10, 8
	ld32	r2, r0
	ldd32	r3, r0, 4
	move	r1, r10
	srspr	r8
	lih	r4, hi(1071644672)
	lil	r5, 0
	lih	rtmp, hi(__adddf3)
	wl16	rtmp, lo(__adddf3)
	movepc	rret, 8
	b	rtmp, #al
	add	r8, 16
	ld32	r2, r10
	ldd32	r3, r10, 4
	move	r1, r8
	lih	r4, hi(1039138816)
	lil	r5, 0
	lih	rtmp, hi(__muldf3)
	wl16	rtmp, lo(__muldf3)
	movepc	rret, 8
	b	rtmp, #al
	ld32	r0, r8
	ldd32	r8, r8, 4
	std32	r8, r9, 4
	st32	r0, r9
	srspadd	24
	pop	rret
	pop	r10
	move	r0, r9
	pop	r9
	pop	r8
	b	rret, #al
	# EPILOGUE
	.size	genrand_real3, .-genrand_real3
	.align	2
	.globl	genrand_res53
	.type	genrand_res53, @function
genrand_res53:
	# PROLOGUE, vars= 40, regs= 6, args= 0, extra= 0
	push	r8
	push	r9
	push	r10
	push	r11
	push	r12
	push	rret
	srspadd	-40
	move	r9, r1
	lih	rtmp, hi(genrand_int32)
	wl16	rtmp, lo(genrand_int32)
	movepc	rret, 8
	b	rtmp, #al
	move	r8, r0
	lih	rtmp, hi(genrand_int32)
	wl16	rtmp, lo(genrand_int32)
	movepc	rret, 8
	b	rtmp, #al
	move	r2, r8
	srspr	r1
	shr	r2, 5
	move	r10, r0
	srspr	r8
	lih	rtmp, hi(__floatunsidf)
	wl16	rtmp, lo(__floatunsidf)
	movepc	rret, 8
	b	rtmp, #al
	srspr	r0
	add	r8, 8
	ldd32	r3, r0, 4
	ld32	r2, r0
	lih	r4, hi(1099956224)
	lil	r5, 0
	move	r1, r8
	lih	rtmp, hi(__muldf3)
	wl16	rtmp, lo(__muldf3)
	movepc	rret, 8
	b	rtmp, #al
	srspr	r11
	add	r11, 16
	move	r2, r10
	move	r1, r11
	srspr	r10
	shr	r2, 6
	add	r10, 24
	ld32	r12, r8
	ldd32	r8, r8, 4
	lih	rtmp, hi(__floatunsidf)
	wl16	rtmp, lo(__floatunsidf)
	movepc	rret, 8
	b	rtmp, #al
	ld32	r4, r11
	ldd32	r5, r11, 4
	move	r2, r12
	move	r3, r8
	move	r1, r10
	srspr	r8
	lih	rtmp, hi(__adddf3)
	wl16	rtmp, lo(__adddf3)
	movepc	rret, 8
	b	rtmp, #al
	add	r8, 32
	ld32	r2, r10
	ldd32	r3, r10, 4
	move	r1, r8
	lih	r4, hi(1017118720)
	lil	r5, 0
	lih	rtmp, hi(__muldf3)
	wl16	rtmp, lo(__muldf3)
	movepc	rret, 8
	b	rtmp, #al
	ld32	r0, r8
	ldd32	r8, r8, 4
	std32	r8, r9, 4
	st32	r0, r9
	srspadd	40
	pop	rret
	pop	r12
	pop	r11
	pop	r10
	move	r0, r9
	pop	r9
	pop	r8
	b	rret, #al
	# EPILOGUE
	.size	genrand_res53, .-genrand_res53
	.section	.rodata.str1.4,"aMS",@progbits,1
	.align	2
.LC5:
	.string	"1000 outputs of genrand_int32()"
	.align	2
.LC6:
	.string	"%10lu "
	.align	2
.LC7:
	.string	"\n1000 outputs of genrand_real2()"
	.align	2
.LC8:
	.string	"%10.8f "
	.section	.text.startup,"ax",@progbits
	.align	2
	.globl	main
	.type	main, @function
main:
	# PROLOGUE, vars= 32, regs= 5, args= 0, extra= 0
	push	r8
	push	r9
	push	r10
	push	r11
	push	rret
	srspadd	-32
	srspr	r1
	lil	r0, 291
	st32	r0, r1
	lil	r0, 564
	std32	r0, r1, 4
	lil	r0, 837
	lil	r2, 4
	std32	r0, r1, 8
	lil	r0, 1110
	std32	r0, r1, 12
	lih	rtmp, hi(init_by_array)
	wl16	rtmp, lo(init_by_array)
	movepc	rret, 8
	b	rtmp, #al
	lih	r1, hi(.LC5)
	wl16	r1, lo(.LC5)
	lih	rtmp, hi(puts)
	wl16	rtmp, lo(puts)
	movepc	rret, 8
	b	rtmp, #al
	lih	rtmp, hi(genrand_int32)
	wl16	rtmp, lo(genrand_int32)
	movepc	rret, 8
	b	rtmp, #al
	lih	r1, hi(.LC6)
	move	r2, r0
	wl16	r1, lo(.LC6)
	lih	rtmp, hi(printf)
	wl16	rtmp, lo(printf)
	movepc	rret, 8
	b	rtmp, #al
	lil	r8, 0
	lil	r9, 5
.L43:
	inc	r8, r8
	cmp	r8, 1000
	br	.L50, #eq
.L45:
	lih	rtmp, hi(genrand_int32)
	wl16	rtmp, lo(genrand_int32)
	movepc	rret, 8
	b	rtmp, #al
	lih	r1, hi(.LC6)
	move	r2, r0
	wl16	r1, lo(.LC6)
	lih	rtmp, hi(printf)
	wl16	rtmp, lo(printf)
	movepc	rret, 8
	b	rtmp, #al
	move	r0, r8
	mod	r0, r9
	cmp	r0, 4
	br	.L43, #ne
	lil	r1, 10
	inc	r8, r8
	lih	rtmp, hi(putchar)
	wl16	rtmp, lo(putchar)
	movepc	rret, 8
	b	rtmp, #al
	cmp	r8, 1000
	br	.L45, #ne
.L50:
	lih	r1, hi(.LC7)
	wl16	r1, lo(.LC7)
	lih	rtmp, hi(puts)
	wl16	rtmp, lo(puts)
	movepc	rret, 8
	b	rtmp, #al
	srspr	r10
	lih	rtmp, hi(genrand_int32)
	wl16	rtmp, lo(genrand_int32)
	movepc	rret, 8
	b	rtmp, #al
	add	r10, 16
	move	r2, r0
	srspr	r9
	move	r1, r10
	lih	rtmp, hi(__floatunsidf)
	wl16	rtmp, lo(__floatunsidf)
	movepc	rret, 8
	b	rtmp, #al
	add	r9, 24
	lih	r4, hi(1039138816)
	lil	r5, 0
	ld32	r2, r10
	ldd32	r3, r10, 4
	move	r1, r9
	lih	rtmp, hi(__muldf3)
	wl16	rtmp, lo(__muldf3)
	movepc	rret, 8
	b	rtmp, #al
	lih	r1, hi(.LC8)
	ld32	r2, r9
	ldd32	r3, r9, 4
	wl16	r1, lo(.LC8)
	lih	rtmp, hi(printf)
	wl16	rtmp, lo(printf)
	movepc	rret, 8
	b	rtmp, #al
	lil	r8, 0
	lil	r11, 5
.L46:
	inc	r8, r8
	cmp	r8, 1000
	br	.L51, #eq
.L48:
	lih	rtmp, hi(genrand_int32)
	wl16	rtmp, lo(genrand_int32)
	movepc	rret, 8
	b	rtmp, #al
	move	r2, r0
	move	r1, r10
	lih	rtmp, hi(__floatunsidf)
	wl16	rtmp, lo(__floatunsidf)
	movepc	rret, 8
	b	rtmp, #al
	lih	r4, hi(1039138816)
	lil	r5, 0
	ld32	r2, r10
	ldd32	r3, r10, 4
	move	r1, r9
	lih	rtmp, hi(__muldf3)
	wl16	rtmp, lo(__muldf3)
	movepc	rret, 8
	b	rtmp, #al
	lih	r1, hi(.LC8)
	ld32	r2, r9
	ldd32	r3, r9, 4
	wl16	r1, lo(.LC8)
	lih	rtmp, hi(printf)
	wl16	rtmp, lo(printf)
	movepc	rret, 8
	b	rtmp, #al
	move	r0, r8
	mod	r0, r11
	cmp	r0, 4
	br	.L46, #ne
	lil	r1, 10
	inc	r8, r8
	lih	rtmp, hi(putchar)
	wl16	rtmp, lo(putchar)
	movepc	rret, 8
	b	rtmp, #al
	cmp	r8, 1000
	br	.L48, #ne
.L51:
	srspadd	32
	pop	rret
	pop	r11
	pop	r10
	pop	r9
	lil	r0, 0
	pop	r8
	b	rret, #al
	# EPILOGUE
	.size	main, .-main
	.section	.rodata
	.align	2
	.type	mag01.2431, @object
	.size	mag01.2431, 8
mag01.2431:
	.long	0
	.long	-1727483681
	.section	.data
	.align	2
	.type	mti, @object
	.size	mti, 4
mti:
	.long	625
	.local	mt
	.comm	mt,2496,4
	.ident	"GCC: (GNU) 5.1.0"

nostdlib 版のコード

ふつうの MT は標準ライブラリが必要なので、標準ライブラリが必要ないコードは以下。

mt19937ar_mist32.c
/* 
   A C-program for MT19937, with initialization improved 2002/1/26.
   Coded by Takuji Nishimura and Makoto Matsumoto.
 
   Before using, initialize the state by using init_genrand(seed)  
   or init_by_array(init_key, key_length).
 
   Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
   All rights reserved.                          
 
   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:
 
     1. Redistributions of source code must retain the above copyright
        notice, this list of conditions and the following disclaimer.
 
     2. Redistributions in binary form must reproduce the above copyright
        notice, this list of conditions and the following disclaimer in the
        documentation and/or other materials provided with the distribution.
 
     3. The names of its contributors may not be used to endorse or promote 
        products derived from this software without specific prior written 
        permission.
 
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
   A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 
   Any feedback is very welcome.
   http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
   email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
*/
 
/* Period parameters */  
#define N 624
#define M 397
#define MATRIX_A 0x9908b0dfUL   /* constant vector a */
#define UPPER_MASK 0x80000000UL /* most significant w-r bits */
#define LOWER_MASK 0x7fffffffUL /* least significant r bits */
 
static unsigned long mt[N]; /* the array for the state vector  */
static int mti=N+1; /* mti==N+1 means mt[N] is not initialized */
 
/* initializes mt[N] with a seed */
void init_genrand(unsigned long s)
{
    mt[0]= s & 0xffffffffUL;
    for (mti=1; mti<N; mti++) {
        mt[mti] = 
	    (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti); 
        /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
        /* In the previous versions, MSBs of the seed affect   */
        /* only MSBs of the array mt[].                        */
        /* 2002/01/09 modified by Makoto Matsumoto             */
        mt[mti] &= 0xffffffffUL;
        /* for >32 bit machines */
    }
}
 
/* initialize by an array with array-length */
/* init_key is the array for initializing keys */
/* key_length is its length */
/* slight change for C++, 2004/2/26 */
void init_by_array(unsigned long init_key[], int key_length)
{
    int i, j, k;
    init_genrand(19650218UL);
    i=1; j=0;
    k = (N>key_length ? N : key_length);
    for (; k; k--) {
        mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL))
          + init_key[j] + j; /* non linear */
        mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
        i++; j++;
        if (i>=N) { mt[0] = mt[N-1]; i=1; }
        if (j>=key_length) j=0;
    }
    for (k=N-1; k; k--) {
        mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL))
          - i; /* non linear */
        mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
        i++;
        if (i>=N) { mt[0] = mt[N-1]; i=1; }
    }
 
    mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */ 
}
 
/* generates a random number on [0,0xffffffff]-interval */
unsigned long genrand_int32(void)
{
    unsigned long y;
    static unsigned long mag01[2]={0x0UL, MATRIX_A};
    /* mag01[x] = x * MATRIX_A  for x=0,1 */
 
    if (mti >= N) { /* generate N words at one time */
        int kk;
 
        if (mti == N+1)   /* if init_genrand() has not been called, */
            init_genrand(5489UL); /* a default initial seed is used */
 
        for (kk=0;kk<N-M;kk++) {
            y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
            mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL];
        }
        for (;kk<N-1;kk++) {
            y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
            mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
        }
        y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
        mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL];
 
        mti = 0;
    }
 
    y = mt[mti++];
 
    /* Tempering */
    y ^= (y >> 11);
    y ^= (y << 7) & 0x9d2c5680UL;
    y ^= (y << 15) & 0xefc60000UL;
    y ^= (y >> 18);
 
    return y;
}
 
/* generates a random number on [0,0x7fffffff]-interval */
long genrand_int31(void)
{
    return (long)(genrand_int32()>>1);
}
 
/* These real versions are due to Isaku Wada, 2002/01/09 added */
 
int main(void)
{
    int i;
    unsigned long init[4]={0x123, 0x234, 0x345, 0x456}, length=4;
    unsigned int *dest;
 
    dest = (unsigned int *)0x01000000;
 
    init_by_array(init, length);
 
    for (i=0; i<1000; i++) {
      *(dest++) = genrand_int32();
    }
 
    return 0;
}
software/samples/mt19937ar.txt · 最終更新: 2015/07/27 23:28 by hktechno