1 | /* |
1 | /* |
2 | * libecb - http://software.schmorp.de/pkg/libecb |
2 | * libecb - http://software.schmorp.de/pkg/libecb |
3 | * |
3 | * |
4 | * Copyright (©) 2009-2014 Marc Alexander Lehmann <libecb@schmorp.de> |
4 | * Copyright (©) 2009-2015 Marc Alexander Lehmann <libecb@schmorp.de> |
5 | * Copyright (©) 2011 Emanuele Giaquinta |
5 | * Copyright (©) 2011 Emanuele Giaquinta |
6 | * All rights reserved. |
6 | * All rights reserved. |
7 | * |
7 | * |
8 | * Redistribution and use in source and binary forms, with or without modifica- |
8 | * Redistribution and use in source and binary forms, with or without modifica- |
9 | * tion, are permitted provided that the following conditions are met: |
9 | * tion, are permitted provided that the following conditions are met: |
… | |
… | |
357 | |
357 | |
358 | /* TODO http://msdn.microsoft.com/en-us/library/k6ktzx3s.aspx __declspec(noreturn) */ |
358 | /* TODO http://msdn.microsoft.com/en-us/library/k6ktzx3s.aspx __declspec(noreturn) */ |
359 | #if ECB_C11 || __IBMC_NORETURN |
359 | #if ECB_C11 || __IBMC_NORETURN |
360 | /* http://pic.dhe.ibm.com/infocenter/compbg/v121v141/topic/com.ibm.xlcpp121.bg.doc/language_ref/noreturn.html */ |
360 | /* http://pic.dhe.ibm.com/infocenter/compbg/v121v141/topic/com.ibm.xlcpp121.bg.doc/language_ref/noreturn.html */ |
361 | #define ecb_noreturn _Noreturn |
361 | #define ecb_noreturn _Noreturn |
|
|
362 | #elif ECB_CPP11 |
|
|
363 | #define ecb_noreturn [[noreturn]] |
|
|
364 | #elif _MSC_VER >= 1200 |
|
|
365 | #define ecb_noreturn __declspec (noreturn) |
362 | #else |
366 | #else |
363 | #define ecb_noreturn ecb_attribute ((__noreturn__)) |
367 | #define ecb_noreturn ecb_attribute ((__noreturn__)) |
364 | #endif |
368 | #endif |
365 | |
369 | |
366 | #if ECB_GCC_VERSION(4,3) |
370 | #if ECB_GCC_VERSION(4,3) |
… | |
… | |
393 | #define ecb_ctz32(x) __builtin_ctz (x) |
397 | #define ecb_ctz32(x) __builtin_ctz (x) |
394 | #define ecb_ctz64(x) __builtin_ctzll (x) |
398 | #define ecb_ctz64(x) __builtin_ctzll (x) |
395 | #define ecb_popcount32(x) __builtin_popcount (x) |
399 | #define ecb_popcount32(x) __builtin_popcount (x) |
396 | /* no popcountll */ |
400 | /* no popcountll */ |
397 | #else |
401 | #else |
398 | ecb_function_ int ecb_ctz32 (uint32_t x) ecb_const; |
402 | ecb_function_ ecb_const int ecb_ctz32 (uint32_t x); |
399 | ecb_function_ int |
403 | ecb_function_ ecb_const int |
400 | ecb_ctz32 (uint32_t x) |
404 | ecb_ctz32 (uint32_t x) |
401 | { |
405 | { |
402 | int r = 0; |
406 | int r = 0; |
403 | |
407 | |
404 | x &= ~x + 1; /* this isolates the lowest bit */ |
408 | x &= ~x + 1; /* this isolates the lowest bit */ |
… | |
… | |
418 | #endif |
422 | #endif |
419 | |
423 | |
420 | return r; |
424 | return r; |
421 | } |
425 | } |
422 | |
426 | |
423 | ecb_function_ int ecb_ctz64 (uint64_t x) ecb_const; |
427 | ecb_function_ ecb_const int ecb_ctz64 (uint64_t x); |
424 | ecb_function_ int |
428 | ecb_function_ ecb_const int |
425 | ecb_ctz64 (uint64_t x) |
429 | ecb_ctz64 (uint64_t x) |
426 | { |
430 | { |
427 | int shift = x & 0xffffffffU ? 0 : 32; |
431 | int shift = x & 0xffffffffU ? 0 : 32; |
428 | return ecb_ctz32 (x >> shift) + shift; |
432 | return ecb_ctz32 (x >> shift) + shift; |
429 | } |
433 | } |
430 | |
434 | |
431 | ecb_function_ int ecb_popcount32 (uint32_t x) ecb_const; |
435 | ecb_function_ ecb_const int ecb_popcount32 (uint32_t x); |
432 | ecb_function_ int |
436 | ecb_function_ ecb_const int |
433 | ecb_popcount32 (uint32_t x) |
437 | ecb_popcount32 (uint32_t x) |
434 | { |
438 | { |
435 | x -= (x >> 1) & 0x55555555; |
439 | x -= (x >> 1) & 0x55555555; |
436 | x = ((x >> 2) & 0x33333333) + (x & 0x33333333); |
440 | x = ((x >> 2) & 0x33333333) + (x & 0x33333333); |
437 | x = ((x >> 4) + x) & 0x0f0f0f0f; |
441 | x = ((x >> 4) + x) & 0x0f0f0f0f; |
438 | x *= 0x01010101; |
442 | x *= 0x01010101; |
439 | |
443 | |
440 | return x >> 24; |
444 | return x >> 24; |
441 | } |
445 | } |
442 | |
446 | |
443 | ecb_function_ int ecb_ld32 (uint32_t x) ecb_const; |
447 | ecb_function_ ecb_const int ecb_ld32 (uint32_t x); |
444 | ecb_function_ int ecb_ld32 (uint32_t x) |
448 | ecb_function_ ecb_const int ecb_ld32 (uint32_t x) |
445 | { |
449 | { |
446 | int r = 0; |
450 | int r = 0; |
447 | |
451 | |
448 | if (x >> 16) { x >>= 16; r += 16; } |
452 | if (x >> 16) { x >>= 16; r += 16; } |
449 | if (x >> 8) { x >>= 8; r += 8; } |
453 | if (x >> 8) { x >>= 8; r += 8; } |
… | |
… | |
452 | if (x >> 1) { r += 1; } |
456 | if (x >> 1) { r += 1; } |
453 | |
457 | |
454 | return r; |
458 | return r; |
455 | } |
459 | } |
456 | |
460 | |
457 | ecb_function_ int ecb_ld64 (uint64_t x) ecb_const; |
461 | ecb_function_ ecb_const int ecb_ld64 (uint64_t x); |
458 | ecb_function_ int ecb_ld64 (uint64_t x) |
462 | ecb_function_ ecb_const int ecb_ld64 (uint64_t x) |
459 | { |
463 | { |
460 | int r = 0; |
464 | int r = 0; |
461 | |
465 | |
462 | if (x >> 32) { x >>= 32; r += 32; } |
466 | if (x >> 32) { x >>= 32; r += 32; } |
463 | |
467 | |
464 | return r + ecb_ld32 (x); |
468 | return r + ecb_ld32 (x); |
465 | } |
469 | } |
466 | #endif |
470 | #endif |
467 | |
471 | |
468 | ecb_function_ ecb_bool ecb_is_pot32 (uint32_t x) ecb_const; |
472 | ecb_function_ ecb_const ecb_bool ecb_is_pot32 (uint32_t x); |
469 | ecb_function_ ecb_bool ecb_is_pot32 (uint32_t x) { return !(x & (x - 1)); } |
473 | ecb_function_ ecb_const ecb_bool ecb_is_pot32 (uint32_t x) { return !(x & (x - 1)); } |
470 | ecb_function_ ecb_bool ecb_is_pot64 (uint64_t x) ecb_const; |
474 | ecb_function_ ecb_const ecb_bool ecb_is_pot64 (uint64_t x); |
471 | ecb_function_ ecb_bool ecb_is_pot64 (uint64_t x) { return !(x & (x - 1)); } |
475 | ecb_function_ ecb_const ecb_bool ecb_is_pot64 (uint64_t x) { return !(x & (x - 1)); } |
472 | |
476 | |
473 | ecb_function_ uint8_t ecb_bitrev8 (uint8_t x) ecb_const; |
477 | ecb_function_ ecb_const uint8_t ecb_bitrev8 (uint8_t x); |
474 | ecb_function_ uint8_t ecb_bitrev8 (uint8_t x) |
478 | ecb_function_ ecb_const uint8_t ecb_bitrev8 (uint8_t x) |
475 | { |
479 | { |
476 | return ( (x * 0x0802U & 0x22110U) |
480 | return ( (x * 0x0802U & 0x22110U) |
477 | | (x * 0x8020U & 0x88440U)) * 0x10101U >> 16; |
481 | | (x * 0x8020U & 0x88440U)) * 0x10101U >> 16; |
478 | } |
482 | } |
479 | |
483 | |
480 | ecb_function_ uint16_t ecb_bitrev16 (uint16_t x) ecb_const; |
484 | ecb_function_ ecb_const uint16_t ecb_bitrev16 (uint16_t x); |
481 | ecb_function_ uint16_t ecb_bitrev16 (uint16_t x) |
485 | ecb_function_ ecb_const uint16_t ecb_bitrev16 (uint16_t x) |
482 | { |
486 | { |
483 | x = ((x >> 1) & 0x5555) | ((x & 0x5555) << 1); |
487 | x = ((x >> 1) & 0x5555) | ((x & 0x5555) << 1); |
484 | x = ((x >> 2) & 0x3333) | ((x & 0x3333) << 2); |
488 | x = ((x >> 2) & 0x3333) | ((x & 0x3333) << 2); |
485 | x = ((x >> 4) & 0x0f0f) | ((x & 0x0f0f) << 4); |
489 | x = ((x >> 4) & 0x0f0f) | ((x & 0x0f0f) << 4); |
486 | x = ( x >> 8 ) | ( x << 8); |
490 | x = ( x >> 8 ) | ( x << 8); |
487 | |
491 | |
488 | return x; |
492 | return x; |
489 | } |
493 | } |
490 | |
494 | |
491 | ecb_function_ uint32_t ecb_bitrev32 (uint32_t x) ecb_const; |
495 | ecb_function_ ecb_const uint32_t ecb_bitrev32 (uint32_t x); |
492 | ecb_function_ uint32_t ecb_bitrev32 (uint32_t x) |
496 | ecb_function_ ecb_const uint32_t ecb_bitrev32 (uint32_t x) |
493 | { |
497 | { |
494 | x = ((x >> 1) & 0x55555555) | ((x & 0x55555555) << 1); |
498 | x = ((x >> 1) & 0x55555555) | ((x & 0x55555555) << 1); |
495 | x = ((x >> 2) & 0x33333333) | ((x & 0x33333333) << 2); |
499 | x = ((x >> 2) & 0x33333333) | ((x & 0x33333333) << 2); |
496 | x = ((x >> 4) & 0x0f0f0f0f) | ((x & 0x0f0f0f0f) << 4); |
500 | x = ((x >> 4) & 0x0f0f0f0f) | ((x & 0x0f0f0f0f) << 4); |
497 | x = ((x >> 8) & 0x00ff00ff) | ((x & 0x00ff00ff) << 8); |
501 | x = ((x >> 8) & 0x00ff00ff) | ((x & 0x00ff00ff) << 8); |
… | |
… | |
500 | return x; |
504 | return x; |
501 | } |
505 | } |
502 | |
506 | |
503 | /* popcount64 is only available on 64 bit cpus as gcc builtin */ |
507 | /* popcount64 is only available on 64 bit cpus as gcc builtin */ |
504 | /* so for this version we are lazy */ |
508 | /* so for this version we are lazy */ |
505 | ecb_function_ int ecb_popcount64 (uint64_t x) ecb_const; |
509 | ecb_function_ ecb_const int ecb_popcount64 (uint64_t x); |
506 | ecb_function_ int |
510 | ecb_function_ ecb_const int |
507 | ecb_popcount64 (uint64_t x) |
511 | ecb_popcount64 (uint64_t x) |
508 | { |
512 | { |
509 | return ecb_popcount32 (x) + ecb_popcount32 (x >> 32); |
513 | return ecb_popcount32 (x) + ecb_popcount32 (x >> 32); |
510 | } |
514 | } |
511 | |
515 | |
512 | ecb_inline uint8_t ecb_rotl8 (uint8_t x, unsigned int count) ecb_const; |
516 | ecb_inline ecb_const uint8_t ecb_rotl8 (uint8_t x, unsigned int count); |
513 | ecb_inline uint8_t ecb_rotr8 (uint8_t x, unsigned int count) ecb_const; |
517 | ecb_inline ecb_const uint8_t ecb_rotr8 (uint8_t x, unsigned int count); |
514 | ecb_inline uint16_t ecb_rotl16 (uint16_t x, unsigned int count) ecb_const; |
518 | ecb_inline ecb_const uint16_t ecb_rotl16 (uint16_t x, unsigned int count); |
515 | ecb_inline uint16_t ecb_rotr16 (uint16_t x, unsigned int count) ecb_const; |
519 | ecb_inline ecb_const uint16_t ecb_rotr16 (uint16_t x, unsigned int count); |
516 | ecb_inline uint32_t ecb_rotl32 (uint32_t x, unsigned int count) ecb_const; |
520 | ecb_inline ecb_const uint32_t ecb_rotl32 (uint32_t x, unsigned int count); |
517 | ecb_inline uint32_t ecb_rotr32 (uint32_t x, unsigned int count) ecb_const; |
521 | ecb_inline ecb_const uint32_t ecb_rotr32 (uint32_t x, unsigned int count); |
518 | ecb_inline uint64_t ecb_rotl64 (uint64_t x, unsigned int count) ecb_const; |
522 | ecb_inline ecb_const uint64_t ecb_rotl64 (uint64_t x, unsigned int count); |
519 | ecb_inline uint64_t ecb_rotr64 (uint64_t x, unsigned int count) ecb_const; |
523 | ecb_inline ecb_const uint64_t ecb_rotr64 (uint64_t x, unsigned int count); |
520 | |
524 | |
521 | ecb_inline uint8_t ecb_rotl8 (uint8_t x, unsigned int count) { return (x >> ( 8 - count)) | (x << count); } |
525 | ecb_inline ecb_const uint8_t ecb_rotl8 (uint8_t x, unsigned int count) { return (x >> ( 8 - count)) | (x << count); } |
522 | ecb_inline uint8_t ecb_rotr8 (uint8_t x, unsigned int count) { return (x << ( 8 - count)) | (x >> count); } |
526 | ecb_inline ecb_const uint8_t ecb_rotr8 (uint8_t x, unsigned int count) { return (x << ( 8 - count)) | (x >> count); } |
523 | ecb_inline uint16_t ecb_rotl16 (uint16_t x, unsigned int count) { return (x >> (16 - count)) | (x << count); } |
527 | ecb_inline ecb_const uint16_t ecb_rotl16 (uint16_t x, unsigned int count) { return (x >> (16 - count)) | (x << count); } |
524 | ecb_inline uint16_t ecb_rotr16 (uint16_t x, unsigned int count) { return (x << (16 - count)) | (x >> count); } |
528 | ecb_inline ecb_const uint16_t ecb_rotr16 (uint16_t x, unsigned int count) { return (x << (16 - count)) | (x >> count); } |
525 | ecb_inline uint32_t ecb_rotl32 (uint32_t x, unsigned int count) { return (x >> (32 - count)) | (x << count); } |
529 | ecb_inline ecb_const uint32_t ecb_rotl32 (uint32_t x, unsigned int count) { return (x >> (32 - count)) | (x << count); } |
526 | ecb_inline uint32_t ecb_rotr32 (uint32_t x, unsigned int count) { return (x << (32 - count)) | (x >> count); } |
530 | ecb_inline ecb_const uint32_t ecb_rotr32 (uint32_t x, unsigned int count) { return (x << (32 - count)) | (x >> count); } |
527 | ecb_inline uint64_t ecb_rotl64 (uint64_t x, unsigned int count) { return (x >> (64 - count)) | (x << count); } |
531 | ecb_inline ecb_const uint64_t ecb_rotl64 (uint64_t x, unsigned int count) { return (x >> (64 - count)) | (x << count); } |
528 | ecb_inline uint64_t ecb_rotr64 (uint64_t x, unsigned int count) { return (x << (64 - count)) | (x >> count); } |
532 | ecb_inline ecb_const uint64_t ecb_rotr64 (uint64_t x, unsigned int count) { return (x << (64 - count)) | (x >> count); } |
529 | |
533 | |
530 | #if ECB_GCC_VERSION(4,3) || (ECB_CLANG_BUILTIN(__builtin_bswap32) && ECB_CLANG_BUILTIN(__builtin_bswap64)) |
534 | #if ECB_GCC_VERSION(4,3) || (ECB_CLANG_BUILTIN(__builtin_bswap32) && ECB_CLANG_BUILTIN(__builtin_bswap64)) |
531 | #define ecb_bswap16(x) (__builtin_bswap32 (x) >> 16) |
535 | #define ecb_bswap16(x) (__builtin_bswap32 (x) >> 16) |
532 | #define ecb_bswap32(x) __builtin_bswap32 (x) |
536 | #define ecb_bswap32(x) __builtin_bswap32 (x) |
533 | #define ecb_bswap64(x) __builtin_bswap64 (x) |
537 | #define ecb_bswap64(x) __builtin_bswap64 (x) |
534 | #else |
538 | #else |
535 | ecb_function_ uint16_t ecb_bswap16 (uint16_t x) ecb_const; |
539 | ecb_function_ ecb_const uint16_t ecb_bswap16 (uint16_t x); |
536 | ecb_function_ uint16_t |
540 | ecb_function_ ecb_const uint16_t |
537 | ecb_bswap16 (uint16_t x) |
541 | ecb_bswap16 (uint16_t x) |
538 | { |
542 | { |
539 | return ecb_rotl16 (x, 8); |
543 | return ecb_rotl16 (x, 8); |
540 | } |
544 | } |
541 | |
545 | |
542 | ecb_function_ uint32_t ecb_bswap32 (uint32_t x) ecb_const; |
546 | ecb_function_ ecb_const uint32_t ecb_bswap32 (uint32_t x); |
543 | ecb_function_ uint32_t |
547 | ecb_function_ ecb_const uint32_t |
544 | ecb_bswap32 (uint32_t x) |
548 | ecb_bswap32 (uint32_t x) |
545 | { |
549 | { |
546 | return (((uint32_t)ecb_bswap16 (x)) << 16) | ecb_bswap16 (x >> 16); |
550 | return (((uint32_t)ecb_bswap16 (x)) << 16) | ecb_bswap16 (x >> 16); |
547 | } |
551 | } |
548 | |
552 | |
549 | ecb_function_ uint64_t ecb_bswap64 (uint64_t x) ecb_const; |
553 | ecb_function_ ecb_const uint64_t ecb_bswap64 (uint64_t x); |
550 | ecb_function_ uint64_t |
554 | ecb_function_ ecb_const uint64_t |
551 | ecb_bswap64 (uint64_t x) |
555 | ecb_bswap64 (uint64_t x) |
552 | { |
556 | { |
553 | return (((uint64_t)ecb_bswap32 (x)) << 32) | ecb_bswap32 (x >> 32); |
557 | return (((uint64_t)ecb_bswap32 (x)) << 32) | ecb_bswap32 (x >> 32); |
554 | } |
558 | } |
555 | #endif |
559 | #endif |
556 | |
560 | |
557 | #if ECB_GCC_VERSION(4,5) || ECB_CLANG_BUILTIN(__builtin_unreachable) |
561 | #if ECB_GCC_VERSION(4,5) || ECB_CLANG_BUILTIN(__builtin_unreachable) |
558 | #define ecb_unreachable() __builtin_unreachable () |
562 | #define ecb_unreachable() __builtin_unreachable () |
559 | #else |
563 | #else |
560 | /* this seems to work fine, but gcc always emits a warning for it :/ */ |
564 | /* this seems to work fine, but gcc always emits a warning for it :/ */ |
561 | ecb_inline void ecb_unreachable (void) ecb_noreturn; |
565 | ecb_inline ecb_noreturn void ecb_unreachable (void); |
562 | ecb_inline void ecb_unreachable (void) { } |
566 | ecb_inline ecb_noreturn void ecb_unreachable (void) { } |
563 | #endif |
567 | #endif |
564 | |
568 | |
565 | /* try to tell the compiler that some condition is definitely true */ |
569 | /* try to tell the compiler that some condition is definitely true */ |
566 | #define ecb_assume(cond) if (!(cond)) ecb_unreachable (); else 0 |
570 | #define ecb_assume(cond) if (!(cond)) ecb_unreachable (); else 0 |
567 | |
571 | |
568 | ecb_inline unsigned char ecb_byteorder_helper (void) ecb_const; |
572 | ecb_inline ecb_const unsigned char ecb_byteorder_helper (void); |
569 | ecb_inline unsigned char |
573 | ecb_inline ecb_const unsigned char |
570 | ecb_byteorder_helper (void) |
574 | ecb_byteorder_helper (void) |
571 | { |
575 | { |
572 | /* the union code still generates code under pressure in gcc, */ |
576 | /* the union code still generates code under pressure in gcc, */ |
573 | /* but less than using pointers, and always seems to */ |
577 | /* but less than using pointers, and always seems to */ |
574 | /* successfully return a constant. */ |
578 | /* successfully return a constant. */ |
… | |
… | |
589 | } u = { 0x11223344 }; |
593 | } u = { 0x11223344 }; |
590 | return u.c; |
594 | return u.c; |
591 | #endif |
595 | #endif |
592 | } |
596 | } |
593 | |
597 | |
594 | ecb_inline ecb_bool ecb_big_endian (void) ecb_const; |
598 | ecb_inline ecb_const ecb_bool ecb_big_endian (void); |
595 | ecb_inline ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11; } |
599 | ecb_inline ecb_const ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11; } |
596 | ecb_inline ecb_bool ecb_little_endian (void) ecb_const; |
600 | ecb_inline ecb_const ecb_bool ecb_little_endian (void); |
597 | ecb_inline ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44; } |
601 | ecb_inline ecb_const ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44; } |
598 | |
602 | |
599 | #if ECB_GCC_VERSION(3,0) || ECB_C99 |
603 | #if ECB_GCC_VERSION(3,0) || ECB_C99 |
600 | #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0)) |
604 | #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0)) |
601 | #else |
605 | #else |
602 | #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n))) |
606 | #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n))) |
… | |
… | |
671 | #else |
675 | #else |
672 | #define ECB_NAN ECB_INFINITY |
676 | #define ECB_NAN ECB_INFINITY |
673 | #endif |
677 | #endif |
674 | |
678 | |
675 | #if ECB_C99 || _XOPEN_VERSION >= 600 || _POSIX_VERSION >= 200112L |
679 | #if ECB_C99 || _XOPEN_VERSION >= 600 || _POSIX_VERSION >= 200112L |
676 | #define ecb_ldexpf(x,e) ldexpf (x, e) |
680 | #define ecb_ldexpf(x,e) ldexpf ((x), (e)) |
677 | #else |
681 | #else |
678 | #define ecb_ldexpf(x,e) (float) ldexp ((double)(x), e) |
682 | #define ecb_ldexpf(x,e) (float) ldexp ((x), (e)) |
679 | #endif |
683 | #endif |
680 | |
684 | |
681 | /* converts an ieee half/binary16 to a float */ |
685 | /* converts an ieee half/binary16 to a float */ |
682 | ecb_function_ float ecb_binary16_to_float (uint16_t x) ecb_const; |
686 | ecb_function_ ecb_const float ecb_binary16_to_float (uint16_t x); |
683 | ecb_function_ float |
687 | ecb_function_ ecb_const float |
684 | ecb_binary16_to_float (uint16_t x) |
688 | ecb_binary16_to_float (uint16_t x) |
685 | { |
689 | { |
686 | int e = (x >> 10) & 0x1f; |
690 | int e = (x >> 10) & 0x1f; |
687 | int m = x & 0x3ff; |
691 | int m = x & 0x3ff; |
688 | float r; |
692 | float r; |
… | |
… | |
694 | |
698 | |
695 | return x & 0x8000 ? -r : r; |
699 | return x & 0x8000 ? -r : r; |
696 | } |
700 | } |
697 | |
701 | |
698 | /* convert a float to ieee single/binary32 */ |
702 | /* convert a float to ieee single/binary32 */ |
699 | ecb_function_ uint32_t ecb_float_to_binary32 (float x) ecb_const; |
703 | ecb_function_ ecb_const uint32_t ecb_float_to_binary32 (float x); |
700 | ecb_function_ uint32_t |
704 | ecb_function_ ecb_const uint32_t |
701 | ecb_float_to_binary32 (float x) |
705 | ecb_float_to_binary32 (float x) |
702 | { |
706 | { |
703 | uint32_t r; |
707 | uint32_t r; |
704 | |
708 | |
705 | #if ECB_STDFP |
709 | #if ECB_STDFP |
… | |
… | |
734 | |
738 | |
735 | return r; |
739 | return r; |
736 | } |
740 | } |
737 | |
741 | |
738 | /* converts an ieee single/binary32 to a float */ |
742 | /* converts an ieee single/binary32 to a float */ |
739 | ecb_function_ float ecb_binary32_to_float (uint32_t x) ecb_const; |
743 | ecb_function_ ecb_const float ecb_binary32_to_float (uint32_t x); |
740 | ecb_function_ float |
744 | ecb_function_ ecb_const float |
741 | ecb_binary32_to_float (uint32_t x) |
745 | ecb_binary32_to_float (uint32_t x) |
742 | { |
746 | { |
743 | float r; |
747 | float r; |
744 | |
748 | |
745 | #if ECB_STDFP |
749 | #if ECB_STDFP |
… | |
… | |
764 | |
768 | |
765 | return r; |
769 | return r; |
766 | } |
770 | } |
767 | |
771 | |
768 | /* convert a double to ieee double/binary64 */ |
772 | /* convert a double to ieee double/binary64 */ |
769 | ecb_function_ uint64_t ecb_double_to_binary64 (double x) ecb_const; |
773 | ecb_function_ ecb_const uint64_t ecb_double_to_binary64 (double x); |
770 | ecb_function_ uint64_t |
774 | ecb_function_ ecb_const uint64_t |
771 | ecb_double_to_binary64 (double x) |
775 | ecb_double_to_binary64 (double x) |
772 | { |
776 | { |
773 | uint64_t r; |
777 | uint64_t r; |
774 | |
778 | |
775 | #if ECB_STDFP |
779 | #if ECB_STDFP |
… | |
… | |
804 | |
808 | |
805 | return r; |
809 | return r; |
806 | } |
810 | } |
807 | |
811 | |
808 | /* converts an ieee double/binary64 to a double */ |
812 | /* converts an ieee double/binary64 to a double */ |
809 | ecb_function_ double ecb_binary64_to_double (uint64_t x) ecb_const; |
813 | ecb_function_ ecb_const double ecb_binary64_to_double (uint64_t x); |
810 | ecb_function_ double |
814 | ecb_function_ ecb_const double |
811 | ecb_binary64_to_double (uint64_t x) |
815 | ecb_binary64_to_double (uint64_t x) |
812 | { |
816 | { |
813 | double r; |
817 | double r; |
814 | |
818 | |
815 | #if ECB_STDFP |
819 | #if ECB_STDFP |