Advertisement
Madmouse

ARM inline assembly random number 1 - 255

Jul 16th, 2015
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.86 KB | None | 0 0
  1. // ------------------------------------------------------------------------------
  2. // THE BEER-WARE LICENSE (Revision 43):
  3. // <aaronryool@gmail.com> wrote this file. As long as you retain this notice you
  4. // can do whatever you want with this stuff. If we meet some day, and you think
  5. // this stuff is worth it, you can buy me a beer in return
  6. // ------------------------------------------------------------------------------
  7.  
  8. unsigned __rand(unsigned y)
  9. {
  10. // Implementation of George Marsaglia's xorshift PRNG
  11. // gets a number between 1 and 255
  12. asm(
  13.     "cpy %0, %1\n\t"
  14.     "eor %0, %0, %0, lsl #13\n\t"
  15.     "eor %0, %0, %0, lsr #9\n\t"
  16.     "eor %0, %0, %0, lsr #7\n\t"
  17.     "ldr r1, =255\n\t"
  18.     "ldr r2, =0x1010102\n\t"
  19.     "umull r3, r2, %0, r2\n\t"
  20.     "mul r1, r2\n\t"
  21.     "sub %0, r1, %0\n\t"
  22.     "and %0, %0, #0xff\n\t"
  23.     "add %0, %0, #1\n\t"
  24.     : "=r"(y) : "r"(y) : );
  25.     return y;
  26.  
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement