I get an 16 Bit integer from C. This integer consists of 16 flags.
How can I convert this integer in a record of 16 booleans?
Thanks!
type Flags is record
Flag1 : Boolean;
Flag2 : Boolean;
- ...
Flag15 : Boolean;
end record;
for Flags use record
Flag1 at 0 range 0 .. 0;
Flag2 at 0 range 1 .. 1;
-- ...
Flag15 at 0 range 15 .. 15;
end record;
for Flags'Size use 16;
-- This is vital, because normally, records are passed by-reference in Ada.
-- However, as we use this type with C, it has to be passed by value.
-- C_Pass_By_Copy was introduced in GNAT and is part of the language since Ada 2005.
pragma Convention (C_Pass_By_Copy, Flags);
You can use this type directly in the declaration of the imported C function instad of the C integer type.
You can just simply perform 16 right bit shifts and bitwise AND the result with 1 to determine whether or not a bit/flag is set. Here's an example (I'm hoping this isn't homework):
#include <stdio.h>
#include <stdint.h>
typedef unsigned char BOOL;
int main(void)
{
unsigned i;
uint16_t flags = 0x6E8B; /* 0b0110111010001011 */
BOOL arr[16];
for (i = 0; i < 16; i++) {
arr[i] = (flags >> i) & 1;
printf("flag %u: %u\n", i+1, arr[i]);
}
return 0;
}
arr[0] will contain the least significant bit, and arr[15] the most significant.
Output:
flag 1: 1
flag 2: 1
flag 3: 0
flag 4: 1
flag 5: 0
flag 6: 0
flag 7: 0
flag 8: 1
flag 9: 0
flag 10: 1
flag 11: 1
flag 12: 1
flag 13: 0
flag 14: 1
flag 15: 1
flag 16: 0
In Ada, you can usually declare an imported function to take parameters or return values of the type you want, rather than a C-equivalent type which you then have to convert.
So, here, you want
type Flags is array (0 .. 15) of Boolean;
for Flags'Component_Size use 1;
for Flags'Size use 16;
pragma Convention (C, Flags);
and you can declare your function as
function Get_Flags return Flags;
pragma Import (C, Get_Flags, “get_flags");
which with
unsigned short get_flags(void) {
return 0x6e8b;
}
and a simple harness gave me
flag 0 is TRUE
flag 1 is TRUE
flag 2 is FALSE
flag 3 is TRUE
flag 4 is FALSE
flag 5 is FALSE
flag 6 is FALSE
flag 7 is TRUE
flag 8 is FALSE
flag 9 is TRUE
flag 10 is TRUE
flag 11 is TRUE
flag 12 is FALSE
flag 13 is TRUE
flag 14 is TRUE
flag 15 is FALSE
As Bo Persson noted, this is fine so long as your code only needs to run on a little-endian machine. If you want it to run on a SPARC or a Powerbook, it’s probably best to use trashgod’s suggestion;
subtype Flags is Interfaces.C.unsigned_short;
use type Flags;
function Get_Flags return Flags;
pragma Import (C, Get_Flags, "get_flags");
and then, probably, name your flag bits (with something more meaningful!)
Flag_3 : constant Flags := 2#0000_0000_0000_1000#;
or (probably more like the C)
Flag_4 : constant Flags := 2 ** 4;
and then check
(Get_Flags and Flag_3) /= 0
In Ada, a modular type allows logical operations to access a value as a bit set. Introduced in Ada 95, an overview may be found in the Ada 95 Rationale, §3.3.2 Modular Types. Depending on implementation, the pre-defined type Interfaces.C.unsigned_short my be a convenient choice for obtaining the value from C.
You can also use overlaying to achieve the desired result; let's assume that these booleans are all meaningful and strictly boolean (i.e. nothing that's an enumeration).
First you need to define your record; I'm going to be using a single Nybble to illustrate, but the principle is applicable. The Nybble is the old DOS attributes: reading (visibility-wise; should be Is_Hidden, in retrospect), write, archive, and system.
Type Nyble_Data is mod 2**4;
For Nyble_Data'Size use 4;
Type Data_Record is record
Can_Read, Can_Write, Is_Archived, Is_System : Boolean:= False;
end record;
-- Ensure 4 bits used.
pragma Pack (Data_Record);
For Data_Record'Size use 4;
-- Specify Layout.
For Data_Record use
record
Can_Read at 0 range 0..0;
Can_Write at 0 range 1..1;
Is_Archived at 0 range 2..2;
Is_System at 0 range 3..3;
end record;
-- This is where the magic occurs.
Function Convert( Data : In Nyble_Data ) Return Data_Record is
Result : Data_Record;
For Result'Address use Data'Address;
Pragma Import( Convention => Ada, Entity => Result );
Pragma Inline( Convert );
begin
Return Result;
end Convert;
-- Test variables.
Input : Nyble_Data:= 5;
Output : Data_Record:= Convert(Input);
-- Display the record.
Procedure Put( Data : In Data_Record ) is
Use Ada.Text_IO;
begin
Put_Line( "Read: " & ASCII.HT & Boolean'Image(Data.Can_Read) );
Put_Line( "Write: " & ASCII.HT & Boolean'Image(Data.Can_Write) );
Put_Line( "Archive:" & ASCII.HT & Boolean'Image(Data.Is_Archived) );
Put_Line( "System: " & ASCII.HT & Boolean'Image(Data.Is_System) );
end Put;
You can use a union containing a short int (or a int_16) and a bit field:
union UMyFlags {
short n;
struct {
flag_1 : 1;
flag_2 : 1;
// other flags ...
} flags;
};
However, because of byte ordering, your code will not be portable on every platform.
Related
I would like to create a function in SPARK_Mode that utilizes the GNAT GCC intrinsic function "__builtin_ctzll".
with Interfaces; use Interfaces;
package GCC_Intrinsic with
SPARK_Mode
is
function DividesLL (A, B : Unsigned_64) return Boolean is (B mod A = 0) with
Ghost,
Pre => A /= 0;
function CTZLL (X : Unsigned_64) return Natural with
Pre => X /= 0,
Post => CTZLL'Result in 0 .. Unsigned_64'Size - 1
and then DividesLL (Unsigned_64 (2)**CTZLL'Result, X)
and then
(for all Y in CTZLL'Result + 1 .. Unsigned_64'Size - 1 =>
not DividesLL (Unsigned_64 (2)**Y, X));
pragma Import (Intrinsic, CTZLL, "__builtin_ctzll");
end GCC_Intrinsic;
I would like to assume the postcondition to be true since it is the definition of the number of trailing zeros which is implied by the documentation. However, I am unsure how to accomplish this, having read much documentation and having tried to use "pragma Assume". I am relatively new to Ada/SPARK and am using GNAT Community 2020. Can someone please help me solve this issue so that gnatprove is able to prove the postcondition of CTZLL?
When I formulate the postcondition (contract) of __builtin_ctzll using Shift_Right, I'm able proof (using GNAT CE 2020 and proof level 1) that test.adb is free of run-time errors if it would be run.
Note: Related documentation: SPARK user's manual, section 7.4.5: Writing Contracts on Imported Subprograms.
intrinsic.ads
pragma Assertion_Policy (Check);
with Interfaces; use Interfaces;
package Intrinsic with SPARK_Mode is
-- Count Trailing Zeros (long long unsigned).
function CTZLL (X : Unsigned_64) return Natural with
Pre => X /= 0,
Post => CTZLL'Result in 0 .. Unsigned_64'Size - 1 and
(for all I in 0 .. CTZLL'Result - 1 =>
(Shift_Right (X, I) and 2#1#) = 2#0#) and
(Shift_Right (X, CTZLL'Result) and 2#1#) = 2#1#;
-- You could also use aspects (Import, Convention, External_Name).
pragma Import (Intrinsic, CTZLL, "__builtin_ctzll");
end Intrinsic;
test.adb
pragma Assertion_Policy (Check);
with Interfaces; use Interfaces;
with Intrinsic; use Intrinsic;
procedure Test with SPARK_Mode is
begin
-- Absence of Run-Time Errors (AoRTE) for this program can be proven:
-- Assert_Failure will not be raised at runtime.
pragma Assert (CTZLL ( 1) = 0);
pragma Assert (CTZLL ( 2) = 1);
pragma Assert (CTZLL ( 3) = 0);
pragma Assert (CTZLL ( 4) = 2);
pragma Assert (CTZLL ( 5) = 0);
pragma Assert (CTZLL ( 6) = 1);
pragma Assert (CTZLL ( 7) = 0);
pragma Assert (CTZLL ( 8) = 3);
pragma Assert (CTZLL ( 9) = 0);
pragma Assert (CTZLL (10) = 1);
pragma Assert (CTZLL (2 ** 63 ) = 63);
pragma Assert (CTZLL (2 ** 64 - 1) = 0);
end Test;
output (of gnatprove)
$ gnatprove -P default.gpr -j0 -u test.adb --level=1 --report=all
Phase 1 of 2: generation of Global contracts ...
Phase 2 of 2: flow analysis and proof ...
test.adb:12:19: info: precondition proved
test.adb:12:19: info: assertion proved
[...]
test.adb:24:19: info: precondition proved
test.adb:24:19: info: assertion proved
For those not familiar with __builtin_ctzll: returns the number of trailing 0-bits in x, starting at the least significant bit position. If x is 0, the result is undefined. See also here. Example:
main.adb
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Interfaces; use Interfaces;
with Intrinsic; use Intrinsic;
procedure Main is
begin
for K in 1 .. 10 loop
Put (K, Width => 3);
Put (K, Width => 9, Base => 2);
Put (CTZLL (Unsigned_64 (K)), Width => 4);
New_Line;
end loop;
end Main;
output (of Main)
$ ./obj/main
1 2#1# 0
2 2#10# 1
3 2#11# 0
4 2#100# 2
5 2#101# 0
6 2#110# 1
7 2#111# 0
8 2#1000# 3
9 2#1001# 0
10 2#1010# 1
I have a ACLs system previously built by someone else and I am trying to understand how bit-masking works there. I have this 4 constants defined:
const NONE = 0;
const READ = 1;
const WRITE = 2;
const UPDATE = 4;
const DELETE = 8;
Then in DB I am seeing users with permissions like 1, 2, 5, 9, 15. I have tried to transform them using this tool and I end up with this result:
0 // NONE
1 // READ
2 // WRITE
3 // UPDATE|DELETE
4 // UPDATE
5 // WRITE|DELETE
6 // WRITE|UPDATE
7 // WRITE|UPDATE|DELETE
8 // DELETE
9 // READ|DELETE
10 // READ|UPDATE
11 // READ|UPDATE|DELETE
12 // READ|WRITE
13 // READ|WRITE|DELETE
14 // READ|WRITE|UPDATE
15 // READ|WRITE|DELETE|UPDATE
How I think this work is as follow:
Decimal Hexadecimal
3 00000011
Because the two last bits are 1 I am assuming that those users having 3 will have UPDATE|DELETE permissions (see table above). Is that right? If not what's the right way to translate from decimal to bit-mask?
0 = NONE is a special case which can be checked by simple comparison.
If you want to ask the question is constant cn with the value of 2^(n-1) set, then we do this with (1 = yes, 0 = no, % = modulo):
(value / cn) % 2
If we want to get all flags that are set, you can do this with the following pseudo code:
c := 1
while value > 0
if value % 2 = 1
// constant c is set
...
end if
value := value / 2
c := c * 2
end while
I have a record made of smaller types, a total of 16 bits in size.
type Type_1 is (val1, val2, val3, val4, val5, val6, val7, val8);
for Type_1 use (val1 => 0, val2 = 1, val3 = 2, val4 => 3
val5 => 4, val6 => 5, val7 => 6, val8 => 7);
for Type_1'Size use 3;
type Type_2 is (val1, val2);
for Type_2 use (val1 => 0, val2 = 1);
for Type_2'Size use 1;
etc, etc
type The_Record is record
element_1 : Type_1;
element_2 : Type_2;
element_3 : Type_3;
element_4 : Type_4;
end record;
for the_Record use record
element_1 at 0 range 0 .. 2;
element_2 at 0 range 3 .. 4;
element_3 at 0 range 5 .. 12;
element_4 at 0 range 13 .. 15;
end record;
for The_Record'Size use 16;
How can I flatten 'The_Record' into an array of bytes or something similar?
Thank you!
You can always use Unchecked_conversion, however, this approach is unchecked and you are therefore telling the compiler (a) that you know what you're doing, and (b) that it cannot help you [otherwise it would be named checked_conversion].
Another way that you can do the conversion is with overlaying, this is my preferred approach (if it is not a simple type-renaming issue) as if things change then the conversion-function can be modified as needed.
-- Subtype-renaming.
Subtype Byte is Interfaces.Unsigned_8;
-- General type for conversion to a collection of bytes.
Type Byte_Array is Array (Positive Range <>) of Byte;
-- Speciffic collection of bytes from The_Record.
Subtype Record_Bytes is Byte_Array(1..The_Record'Size/Byte'Size);
-- Converting record to bytes...
Function Convert( Input : The_Record ) return Record_Bytes is
Result : Constant Record_Bytes;
For Result'Address use Input'Address;
Pragma Import( Convention => Ada, Entity => Result );
begin
Return Result;
end Convert;
-- Converting bytes to record... in Ada 2012!
Function Convert( Input : Record_Bytes ) return The_Record is
Result : The_Record with
Import, Convention => Ada, Address => Input'Address;
begin
Return Result;
end Convert;
Another, and probably better, way (if you are trying for serialization. as I suspect) of doing this would be to create read/write functions and override the default 'read and 'write attributes.
This in an example of how to use the Unchecked Conversion way:
with Ada.Unchecked_Conversion;
with Ada.Text_Io;
procedure Uc is
type The_Record is record
A : Integer;
B : Integer;
end record;
-- define a byte sized thing...
type U8 is mod 2**8;
for U8'Size use 8;
-- have your array defined according to
-- the size of the record it needs to hold
type U8_Record_Array is array (1 .. The_Record'Size / U8'Size) of U8;
-- instantiate Unchecked_Conversion
function To_Array is new Ada.Unchecked_Conversion (Source => The_Record,
Target => U8_Record_Array);
-- Declare a record and convert it to an array
R : constant The_Record := (A => 1, B => 2);
A : constant U8_Record_Array := To_Array (R);
begin
-- Print the Array As Bytes
for I in A'Range loop
Ada.Text_Io.Put (U8'Image (A(I)));
end loop;
end Uc;
Depending on Exactly what you are intending to do, Both the Overlay & Unchecked Conversion ways will have pros & cons associated with them :)
I have a pretty simple Ada project on my hands. The task is to take a collection of a "voter's" votes and compare it to each "candidate's" score and determine which candidate best matches the voter.
The input looks like this, followed by the output that should be put out:
Input:
0 0 0 1 1 1 -1 -1 -1 1
7
A
1 1 1 1 1 1 1 1 1 1
B
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1
C
1 -1 1 -1 1 -1 1 -1 1 -1
D
1 0 1 0 1 0 1 0 1 0
E
0 -1 0 -1 0 -1 0 -1 0 -1
F
1 1 1 1 0 0 0 0 0 0
G
0 0 0 1 -1 0 0 -1 1 1
Output:
A
F
G
So far, I have a procedure that will take the votes of each candidate and compare them to the votes of the voter. I know what I need to do as I have done it before in Java, but I am not sure how I should take the input in Ada. Here is what I have so far.
with Ada.Text_IO, Ada.Integer_Text_IO;
use Ada.Text_IO, Ada.Integer_Text_IO;
procedure Candidates is
-- take an array of candidates and determine which candidate best matches
-- the user's votes, then return those candidates
Number_Of_Candidates: Integer;
subtype VoterArray_Index is Integer range 1..10;
subtype CandidatesArray_Index is Integer range 1..Number_Of_Candidates;
type VoterArray is array(VoterArray_Index) of Integer;
type CandidatesArray is array(Character range 'A' .. 'Z') of array;
type Best_CandidatesArray is array(CandidatesArray_Index) of array;
Voter: VoterArray;
Candidates: CandidatesArray;
Best_Candidates: Best_CandidatesArray;
function Get_Input() is
-- get the input and put it into the correct arrays
Current_Line : string;
begin
Get(Current_Line);
function Get_Best_Score(CandidatesArray: in out CandidatesArray) is
-- go through the arrays and find the best score
SameAnswers: Integer;
DifferentAnswers: Integer;
BestScore: Integer;
subtype CandidateArray is array(VoterArray_Index) of Integer;
Candidate: CandidateArray;
begin
for I in CandidatesArray_Index loop
Candidate := Candidates(I);
for J in VoterArray_Index loop
if Candidate(J) /= 0 and Voter(J) /= 0 then
if Candidate(J) /= Voter(J) then
DifferentAnswers := DifferentAnswers + 1
else
SameAnswers := SameAnswers + 1
end if;
end if;
end loop;
if SameAnswers - DifferentAnswers >= BestScore then
Best_Candidates(I) := Candidate;
end if;
SameAnswers := 0;
DifferentAnswers := 0;
end loop;
end Get_Best_Score;
As you can see, I'm not sure how to take the numbers and put them into an array. If you have any suggestions or a different way I should go about things, I'm all ears.
Thanks in advance.
You could use streams in order to read the data in:
Integer'Read( STREAM_HANDLE, VARIABLE )
Another option is reading the values via Get for each element of your array, I'd recommend a helper-function in case you need to tweak the procedure for handling format changes:
Function Get_Vote ( File : File_Type ) Return Integer is
begin
Return Result : Integer do
Integer_IO.Get(
File => File,
Item => Result
);
End return;
end Read_Votes;
and
For Index in VOTE_ARRAY'range loop
VOTE_ARRAY( Index ) := Get_Vote( INPUT_FILE );
End loop;
Because the number of rows is given in the file, a constrained array has to be large enough to hold all possible elements. Instead, you can declare an unconstrained array:
subtype Voter_Index is Positive range 1 .. 10;
type Voter_Array is array(Voter_Index) of Integer;
type Candidate_Array is array(Character range <>) of Voter_Array;
Later, when you know the actual count, you can allocate only the space actually required for the array. This declaration puts Candidates on the stack in a nested scope:
Number_Of_Candidates := ...;
declare
Candidates : Candidate_Array(
'A' .. Character'Val(Character'Pos('A') + Number_Of_Candidates));
begin
...
end;
Alternatively, you can allocate space on the heap:
type Candidate_Array_Ptr is access Candidate_Array;
Candidates: Candidate_Array_Ptr;
begin
Number_Of_Candidates := ...;
Candidates := new Candidate_Array(
'A' .. Character'Val(Character'Pos('A') + Number_Of_Candidates));
end;
In either case, you can access the array elements as required:
for i in Candidates'Range loop
for j in Voter_Array'Range loop
Ada.Integer_Text_IO.put(Candidates(i)(j), 5);
end loop;
Ada.Text_IO.New_Line;
end loop;
Addendum: This approach assumes that candidate names are consecutive Characters. As an alternative, consider an array of Candidate_Record, where each Name is read from the file:
type Candidate_Record is
record
Name : Character;
Votes : Voter_Array;
end record;
type Candidate_Array is array(Positive range <>) of Candidate_Record;
Candidates : Candidate_Array(1 .. Number_Of_Candidates);
for i in Candidates'Range loop
Ada.Text_IO.Put(Candidates(i).Name & ':');
for j in Voter_Array'Range loop
Ada.Integer_Text_IO.Put(Candidates(i).Votes(j), 5);
end loop;
Ada.Text_IO.New_Line;
end loop;
If I have an unsigned(MAX downto 0) containing the value 2**MAX - 1, do the VHDL (87|93|200X) standards define what happens when I increment it by one? (Or, similarly, when I decrement it by one from zero?)
Short answer:
There is no overflow handling, the overflow carry is simply lost. Thus the result is simply the integer result of your operation modulo 2^MAX.
Longer answer:
The numeric_std package is a standard package but it is not is the Core the VHDL standards (87,93,200X).
For reference : numeric_std.vhd
The + operator in the end calls the ADD_UNSIGNED (L, R : unsigned; C : std_logic) function (with C = '0'). Note that any integer/natural operand is first converted into an unsigned.
The function's definition is:
function ADD_UNSIGNED (L, R : unsigned; C : std_logic) return unsigned is
constant L_left : integer := L'length-1;
alias XL : unsigned(L_left downto 0) is L;
alias XR : unsigned(L_left downto 0) is R;
variable RESULT : unsigned(L_left downto 0);
variable CBIT : std_logic := C;
begin
for i in 0 to L_left loop
RESULT(i) := CBIT xor XL(i) xor XR(i);
CBIT := (CBIT and XL(i)) or (CBIT and XR(i)) or (XL(i) and XR(i));
end loop;
return RESULT;
end ADD_UNSIGNED;
As you can see an "overflow" occurs if CBIT='1' (carry bit) for i = L_left. The result bit RESULT(i) is calculated normally and the last carry bot value is ignored.
I've had the problem with wanting an unsigned to overflow/underflow as in C or in Verilog and here is what I came up with (result and delta are unsigned):
result <= unsigned(std_logic_vector(resize(('1' & result) - delta, result'length))); -- proper underflow
result <= unsigned(std_logic_vector(resize(('0' & result) + delta, result'length))); -- proper overflow
For overflow '0' & result makes an unsigned which is 1 bit larger to be able to correctly accommodate the value of the addition. The MSB is then removed by the resize command which yields the correct overflow value. Same for underflow.
For a value of MAX equal to 7 adding 1 to 2**7 - 1 (127) will result in the value 2**7 (128).
The maximum unsigned value is determined by the length of an unsigned array type:
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity foo is
end entity;
architecture faa of foo is
constant MAX: natural := 7;
signal somename: unsigned (MAX downto 0) := (others => '1');
begin
UNLABELED:
process
begin
report "somename'length = " & integer'image(somename'length);
report "somename maximum value = " &integer'image(to_integer(somename));
wait;
end process;
end architecture;
The aggregate (others => '1') represents a '1' in each element of somename which is an unsigned array type and represents the maximum binary value possible.
This gives:
foo.vhdl:15:9:#0ms:(report note): somename'length = 8
foo.vhdl:16:9:#0ms:(report note): somename maximum value = 255
The length is 8 and the numerical value range representable by the unsigned array type is from 0 to 2**8 - 1 (255), the maximum possible value is greater than 2**7 (128) and there is no overflow.
This was noticed in a newer question VHDL modulo 2^32 addition. In the context of your accepted answer it assumes you meant length instead of the leftmost value.
The decrement from zero case does result in a value of 2**8 - 1 (255) (MAX = 7). An underflow or an overflow depending on your math religion.
Hat tip to Jonathan Drolet for pointing this out in the linked newer question.