Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. float temp;
  2. asm volatile("fld %2 n"
  3. "fld %3 n"
  4. "fcomi n"
  5. "fcmovbe %%st(1), %%st n"
  6. "fstp %0 n"
  7. "fstp %1 n"
  8. :"=m"(maxval), "=m"(temp)
  9. :"m"(val), "m"(maxval));
  10.  
  11. if(val>maxval)
  12. maxval=val;
  13.  
  14. if(val>maxval)
  15. val2=maxval;
  16.  
  17. float findmax_asm(const float *arr, size_t len)
  18. {
  19. float maxval = -INFINITY;
  20. for (size_t i=0 ; i<len ; i++) {
  21. float val = arr[i];
  22. float temp;
  23. asm volatile("fld %2 n"
  24. "fld %3 n"
  25. "fcomi n"
  26. "fcmovbe %%st(1), %%st n"
  27. "fstp %0 n"
  28. "fstp %1 n"
  29. :"=m"(maxval), "=m"(temp)
  30. :"m"(val), "m"(maxval));
  31. }
  32. return maxval;
  33. }
  34.  
  35. .L20:
  36. flds (%eax) # MEM[base: _18, offset: 0B]
  37. fstps 8(%esp) # val
  38.  
  39. ## inline asm starts here
  40. fld 8(%esp) # val
  41. fld 4(%esp) # maxval # st(0) = maxval, st(1) = val
  42. fcomi # implicitly %st(1), %st i.e. fcomi val, maxval
  43. fcmovbe %st(1), %st # copy if maxval<=val
  44. fstp 4(%esp) # maxval # store updated maxval back to memory
  45. fstp 12(%esp) # temp # and uselessly store temp = val
  46. ## and ends here
  47.  
  48. addl $4, %eax #, ivtmp.25
  49. cmpl %edx, %eax # _25, ivtmp.25 # induction-variable temporary pointers invented by the compiler to turn arr[i] into a pointer-increment
  50. jne .L20 #, # }while(p != endp);
  51.  
  52. # loop ends with maxval in memory.
  53.  
  54. flds 4(%esp) # maxval
  55. .L23:
  56. addl $16, %esp #,
  57. ret # with %st = maxval return value
  58.  
  59. maxval = (val < maxval) ? maxval : val;
  60.  
  61. #include <stdint.h>
  62. #include <stdlib.h>
  63. #include <math.h>
  64.  
  65. float findmax_c(const float *arr, size_t len)
  66. {
  67. float maxval = -INFINITY;
  68. for (size_t i=0 ; i<len ; i++) {
  69. float val = arr[i];
  70. maxval = (val < maxval) ? maxval : val; // maxss (mem), reg
  71. }
  72. return maxval;
  73. }
  74.  
  75. .L4:
  76. flds (%eax) # MEM[base: _1, offset: 0B]
  77. fxch %st(1) #
  78. fcomi %st(1), %st #,
  79. fcmovbe %st(1), %st #,,
  80. fstp %st(1) #
  81. addl $4, %eax #, ivtmp.9
  82. cmpl %eax, %edx # ivtmp.9, _5
  83. jne .L4 #,
  84. ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement