Random Halfword Generator PPC ASM
#1
This is my attempt of writing the Middle Square Theorem (using the Weyl sequence to prevent convergence). I had to 'downsize' though since my Seed is 32 bits instead of 64. Thus, halfword output is used for the random output instead of a word.

The seed is using the lower 32 bits of the TB. Ofc, including the upper 32 bits is not a good idea.

For more info, visit this link - https://pthree.org/2018/07/30/middle-squ...ence-prng/

======

Args:
r3 = the amount of cycles the generator will do before spitting the output. All values treated logically. If the user puts 0 as the value, it is auto adjusted to 4.

Source: #The following snippet of code is meant to be used as a subroutine reached via a branch-link instruction

cmpwi r3, 0
bne+ move_to_ctr

li r3, 4 #If user sets r3 arg to 0, adjust it to 4.

move_to_ctr:
mtctr r3 #LR doesn't need to be backed up for this subroutine
mftbl r4 #Seed register

li r3, 0 #Square-value register, start at 0
li r5, 0 #Starting Weyl variable, start at 0

clrlwi. r0, r4, 31 #Seed needs to be an odd number
bne- loop

addi r4, r4, 1 #Make seed odd

loop:
mullw r3, r3, r3 #Square the Value

add r5, r4, r5 #Add the constant seed to Weyl variable. Result is new Weyl variable
add r3, r3, r5 #Add Weyl variable to squared value

srwi r3, r3, 16 #Place upper 16 bits into lower 16 bits, upper 16 bits are now cleared
bdnz+ loop

blr #r3 contains random halfword result
Reply
#2
This is really cool.
Super Mario Eclipse, what Super Mario Sunshine could've been.
Reply
#3
Thx. It's part of a code I was working on. Thought i would share it.

Do you have any ideas for a 64 bit seed? If so, i can use mullw mulhwu for a random word output instead of halfword
Reply
#4
I'll see if I can come up with something
Super Mario Eclipse, what Super Mario Sunshine could've been.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)