6318 
6318 

6319 
6319 
/*

6320 
6320 
* callseq:


6321 
* big.bitsize > integer


6322 
*


6323 
* Returns the number of bits of the absolute value of <i>big</i>.


6324 
*


6325 
* (2**100001).bitsize #=> 10001


6326 
* (2**10000).bitsize #=> 10001


6327 
* (2**10000+1).bitsize #=> 10000


6328 
*


6329 
* (2**10001).bitsize #=> 1001


6330 
* (2**1000).bitsize #=> 1001


6331 
* (2**1000+1).bitsize #=> 1000


6332 
*


6333 
* (2**10001).bitsize #=> 1000


6334 
* (2**1000).bitsize #=> 1001


6335 
* (2**1000+1).bitsize #=> 1001


6336 
*


6337 
* (2**100001).bitsize #=> 10000


6338 
* (2**10000).bitsize #=> 10001


6339 
* (2**10000+1).bitsize #=> 10001


6340 
*


6341 
*/


6342 


6343 
static VALUE


6344 
rb_big_bitsize(VALUE big)


6345 
{


6346 
int nlz_bits;


6347 
size_t numbytes;


6348 


6349 
static const BDIGIT char_bit[1] = { CHAR_BIT };


6350 
BDIGIT numbytes_bary[bdigit_roomof(sizeof(size_t))];


6351 
BDIGIT nlz_bary[1];


6352 
BDIGIT result_bary[bdigit_roomof(sizeof(size_t)+1)];


6353 


6354 
numbytes = rb_absint_size(big, &nlz_bits);


6355 


6356 
if (numbytes <= SIZE_MAX / CHAR_BIT) {


6357 
return SIZET2NUM(numbytes * CHAR_BIT  nlz_bits);


6358 
}


6359 


6360 
nlz_bary[0] = nlz_bits;


6361 


6362 
bary_unpack(BARY_ARGS(numbytes_bary), &numbytes, 1, sizeof(numbytes), 0,


6363 
INTEGER_PACK_NATIVE_BYTE_ORDER);


6364 
BARY_MUL1(result_bary, numbytes_bary, char_bit);


6365 
BARY_SUB(result_bary, result_bary, nlz_bary);


6366 


6367 
return rb_integer_unpack(result_bary, numberof(result_bary), sizeof(BDIGIT), 0,


6368 
INTEGER_PACK_LSWORD_FIRSTINTEGER_PACK_NATIVE_BYTE_ORDER);


6369 
}


6370 


6371 
/*


6372 
* callseq:


6373 
* fix.bitsize > integer


6374 
*


6375 
* Returns the number of bits of the absolute value of <i>fix</i>.


6376 
*


6377 
* (2**121).bitsize) #=> 13


6378 
* (2**12).bitsize) #=> 13


6379 
* (2**12+1).bitsize) #=> 12


6380 
* 0x100.bitsize #=> 9


6381 
* 0xff.bitsize #=> 8


6382 
* 1.bitsize #=> 1


6383 
* 0.bitsize #=> 0


6384 
* 1.bitsize #=> 1


6385 
* 0xff.bitsize #=> 8


6386 
* 0x100.bitsize #=> 9


6387 
* (2**121).bitsize #=> 12


6388 
* (2**12).bitsize #=> 13


6389 
* (2**12+1).bitsize #=> 13


6390 
*/


6391 


6392 
static VALUE


6393 
rb_fix_bitsize(VALUE fix)


6394 
{


6395 
long v = FIX2LONG(fix);


6396 
if (v < 0)


6397 
v = v;


6398 
return LONG2FIX(bitsize(v));


6399 
}


6400 


6401 
/*


6402 
* callseq:

6321 
6403 
* big.odd? > true or false

6322 
6404 
*

6323 
6405 
* Returns <code>true</code> if <i>big</i> is an odd number.

...  ...  
6407 
6489 
rb_define_method(rb_cBignum, "abs", rb_big_abs, 0);

6408 
6490 
rb_define_method(rb_cBignum, "magnitude", rb_big_abs, 0);

6409 
6491 
rb_define_method(rb_cBignum, "size", rb_big_size, 0);


6492 
rb_define_method(rb_cBignum, "bitsize", rb_big_bitsize, 0);

6410 
6493 
rb_define_method(rb_cBignum, "odd?", rb_big_odd_p, 0);

6411 
6494 
rb_define_method(rb_cBignum, "even?", rb_big_even_p, 0);

6412 
6495 


6496 
rb_define_method(rb_cFixnum, "bitsize", rb_fix_bitsize, 0);


6497 

6413 
6498 
power_cache_init();

6414 
6499 
}
