hotspot/src/cpu/x86/vm/macroAssembler_x86.cpp
changeset 33089 f4e956ed8b43
parent 33066 d98eab8215c4
child 33160 c59f1676d27e
child 33155 73bf16b22e89
equal deleted inserted replaced
33088:34fe49ecee13 33089:f4e956ed8b43
  3031 
  3031 
  3032 void MacroAssembler::fldcw(AddressLiteral src) {
  3032 void MacroAssembler::fldcw(AddressLiteral src) {
  3033   Assembler::fldcw(as_Address(src));
  3033   Assembler::fldcw(as_Address(src));
  3034 }
  3034 }
  3035 
  3035 
       
  3036 void MacroAssembler::mulpd(XMMRegister dst, AddressLiteral src) {
       
  3037   if (reachable(src)) {
       
  3038     Assembler::mulpd(dst, as_Address(src));
       
  3039   } else {
       
  3040     lea(rscratch1, src);
       
  3041     Assembler::mulpd(dst, Address(rscratch1, 0));
       
  3042   }
       
  3043 }
       
  3044 
  3036 void MacroAssembler::pow_exp_core_encoding() {
  3045 void MacroAssembler::pow_exp_core_encoding() {
  3037   // kills rax, rcx, rdx
  3046   // kills rax, rcx, rdx
  3038   subptr(rsp,sizeof(jdouble));
  3047   subptr(rsp,sizeof(jdouble));
  3039   // computes 2^X. Stack: X ...
  3048   // computes 2^X. Stack: X ...
  3040   // f2xm1 computes 2^X-1 but only operates on -1<=X<=1. Get int(X) and
  3049   // f2xm1 computes 2^X-1 but only operates on -1<=X<=1. Get int(X) and
  3103   pow_exp_core_encoding(); // Stack: exp(X) ...
  3112   pow_exp_core_encoding(); // Stack: exp(X) ...
  3104   restore_precision();
  3113   restore_precision();
  3105   BLOCK_COMMENT("} fast_pow");
  3114   BLOCK_COMMENT("} fast_pow");
  3106 }
  3115 }
  3107 
  3116 
  3108 void MacroAssembler::fast_exp() {
  3117 void MacroAssembler::pow_or_exp(int num_fpu_regs_in_use) {
  3109   // computes exp(X) = 2^(X * log2(e))
       
  3110   // if fast computation is not possible, result is NaN. Requires
       
  3111   // fallback from user of this macro.
       
  3112   // increase precision for intermediate steps of the computation
       
  3113   increase_precision();
       
  3114   fldl2e();                // Stack: log2(e) X ...
       
  3115   fmulp(1);                // Stack: (X*log2(e)) ...
       
  3116   pow_exp_core_encoding(); // Stack: exp(X) ...
       
  3117   restore_precision();
       
  3118 }
       
  3119 
       
  3120 void MacroAssembler::pow_or_exp(bool is_exp, int num_fpu_regs_in_use) {
       
  3121   // kills rax, rcx, rdx
  3118   // kills rax, rcx, rdx
  3122   // pow and exp needs 2 extra registers on the fpu stack.
  3119   // pow and exp needs 2 extra registers on the fpu stack.
  3123   Label slow_case, done;
  3120   Label slow_case, done;
  3124   Register tmp = noreg;
  3121   Register tmp = noreg;
  3125   if (!VM_Version::supports_cmov()) {
  3122   if (!VM_Version::supports_cmov()) {
  3127     tmp = rdx;
  3124     tmp = rdx;
  3128   }
  3125   }
  3129   Register tmp2 = rax;
  3126   Register tmp2 = rax;
  3130   Register tmp3 = rcx;
  3127   Register tmp3 = rcx;
  3131 
  3128 
  3132   if (is_exp) {
  3129   // Stack: X Y
  3133     // Stack: X
  3130   Label x_negative, y_not_2;
  3134     fld_s(0);                   // duplicate argument for runtime call. Stack: X X
  3131 
  3135     fast_exp();                 // Stack: exp(X) X
  3132   static double two = 2.0;
  3136     fcmp(tmp, 0, false, false); // Stack: exp(X) X
  3133   ExternalAddress two_addr((address)&two);
  3137     // exp(X) not equal to itself: exp(X) is NaN go to slow case.
  3134 
  3138     jcc(Assembler::parity, slow_case);
  3135   // constant maybe too far on 64 bit
  3139     // get rid of duplicate argument. Stack: exp(X)
  3136   lea(tmp2, two_addr);
  3140     if (num_fpu_regs_in_use > 0) {
  3137   fld_d(Address(tmp2, 0));    // Stack: 2 X Y
  3141       fxch();
  3138   fcmp(tmp, 2, true, false);  // Stack: X Y
  3142       fpop();
  3139   jcc(Assembler::parity, y_not_2);
  3143     } else {
  3140   jcc(Assembler::notEqual, y_not_2);
  3144       ffree(1);
  3141 
  3145     }
  3142   fxch(); fpop();             // Stack: X
  3146     jmp(done);
  3143   fmul(0);                    // Stack: X*X
  3147   } else {
  3144 
  3148     // Stack: X Y
  3145   jmp(done);
  3149     Label x_negative, y_not_2;
  3146 
  3150 
  3147   bind(y_not_2);
  3151     static double two = 2.0;
  3148 
  3152     ExternalAddress two_addr((address)&two);
  3149   fldz();                     // Stack: 0 X Y
  3153 
  3150   fcmp(tmp, 1, true, false);  // Stack: X Y
  3154     // constant maybe too far on 64 bit
  3151   jcc(Assembler::above, x_negative);
  3155     lea(tmp2, two_addr);
  3152 
  3156     fld_d(Address(tmp2, 0));    // Stack: 2 X Y
  3153   // X >= 0
  3157     fcmp(tmp, 2, true, false);  // Stack: X Y
  3154 
  3158     jcc(Assembler::parity, y_not_2);
  3155   fld_s(1);                   // duplicate arguments for runtime call. Stack: Y X Y
  3159     jcc(Assembler::notEqual, y_not_2);
  3156   fld_s(1);                   // Stack: X Y X Y
  3160 
  3157   fast_pow();                 // Stack: X^Y X Y
  3161     fxch(); fpop();             // Stack: X
  3158   fcmp(tmp, 0, false, false); // Stack: X^Y X Y
  3162     fmul(0);                    // Stack: X*X
  3159   // X^Y not equal to itself: X^Y is NaN go to slow case.
  3163 
  3160   jcc(Assembler::parity, slow_case);
  3164     jmp(done);
  3161   // get rid of duplicate arguments. Stack: X^Y
  3165 
  3162   if (num_fpu_regs_in_use > 0) {
  3166     bind(y_not_2);
  3163     fxch(); fpop();
  3167 
  3164     fxch(); fpop();
  3168     fldz();                     // Stack: 0 X Y
  3165   } else {
  3169     fcmp(tmp, 1, true, false);  // Stack: X Y
  3166     ffree(2);
  3170     jcc(Assembler::above, x_negative);
  3167     ffree(1);
  3171 
  3168   }
  3172     // X >= 0
  3169   jmp(done);
  3173 
  3170 
  3174     fld_s(1);                   // duplicate arguments for runtime call. Stack: Y X Y
  3171   // X <= 0
  3175     fld_s(1);                   // Stack: X Y X Y
  3172   bind(x_negative);
  3176     fast_pow();                 // Stack: X^Y X Y
  3173 
  3177     fcmp(tmp, 0, false, false); // Stack: X^Y X Y
  3174   fld_s(1);                   // Stack: Y X Y
  3178     // X^Y not equal to itself: X^Y is NaN go to slow case.
  3175   frndint();                  // Stack: int(Y) X Y
  3179     jcc(Assembler::parity, slow_case);
  3176   fcmp(tmp, 2, false, false); // Stack: int(Y) X Y
  3180     // get rid of duplicate arguments. Stack: X^Y
  3177   jcc(Assembler::notEqual, slow_case);
  3181     if (num_fpu_regs_in_use > 0) {
  3178 
  3182       fxch(); fpop();
  3179   subptr(rsp, 8);
  3183       fxch(); fpop();
  3180 
  3184     } else {
  3181   // For X^Y, when X < 0, Y has to be an integer and the final
  3185       ffree(2);
  3182   // result depends on whether it's odd or even. We just checked
  3186       ffree(1);
  3183   // that int(Y) == Y.  We move int(Y) to gp registers as a 64 bit
  3187     }
  3184   // integer to test its parity. If int(Y) is huge and doesn't fit
  3188     jmp(done);
  3185   // in the 64 bit integer range, the integer indefinite value will
  3189 
  3186   // end up in the gp registers. Huge numbers are all even, the
  3190     // X <= 0
  3187   // integer indefinite number is even so it's fine.
  3191     bind(x_negative);
       
  3192 
       
  3193     fld_s(1);                   // Stack: Y X Y
       
  3194     frndint();                  // Stack: int(Y) X Y
       
  3195     fcmp(tmp, 2, false, false); // Stack: int(Y) X Y
       
  3196     jcc(Assembler::notEqual, slow_case);
       
  3197 
       
  3198     subptr(rsp, 8);
       
  3199 
       
  3200     // For X^Y, when X < 0, Y has to be an integer and the final
       
  3201     // result depends on whether it's odd or even. We just checked
       
  3202     // that int(Y) == Y.  We move int(Y) to gp registers as a 64 bit
       
  3203     // integer to test its parity. If int(Y) is huge and doesn't fit
       
  3204     // in the 64 bit integer range, the integer indefinite value will
       
  3205     // end up in the gp registers. Huge numbers are all even, the
       
  3206     // integer indefinite number is even so it's fine.
       
  3207 
  3188 
  3208 #ifdef ASSERT
  3189 #ifdef ASSERT
  3209     // Let's check we don't end up with an integer indefinite number
  3190   // Let's check we don't end up with an integer indefinite number
  3210     // when not expected. First test for huge numbers: check whether
  3191   // when not expected. First test for huge numbers: check whether
  3211     // int(Y)+1 == int(Y) which is true for very large numbers and
  3192   // int(Y)+1 == int(Y) which is true for very large numbers and
  3212     // those are all even. A 64 bit integer is guaranteed to not
  3193   // those are all even. A 64 bit integer is guaranteed to not
  3213     // overflow for numbers where y+1 != y (when precision is set to
  3194   // overflow for numbers where y+1 != y (when precision is set to
  3214     // double precision).
  3195   // double precision).
  3215     Label y_not_huge;
  3196   Label y_not_huge;
  3216 
  3197 
  3217     fld1();                     // Stack: 1 int(Y) X Y
  3198   fld1();                     // Stack: 1 int(Y) X Y
  3218     fadd(1);                    // Stack: 1+int(Y) int(Y) X Y
  3199   fadd(1);                    // Stack: 1+int(Y) int(Y) X Y
  3219 
  3200 
  3220 #ifdef _LP64
  3201 #ifdef _LP64
  3221     // trip to memory to force the precision down from double extended
  3202   // trip to memory to force the precision down from double extended
  3222     // precision
  3203   // precision
  3223     fstp_d(Address(rsp, 0));
  3204   fstp_d(Address(rsp, 0));
  3224     fld_d(Address(rsp, 0));
  3205   fld_d(Address(rsp, 0));
  3225 #endif
  3206 #endif
  3226 
  3207 
  3227     fcmp(tmp, 1, true, false);  // Stack: int(Y) X Y
  3208   fcmp(tmp, 1, true, false);  // Stack: int(Y) X Y
  3228 #endif
  3209 #endif
  3229 
  3210 
  3230     // move int(Y) as 64 bit integer to thread's stack
  3211   // move int(Y) as 64 bit integer to thread's stack
  3231     fistp_d(Address(rsp,0));    // Stack: X Y
  3212   fistp_d(Address(rsp,0));    // Stack: X Y
  3232 
  3213 
  3233 #ifdef ASSERT
  3214 #ifdef ASSERT
  3234     jcc(Assembler::notEqual, y_not_huge);
  3215   jcc(Assembler::notEqual, y_not_huge);
  3235 
  3216 
  3236     // Y is huge so we know it's even. It may not fit in a 64 bit
  3217   // Y is huge so we know it's even. It may not fit in a 64 bit
  3237     // integer and we don't want the debug code below to see the
  3218   // integer and we don't want the debug code below to see the
  3238     // integer indefinite value so overwrite int(Y) on the thread's
  3219   // integer indefinite value so overwrite int(Y) on the thread's
  3239     // stack with 0.
  3220   // stack with 0.
  3240     movl(Address(rsp, 0), 0);
  3221   movl(Address(rsp, 0), 0);
  3241     movl(Address(rsp, 4), 0);
  3222   movl(Address(rsp, 4), 0);
  3242 
  3223 
  3243     bind(y_not_huge);
  3224   bind(y_not_huge);
  3244 #endif
  3225 #endif
  3245 
  3226 
  3246     fld_s(1);                   // duplicate arguments for runtime call. Stack: Y X Y
  3227   fld_s(1);                   // duplicate arguments for runtime call. Stack: Y X Y
  3247     fld_s(1);                   // Stack: X Y X Y
  3228   fld_s(1);                   // Stack: X Y X Y
  3248     fabs();                     // Stack: abs(X) Y X Y
  3229   fabs();                     // Stack: abs(X) Y X Y
  3249     fast_pow();                 // Stack: abs(X)^Y X Y
  3230   fast_pow();                 // Stack: abs(X)^Y X Y
  3250     fcmp(tmp, 0, false, false); // Stack: abs(X)^Y X Y
  3231   fcmp(tmp, 0, false, false); // Stack: abs(X)^Y X Y
  3251     // abs(X)^Y not equal to itself: abs(X)^Y is NaN go to slow case.
  3232   // abs(X)^Y not equal to itself: abs(X)^Y is NaN go to slow case.
  3252 
  3233 
  3253     pop(tmp2);
  3234   pop(tmp2);
  3254     NOT_LP64(pop(tmp3));
  3235   NOT_LP64(pop(tmp3));
  3255     jcc(Assembler::parity, slow_case);
  3236   jcc(Assembler::parity, slow_case);
  3256 
  3237 
  3257 #ifdef ASSERT
  3238 #ifdef ASSERT
  3258     // Check that int(Y) is not integer indefinite value (int
  3239   // Check that int(Y) is not integer indefinite value (int
  3259     // overflow). Shouldn't happen because for values that would
  3240   // overflow). Shouldn't happen because for values that would
  3260     // overflow, 1+int(Y)==Y which was tested earlier.
  3241   // overflow, 1+int(Y)==Y which was tested earlier.
  3261 #ifndef _LP64
  3242 #ifndef _LP64
  3262     {
  3243   {
  3263       Label integer;
  3244     Label integer;
  3264       testl(tmp2, tmp2);
  3245     testl(tmp2, tmp2);
  3265       jcc(Assembler::notZero, integer);
  3246     jcc(Assembler::notZero, integer);
  3266       cmpl(tmp3, 0x80000000);
  3247     cmpl(tmp3, 0x80000000);
  3267       jcc(Assembler::notZero, integer);
  3248     jcc(Assembler::notZero, integer);
  3268       STOP("integer indefinite value shouldn't be seen here");
  3249     STOP("integer indefinite value shouldn't be seen here");
  3269       bind(integer);
  3250     bind(integer);
  3270     }
  3251   }
  3271 #else
  3252 #else
  3272     {
  3253   {
  3273       Label integer;
  3254     Label integer;
  3274       mov(tmp3, tmp2); // preserve tmp2 for parity check below
  3255     mov(tmp3, tmp2); // preserve tmp2 for parity check below
  3275       shlq(tmp3, 1);
  3256     shlq(tmp3, 1);
  3276       jcc(Assembler::carryClear, integer);
  3257     jcc(Assembler::carryClear, integer);
  3277       jcc(Assembler::notZero, integer);
  3258     jcc(Assembler::notZero, integer);
  3278       STOP("integer indefinite value shouldn't be seen here");
  3259     STOP("integer indefinite value shouldn't be seen here");
  3279       bind(integer);
  3260     bind(integer);
  3280     }
  3261   }
  3281 #endif
  3262 #endif
  3282 #endif
  3263 #endif
  3283 
  3264 
  3284     // get rid of duplicate arguments. Stack: X^Y
  3265   // get rid of duplicate arguments. Stack: X^Y
  3285     if (num_fpu_regs_in_use > 0) {
  3266   if (num_fpu_regs_in_use > 0) {
  3286       fxch(); fpop();
  3267     fxch(); fpop();
  3287       fxch(); fpop();
  3268     fxch(); fpop();
  3288     } else {
  3269   } else {
  3289       ffree(2);
  3270     ffree(2);
  3290       ffree(1);
  3271     ffree(1);
  3291     }
  3272   }
  3292 
  3273 
  3293     testl(tmp2, 1);
  3274   testl(tmp2, 1);
  3294     jcc(Assembler::zero, done); // X <= 0, Y even: X^Y = abs(X)^Y
  3275   jcc(Assembler::zero, done); // X <= 0, Y even: X^Y = abs(X)^Y
  3295     // X <= 0, Y even: X^Y = -abs(X)^Y
  3276   // X <= 0, Y even: X^Y = -abs(X)^Y
  3296 
  3277 
  3297     fchs();                     // Stack: -abs(X)^Y Y
  3278   fchs();                     // Stack: -abs(X)^Y Y
  3298     jmp(done);
  3279   jmp(done);
  3299   }
       
  3300 
  3280 
  3301   // slow case: runtime call
  3281   // slow case: runtime call
  3302   bind(slow_case);
  3282   bind(slow_case);
  3303 
  3283 
  3304   fpop();                       // pop incorrect result or int(Y)
  3284   fpop();                       // pop incorrect result or int(Y)
  3305 
  3285 
  3306   fp_runtime_fallback(is_exp ? CAST_FROM_FN_PTR(address, SharedRuntime::dexp) : CAST_FROM_FN_PTR(address, SharedRuntime::dpow),
  3286   fp_runtime_fallback(CAST_FROM_FN_PTR(address, SharedRuntime::dpow), 2, num_fpu_regs_in_use);
  3307                       is_exp ? 1 : 2, num_fpu_regs_in_use);
       
  3308 
  3287 
  3309   // Come here with result in F-TOS
  3288   // Come here with result in F-TOS
  3310   bind(done);
  3289   bind(done);
  3311 }
  3290 }
  3312 
  3291