Sunday, 11 August 2013

COBOL TRUNC Compiler Option: STD, OPT, and BIN Explained

01 BIN-VALUE PIC S9(4) COMP. has a four-digit COBOL PICTURE but normally occupies a two-byte binary field. The COBOL TRUNC compiler option decides whether moves and arithmetic respect the four decimal digits, assume that incoming data already fits them, or use the full native binary capacity.

COBOL TRUNC compiler option comparison for TRUNC STD, TRUNC OPT, TRUNC BIN, and COMP-5
TRUNC(STD) follows PICTURE digits, TRUNC(OPT) assumes conforming data, and TRUNC(BIN) uses native binary boundaries.

What the COBOL TRUNC option controls

TRUNC(STD|OPT|BIN) affects binary data during MOVE and arithmetic operations. In IBM Enterprise COBOL, BINARY, COMP, and COMP-4 are the relevant usages. The setting can change generated instructions and program results.

The option matters because a binary item's storage capacity can be wider than the decimal range stated by its PICTURE. A signed PIC S9(4) COMP item describes values through four decimal digits, yet its two-byte signed representation can hold a larger native range.

Scope: TRUNC does not control DISPLAY or packed-decimal fields, and it has no effect on COMP-5. The COBOL USAGE clause guide compares those storage formats.

TRUNC(STD): truncate to the PICTURE digits

TRUNC(STD) is the documented default. For a BINARY receiving field in a MOVE or arithmetic result, it truncates to the number of digits in that field's PICTURE. IBM identifies STD as the option that conforms to the 85 COBOL Standard.

01  BIN-VALUE  PIC S9(4) COMP.

    MOVE 12345 TO BIN-VALUE
    DISPLAY BIN-VALUE

Under STD, the receiving item is limited to four decimal digits, so the excess high-order digit is removed and the value is 2345. This is decimal or base-10 truncation; it is not a test for whether 12345 would physically fit in two bytes.

Choose STD when the program requires PICTURE-based truncation or when compatibility with that defined behavior matters more than the extra generated work.

TRUNC(OPT): assume data conforms to PICTURE

TRUNC(OPT) lets the compiler assume that values moved into BINARY areas conform to their PICTURE specifications. It can avoid the extra runtime truncation code generated by STD or BIN and generally provides the best performance of the three choices.

OPT is safe only when every value supplied to a BINARY item stays within its declared decimal precision. If the preceding example moves 12345 into PIC S9(4) COMP, that assumption is already false. IBM warns that the result can depend on the generated code sequence, so OPT must not be used to obtain a particular out-of-range value.

Data contract required: do not switch a program to TRUNC(OPT) solely for speed. First find binary fields populated by files, Db2, IMS, C, PL/I, assembler, copybooks, or calls where the producer might store values beyond the PICTURE digits.

TRUNC(BIN): use native binary boundaries

TRUNC(BIN) treats BINARY, COMP, and COMP-4 items as native hardware binary fields, as though each had been declared COMP-5. Receiving values are truncated at two-, four-, or eight-byte boundaries instead of the decimal PICTURE limit. Sending binary fields also use their full storage content when the receiver is numeric.

For the PIC S9(4) COMP example, 12345 fits in a signed two-byte field, so BIN preserves it even though it exceeds the four-digit PICTURE range. A still larger value can wrap or otherwise be truncated at the two-byte boundary.

IBM recommends BIN for programs that receive native binary values from other products when those values might not conform to the COBOL PICTURE. The tradeoff is generated code and intermediate handling: BIN is usually the slowest TRUNC choice.

TRUNC comparison table

OptionBinary ruleUse whenMain caution
TRUNC(STD)Truncates to decimal PICTURE digits.Defined PICTURE-based behavior or standard conformance is required.Generates extra work when BINARY receiving fields change.
TRUNC(OPT)Compiler assumes the value conforms to PICTURE and chooses an efficient sequence.All producers honor the declared precision and testing confirms it.Out-of-range data can produce unpredictable results.
TRUNC(BIN)Uses the full two-, four-, or eight-byte native binary field.External producers can supply valid native binary values beyond PICTURE digits.Usually the slowest option and changes DISPLAY/send behavior for binary data.

TRUNC and COMP-5 are not the same decision

