-- +------------------------------------ -- | Copyright 1992-1997 Future Parallel -- | VLSI Design Lab -- | Library: VFP -- | Designer: Tim Pagden -- | Opened: 28 Dec 1997 -- +------------------------------------ package body integer_class is -- extensions to std.standard.integer -- integer defined in std.standard -- arithmetic operators -- defined in std.standard -- general purpose functions function string_length ( -- 05.04.1999 a: integer ) return integer is use std.textio.all; variable a_int : integer; variable int_line : line; variable num_chars : integer := 0; variable result : integer; begin -- copy a so you can use int subtypes (robustly), too a_int := a; -- write a to a line, then L'length should return the number of chars, surely write(int_line, a_int); num_chars := int_line'LENGTH; deallocate(int_line); result := num_chars; return result; end string_length; function integer_wordlength ( a: integer ) return integer is variable wordlength: integer; begin if (a >= -2) and (a <= 1) then wordlength := 2; elsif (a >= -4) and (a <= 3) then wordlength := 3; elsif (a >= -8) and (a <= 7) then wordlength := 4; elsif (a >= -16) and (a <= 15) then wordlength := 5; elsif (a >= -32) and (a <= 31) then wordlength := 6; elsif (a >= -64) and (a <= 63) then wordlength := 7; elsif (a >= -128) and (a <= 127) then wordlength := 8; elsif (a >= -256) and (a <= 255) then wordlength := 9; elsif (a >= -512) and (a <= 511) then wordlength := 10; elsif (a >= -1024) and (a <= 1023) then wordlength := 11; elsif (a >= -2048) and (a <= 2047) then wordlength := 12; elsif (a >= -4096) and (a <= 4095) then wordlength := 13; elsif (a >= -8192) and (a <= 8191) then wordlength := 14; elsif (a >= -16384) and (a <= 16383) then wordlength := 15; elsif (a >= -32768) and (a <= 32767) then wordlength := 16; elsif (a >= -65536) and (a <= 65535) then wordlength := 17; elsif (a >= -131072) and (a <= 131071) then wordlength := 18; elsif (a >= -262144) and (a <= 262143) then wordlength := 19; elsif (a >= -524288) and (a <= 524287) then wordlength := 20; elsif (a >= -1048576) and (a <= 1048575) then wordlength := 21; elsif (a >= -2097152) and (a <= 2097151) then wordlength := 22; elsif (a >= -4194304) and (a <= 4194303) then wordlength := 23; elsif (a >= -8388608) and (a <= 8388607) then wordlength := 24; elsif (a >= -16777216) and (a <= 16777215) then wordlength := 25; elsif (a >= -33554432) and (a <= 33554431) then wordlength := 26; elsif (a >= -67108864) and (a <= 67108863) then wordlength := 27; elsif (a >= -134217728) and (a <= 134217727) then wordlength := 28; elsif (a >= -268435456) and (a <= 268435455) then wordlength := 29; elsif (a >= -536870912) and (a <= 536870911) then wordlength := 30; elsif (a >= -1073741824) and (a <= 1073741823) then wordlength := 31; elsif (a >= integer'low) and (a <= integer'high) then -- i'low = -2147483647 wordlength := 32; -- or -2147483648 end if; -- depending upon implementation return wordlength; end integer_wordlength; function integer_charlength ( -- 29.12.97 a: integer ) return integer is variable a_natural : natural; variable num_chars : integer := 0; begin a_natural := abs(a); case a_natural is when 0 to 9 => num_chars := 1; when 10 to 99 => num_chars := 2; when 100 to 999 => num_chars := 3; when 1000 to 9999 => num_chars := 4; when 10_000 to 99_999 => num_chars := 5; when 100_000 to 999_999 => num_chars := 6; when 1_000_000 to 9_999_999 => num_chars := 7; when 10_000_000 to 99_999_999 => num_chars := 8; when 100_000_000 to 999_999_999 => num_chars := 9; when 1_000_000_000 to integer'high => num_chars := 10; when others => -- required for active VHDL cos it's too daft to work out all cases have been covered num_chars := 10; -- although I'm prepared to believe this is a compile-time rather than run-time catch end case; if a < 0 then num_chars := num_chars+1; end if; return num_chars; end integer_charlength; -- ancillary function required in order to -- enable the integer -> ? functions in generic_conversions -- to be completed with only the required number of -- bits of precision in the length of the vectors. function next_greater_binary_power_minus_1 ( a: integer ) return integer is variable power_minus_1: integer; begin if (a >= -2) and (a <= 1) then power_minus_1 := 1; elsif (a >= -4) and (a <= 3) then power_minus_1 := 3; elsif (a >= -8) and (a <= 7) then power_minus_1 := 7; elsif (a >= -16) and (a <= 15) then power_minus_1 := 15; elsif (a >= -32) and (a <= 31) then power_minus_1 := 31; elsif (a >= -64) and (a <= 63) then power_minus_1 := 63; elsif (a >= -128) and (a <= 127) then power_minus_1 := 127; elsif (a >= -256) and (a <= 255) then power_minus_1 := 255; elsif (a >= -512) and (a <= 511) then power_minus_1 := 511; elsif (a >= -1024) and (a <= 1023) then power_minus_1 := 1023; elsif (a >= -2048) and (a <= 2047) then power_minus_1 := 2047; elsif (a >= -4096) and (a <= 4095) then power_minus_1 := 4095; elsif (a >= -8192) and (a <= 8191) then power_minus_1 := 8191; elsif (a >= -16384) and (a <= 16383) then power_minus_1 := 16383; elsif (a >= -32768) and (a <= 32767) then power_minus_1 := 32767; elsif (a >= -65536) and (a <= 65535) then power_minus_1 := 65535; elsif (a >= -131072) and (a <= 131071) then power_minus_1 := 131071; elsif (a >= -262144) and (a <= 262143) then power_minus_1 := 262143; elsif (a >= -524288) and (a <= 524287) then power_minus_1 := 524287; elsif (a >= -1048576) and (a <= 1048575) then power_minus_1 := 1048575; elsif (a >= -2097152) and (a <= 2097151) then power_minus_1 := 2097151; elsif (a >= -4194304) and (a <= 4194303) then power_minus_1 := 4194303; elsif (a >= -8388608) and (a <= 8388607) then power_minus_1 := 8388607; elsif (a >= -16777216) and (a <= 16777215) then power_minus_1 := 16777215; elsif (a >= -33554432) and (a <= 33554431) then power_minus_1 := 33554431; elsif (a >= -67108864) and (a <= 67108863) then power_minus_1 := 67108863; elsif (a >= -134217728) and (a <= 134217727) then power_minus_1 := 134217727; elsif (a >= -268435456) and (a <= 268435455) then power_minus_1 := 268435455; elsif (a >= -536870912) and (a <= 536870911) then power_minus_1 := 536870911; elsif (a >= -1073741824) and (a <= 1073741823) then power_minus_1 := 1073741823; elsif (a >= integer'low) and (a <= integer'high) then -- i'low = -2147483647 power_minus_1 := 2147483647; -- or -2147483648 end if; -- depending upon implementation return power_minus_1; end next_greater_binary_power_minus_1; -- hardware functions -- none yet defined -- mathematical functions function is_factor_of_32 (a: integer) return boolean is -- 15.11.95 begin case a is when 1 | 2 | 4 | 8 | 16 | 32 => return TRUE; when others => return FALSE; end case; end is_factor_of_32; function is_power_of_2 (a: integer) return boolean is -- 15.11.95 begin case a is when 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128 | 256 | 512 | 1024 | 2048 | 4096 | 8192 | 16384 | 32768 | 65536 | 131072 | 262144 | 524288 | 1048576 | 2097152 | 4194304 | 8388608 | 16777216 | 33554432 | 67108864 | 134217728 | 268435456 | 536870912 | 1073741824 => return TRUE; when others => return FALSE; end case; end is_power_of_2; function log_2 (a: integer) return integer is -- 15.11.95 variable remainder : integer; variable log_2 : integer; begin if a > 0 then return integer_wordlength(a) - 2; else assert FALSE report "log_2(a) returns an out-of-range value" severity warning; return integer'low; -- a stupid value => a 2**32-bit vector -- with an LSB of 1 !!! end if; end log_2; end integer_class;