** Add a float to list of constants and return its index. Floats ** with integral values need a different key, to avoid collision ** with actual integers. To that end, we add to the number its smaller ** power-of-two fraction that is still significant in its scale. ** (For doubles, the fraction would be 2^-52). ** This method is not bulletproof: different numbers may generate the ** same key (e.g.
| 615 | ** with a duplicate. |
| 616 | */ |
| 617 | static int luaK_numberK (FuncState *fs, lua_Number r) { |
| 618 | TValue o, kv; |
| 619 | setfltvalue(&o, r); /* value as a TValue */ |
| 620 | if (r == 0) { /* handle zero as a special case */ |
| 621 | setpvalue(&kv, fs); /* use FuncState as index */ |
| 622 | return k2proto(fs, &kv, &o); /* cannot collide */ |
| 623 | } |
| 624 | else { |
| 625 | const int nbm = l_floatatt(MANT_DIG); |
| 626 | const lua_Number q = l_mathop(ldexp)(l_mathop(1.0), -nbm + 1); |
| 627 | const lua_Number k = r * (1 + q); /* key */ |
| 628 | lua_Integer ik; |
| 629 | setfltvalue(&kv, k); /* key as a TValue */ |
| 630 | if (!luaV_flttointeger(k, &ik, F2Ieq)) { /* not an integer value? */ |
| 631 | int n = k2proto(fs, &kv, &o); /* use key */ |
| 632 | if (luaV_rawequalobj(&fs->f->k[n], &o)) /* correct value? */ |
| 633 | return n; |
| 634 | } |
| 635 | /* else, either key is still an integer or there was a collision; |
| 636 | anyway, do not try to reuse constant; instead, create a new one */ |
| 637 | return addk(fs, fs->f, &o); |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | |
| 642 | /* |
no test coverage detected