A COMP-5 declaration is a data-item choice. That item always uses native binary behavior regardless of the program's TRUNC setting. By contrast, TRUNC(BIN) applies native behavior broadly to BINARY, COMP, and COMP-4 items throughout the compiled program.

Use COMP-5 when a specific copybook field has a native binary contract. This documents the requirement at the field and avoids changing unrelated COMP items. Use TRUNC(BIN) when the entire program requires that treatment or changing the copybook is not feasible.

Specify TRUNC in source or compilation JCL

A source option is direct and visible in the program:

CBL TRUNC(OPT)
IDENTIFICATION DIVISION.
PROGRAM-ID. PAYCALC.

The compiler option can also be passed to the compiler step:

//COBOL    EXEC PGM=IGYCRCTL,
//             PARM='TRUNC(OPT),LIST,MAP'
//SYSPRINT DD  SYSOUT=*
//SYSIN    DD  DSN=APP.COBOL(PAYCALC),DISP=SHR

If a cataloged compile procedure is used, qualify PARM with the procedure's internal compiler-step name, such as PARM.COBOL='TRUNC(OPT)'. The JCL cataloged procedure guide explains qualified EXEC overrides.

Always inspect the compiler listing. A site can set installation defaults or fix an option so that it cannot be overridden. The listing records the effective compiler options used for that load module.

Test before changing STD, OPT, or BIN

  1. Inventory BINARY, COMP, and COMP-4 fields, including copybooks.
  2. Identify values supplied by non-COBOL programs, subsystems, files, and calls.
  3. Compare each producer's native range with the receiving PICTURE digits.
  4. Compile a test version with NUMCHECK(BIN(TRUNCBIN)) when evaluating a move from BIN toward STD or OPT.
  5. Exercise maximum, minimum, negative, overflow, SIZE ERROR, and DISPLAY cases.
  6. Compare compiler listings and regression output before promotion.

NUMCHECK(BIN(TRUNCBIN)) can detect binary contents that exceed the PICTURE definition and would matter when moving away from TRUNC(BIN). It adds runtime checking, so use it as a controlled test aid rather than assuming it belongs permanently in a performance-sensitive build.

Common TRUNC mistakes

  • Assuming a value that fits the physical binary field also conforms to its PICTURE.
  • Expecting TRUNC to change COMP-5, COMP-3, or DISPLAY data.
  • Using OPT with unchecked binary values from another language or subsystem.
  • Setting BIN for every program when only one interface field needs COMP-5.
  • Changing the option and comparing only normal values rather than boundary cases.
  • Reading the JCL option but not confirming the effective value in the compiler listing.

TRUNC is only one numeric-code-generation decision. Review the COBOL ARITH compiler option for decimal intermediate precision and the COBOL SSRANGE option for subscript and reference-modification checks.

Selection checklist

  • Choose STD when PICTURE-digit truncation is the required result.
  • Choose OPT only when all BINARY data obeys its PICTURE and tests support the change.
  • Choose BIN when the full native binary value is part of an external data contract.
  • Choose COMP-5 for an individual field that explicitly needs native binary semantics.
  • Document the choice because a later compiler-option change can alter results without changing source statements.

For related compiler settings, see the COBOL DATA compiler option and the COBOL OPTIMIZE compiler option.

Official IBM references

COBOL TRUNC compiler option FAQ

What is the default COBOL TRUNC compiler option?

TRUNC(STD) is the documented default for current IBM Enterprise COBOL for z/OS. Installation defaults can be changed, so confirm the option listing for the compiled program.

Which COBOL fields are affected by TRUNC?

TRUNC controls BINARY, COMP, and COMP-4 processing in moves and arithmetic. It does not affect COMP-5 items, which always use native binary storage behavior.

When should TRUNC(BIN) be used?

Use it when binary fields can receive valid native binary values from Db2, IMS, C, PL/I, or another producer even when those values exceed the COBOL PICTURE digit limit.

Can changing TRUNC alter program results?

Yes. The setting changes runtime logic, so the same source can produce different values when a binary field contains data outside its PICTURE specification. Compile and regression-test before switching.

Treat TRUNC as part of the binary data contract: verify who writes each value, what its PICTURE promises, and which boundary the receiving program must enforce.

No comments:

Post a Comment

New In-feed ads