Porting Applications to HPUX 64bit - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Porting Applications to HPUX 64bit

Description:

Using literals and masks that assume 32 bits. Hardcoded bit shift values. ... 32 Number of bits in 32-bit word. 0x80000000 Min value of signed 32-bit word. ... – PowerPoint PPT presentation

Number of Views:53
Avg rating:3.0/5.0
Slides: 25
Provided by: dansc9
Category:

less

Transcript and Presenter's Notes

Title: Porting Applications to HPUX 64bit


1
Porting Applications to HP-UX 64-bit
  • Daniel G. Schmidt
  • Technical Consultant
  • DGSA
  • Working with Hewlett-Packard
  • 15 August 2001

2
Why 64-bit?
  • Large Address Space (gt 4G)
  • Large files (gt 4G) using standard API.
  • Large process space.
  • Performance advantage using memory in place of
    disk I/O.
  • Resources
  • HP-UX 11.0 Software Transition Kit (STK)
  • Web Documentation
  • devresource.hp.com
  • docs.hp.com
  • HP-UX 64-bit Porting and Transition Guide
    http//docs.hp.com80/hpux/onlinedocs/5966-9844/59
    66-9844.html
  • www.software.hp.com
  • HP Technical Consulting

3
Porting Code to 64-bit
  • Preparing for the Port
  • Acquire 64-bit versions of all 3rd party
    libraries.
  • Port all of your own libraries to 64-bit.
  • Rewrite assembly code to be 64-bit.
  • Add DD64 M2 to makefiles
  • Compiling 64-bit Modules
  • LP64 Data Model
  • Size of long and int now differ.
  • Pointers are 64 bit values.
  • Pointers and longs are 64-bit aligned.
  • Predefined types size_t and ptrdiff_t are 64-bit
    integral types.
  • Finding Fixing Non-Portable Constructs
  • Transition warnings option (M2) in lint and cc.
  • Use function prototypes.
  • Consistently use strict data types.
  • Verify use of any long from 32-bit source base.

4
Non-Portable Constructs
  • Data Truncation
  • Assigning longs to ints.
  • Storing pointers in ints.
  • Truncating function return values.
  • Inappropriate print specifiers.
  • Data Type Promotion
  • Arithmetic between signed and unsigned numbers.
  • Pointers
  • Casting pointers to ints or ints to pointers.
  • Pointer arithmetic between longs and ints.

5
Non-Portable Constructs (cont.)
  • Data Alignment and Data Sharing
  • Passing invalid structure references.
  • Hardcoded or assumed offsets within structures.
  • Using unnamed and unqualified bit fields.
  • Hardcoded Constants
  • Hardcoded size of data types.
  • Using literals and masks that assume 32 bits.
  • Hardcoded bit shift values.
  • Hardcoded constants with malloc(), memory(3),
    string(3).

6
Data Truncation
  • Assigning longs to ints.
  • Example Problem
  • include ltstdlib.hgt
  • char string
  • ...
  • int index atol(string)
  • Solutions
  • long index atol(string)
  • int index atoi(string)
  • Other Cases
  • Beware of other constructs, such as passing a
    long to a function that accepts only an int.
  • Compiler Errors/Warnings
  • cc M2 warning 720 ... may overflow integer.
  • aCC w Warning (suggestion) 818 ... truncation
    in value may result.

