Software discussion

Web software | www.agner.org

PRNGs - Save/Restore
Author: BiShop Date: 2005-09-01 09:45
I need to save/restore the entire state of one of your PRNGs, because I have an application that needs to generate one or two random numbers, and then exit. If I just used a new seed each time, it would be entirely a matter of choosing seeds, which is not so nice solution.
Therefore, I created these two functions that can be used to do the job. However, it is completely incompatible with C version of code and between versions. It saves the entire .DATA section to a buffer.

.DATA

ALIGN 16
DATA_BEGIN DD 20050901 ; this also serves for version checking
; ===== other data =====
ALIGN 16
DATA_END DD 0


.CODE

; Mersenne Twister: sizeof(buffer) = 2640
; extern "C" void TSave (void *buffer);
; extern "C" bool TRestore (void *buffer);

TSave PROC NEAR
public TSave
push esi
push edi
cld
mov esi, offset DATA_BEGIN
mov edi, [esp+8+4]
mov ecx, offset (DATA_END - DATA_BEGIN) / 4
rep movsd
pop edi
pop esi
ret
TSave ENDP

TRestore PROC NEAR
public TRestore
push esi
push edi
mov esi, [esp+8+4]
mov eax, [esi]
cmp [DATA_BEGIN], eax
je TRcpy ; correct version?
xor eax, eax ; nope, call TRandomInit
jmp TRend
TRcpy: cld
mov edi, offset DATA_BEGIN
mov ecx, offset (DATA_END - DATA_BEGIN) / 4
rep movsd
mov eax, 1 ; oki
TRend: pop edi
pop esi
ret
TRestore ENDP

 
thread PRNGs - Save/Restore - BiShop - 2005-09-01
last reply PRNGs - Save/Restore - Agner - 2005-09-01