Guest User

Untitled

a guest
Sep 10th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "fr1 := GMPfrWrapper new.
  2. fr1 prim_mpfr_prec_roundWithPrecision:4096 withRounding: 0.
  3. fr1 set_d: 1.
  4. perform 10,000,000 4096-digit  additions fr1 := fr1 + fr1
  5. [fr1 test:10000000.] timeToRun   => 1971 miliseconds"
  6.  
  7.  
  8.  
  9. test:cnt
  10.  
  11. "calling GNU mpfr add function cnt times using assembler within a Smalltalk method"
  12.  
  13.  
  14.  
  15.  
  16. "calling function: mpfr_add (mpzf_t gmpf, mpf_t op1, mpf_t op2)"
  17.  
  18.     <primitive: #primitiveNativeCall module: #NativeBoostPlugin>
  19.    
  20.     ^ NBFFICallout cdecl: #( void (mpfr_t gmpfr, ulong cnt))
  21.     emitCall: [:gen | | asm  proxy smpfr scnt|
  22.         asm := gen asm.
  23.         proxy := gen proxy.
  24.        
  25.         "reserve temps for parameters passed in from Smalltalk"
  26.         smpfr := gen reserveTemp.
  27.         scnt := gen reserveTemp.
  28.        
  29.         "move parameters into temps"
  30.  
  31.         asm
  32.                  mov: asm ESP ptr to:  asm EAX;
  33.                 mov: asm EAX to: smpfr;   "gmpfr -> smpfr"
  34.                 mov: asm ESP ptr + 4 to: asm EAX;
  35.             mov: asm EAX to: scnt.  "cnt -> scnt"
  36.            
  37.            
  38.                 "setup loop compare and jmp or push ECX and pass through to function call"
  39.         asm
  40.             mov: scnt to: asm ECX;
  41.             label: #top;
  42.             cmp: asm ECX with: 0;
  43.             jz: #bottom;
  44.             push: asm ECX.
  45.        
  46.         "call function"
  47.        
  48.         asm cdeclCall: [:call | | gmpfrAddFcnPtr |   "handles stack alignment"
  49.  
  50.         "push function arguments (note, cdecl push order is from right to left ) "
  51.         asm
  52.             push: smpfr;
  53.             push: smpfr;
  54.             push: smpfr.
  55.  
  56.         "get a function address"
  57.         gmpfrAddFcnPtr := self nbGetSymbolAddress: 'mpfr_add' module:self nbLibraryNameOrHandle.
  58.  
  59.  
  60.         "put a function address into EAX, and call function "
  61.  
  62.          asm mov: gmpfrAddFcnPtr asUImm to: asm EAX.
  63.         asm call: asm EAX.
  64.  
  65.  
  66.     ] alignment: gen stackAlignment .
  67.  
  68.     "restore ECX and jump to top for test"
  69.  
  70.     asm
  71.         pop: asm ECX;
  72.         dec: asm ECX;
  73.         jmp: #top.
  74.        
  75.     asm label: #bottom.
  76.  
  77.     ]
Add Comment
Please, Sign In to add comment