7
Data Truncation
  • Storing pointers in ints.
  • Example Problem
  • int i
  • int j i
  • Explanation j can only contain a truncated copy
    of i.
  • Solution
  • int i
  • int j i
  • Compiler Errors/Warnings
  • cc warning 727 ... truncates pointer into 32
    bit integer.
  • aCC Error 203 ... Cannot assign 'int' with
    'int '.
  • aCC Error 818 ... truncation in value may
    result. (when there is a cast ie."j (int)i)

8
Data Truncation
  • Truncating function return values.
  • Example Problem
  • char buf malloc(1024)
  • Solution Without proper prototype, compiler
    assumes malloc() returns int.
  • include ltstdlib.hgt
  • char buf malloc(1024)
  • Compiler Errors/Warnings
  • cc warning 724 ... Initialization converts
    default int return type to pointer.
  • cc M0 warning 714 ... Function "malloc"
    called with no prototype or definition in scope.
  • aCC Error 328 ... Function 'malloc' has not
    been defined yet cannot call.

9
Data Truncation
  • Storing pointers in doubles.
  • Example Problem
  • void func(double d) Void p (void )d ...
  • ...
  • void p something
  • func((double)p) / real case I found in code /
  • Solution doubles, with 52 bits of mantissa,
    cannot represent a pointer.
  • / If you must store a pointer in a double,
  • use these macros. Both require an lvalue. /
  • define PTR_INTO_DOUBLE(p) (((double )((p))))
  • define PTR_FROM_DOUBLE(d) (((void )((d))))
  • Compiler Errors/Warnings
  • cc Error 1527 ... Incompatible types in cast
    Must cast from scalar to scalar or to void type.
  • aCC Error 331 ... Illegal cast expression
    cannot cast expression type 'void ' to 'double'.

10
Data Truncation
  • Inappropriate print specifiers.
  • Example Problem
  • include ltstdio.hgt
  • long val
  • scanf(d, val)
  • printf(Number at x is d\n, val, val)
  • Solution
  • ...
  • scanf(ld, val)
  • printf(Number at p is ld\n, val, val)
  • Compiler Errors/Warnings
  • None currently.

11
Data Type Promotion
  • Arithmetic between signed and unsigned numbers.
  • Example Problem
  • long result
  • int i -2
  • unsigned int j 1
  • result i j
  • Explanation Signed value, i, is promoted to
    unsigned.
  • ILP32 model yields result -1
    (0xffffffff)
  • LP64 model yields result 4294967295
    (0x00000000ffffffff)
  • Solution Be consistent, use same data types in
    equations.
  • Use all ints, all unsigned ints, or all longs,
    depending on need.
  • Learn the data type promotion rules.
  • Compiler Errors/Warnings
  • cc none currently.
  • aCC Warning (suggestion) 887 ... Type
    'unsigned int' is smaller than type.

12
Data Type Promotion
  • Comparing signed and unsigned numbers.
  • Example Problem
  • long L -1
  • unsigned int u 1
  • printf(ld s d\n, L, (L gt u) ? gt lt,
    u)
  • Explanation Data is promoted differently in
    ILP32 and LP64.
  • ILP32 prints -1 gt 1
  • LP64 prints -1 lt 1
  • Solution Be consistent, use same data types in
    comparisons.
  • long u 1
  • Solution Cast to same data types in
    comparisons.
  • (L lt (long)u)
  • Compiler Errors/Warnings
  • cc warning 734 ... Different types treated as
    signed for gt.
  • aCC none currently.

13
Pointers
  • Casting pointers to ints or ints to pointers.
  • Example Problem
  • struct S sptr
  • ...
  • sptr (struct S )((int)sptr sizeof(struct
    S))
  • Explanation
  • 64-bit pointers dont fit in 32 bits so they get
    truncated.
  • Solution
  • ...
  • sptr sptr 1
  • Compiler Errors/Warnings
  • cc warning 727 ... truncates pointer into 32
    bit integer.
  • aCC Error 818 ... truncation in value may
    result.

14
Pointers
  • Pointer arithmetic between longs and ints.
  • Example Problem
  • long L 0x123
  • int ptr L
  • if (L ptr)
  • ...
  • Explanation
  • ptr points to upper half of L not to L itself.
  • ptr is 0 because L is 0x00000000 00000123.
  • Solution
  • ...
  • long ptr L
  • Compiler Errors/Warnings
  • cc warning 728 ... Cast converts long to
    int.
  • aCC error 440 Cannot initialize int with
    long .

15
Data Alignment and Data Sharing
  • Invalid structure references.
  • Example Problem
  • typedef struct
  • char p
  • int value
  • S
  • S structptr
  • int intptr (int )structptr
  • intptr1 20 / set value within struct /
  • Explanation
  • 64-bit longs and pointers change the size of
    structures.
  • 64-bit longs and pointers change the offsets
    within structures.
  • Solution Use the structure definition.
  • structptr-gtvalue 20
  • Compiler Errors/Warnings
  • None currently.

0
96
32
64
p
i
ILP32
p
i
LP64
16
Data Alignment and Data Sharing
  • Hardcoded or assumed offsets within structures.
  • Example
  • typedef struct
  • int i
  • char p,c
  • S
  • Explanation
  • 64-bit constraints can change the alignment and
    padding of structures.
  • Best to put largest, most strictly aligned
    elements first in structure definition.
  • Be extra careful when reading binary data
    structures!
  • Compiler Errors/Warnings None.

17
Data Alignment and Data Sharing
  • Using unnamed and unqualified bit fields.
  • Example Problem
  • struct char foo
  • long 3 /unnamed/
  • short s5
  • S
  • Explanation
  • Padding varies. In this case, LP64 is less
    restrictive.
  • Solution
  • Dont define unnamed or unqualified bit fields.
  • Compiler Errors/Warnings
  • cc warning 750 ... Unnamed, non-zero bitfields
    do not affect alignment.
  • aCC none.

0
8
11
32
16
foo
s
pad
ILP32
foo
s
LP64
18
Hardcoded Constants
  • Hardcoded size of data types.
  • Example Problem
  • offset n 4 / assumes 32 bits /
  • Solution
  • offset n sizeof(int) / offset of true int
    /
  • Solution
  • offset n sizeof(long) / offset of true
    long /
  • Solution
  • long val
  • offset n sizeof(val) / offset to size of
    val /
  • Compiler Errors/Warnings
  • None.

19
Hardcoded Constants
  • Using literals and masks that assume 32 bits.
  • Watch for these and other typical hardcoded
    constants
  • 4 Number of bytes in 32-bit word.
  • 32 Number of bits in 32-bit word.
  • 0x80000000 Min value of signed 32-bit word.
  • 0x7fffffff Max value of signed 32-bit word.
  • 0xffffffff Max value of unsigned 32-bit word.
  • Use defined data types as found in ltinttypes.hgt
  • Compiler Errors/Warnings
  • None currently.

20
Hardcoded Constants
  • Hardcoded bit shift values.
  • Example Problem
  • unsigned long n
  • n 1 ltlt (32 - 1) / set sign bit /
  • Explanation Cannot assume 32-bit longs!
  • Solution
  • include ltlimits.hgt
  • unsigned long n
  • n LONG_MAX
  • Solution
  • include ltlimits.hgt
  • unsigned long n
  • n 1L ltlt (LONG_BIT - 1)
  • Compiler Errors/Warnings
  • None.

21
Hardcoded Constants
  • Hardcoded constants with malloc(), memory(3),
    string(3).
  • Example Problem
  • include ltstdlib.hgt
  • define BSIZE 4096
  • char ptr_buf
  • ptr_buf (char )malloc(BSIZE 4)
  • Explanation Assumes pointer is 4 bytes.
  • Solution
  • ...
  • void ptr_buf
  • ptr_buf (void )malloc(BSIZE sizeof(void
    ))
  • Compiler Errors/Warnings
  • None currently.

22
64-bit Safe vs. 64-bit Aware
  • 64-bit Safe .
  • Compile for 64-bit (DD64 or DA2.0W).
  • Fix all compile errors.
  • Consider fixing compile warnings including M2
    warnings.
  • Insure that all non-portable constructs are
    fixed.
  • 64-bit Aware.
  • Convert program internals to use the 64-bit
    addressing.
  • Beware of programs that do their own memory
    management.

23
Performance
  • Impacts of port.
  • Larger process/data space affects cache and TLB
    hit rates.
  • 64-bit long integer division is slow.
  • 32-bit array indices must be sign extended
    repeatedly.
  • Code is always position independent (PIC).
  • Tuning.
  • Avoid mixing 32 and 64-bit operations.
  • Fix array references to use long indices.
  • Avoid 64-bit long division.
  • Use Onoextern if object modules are not in
    shared library.
  • Use ESfic if fully archive bound.

24
Porting Code to 64-bit Summary
  • Why?
  • Large files address space.
  • Performance advantage of memory over disk I/O.
  • Beware!
  • Data size differences (pointers, longs and
    predefined types).
  • Data truncation (casts, return values...).
  • Data type promotion pitfalls.
  • Data alignment and padding differences.
  • Hardcoded constants.
  • 64-bit Safe vs. 64-bit Aware.
  • Compile DA2.0W, fix compile/link errors and
    possibly M2 warnings.
  • Convert program to really use 64-bit addressing.
  • Impacts.
  • Performance costs are real but tuning can help.
Write a Comment
User Comments (0)
About PowerShow.com