Ticket #22: BaseFunctions_ByteOrder.diff
| File BaseFunctions_ByteOrder.diff, 26.3 kB (added by cneumann, 2 years ago) |
|---|
-
Source/Base/Base/OSGBaseFunctions.h
old new 256 256 inline 257 257 bool osgIsBigEndian(void); 258 258 259 // The osgHostTo??? and osg???ToHost templates are NOT implemented, there are 260 // only overloads for some types available, these are: 261 // UInt16, UInt32, UInt64, Real32, Real64, Real128 262 263 template <class TypeT> 264 TypeT osgHostToBigEndian(TypeT src); 265 266 template <class TypeT> 267 TypeT osgHostToLittleEndian(TypeT src); 268 269 template <class TypeT> 270 TypeT osgBigEndianToHost(TypeT src); 271 272 template <class TypeT> 273 TypeT osgLittleEndianToHost(TypeT src); 274 259 275 // host to network 260 276 261 277 inline 262 UInt16 osghtons(UInt16 src);278 UInt16 osghtons (UInt16 src); 263 279 264 280 inline 265 UInt32 osghtonl(UInt32 src);281 UInt32 osghtonl (UInt32 src); 266 282 267 283 inline 268 UInt64 osghtonll(UInt64 src);284 UInt64 osghtonll(UInt64 src); 269 285 270 286 inline 271 Real32 osghtonf(Real32 src);287 Real32 osghtonf (Real32 src); 272 288 273 289 inline 274 Real64 osghtond(Real64 src);290 Real64 osghtond (Real64 src); 275 291 276 292 inline 277 293 Real128 osghtondd(Real128 src); … … 279 295 // network to host 280 296 281 297 inline 282 UInt16 osgntohs (UInt16 src);298 UInt16 osgntohs (UInt16 src); 283 299 284 300 inline 285 UInt32 osgntohl (UInt32 src);301 UInt32 osgntohl (UInt32 src); 286 302 287 303 inline 288 UInt64 osgntohll(UInt64 src);304 UInt64 osgntohll(UInt64 src); 289 305 290 306 inline 291 Real32 osgntohf (Real32 src);307 Real32 osgntohf (Real32 src); 292 308 293 309 inline 294 Real64 osgntohd (Real64 src);310 Real64 osgntohd (Real64 src); 295 311 296 312 inline 297 Real128 osgntohd (Real128 src);313 Real128 osgntohdd(Real128 src); 298 314 299 315 /*---------------------------------------------------------------------*/ 300 316 /* Case String Runtime Functions */ -
Source/Base/Base/OSGBaseFunctions.inl
old new 1575 1575 return (BYTE_ORDER) == (BIG_ENDIAN); 1576 1576 } 1577 1577 1578 /*! Convert a UInt16 from host byte order to big endian byte order. 1579 1580 \param[in] src Input value in host byte order. 1581 \return The input converted to big endian byte order. 1582 1583 \note An actual conversion only happens on little endian architectures. 1584 1585 \ingroup GrpBaseBaseMiscFn 1586 */ 1587 inline 1588 UInt16 osgHostToBigEndian(UInt16 src) 1589 { 1578 1590 #if BYTE_ORDER == LITTLE_ENDIAN 1591 return (src >> 8) | (src << 8); 1592 #else 1593 return src; 1594 #endif 1595 } 1579 1596 1580 // host to network 1597 /*! Convert a UInt16 from host byte order to little endian byte order. 1581 1598 1582 /*! Convert a UInt16 from host byte order to network byte order.1583 1584 1599 \param[in] src Input value in host byte order. 1585 \return The input converted to networkbyte order.1600 \return The input converted to little endian byte order. 1586 1601 1587 \note Network byte order is big endian, so an actual conversion only 1588 happens on little endian architectures. 1602 \note An actual conversion only happens on big endian architectures. 1589 1603 1590 1604 \ingroup GrpBaseBaseMiscFn 1591 1605 */ 1592 1606 inline 1593 UInt16 osg htons(UInt16 src)1607 UInt16 osgHostToLittleEndian(UInt16 src) 1594 1608 { 1609 #if BYTE_ORDER == LITTLE_ENDIAN 1610 return src; 1611 #else 1595 1612 return (src >> 8) | (src << 8); 1613 #endif 1596 1614 } 1597 1615 1598 /*! Convert a UInt 32 from host byte order to networkbyte order.1616 /*! Convert a UInt16 from big endian byte order to host byte order. 1599 1617 1600 \param[in] src Input value in hostbyte order.1601 \return The input converted to networkbyte order.1618 \param[in] src Input value in big endian byte order. 1619 \return The input converted to host byte order. 1602 1620 1603 \note Network byte order is big endian, so an actual conversion only 1604 happens on little endian architectures. 1621 \note An actual conversion only happens on little endian architectures. 1605 1622 1606 1623 \ingroup GrpBaseBaseMiscFn 1607 1624 */ 1608 1625 inline 1609 UInt 32 osghtonl(UInt32src)1626 UInt16 osgBigEndianToHost(UInt16 src) 1610 1627 { 1611 return ((src&0x000000ff) << 24) | 1612 ((src&0x0000ff00) << 8 ) | 1613 ((src&0x00ff0000) >> 8 ) | 1614 ((src&0xff000000) >> 24); 1628 #if BYTE_ORDER == LITTLE_ENDIAN 1629 return (src >> 8) | (src << 8); 1630 #else 1631 return src; 1632 #endif 1615 1633 } 1616 1634 1617 /*! Convert a UInt 64 from host byte order to networkbyte order.1635 /*! Convert a UInt16 from little endian byte order to host byte order. 1618 1636 1637 \param[in] src Input value in little endian byte order. 1638 \return The input converted to host byte order. 1639 1640 \note An actual conversion only happens on big endian architectures. 1641 1642 \ingroup GrpBaseBaseMiscFn 1643 */ 1644 inline 1645 UInt16 osgLittleEndianToHost(UInt16 src) 1646 { 1647 #if BYTE_ORDER == LITTLE_ENDIAN 1648 return src; 1649 #else 1650 return (src >> 8) | (src << 8); 1651 #endif 1652 } 1653 1654 /*! Convert a UInt32 from host byte order to big endian byte order. 1655 1619 1656 \param[in] src Input value in host byte order. 1620 \return The input converted to networkbyte order.1657 \return The input converted to big endian byte order. 1621 1658 1622 \note Network byte order is big endian, so an actual conversion only 1623 happens on little endian architectures. 1659 \note An actual conversion only happens on little endian architectures. 1624 1660 1625 1661 \ingroup GrpBaseBaseMiscFn 1626 1662 */ 1627 1663 inline 1628 UInt 64 osghtonll(UInt64src)1664 UInt32 osgHostToBigEndian(UInt32 src) 1629 1665 { 1630 #ifdef OSG_LONGLONG_HAS_LL 1631 return ((src&0x00000000000000ffLL) << 56) | 1632 ((src&0x000000000000ff00LL) << 40) | 1633 ((src&0x0000000000ff0000LL) << 24) | 1634 ((src&0x00000000ff000000LL) << 8 ) | 1635 ((src&0x000000ff00000000LL) >> 8 ) | 1636 ((src&0x0000ff0000000000LL) >> 24) | 1637 ((src&0x00ff000000000000LL) >> 40) | 1638 ((src&0xff00000000000000LL) >> 56); 1666 #if BYTE_ORDER == LITTLE_ENDIAN 1667 return ((src & 0x000000ff) << 24) | 1668 ((src & 0x0000ff00) << 8 ) | 1669 ((src & 0x00ff0000) >> 8 ) | 1670 ((src & 0xff000000) >> 24); 1639 1671 #else 1640 return ((src&0x00000000000000ff) << 56) | 1641 ((src&0x000000000000ff00) << 40) | 1642 ((src&0x0000000000ff0000) << 24) | 1643 ((src&0x00000000ff000000) << 8 ) | 1644 ((src&0x000000ff00000000) >> 8 ) | 1645 ((src&0x0000ff0000000000) >> 24) | 1646 ((src&0x00ff000000000000) >> 40) | 1647 ((src&0xff00000000000000) >> 56); 1672 return src; 1648 1673 #endif 1649 1674 } 1650 1675 1651 /*! Convert a Real32 from host byte order to networkbyte order.1676 /*! Convert a UInt32 from host byte order to little endian byte order. 1652 1677 1653 1678 \param[in] src Input value in host byte order. 1654 \return The input converted to networkbyte order.1679 \return The input converted to little endian byte order. 1655 1680 1656 \note Network byte order is big endian, so an actual conversion only 1657 happens on little endian architectures. 1681 \note An actual conversion only happens on big endian architectures. 1658 1682 1659 1683 \ingroup GrpBaseBaseMiscFn 1660 1684 */ 1661 1685 inline 1662 Real32 osghtonf(Real32 src) 1686 UInt32 osgHostToLittleEndian(UInt32 src) 1663 1687 { 1664 UInt8 *p = (UInt8 *) &src; 1688 #if BYTE_ORDER == LITTLE_ENDIAN 1689 return src; 1690 #else 1691 return ((src & 0x000000ff) << 24) | 1692 ((src & 0x0000ff00) << 8 ) | 1693 ((src & 0x00ff0000) >> 8 ) | 1694 ((src & 0xff000000) >> 24); 1695 #endif 1696 } 1665 1697 1666 std::swap(p[0], p[3]); 1667 std::swap(p[1], p[2]); 1698 /*! Convert a UInt32 from big endian byte order to host byte order. 1668 1699 1700 \param[in] src Input value in big endian byte order. 1701 \return The input converted to host byte order. 1702 1703 \note An actual conversion only happens on little endian architectures. 1704 1705 \ingroup GrpBaseBaseMiscFn 1706 */ 1707 inline 1708 UInt32 osgBigEndianToHost(UInt32 src) 1709 { 1710 #if BYTE_ORDER == LITTLE_ENDIAN 1711 return ((src & 0x000000ff) << 24) | 1712 ((src & 0x0000ff00) << 8 ) | 1713 ((src & 0x00ff0000) >> 8 ) | 1714 ((src & 0xff000000) >> 24); 1715 #else 1669 1716 return src; 1717 #endif 1670 1718 } 1671 1719 1672 /*! Convert a Real64 from host byte order to networkbyte order.1720 /*! Convert a UInt32 from little endian byte order to host byte order. 1673 1721 1722 \param[in] src Input value in little endian byte order. 1723 \return The input converted to host byte order. 1724 1725 \note An actual conversion only happens on big endian architectures. 1726 1727 \ingroup GrpBaseBaseMiscFn 1728 */ 1729 inline 1730 UInt32 osgLittleEndianToHost(UInt32 src) 1731 { 1732 #if BYTE_ORDER == LITTLE_ENDIAN 1733 return src; 1734 #else 1735 return ((src & 0x000000ff) << 24) | 1736 ((src & 0x0000ff00) << 8 ) | 1737 ((src & 0x00ff0000) >> 8 ) | 1738 ((src & 0xff000000) >> 24); 1739 #endif 1740 } 1741 1742 // define some macros for long long int literals (these are undef'ed below) 1743 // XXX should these go into OSGBaseTypes.h ?? (cneumann) 1744 #ifdef OSG_LONGLONG_HAS_LL 1745 1746 #define OSG_UINT64_LITERAL(value) \ 1747 value##ULL 1748 1749 #define OSG_INT64_LITERAL(value) \ 1750 value##LL 1751 1752 #else 1753 1754 #define OSG_UINT64_LITERAL(value) \ 1755 value##U 1756 1757 #define OSG_INT64_LITERAL(value) \ 1758 value 1759 1760 #endif 1761 1762 /*! Convert a UInt64 from host byte order to big endian byte order. 1763 1674 1764 \param[in] src Input value in host byte order. 1675 \return The input converted to networkbyte order.1765 \return The input converted to big endian byte order. 1676 1766 1677 \note Network byte order is big endian, so an actual conversion only 1678 happens on little endian architectures. 1767 \note An actual conversion only happens on little endian architectures. 1679 1768 1680 1769 \ingroup GrpBaseBaseMiscFn 1681 1770 */ 1682 1771 inline 1683 Real64 osghtond(Real64 src) 1772 UInt64 osgHostToBigEndian(UInt64 src) 1684 1773 { 1685 UInt8 *p = (UInt8 *) &src; 1774 #if BYTE_ORDER == LITTLE_ENDIAN 1775 return ((src & OSG_UINT64_LITERAL(0x00000000000000ff)) << 56) | 1776 ((src & OSG_UINT64_LITERAL(0x000000000000ff00)) << 40) | 1777 ((src & OSG_UINT64_LITERAL(0x0000000000ff0000)) << 24) | 1778 ((src & OSG_UINT64_LITERAL(0x00000000ff000000)) << 8 ) | 1779 ((src & OSG_UINT64_LITERAL(0x000000ff00000000)) >> 8 ) | 1780 ((src & OSG_UINT64_LITERAL(0x0000ff0000000000)) >> 24) | 1781 ((src & OSG_UINT64_LITERAL(0x00ff000000000000)) >> 40) | 1782 ((src & OSG_UINT64_LITERAL(0xff00000000000000)) >> 56); 1783 #else 1784 return src; 1785 #endif 1786 } 1686 1787 1687 std::swap(p[0], p[7]); 1688 std::swap(p[1], p[6]); 1689 std::swap(p[2], p[5]); 1690 std::swap(p[3], p[4]); 1788 /*! Convert a UInt64 from host byte order to little endian byte order. 1691 1789 1790 \param[in] src Input value in host byte order. 1791 \return The input converted to little endian byte order. 1792 1793 \note An actual conversion only happens on big endian architectures. 1794 1795 \ingroup GrpBaseBaseMiscFn 1796 */ 1797 inline 1798 UInt64 osgHostToLittleEndian(UInt64 src) 1799 { 1800 #if BYTE_ORDER == LITTLE_ENDIAN 1692 1801 return src; 1802 #else 1803 return ((src & OSG_UINT64_LITERAL(0x00000000000000ff)) << 56) | 1804 ((src & OSG_UINT64_LITERAL(0x000000000000ff00)) << 40) | 1805 ((src & OSG_UINT64_LITERAL(0x0000000000ff0000)) << 24) | 1806 ((src & OSG_UINT64_LITERAL(0x00000000ff000000)) << 8 ) | 1807 ((src & OSG_UINT64_LITERAL(0x000000ff00000000)) >> 8 ) | 1808 ((src & OSG_UINT64_LITERAL(0x0000ff0000000000)) >> 24) | 1809 ((src & OSG_UINT64_LITERAL(0x00ff000000000000)) >> 40) | 1810 ((src & OSG_UINT64_LITERAL(0xff00000000000000)) >> 56); 1811 #endif 1693 1812 } 1694 1813 1695 /*! Convert a Real128 from host byte order to networkbyte order.1814 /*! Convert a UInt64 from big endian byte order to host byte order. 1696 1815 1697 \param[in] src Input value in hostbyte order.1698 \return The input converted to networkbyte order.1816 \param[in] src Input value in big endian byte order. 1817 \return The input converted to host byte order. 1699 1818 1700 \note Network byte order is big endian, so an actual conversion only 1701 happens on little endian architectures. 1819 \note An actual conversion only happens on little endian architectures. 1702 1820 1703 1821 \ingroup GrpBaseBaseMiscFn 1704 1822 */ 1705 1823 inline 1706 Real128 osghtondd(Real128 src) 1824 UInt64 osgBigEndianToHost(UInt64 src) 1707 1825 { 1708 UInt8 *p = (UInt8 *) &src; 1826 #if BYTE_ORDER == LITTLE_ENDIAN 1827 return ((src & OSG_UINT64_LITERAL(0x00000000000000ff)) << 56) | 1828 ((src & OSG_UINT64_LITERAL(0x000000000000ff00)) << 40) | 1829 ((src & OSG_UINT64_LITERAL(0x0000000000ff0000)) << 24) | 1830 ((src & OSG_UINT64_LITERAL(0x00000000ff000000)) << 8 ) | 1831 ((src & OSG_UINT64_LITERAL(0x000000ff00000000)) >> 8 ) | 1832 ((src & OSG_UINT64_LITERAL(0x0000ff0000000000)) >> 24) | 1833 ((src & OSG_UINT64_LITERAL(0x00ff000000000000)) >> 40) | 1834 ((src & OSG_UINT64_LITERAL(0xff00000000000000)) >> 56); 1835 #else 1836 return src; 1837 #endif 1838 } 1709 1839 1710 std::swap(p[0], p[15]); 1711 std::swap(p[1], p[14]); 1712 std::swap(p[2], p[13]); 1713 std::swap(p[3], p[12]); 1714 std::swap(p[4], p[11]); 1715 std::swap(p[5], p[10]); 1716 std::swap(p[6], p[9]); 1717 std::swap(p[7], p[8]); 1840 /*! Convert a UInt64 from little endian byte order to host byte order. 1718 1841 1842 \param[in] src Input value in little endian byte order. 1843 \return The input converted to host byte order. 1844 1845 \note An actual conversion only happens on big endian architectures. 1846 1847 \ingroup GrpBaseBaseMiscFn 1848 */ 1849 inline 1850 UInt64 osgLittleEndianToHost(UInt64 src) 1851 { 1852 #if BYTE_ORDER == LITTLE_ENDIAN 1719 1853 return src; 1854 #else 1855 return ((src & OSG_UINT64_LITERAL(0x00000000000000ff)) << 56) | 1856 ((src & OSG_UINT64_LITERAL(0x000000000000ff00)) << 40) | 1857 ((src & OSG_UINT64_LITERAL(0x0000000000ff0000)) << 24) | 1858 ((src & OSG_UINT64_LITERAL(0x00000000ff000000)) << 8 ) | 1859 ((src & OSG_UINT64_LITERAL(0x000000ff00000000)) >> 8 ) | 1860 ((src & OSG_UINT64_LITERAL(0x0000ff0000000000)) >> 24) | 1861 ((src & OSG_UINT64_LITERAL(0x00ff000000000000)) >> 40) | 1862 ((src & OSG_UINT64_LITERAL(0xff00000000000000)) >> 56); 1863 #endif 1720 1864 } 1721 1865 1722 // network to host 1866 // undef the long long int helper macros 1867 #undef OSG_UINT64_LITERAL 1868 #undef OSG_INT64_LITERAL 1723 1869 1724 /*! Convert a UInt16 from network byte order to hostbyte order.1870 /*! Convert a Real32 from host byte order to big endian byte order. 1725 1871 1726 1872 \param[in] src Input value in host byte order. 1727 \return The input converted to networkbyte order.1873 \return The input converted to big endian byte order. 1728 1874 1729 \note Network byte order is big endian, so an actual conversion only 1730 happens on little endian architectures. 1875 \note An actual conversion only happens on little endian architectures. 1731 1876 1732 1877 \ingroup GrpBaseBaseMiscFn 1733 1878 */ 1734 1879 inline 1735 UInt16 osgntohs(UInt16 src) 1880 Real32 osgHostToBigEndian(Real32 src) 1736 1881 { 1737 return (src >> 8) | (src << 8); 1882 #if BYTE_ORDER == LITTLE_ENDIAN 1883 UInt8 *p = reinterpret_cast<UInt8*>(&src); 1884 1885 std::swap(p[0], p[3]); 1886 std::swap(p[1], p[2]); 1887 1888 return src; 1889 #else 1890 return src; 1891 #endif 1738 1892 } 1739 1893 1740 /*! Convert a UInt32 from network byte order to hostbyte order.1894 /*! Convert a Real32 from host byte order to little endian byte order. 1741 1895 1742 1896 \param[in] src Input value in host byte order. 1743 \return The input converted to networkbyte order.1897 \return The input converted to little endian byte order. 1744 1898 1745 \note Network byte order is big endian, so an actual conversion only 1746 happens on little endian architectures. 1899 \note An actual conversion only happens on big endian architectures. 1747 1900 1748 1901 \ingroup GrpBaseBaseMiscFn 1749 1902 */ 1750 1903 inline 1751 UInt32 osgntohl(UInt32 src) 1904 Real32 osgHostToLittleEndian(Real32 src) 1752 1905 { 1753 return ((src&0x000000ff) << 24) | 1754 ((src&0x0000ff00) << 8 ) | 1755 ((src&0x00ff0000) >> 8 ) | 1756 ((src&0xff000000) >> 24); 1906 #if BYTE_ORDER == LITTLE_ENDIAN 1907 return src; 1908 #else 1909 UInt8 *p = reinterpret_cast<UInt8*>(&src); 1910 1911 std::swap(p[0], p[3]); 1912 std::swap(p[1], p[2]); 1913 1914 return src; 1915 #endif 1757 1916 } 1758 1917 1759 /*! Convert a UInt64 from networkbyte order to host byte order.1918 /*! Convert a Real32 from big endian byte order to host byte order. 1760 1919 1761 \param[in] src Input value in hostbyte order.1762 \return The input converted to networkbyte order.1920 \param[in] src Input value in big endian byte order. 1921 \return The input converted to host byte order. 1763 1922 1764 \note Network byte order is big endian, so an actual conversion only 1765 happens on little endian architectures. 1923 \note An actual conversion only happens on little endian architectures. 1766 1924 1767 1925 \ingroup GrpBaseBaseMiscFn 1768 1926 */ 1769 1927 inline 1770 UInt64 osgntohll(UInt64 src) 1928 Real32 osgBigEndianToHost(Real32 src) 1771 1929 { 1772 #ifdef OSG_LONGLONG_HAS_LL 1773 return ((src&0x00000000000000ffLL) << 56) | 1774 ((src&0x000000000000ff00LL) << 40) | 1775 ((src&0x0000000000ff0000LL) << 24) | 1776 ((src&0x00000000ff000000LL) << 8 ) | 1777 ((src&0x000000ff00000000LL) >> 8 ) | 1778 ((src&0x0000ff0000000000LL) >> 24) | 1779 ((src&0x00ff000000000000LL) >> 40) | 1780 ((src&0xff00000000000000LL) >> 56); 1930 #if BYTE_ORDER == LITTLE_ENDIAN 1931 UInt8 *p = reinterpret_cast<UInt8*>(&src); 1932 1933 std::swap(p[0], p[3]); 1934 std::swap(p[1], p[2]); 1935 1936 return src; 1781 1937 #else 1782 return ((src&0x00000000000000ff) << 56) | 1783 ((src&0x000000000000ff00) << 40) | 1784 ((src&0x0000000000ff0000) << 24) | 1785 ((src&0x00000000ff000000) << 8 ) | 1786 ((src&0x000000ff00000000) >> 8 ) | 1787 ((src&0x0000ff0000000000) >> 24) | 1788 ((src&0x00ff000000000000) >> 40) | 1789 ((src&0xff00000000000000) >> 56); 1938 return src; 1790 1939 #endif 1791 1940 } 1792 1941 1793 /*! Convert a Real32 from networkbyte order to host byte order.1942 /*! Convert a Real32 from little endian byte order to host byte order. 1794 1943 1795 \param[in] src Input value in hostbyte order.1796 \return The input converted to networkbyte order.1944 \param[in] src Input value in little endian byte order. 1945 \return The input converted to host byte order. 1797 1946 1798 \note Network byte order is big endian, so an actual conversion only 1799 happens on little endian architectures. 1947 \note An actual conversion only happens on big endian architectures. 1800 1948 1801 1949 \ingroup GrpBaseBaseMiscFn 1802 1950 */ 1803 inline 1804 Real32 osg ntohf(Real32 src)1951 inline 1952 Real32 osgLittleEndianToHost(Real32 src) 1805 1953 { 1806 UInt8 *p = (UInt8 *) &src; 1807 1954 #if BYTE_ORDER == LITTLE_ENDIAN 1955 return src; 1956 #else 1957 UInt8 *p = reinterpret_cast<UInt8*>(&src); 1958 1808 1959 std::swap(p[0], p[3]); 1809 1960 std::swap(p[1], p[2]); 1961 1962 return src; 1963 #endif 1964 } 1810 1965 1966 /*! Convert a Real64 from host byte order to big endian byte order. 1967 1968 \param[in] src Input value in host byte order. 1969 \return The input converted to big endian byte order. 1970 1971 \note An actual conversion only happens on little endian architectures. 1972 1973 \ingroup GrpBaseBaseMiscFn 1974 */ 1975 inline 1976 Real64 osgHostToBigEndian(Real64 src) 1977 { 1978 #if BYTE_ORDER == LITTLE_ENDIAN 1979 UInt8 *p = reinterpret_cast<UInt8*>(&src); 1980 1981 std::swap(p[0], p[7]); 1982 std::swap(p[1], p[6]); 1983 std::swap(p[2], p[5]); 1984 std::swap(p[3], p[4]); 1985 1811 1986 return src; 1987 #else 1988 return src; 1989 #endif 1812 1990 } 1813 1991 1814 /*! Convert a Real64 from network byte order to hostbyte order.1992 /*! Convert a Real64 from host byte order to little endian byte order. 1815 1993 1816 1994 \param[in] src Input value in host byte order. 1817 \return The input converted to networkbyte order.1995 \return The input converted to little endian byte order. 1818 1996 1819 \note Network byte order is big endian, so an actual conversion only 1820 happens on little endian architectures. 1997 \note An actual conversion only happens on big endian architectures. 1821 1998 1822 1999 \ingroup GrpBaseBaseMiscFn 1823 2000 */ 1824 2001 inline 1825 Real64 osg ntohd(Real64 src)2002 Real64 osgHostToLittleEndian(Real64 src) 1826 2003 { 1827 UInt8 *p = (UInt8 *) &src; 2004 #if BYTE_ORDER == LITTLE_ENDIAN 2005 return src; 2006 #else 2007 UInt8 *p = reinterpret_cast<UInt8*>(&src); 2008 2009 std::swap(p[0], p[7]); 2010 std::swap(p[1], p[6]); 2011 std::swap(p[2], p[5]); 2012 std::swap(p[3], p[4]); 2013 2014 return src; 2015 #endif 2016 } 1828 2017 2018 /*! Convert a Real64 from big endian byte order to host byte order. 2019 2020 \param[in] src Input value in big endian byte order. 2021 \return The input converted to host byte order. 2022 2023 \note An actual conversion only happens on little endian architectures. 2024 2025 \ingroup GrpBaseBaseMiscFn 2026 */ 2027 inline 2028 Real64 osgBigEndianToHost(Real64 src) 2029 { 2030 #if BYTE_ORDER == LITTLE_ENDIAN 2031 UInt8 *p = reinterpret_cast<UInt8*>(&src); 2032 1829 2033 std::swap(p[0], p[7]); 1830 2034 std::swap(p[1], p[6]); 1831 2035 std::swap(p[2], p[5]); 1832 2036 std::swap(p[3], p[4]); 2037 2038 return src; 2039 #else 2040 return src; 2041 #endif 2042 } 1833 2043 2044 /*! Convert a Real64 from little endian byte order to host byte order. 2045 2046 \param[in] src Input value in little endian byte order. 2047 \return The input converted to host byte order. 2048 2049 \note An actual conversion only happens on big endian architectures. 2050 2051 \ingroup GrpBaseBaseMiscFn 2052 */ 2053 inline 2054 Real64 osgLittleEndianToHost(Real64 src) 2055 { 2056 #if BYTE_ORDER == LITTLE_ENDIAN 1834 2057 return src; 2058 #else 2059 UInt8 *p = reinterpret_cast<UInt8*>(&src); 2060 2061 std::swap(p[0], p[7]); 2062 std::swap(p[1], p[6]); 2063 std::swap(p[2], p[5]); 2064 std::swap(p[3], p[4]); 2065 2066 return src; 2067 #endif 1835 2068 } 1836 2069 1837 /*! Convert a Real128 from network byte order to hostbyte order.2070 /*! Convert a Real128 from host byte order to big endian byte order. 1838 2071 1839 2072 \param[in] src Input value in host byte order. 1840 \return The input converted to networkbyte order.2073 \return The input converted to big endian byte order. 1841 2074 1842 \note Network byte order is big endian, so an actual conversion only 1843 happens on little endian architectures. 2075 \note An actual conversion only happens on little endian architectures. 1844 2076 1845 2077 \ingroup GrpBaseBaseMiscFn 1846 2078 */ 1847 2079 inline 1848 Real128 osg ntohdd(Real128 src)2080 Real128 osgHostToBigEndian(Real128 src) 1849 2081 { 1850 UInt8 *p = (UInt8 *) &src; 2082 #if BYTE_ORDER == LITTLE_ENDIAN 2083 UInt8 *p = reinterpret_cast<UInt8*>(&src); 2084 2085 std::swap(p[0], p[15]); 2086 std::swap(p[1], p[14]); 2087 std::swap(p[2], p[13]); 2088 std::swap(p[3], p[12]); 2089 std::swap(p[4], p[11]); 2090 std::swap(p[5], p[10]); 2091 std::swap(p[6], p[9]); 2092 std::swap(p[7], p[8]); 2093 2094 return src; 2095 #else 2096 return src; 2097 #endif 2098 } 1851 2099 2100 /*! Convert a Real128 from host byte order to little endian byte order. 2101 2102 \param[in] src Input value in host byte order. 2103 \return The input converted to little endian byte order. 2104 2105 \note An actual conversion only happens on big endian architectures. 2106 2107 \ingroup GrpBaseBaseMiscFn 2108 */ 2109 inline 2110 Real128 osgHostToLittleEndian(Real128 src) 2111 { 2112 #if BYTE_ORDER == LITTLE_ENDIAN 2113 return src; 2114 #else 2115 UInt8 *p = reinterpret_cast<UInt8*>(&src); 2116 1852 2117 std::swap(p[0], p[15]); 1853 2118 std::swap(p[1], p[14]); 1854 2119 std::swap(p[2], p[13]); … … 1857 2122 std::swap(p[5], p[10]); 1858 2123 std::swap(p[6], p[9]); 1859 2124 std::swap(p[7], p[8]); 2125 2126 return src; 2127 #endif 2128 } 1860 2129 2130 /*! Convert a Real128 from big endian byte order to host byte order. 2131 2132 \param[in] src Input value in big endian byte order. 2133 \return The input converted to host byte order. 2134 2135 \note An actual conversion only happens on little endian architectures. 2136 2137 \ingroup GrpBaseBaseMiscFn 2138 */ 2139 inline 2140 Real128 osgBigEndianToHost(Real128 src) 2141 { 2142 #if BYTE_ORDER == LITTLE_ENDIAN 2143 UInt8 *p = reinterpret_cast<UInt8*>(&src); 2144 2145 std::swap(p[0], p[15]); 2146 std::swap(p[1], p[14]); 2147 std::swap(p[2], p[13]); 2148 std::swap(p[3], p[12]); 2149 std::swap(p[4], p[11]); 2150 std::swap(p[5], p[10]); 2151 std::swap(p[6], p[9]); 2152 std::swap(p[7], p[8]); 2153 1861 2154 return src; 2155 #else 2156 return src; 2157 #endif 1862 2158 } 1863 2159 2160 /*! Convert a Real128 from little endian byte order to host byte order. 2161 2162 \param[in] src Input value in little endian byte order. 2163 \return The input converted to host byte order. 2164 2165 \note An actual conversion only happens on big endian architectures. 2166 2167 \ingroup GrpBaseBaseMiscFn 2168 */ 2169 inline 2170 Real128 osgLittleEndianToHost(Real128 src) 2171 { 2172 #if BYTE_ORDER == LITTLE_ENDIAN 2173 return src; 1864 2174 #else 2175 UInt8 *p = reinterpret_cast<UInt8*>(&src); 2176 2177 std::swap(p[0], p[15]); 2178 std::swap(p[1], p[14]); 2179 std::swap(p[2], p[13]); 2180 std::swap(p[3], p[12]); 2181 std::swap(p[4], p[11]); 2182 std::swap(p[5], p[10]); 2183 std::swap(p[6], p[9]); 2184 std::swap(p[7], p[8]); 2185 2186 return src; 2187 #endif 2188 } 1865 2189 1866 // host to network1867 // perhaps we should use macros here ...1868 1869 2190 /*! Convert a UInt16 from host byte order to network byte order. 1870 2191 1871 2192 \param[in] src Input value in host byte order. … … 1879 2200 inline 1880 2201 UInt16 osghtons(UInt16 src) 1881 2202 { 1882 return src;2203 return osgHostToBigEndian(src); 1883 2204 } 1884 2205 1885 2206 /*! Convert a UInt32 from host byte order to network byte order. … … 1895 2216 inline 1896 2217 UInt32 osghtonl(UInt32 src) 1897 2218 { 1898 return src;2219 return osgHostToBigEndian(src); 1899 2220 } 1900 2221 1901 2222 /*! Convert a UInt64 from host byte order to network byte order. … … 1911 2232 inline 1912 2233 UInt64 osghtonll(UInt64 src) 1913 2234 { 1914 return src;2235 return osgHostToBigEndian(src); 1915 2236 } 1916 2237 1917 2238 /*! Convert a Real32 from host byte order to network byte order. … … 1927 2248 inline 1928 2249 Real32 osghtonf(Real32 src) 1929 2250 { 1930 return src;2251 return osgHostToBigEndian(src); 1931 2252 } 1932 2253 1933 2254 /*! Convert a Real64 from host byte order to network byte order. … … 1943 2264 inline 1944 2265 Real64 osghtond(Real64 src) 1945 2266 { 1946 return src;2267 return osgHostToBigEndian(src); 1947 2268 } 1948 2269 1949 2270 /*! Convert a Real128 from host byte order to network byte order. … … 1959 2280 inline 1960 2281 Real128 osghtondd(Real128 src) 1961 2282 { 1962 return src;2283 return osgHostToBigEndian(src); 1963 2284 } 1964 2285 1965 // network to host1966 1967 2286 /*! Convert a UInt16 from network byte order to host byte order. 1968 2287 1969 2288 \param[in] src Input value in host byte order. … … 1977 2296 inline 1978 2297 UInt16 osgntohs(UInt16 src) 1979 2298 { 1980 return src;2299 return osgBigEndianToHost(src); 1981 2300 } 1982 2301 1983 2302 /*! Convert a UInt32 from network byte order to host byte order. … … 1993 2312 inline 1994 2313 UInt32 osgntohl(UInt32 src) 1995 2314 { 1996 return src;2315 return osgBigEndianToHost(src); 1997 2316 } 1998 2317 1999 2318 /*! Convert a UInt64 from network byte order to host byte order. … … 2009 2328 inline 2010 2329 UInt64 osgntohll(UInt64 src) 2011 2330 { 2012 return src;2331 return osgBigEndianToHost(src); 2013 2332 } 2014 2333 2015 2334 /*! Convert a Real32 from network byte order to host byte order. … … 2025 2344 inline 2026 2345 Real32 osgntohf(Real32 src) 2027 2346 { 2028 return src;2347 return osgBigEndianToHost(src); 2029 2348 } 2030 2349 2031 2350 /*! Convert a Real64 from network byte order to host byte order. … … 2041 2360 inline 2042 2361 Real64 osgntohd(Real64 src) 2043 2362 { 2044 return src;2363 return osgBigEndianToHost(src); 2045 2364 } 2046 2365 2047 2366 /*! Convert a Real128 from network byte order to host byte order. … … 2057 2376 inline 2058 2377 Real128 osgntohdd(Real128 src) 2059 2378 { 2060 return src;2379 return osgBigEndianToHost(src); 2061 2380 } 2062 2381 2063 #endif2064 2065 2382 /*! \} */ 2066 2383 /*---------------------------------------------------------------------*/ 2067 2384 /*! \name C strings */
