| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT RUN-TIME COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- G N A T . B Y T E _ S W A P P I N G -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 2006-2025, AdaCore -- |
| 10 |
-- -- |
| 11 |
-- GNAT is free software; you can redistribute it and/or modify it under -- |
| 12 |
-- terms of the GNU General Public License as published by the Free Soft- -- |
| 13 |
-- ware Foundation; either version 3, or (at your option) any later ver- -- |
| 14 |
-- sion. GNAT is distributed in the hope that it will be useful, but WITH- -- |
| 15 |
-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -- |
| 16 |
-- or FITNESS FOR A PARTICULAR PURPOSE. -- |
| 17 |
-- -- |
| 18 |
-- As a special exception under Section 7 of GPL version 3, you are granted -- |
| 19 |
-- additional permissions described in the GCC Runtime Library Exception, -- |
| 20 |
-- version 3.1, as published by the Free Software Foundation. -- |
| 21 |
-- -- |
| 22 |
-- You should have received a copy of the GNU General Public License and -- |
| 23 |
-- a copy of the GCC Runtime Library Exception along with this program; -- |
| 24 |
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see -- |
| 25 |
-- <http://www.gnu.org/licenses/>. -- |
| 26 |
-- -- |
| 27 |
-- GNAT was originally developed by the GNAT team at New York University. -- |
| 28 |
-- Extensive contributions were provided by Ada Core Technologies Inc. -- |
| 29 |
-- -- |
| 30 |
------------------------------------------------------------------------------ |
| 31 |
|
| 32 |
-- This is a general implementation that uses GCC intrinsics to take |
| 33 |
-- advantage of any machine-specific instructions. |
| 34 |
|
| 35 |
with Ada.Unchecked_Conversion; use Ada; |
| 36 |
|
| 37 |
with System.Byte_Swapping; use System.Byte_Swapping; |
| 38 |
|
| 39 |
package body GNAT.Byte_Swapping is |
| 40 |
|
| 41 |
-------------- |
| 42 |
-- Swapped2 -- |
| 43 |
-------------- |
| 44 |
|
| 45 |
function Swapped2 (Input : Item) return Item is |
| 46 |
function As_U16 is new Unchecked_Conversion (Item, U16); |
| 47 |
function As_Item is new Unchecked_Conversion (U16, Item); |
| 48 |
pragma Compile_Time_Warning (Item'Max_Size_In_Storage_Elements /= 2, |
| 49 |
"storage size must be 2 bytes"); |
| 50 |
begin |
| 51 |
return As_Item (Bswap_16 (As_U16 (Input))); |
| 52 |
end Swapped2; |
| 53 |
|
| 54 |
-------------- |
| 55 |
-- Swapped4 -- |
| 56 |
-------------- |
| 57 |
|
| 58 |
function Swapped4 (Input : Item) return Item is |
| 59 |
function As_U32 is new Unchecked_Conversion (Item, U32); |
| 60 |
function As_Item is new Unchecked_Conversion (U32, Item); |
| 61 |
pragma Compile_Time_Warning (Item'Max_Size_In_Storage_Elements /= 4, |
| 62 |
"storage size must be 4 bytes"); |
| 63 |
begin |
| 64 |
return As_Item (Bswap_32 (As_U32 (Input))); |
| 65 |
end Swapped4; |
| 66 |
|
| 67 |
-------------- |
| 68 |
-- Swapped8 -- |
| 69 |
-------------- |
| 70 |
|
| 71 |
function Swapped8 (Input : Item) return Item is |
| 72 |
function As_U64 is new Unchecked_Conversion (Item, U64); |
| 73 |
function As_Item is new Unchecked_Conversion (U64, Item); |
| 74 |
pragma Compile_Time_Warning (Item'Max_Size_In_Storage_Elements /= 8, |
| 75 |
"storage size must be 8 bytes"); |
| 76 |
begin |
| 77 |
return As_Item (Bswap_64 (As_U64 (Input))); |
| 78 |
end Swapped8; |
| 79 |
|
| 80 |
----------- |
| 81 |
-- Swap2 -- |
| 82 |
----------- |
| 83 |
|
| 84 |
procedure Swap2 (Location : System.Address) is |
| 85 |
X : U16; |
| 86 |
for X'Address use Location; |
| 87 |
begin |
| 88 |
X := Bswap_16 (X); |
| 89 |
end Swap2; |
| 90 |
|
| 91 |
----------- |
| 92 |
-- Swap4 -- |
| 93 |
----------- |
| 94 |
|
| 95 |
procedure Swap4 (Location : System.Address) is |
| 96 |
X : U32; |
| 97 |
for X'Address use Location; |
| 98 |
begin |
| 99 |
X := Bswap_32 (X); |
| 100 |
end Swap4; |
| 101 |
|
| 102 |
----------- |
| 103 |
-- Swap8 -- |
| 104 |
----------- |
| 105 |
|
| 106 |
procedure Swap8 (Location : System.Address) is |
| 107 |
X : U64; |
| 108 |
for X'Address use Location; |
| 109 |
begin |
| 110 |
X := Bswap_64 (X); |
| 111 |
end Swap8; |
| 112 |
|
| 113 |
end GNAT.Byte_Swapping; |