Changeset e277659875f69846f88c96fc353cf8162c22a8ae
- Timestamp:
- 09/09/04 15:04:12
(4 years ago)
- Author:
- Gildas Bazin <gbazin@videolan.org>
- git-committer:
- Gildas Bazin <gbazin@videolan.org> 1094735052 +0000
- git-parent:
[01993028a69da43a21962371aa35124262e73335]
- git-author:
- Gildas Bazin <gbazin@videolan.org> 1094735052 +0000
- Message:
* include/vlc_common.h, src/extras/libc.c: added GCD() and vlc_reduce().
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r24668b5 |
re277659 |
|
| 476 | 476 | #endif |
|---|
| 477 | 477 | |
|---|
| | 478 | static int64_t GCD( int64_t a, int64_t b ) |
|---|
| | 479 | { |
|---|
| | 480 | if( b ) return GCD( b, a % b ); |
|---|
| | 481 | else return a; |
|---|
| | 482 | } |
|---|
| | 483 | |
|---|
| 478 | 484 | /* Dynamic array handling: realloc array, move data, increment position */ |
|---|
| 479 | 485 | #define INSERT_ELEM( p_ar, i_oldsize, i_pos, elem ) \ |
|---|
| … | … | |
| 909 | 915 | #endif |
|---|
| 910 | 916 | |
|---|
| | 917 | VLC_EXPORT( vlc_bool_t, vlc_reduce, ( int *, int *, int64_t, int64_t, int64_t ) ); |
|---|
| | 918 | |
|---|
| 911 | 919 | /* vlc_wraptext (defined in src/extras/libc.c) */ |
|---|
| 912 | 920 | #define wraptext vlc_wraptext |
|---|
| r0c9a991 |
re277659 |
|
| 422 | 422 | #endif |
|---|
| 423 | 423 | } |
|---|
| | 424 | |
|---|
| | 425 | /***************************************************************************** |
|---|
| | 426 | * reduce a fraction |
|---|
| | 427 | * (adapted from libavcodec, author Michael Niedermayer <michaelni@gmx.at>) |
|---|
| | 428 | *****************************************************************************/ |
|---|
| | 429 | vlc_bool_t vlc_reduce( int *pi_dst_nom, int *pi_dst_den, |
|---|
| | 430 | int64_t i_nom, int64_t i_den, int64_t i_max ) |
|---|
| | 431 | { |
|---|
| | 432 | vlc_bool_t b_exact = 1, b_sign = 0; |
|---|
| | 433 | int64_t i_gcd; |
|---|
| | 434 | |
|---|
| | 435 | if( i_den == 0 ) |
|---|
| | 436 | { |
|---|
| | 437 | i_nom = 0; |
|---|
| | 438 | i_den = 1; |
|---|
| | 439 | return 1; |
|---|
| | 440 | } |
|---|
| | 441 | |
|---|
| | 442 | if( i_den < 0 ) |
|---|
| | 443 | { |
|---|
| | 444 | i_den = - i_den; |
|---|
| | 445 | i_nom = - i_nom; |
|---|
| | 446 | } |
|---|
| | 447 | |
|---|
| | 448 | if( i_nom < 0 ) |
|---|
| | 449 | { |
|---|
| | 450 | i_nom = - i_nom; |
|---|
| | 451 | b_sign = 1; |
|---|
| | 452 | } |
|---|
| | 453 | |
|---|
| | 454 | i_gcd = GCD( i_nom, i_den ); |
|---|
| | 455 | i_nom /= i_gcd; |
|---|
| | 456 | i_den /= i_gcd; |
|---|
| | 457 | |
|---|
| | 458 | if( i_nom > i_max || i_den > i_max ) |
|---|
| | 459 | { |
|---|
| | 460 | int i_a0_num = 0, i_a0_den = 1, i_a1_num = 1, i_a1_den = 0; |
|---|
| | 461 | b_exact = 0; |
|---|
| | 462 | |
|---|
| | 463 | for( ; ; ) |
|---|
| | 464 | { |
|---|
| | 465 | int64_t i_x = i_nom / i_den; |
|---|
| | 466 | int64_t i_a2n = i_x * i_a1_num + i_a0_num; |
|---|
| | 467 | int64_t i_a2d = i_x * i_a1_den + i_a0_den; |
|---|
| | 468 | |
|---|
| | 469 | if( i_a2n > i_max || i_a2d > i_max ) break; |
|---|
| | 470 | |
|---|
| | 471 | i_nom %= i_den; |
|---|
| | 472 | |
|---|
| | 473 | i_a0_num = i_a1_num; i_a0_den = i_a1_den; |
|---|
| | 474 | i_a1_num = i_a2n; i_a1_den = i_a2d; |
|---|
| | 475 | if( i_nom == 0 ) break; |
|---|
| | 476 | i_x = i_nom; i_nom = i_den; i_den = i_x; |
|---|
| | 477 | } |
|---|
| | 478 | i_nom = i_a1_num; |
|---|
| | 479 | i_den = i_a1_den; |
|---|
| | 480 | } |
|---|
| | 481 | |
|---|
| | 482 | if( b_sign ) i_nom = - i_nom; |
|---|
| | 483 | |
|---|
| | 484 | *pi_dst_nom = i_nom; |
|---|
| | 485 | *pi_dst_den = i_den; |
|---|
| | 486 | |
|---|
| | 487 | return b_exact; |
|---|
| | 488 | } |
|---|