Files
calc/zprime.c
Landon Curt Noll ff90bc0e3a add E_STRING to error, errno, strerror, change multiple E_STRING's
While help/errstr has been added, the errstr builtin function is
not yet written.  In anticipation of the new errstr builtin the
rest of the calc error system has been updated to associated errsym
E_STRING's with errnum error codes and errmsg error messages.

Minor improvements to help/rand.

The verify_error_table() function that does a verification
the error_table[] array and setup private_error_alias[] array
is now called by libcalc_call_me_first().

Fix comment about wrong include file in have_sys_mount.h.

Removed unused booltostr() and strtobool() macros from bool.h.

Moved define of math_error(char *, ...) from zmath.h to errtbl.h.
The errtbl.h include file, unless ERRCODE_SRC is defined
also includes attribute.h and errsym.h.

Group calc error related builtin support functions together in func.c.

Make switch indenting in func.c consistent.

Passing an invalid argument to error(), errno() or strerror() will
set errno AND throw a math error.  Before errno would be set and
an error value was returned.  Before there was no way to tell if
the error value was a result of the arg or if an error detected.

Added E_STRING to error([errnum | "E_STRING"]) builtin function.
Added E_STRING to errno([errnum | "E_STRING"]) builtin function.
Added E_STRING to strerror([errnum | "E_STRING"]) builtin function.
Calling these functions with an E_STRING errsym is the same as calling
them with the matching errnum code.

Standardized on calc computation error related E_STRING strings
where there are a set of related codes.  Changed "E_...digits" into
"E_..._digits".  For example, E_FPUTC1 became E_FPUTC_1, E_FPUTC2
became E_FPUTC_2, and E_FPUTC3 became E_FPUTC_3.  In a few cases
such as E_APPR became E_APPR_1, because there was a E_APPR2 (which
became E_APPR_2) and E_APPR3 (which became E_APPR_3).  To other
special cases, E_ILOG10 became E_IBASE10_LOG and E_ILOG2 became
E_IBASE2_LOG because E_ILOG10 and E_ILOG2 are both independent calc
computation error related E_STRING strings.  Now related sets of
E_STRING strings end in _ (underscore) followed by digits.

The following is the list of E_STRING strings changes:

    E_APPR ==> E_APPR_1
    E_ROUND ==> E_ROUND_1
    E_SQRT ==> E_SQRT_1
    E_ROOT ==> E_ROOT_1
    E_SHIFT ==> E_SHIFT_1
    E_SCALE ==> E_SCALE_1
    E_POWI ==> E_POWI_1
    E_POWER ==> E_POWER_1
    E_QUO ==> E_QUO_1
    E_MOD ==> E_MOD_1
    E_ABS ==> E_ABS_1
    E_APPR2 ==> E_APPR_2
    E_APPR3 ==> E_APPR_3
    E_ROUND2 ==> E_ROUND_2
    E_ROUND3 ==> E_ROUND_3
    E_BROUND2 ==> E_BROUND_2
    E_BROUND3 ==> E_BROUND_3
    E_SQRT2 ==> E_SQRT_2
    E_SQRT3 ==> E_SQRT_3
    E_ROOT2 ==> E_ROOT_2
    E_ROOT3 ==> E_ROOT_3
    E_SHIFT2 ==> E_SHIFT_2
    E_SCALE2 ==> E_SCALE_2
    E_POWI2 ==> E_POWI_2
    E_POWER2 ==> E_POWER_2
    E_POWER3 ==> E_POWER_3
    E_QUO2 ==> E_QUO_2
    E_QUO3 ==> E_QUO_3
    E_MOD2 ==> E_MOD_2
    E_MOD3 ==> E_MOD_3
    E_ABS2 ==> E_ABS_2
    E_EXP1 ==> E_EXP_1
    E_EXP2 ==> E_EXP_2
    E_FPUTC1 ==> E_FPUTC_1
    E_FPUTC2 ==> E_FPUTC_2
    E_FPUTC3 ==> E_FPUTC_3
    E_FGETC1 ==> E_FGETC_1
    E_FGETC2 ==> E_FGETC_2
    E_FOPEN1 ==> E_FOPEN_1
    E_FOPEN2 ==> E_FOPEN_2
    E_FREOPEN1 ==> E_FREOPEN_1
    E_FREOPEN2 ==> E_FREOPEN_2
    E_FREOPEN3 ==> E_FREOPEN_3
    E_FCLOSE1 ==> E_FCLOSE_1
    E_FPUTS1 ==> E_FPUTS_1
    E_FPUTS2 ==> E_FPUTS_2
    E_FPUTS3 ==> E_FPUTS_3
    E_FGETS1 ==> E_FGETS_1
    E_FGETS2 ==> E_FGETS_2
    E_FPUTSTR1 ==> E_FPUTSTR_1
    E_FPUTSTR2 ==> E_FPUTSTR_2
    E_FPUTSTR3 ==> E_FPUTSTR_3
    E_FGETSTR1 ==> E_FGETSTR_1
    E_FGETSTR2 ==> E_FGETSTR_2
    E_FGETLINE1 ==> E_FGETLINE_1
    E_FGETLINE2 ==> E_FGETLINE_2
    E_FGETFIELD1 ==> E_FGETFIELD_1
    E_FGETFIELD2 ==> E_FGETFIELD_2
    E_REWIND1 ==> E_REWIND_1
    E_PRINTF1 ==> E_PRINTF_1
    E_PRINTF2 ==> E_PRINTF_2
    E_FPRINTF1 ==> E_FPRINTF_1
    E_FPRINTF2 ==> E_FPRINTF_2
    E_FPRINTF3 ==> E_FPRINTF_3
    E_STRPRINTF1 ==> E_STRPRINTF_1
    E_STRPRINTF2 ==> E_STRPRINTF_2
    E_FSCAN1 ==> E_FSCAN_1
    E_FSCAN2 ==> E_FSCAN_2
    E_FSCANF1 ==> E_FSCANF_1
    E_FSCANF2 ==> E_FSCANF_2
    E_FSCANF3 ==> E_FSCANF_3
    E_FSCANF4 ==> E_FSCANF_4
    E_STRSCANF1 ==> E_STRSCANF_1
    E_STRSCANF2 ==> E_STRSCANF_2
    E_STRSCANF3 ==> E_STRSCANF_3
    E_STRSCANF4 ==> E_STRSCANF_4
    E_SCANF1 ==> E_SCANF_1
    E_SCANF2 ==> E_SCANF_2
    E_SCANF3 ==> E_SCANF_3
    E_FTELL1 ==> E_FTELL_1
    E_FTELL2 ==> E_FTELL_2
    E_FSEEK1 ==> E_FSEEK_1
    E_FSEEK2 ==> E_FSEEK_2
    E_FSEEK3 ==> E_FSEEK_3
    E_FSIZE1 ==> E_FSIZE_1
    E_FSIZE2 ==> E_FSIZE_2
    E_FEOF1 ==> E_FEOF_1
    E_FEOF2 ==> E_FEOF_2
    E_FERROR1 ==> E_FERROR_1
    E_FERROR2 ==> E_FERROR_2
    E_UNGETC1 ==> E_UNGETC_1
    E_UNGETC2 ==> E_UNGETC_2
    E_UNGETC3 ==> E_UNGETC_3
    E_ISATTY1 ==> E_ISATTY_1
    E_ISATTY2 ==> E_ISATTY_2
    E_ACCESS1 ==> E_ACCESS_1
    E_ACCESS2 ==> E_ACCESS_2
    E_SEARCH1 ==> E_SEARCH_1
    E_SEARCH2 ==> E_SEARCH_2
    E_SEARCH3 ==> E_SEARCH_3
    E_SEARCH4 ==> E_SEARCH_4
    E_SEARCH5 ==> E_SEARCH_5
    E_SEARCH6 ==> E_SEARCH_6
    E_RSEARCH1 ==> E_RSEARCH_1
    E_RSEARCH2 ==> E_RSEARCH_2
    E_RSEARCH3 ==> E_RSEARCH_3
    E_RSEARCH4 ==> E_RSEARCH_4
    E_RSEARCH5 ==> E_RSEARCH_5
    E_RSEARCH6 ==> E_RSEARCH_6
    E_REWIND2 ==> E_REWIND_2
    E_STRERROR1 ==> E_STRERROR_1
    E_STRERROR2 ==> E_STRERROR_2
    E_COS1 ==> E_COS_1
    E_COS2 ==> E_COS_2
    E_SIN1 ==> E_SIN_1
    E_SIN2 ==> E_SIN_2
    E_EVAL2 ==> E_EVAL_2
    E_ARG1 ==> E_ARG_1
    E_ARG2 ==> E_ARG_2
    E_POLAR1 ==> E_POLAR_1
    E_POLAR2 ==> E_POLAR_2
    E_MATFILL1 ==> E_MATFILL_1
    E_MATFILL2 ==> E_MATFILL_2
    E_MATTRANS1 ==> E_MATTRANS_1
    E_MATTRANS2 ==> E_MATTRANS_2
    E_DET1 ==> E_DET_1
    E_DET2 ==> E_DET_2
    E_DET3 ==> E_DET_3
    E_MATMIN1 ==> E_MATMIN_1
    E_MATMIN2 ==> E_MATMIN_2
    E_MATMIN3 ==> E_MATMIN_3
    E_MATMAX1 ==> E_MATMAX_1
    E_MATMAX2 ==> E_MATMAX_2
    E_MATMAX3 ==> E_MATMAX_3
    E_CP1 ==> E_CP_1
    E_CP2 ==> E_CP_2
    E_CP3 ==> E_CP_3
    E_DP1 ==> E_DP_1
    E_DP2 ==> E_DP_2
    E_DP3 ==> E_DP_3
    E_SUBSTR1 ==> E_SUBSTR_1
    E_SUBSTR2 ==> E_SUBSTR_2
    E_INSERT1 ==> E_INSERT_1
    E_INSERT2 ==> E_INSERT_2
    E_DELETE1 ==> E_DELETE_1
    E_DELETE2 ==> E_DELETE_2
    E_LN1 ==> E_LN_1
    E_LN2 ==> E_LN_2
    E_ERROR1 ==> E_ERROR_1
    E_ERROR2 ==> E_ERROR_2
    E_EVAL3 ==> E_EVAL_3
    E_EVAL4 ==> E_EVAL_4
    E_RM1 ==> E_RM_1
    E_RM2 ==> E_RM_2
    E_BLK1 ==> E_BLK_1
    E_BLK2 ==> E_BLK_2
    E_BLK3 ==> E_BLK_3
    E_BLK4 ==> E_BLK_4
    E_BLKFREE1 ==> E_BLKFREE_1
    E_BLKFREE2 ==> E_BLKFREE_2
    E_BLKFREE3 ==> E_BLKFREE_3
    E_BLKFREE4 ==> E_BLKFREE_4
    E_BLKFREE5 ==> E_BLKFREE_5
    E_BLOCKS1 ==> E_BLOCKS_1
    E_BLOCKS2 ==> E_BLOCKS_2
    E_COPY1 ==> E_COPY_01
    E_COPY2 ==> E_COPY_02
    E_COPY3 ==> E_COPY_03
    E_COPY4 ==> E_COPY_04
    E_COPY5 ==> E_COPY_05
    E_COPY6 ==> E_COPY_06
    E_COPY7 ==> E_COPY_07
    E_COPY8 ==> E_COPY_08
    E_COPY9 ==> E_COPY_09
    E_COPY10 ==> E_COPY_10
    E_COPY11 ==> E_COPY_11
    E_COPY12 ==> E_COPY_12
    E_COPY13 ==> E_COPY_13
    E_COPY14 ==> E_COPY_14
    E_COPY15 ==> E_COPY_15
    E_COPY16 ==> E_COPY_16
    E_COPY17 ==> E_COPY_17
    E_COPYF1 ==> E_COPYF_1
    E_COPYF2 ==> E_COPYF_2
    E_COPYF3 ==> E_COPYF_3
    E_COPYF4 ==> E_COPYF_4
    E_PROTECT1 ==> E_PROTECT_1
    E_PROTECT2 ==> E_PROTECT_2
    E_PROTECT3 ==> E_PROTECT_3
    E_MATFILL3 ==> E_MATFILL_3
    E_MATFILL4 ==> E_MATFILL_4
    E_MATTRACE1 ==> E_MATTRACE_1
    E_MATTRACE2 ==> E_MATTRACE_2
    E_MATTRACE3 ==> E_MATTRACE_3
    E_TAN1 ==> E_TAN_1
    E_TAN2 ==> E_TAN_2
    E_COT1 ==> E_COT_1
    E_COT2 ==> E_COT_2
    E_SEC1 ==> E_SEC_1
    E_SEC2 ==> E_SEC_2
    E_CSC1 ==> E_CSC_1
    E_CSC2 ==> E_CSC_2
    E_SINH1 ==> E_SINH_1
    E_SINH2 ==> E_SINH_2
    E_COSH1 ==> E_COSH_1
    E_COSH2 ==> E_COSH_2
    E_TANH1 ==> E_TANH_1
    E_TANH2 ==> E_TANH_2
    E_COTH1 ==> E_COTH_1
    E_COTH2 ==> E_COTH_2
    E_SECH1 ==> E_SECH_1
    E_SECH2 ==> E_SECH_2
    E_CSCH1 ==> E_CSCH_1
    E_CSCH2 ==> E_CSCH_2
    E_ASIN1 ==> E_ASIN_1
    E_ASIN2 ==> E_ASIN_2
    E_ACOS1 ==> E_ACOS_1
    E_ACOS2 ==> E_ACOS_2
    E_ATAN1 ==> E_ATAN_1
    E_ATAN2 ==> E_ATAN_2
    E_ACOT1 ==> E_ACOT_1
    E_ACOT2 ==> E_ACOT_2
    E_ASEC1 ==> E_ASEC_1
    E_ASEC2 ==> E_ASEC_2
    E_ACSC1 ==> E_ACSC_1
    E_ACSC2 ==> E_ACSC_2
    E_ASINH1 ==> E_ASINH_1
    E_ASINH2 ==> E_ASINH_2
    E_ACOSH1 ==> E_ACOSH_1
    E_ACOSH2 ==> E_ACOSH_2
    E_ATANH1 ==> E_ATANH_1
    E_ATANH2 ==> E_ATANH_2
    E_ACOTH1 ==> E_ACOTH_1
    E_ACOTH2 ==> E_ACOTH_2
    E_ASECH1 ==> E_ASECH_1
    E_ASECH2 ==> E_ASECH_2
    E_ACSCH1 ==> E_ACSCH_1
    E_ACSCH2 ==> E_ACSCH_2
    E_GD1 ==> E_GD_1
    E_GD2 ==> E_GD_2
    E_AGD1 ==> E_AGD_1
    E_AGD2 ==> E_AGD_2
    E_BIT1 ==> E_BIT_1
    E_BIT2 ==> E_BIT_2
    E_SETBIT1 ==> E_SETBIT_1
    E_SETBIT2 ==> E_SETBIT_2
    E_SETBIT3 ==> E_SETBIT_3
    E_SEG1 ==> E_SEG_1
    E_SEG2 ==> E_SEG_2
    E_SEG3 ==> E_SEG_3
    E_HIGHBIT1 ==> E_HIGHBIT_1
    E_HIGHBIT2 ==> E_HIGHBIT_2
    E_LOWBIT1 ==> E_LOWBIT_1
    E_LOWBIT2 ==> E_LOWBIT_2
    E_HEAD1 ==> E_HEAD_1
    E_HEAD2 ==> E_HEAD_2
    E_TAIL1 ==> E_TAIL_1
    E_TAIL2 ==> E_TAIL_2
    E_XOR1 ==> E_XOR_1
    E_XOR2 ==> E_XOR_2
    E_INDICES1 ==> E_INDICES_1
    E_INDICES2 ==> E_INDICES_2
    E_EXP3 ==> E_EXP_3
    E_SINH3 ==> E_SINH_3
    E_COSH3 ==> E_COSH_3
    E_SIN3 ==> E_SIN_3
    E_COS3 ==> E_COS_3
    E_GD3 ==> E_GD_3
    E_AGD3 ==> E_AGD_3
    E_POWER4 ==> E_POWER_4
    E_ROOT4 ==> E_ROOT_4
    E_DGT1 ==> E_DGT_1
    E_DGT2 ==> E_DGT_2
    E_DGT3 ==> E_DGT_3
    E_PLCS1 ==> E_PLCS_1
    E_PLCS2 ==> E_PLCS_2
    E_DGTS1 ==> E_DGTS_1
    E_DGTS2 ==> E_DGTS_2
    E_ILOG10 ==> E_IBASE10_LOG
    E_ILOG2 ==> E_IBASE2_LOG
    E_COMB1 ==> E_COMB_1
    E_COMB2 ==> E_COMB_2
    E_ASSIGN1 ==> E_ASSIGN_1
    E_ASSIGN2 ==> E_ASSIGN_2
    E_ASSIGN3 ==> E_ASSIGN_3
    E_ASSIGN4 ==> E_ASSIGN_4
    E_ASSIGN5 ==> E_ASSIGN_5
    E_ASSIGN6 ==> E_ASSIGN_6
    E_ASSIGN7 ==> E_ASSIGN_7
    E_ASSIGN8 ==> E_ASSIGN_8
    E_ASSIGN9 ==> E_ASSIGN_9
    E_SWAP1 ==> E_SWAP_1
    E_SWAP2 ==> E_SWAP_2
    E_SWAP3 ==> E_SWAP_3
    E_QUOMOD1 ==> E_QUOMOD_1
    E_QUOMOD2 ==> E_QUOMOD_2
    E_QUOMOD3 ==> E_QUOMOD_3
    E_PREINC1 ==> E_PREINC_1
    E_PREINC2 ==> E_PREINC_2
    E_PREINC3 ==> E_PREINC_3
    E_PREDEC1 ==> E_PREDEC_1
    E_PREDEC2 ==> E_PREDEC_2
    E_PREDEC3 ==> E_PREDEC_3
    E_POSTINC1 ==> E_POSTINC_1
    E_POSTINC2 ==> E_POSTINC_2
    E_POSTINC3 ==> E_POSTINC_3
    E_POSTDEC1 ==> E_POSTDEC_1
    E_POSTDEC2 ==> E_POSTDEC_2
    E_POSTDEC3 ==> E_POSTDEC_3
    E_INIT1 ==> E_INIT_01
    E_INIT2 ==> E_INIT_02
    E_INIT3 ==> E_INIT_03
    E_INIT4 ==> E_INIT_04
    E_INIT5 ==> E_INIT_05
    E_INIT6 ==> E_INIT_06
    E_INIT7 ==> E_INIT_07
    E_INIT8 ==> E_INIT_08
    E_INIT9 ==> E_INIT_09
    E_INIT10 ==> E_INIT_10
    E_LIST1 ==> E_LIST_1
    E_LIST2 ==> E_LIST_2
    E_LIST3 ==> E_LIST_3
    E_LIST4 ==> E_LIST_4
    E_LIST5 ==> E_LIST_5
    E_LIST6 ==> E_LIST_6
    E_MODIFY1 ==> E_MODIFY_1
    E_MODIFY2 ==> E_MODIFY_2
    E_MODIFY3 ==> E_MODIFY_3
    E_MODIFY4 ==> E_MODIFY_4
    E_MODIFY5 ==> E_MODIFY_5
    E_FPATHOPEN1 ==> E_FPATHOPEN_1
    E_FPATHOPEN2 ==> E_FPATHOPEN_2
    E_LOG1 ==> E_LOG_1
    E_LOG2 ==> E_LOG_2
    E_LOG3 ==> E_LOG_3
    E_FGETFILE1 ==> E_FGETFILE_1
    E_FGETFILE2 ==> E_FGETFILE_2
    E_FGETFILE3 ==> E_FGETFILE_3
    E_TAN3 ==> E_TAN_3
    E_TAN4 ==> E_TAN_4
    E_COT3 ==> E_COT_3
    E_COT4 ==> E_COT_4
    E_SEC3 ==> E_SEC_3
    E_CSC3 ==> E_CSC_3
    E_TANH3 ==> E_TANH_3
    E_TANH4 ==> E_TANH_4
    E_COTH3 ==> E_COTH_3
    E_COTH4 ==> E_COTH_4
    E_SECH3 ==> E_SECH_3
    E_CSCH3 ==> E_CSCH_3
    E_ASIN3 ==> E_ASIN_3
    E_ACOS3 ==> E_ACOS_3
    E_ASINH3 ==> E_ASINH_3
    E_ACOSH3 ==> E_ACOSH_3
    E_ATAN3 ==> E_ATAN_3
    E_ACOT3 ==> E_ACOT_3
    E_ASEC3 ==> E_ASEC_3
    E_ACSC3 ==> E_ACSC_3
    E_ATANH3 ==> E_ATANH_3
    E_ACOTH3 ==> E_ACOTH_3
    E_ASECH3 ==> E_ASECH_3
    E_ACSCH3 ==> E_ACSCH_3
    E_D2R1 ==> E_D2R_1
    E_D2R2 ==> E_D2R_2
    E_R2D1 ==> E_R2D_1
    E_R2D2 ==> E_R2D_2
    E_G2R1 ==> E_G2R_1
    E_G2R2 ==> E_G2R_2
    E_R2G1 ==> E_R2G_1
    E_R2G2 ==> E_R2G_2
    E_D2G1 ==> E_D2G_1
    E_G2D1 ==> E_G2D_1
    E_D2DMS1 ==> E_D2DMS_1
    E_D2DMS2 ==> E_D2DMS_2
    E_D2DMS3 ==> E_D2DMS_3
    E_D2DMS4 ==> E_D2DMS_4
    E_D2DM1 ==> E_D2DM_1
    E_D2DM2 ==> E_D2DM_2
    E_D2DM3 ==> E_D2DM_3
    E_D2DM4 ==> E_D2DM_4
    E_G2GMS1 ==> E_G2GMS_1
    E_G2GMS2 ==> E_G2GMS_2
    E_G2GMS3 ==> E_G2GMS_3
    E_G2GMS4 ==> E_G2GMS_4
    E_G2GM1 ==> E_G2GM_1
    E_G2GM2 ==> E_G2GM_2
    E_G2GM3 ==> E_G2GM_3
    E_G2GM4 ==> E_G2GM_4
    E_H2HMS1 ==> E_H2HMS_1
    E_H2HMS2 ==> E_H2HMS_2
    E_H2HMS3 ==> E_H2HMS_3
    E_H2HMS4 ==> E_H2HMS_4
    E_H2HM1 ==> E_H2HM_1
    E_H2HM2 ==> E_H2HM_2
    E_H2HM3 ==> E_H2HM_3
    E_H2HM4 ==> E_H2HM_4
    E_DMS2D1 ==> E_DMS2D_1
    E_DMS2D2 ==> E_DMS2D_2
    E_DM2D1 ==> E_DM2D_1
    E_DM2D2 ==> E_DM2D_2
    E_GMS2G1 ==> E_GMS2G_1
    E_GMS2G2 ==> E_GMS2G_2
    E_GM2G1 ==> E_GM2G_1
    E_GM2G2 ==> E_GM2G_2
    E_HMS2H1 ==> E_HMS2H_1
    E_HMS2H2 ==> E_HMS2H_2
    E_HM2H1 ==> E_HM2H_1
    E_HM2H2 ==> E_HM2H_2
    E_VERSIN1 ==> E_VERSIN_1
    E_VERSIN2 ==> E_VERSIN_2
    E_VERSIN3 ==> E_VERSIN_3
    E_AVERSIN1 ==> E_AVERSIN_1
    E_AVERSIN2 ==> E_AVERSIN_2
    E_AVERSIN3 ==> E_AVERSIN_3
    E_COVERSIN1 ==> E_COVERSIN_1
    E_COVERSIN2 ==> E_COVERSIN_2
    E_COVERSIN3 ==> E_COVERSIN_3
    E_ACOVERSIN1 ==> E_ACOVERSIN_1
    E_ACOVERSIN2 ==> E_ACOVERSIN_2
    E_ACOVERSIN3 ==> E_ACOVERSIN_3
    E_VERCOS1 ==> E_VERCOS_1
    E_VERCOS2 ==> E_VERCOS_2
    E_VERCOS3 ==> E_VERCOS_3
    E_AVERCOS1 ==> E_AVERCOS_1
    E_AVERCOS2 ==> E_AVERCOS_2
    E_AVERCOS3 ==> E_AVERCOS_3
    E_COVERCOS1 ==> E_COVERCOS_1
    E_COVERCOS2 ==> E_COVERCOS_2
    E_COVERCOS3 ==> E_COVERCOS_3
    E_ACOVERCOS1 ==> E_ACOVERCOS_1
    E_ACOVERCOS2 ==> E_ACOVERCOS_2
    E_ACOVERCOS3 ==> E_ACOVERCOS_3
    E_TAN5 ==> E_TAN_5
    E_COT5 ==> E_COT_5
    E_COT6 ==> E_COT_6
    E_SEC5 ==> E_SEC_5
    E_CSC5 ==> E_CSC_5
    E_CSC6 ==> E_CSC_6
2023-09-19 18:34:21 -07:00

1652 lines
35 KiB
C

/*
* zprime - rapid small prime routines
*
* Copyright (C) 1999-2007,2021-2023 Landon Curt Noll
*
* Calc is open software; you can redistribute it and/or modify it under
* the terms of the version 2.1 of the GNU Lesser General Public License
* as published by the Free Software Foundation.
*
* Calc is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
* Public License for more details.
*
* A copy of version 2.1 of the GNU Lesser General Public License is
* distributed with calc under the filename COPYING-LGPL. You should have
* received a copy with calc; if not, write to Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Under source code control: 1994/05/29 04:34:36
* File existed as early as: 1994
*
* chongo <was here> /\oo/\ http://www.isthe.com/chongo/
* Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/
*/
#include "zmath.h"
#include "prime.h"
#include "jump.h"
#include "config.h"
#include "zrand.h"
#include "have_const.h"
#include "errtbl.h"
#include "banned.h" /* include after system header <> includes */
/*
* When performing a probabilistic primality test, check to see
* if the number has a factor <= PTEST_PRECHECK.
*
* XXX - what should this value be? Perhaps this should be a function
* of the size of the text value and the number of tests?
*/
#define PTEST_PRECHECK ((FULL)101)
/*
* product of primes that fit into a long
*/
STATIC CONST FULL pfact_tbl[MAX_PFACT_VAL+1] = {
1, 1, 2, 6, 6, 30, 30, 210, 210, 210, 210, 2310, 2310, 30030, 30030,
30030, 30030, 510510, 510510, 9699690, 9699690, 9699690, 9699690,
223092870, 223092870, 223092870, 223092870, 223092870, 223092870
#if FULL_BITS == 64
, U(6469693230), U(6469693230), U(200560490130), U(200560490130),
U(200560490130), U(200560490130), U(200560490130), U(200560490130),
U(7420738134810), U(7420738134810), U(7420738134810), U(7420738134810),
U(304250263527210), U(304250263527210), U(13082761331670030),
U(13082761331670030), U(13082761331670030), U(13082761331670030),
U(614889782588491410), U(614889782588491410), U(614889782588491410),
U(614889782588491410), U(614889782588491410), U(614889782588491410)
#endif
};
/*
* determine the top 1 bit of a 8 bit value:
*
* topbit[0] == 0 by convention
* topbit[x] gives the highest 1 bit of x
*/
STATIC CONST unsigned char topbit[256] = {
0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
};
/*
* integer square roots of powers of 2
*
* isqrt_pow2[x] == (int)(sqrt(2 to the x power)) (for 0 <= x < 64)
*
* We have enough table entries for a FULL that is 64 bits long.
*/
STATIC CONST FULL isqrt_pow2[64] = {
1, 1, 2, 2, 4, 5, 8, 11, /* 0 .. 7 */
16, 22, 32, 45, 64, 90, 128, 181, /* 8 .. 15 */
256, 362, 512, 724, 1024, 1448, 2048, 2896, /* 16 .. 23 */
4096, 5792, 8192, 11585, 16384, 23170, 32768, 46340, /* 24 .. 31 */
65536, 92681, 131072, 185363, /* 32 .. 35 */
262144, 370727, 524288, 741455, /* 36 .. 39 */
1048576, 1482910, 2097152, 2965820, /* 40 .. 43 */
4194304, 5931641, 8388608, 11863283, /* 44 .. 47 */
16777216, 23726566, 33554432, 47453132, /* 48 .. 51 */
67108864, 94906265, 134217728, 189812531, /* 52 .. 55 */
268435456, 379625062, 536870912, 759250124, /* 56 .. 59 */
1073741824, 1518500249, 0x80000000, 0xb504f333 /* 60 .. 63 */
};
/*
* static functions
*/
S_FUNC FULL fsqrt(FULL v); /* quick square root of v */
S_FUNC long pix(FULL x); /* pi of x */
S_FUNC FULL small_factor(ZVALUE n, FULL limit); /* factor or 0 */
/*
* Determine if a value is a small (32 bit) prime
*
* Returns:
* 1 z is a prime <= MAX_SM_VAL
* 0 z is not a prime <= MAX_SM_VAL
* -1 z > MAX_SM_VAL
*/
FLAG
zisprime(ZVALUE z)
{
FULL n; /* number to test */
FULL isqr; /* factor limit */
CONST unsigned short *tp; /* pointer to a prime factor */
z.sign = 0;
if (zisleone(z)) {
return 0;
}
/* even numbers > 2 are not prime */
if (ziseven(z)) {
/*
* "2 is the greatest odd prime because it is the least even!"
* - Dr. Dan Jurca 1978
*/
return zisabstwo(z);
}
/* ignore non-small values */
if (zge32b(z)) {
return -1;
}
/* we now know that we are dealing with a value 0 <= n < 2^32 */
n = ztofull(z);
/* lookup small cases in pr_map */
if (n <= MAX_MAP_VAL) {
return (pr_map_bit(n) ? 1 : 0);
}
/* ignore Saber-C warning #530 about empty for statement */
/* OK to ignore in proc zisprime */
/* a number >=2^16 and < 2^32 */
for (isqr=fsqrt(n), tp=prime; (*tp <= isqr) && (n % *tp); ++tp) {
}
return ((*tp <= isqr && *tp != 1) ? 0 : 1);
}
/*
* Determine the next small (32 bit) prime > a 32 bit value.
*
* given:
* z search point
*
* Returns:
* 0 next prime is 2^32+15
* 1 abs(z) >= 2^32
* smallest prime > abs(z) otherwise
*/
FULL
znprime(ZVALUE z)
{
FULL n; /* search point */
z.sign = 0;
/* ignore large values */
if (zge32b(z)) {
return (FULL)1;
}
/* deal a search point of 0 or 1 */
if (zisabsleone(z)) {
return (FULL)2;
}
/* deal with returning a value that is beyond our reach */
n = ztofull(z);
if (n >= MAX_SM_PRIME) {
return (FULL)0;
}
/* return the next prime */
return next_prime(n);
}
/*
* Compute the next prime beyond a small (32 bit) value.
*
* This function assumes that 2 <= n < 2^32-5.
*
* given:
* n search point
*/
FULL
next_prime(FULL n)
{
CONST unsigned short *tp; /* pointer to a prime factor */
CONST unsigned char *j; /* current jump increment */
int tmp;
/* find our search point */
n = ((n & 0x1) ? n+2 : n+1);
/* if we can just search the bit map, then search it */
if (n <= MAX_MAP_PRIME) {
/* search until we find a 1 bit */
while (pr_map_bit(n) == 0) {
n += (FULL)2;
}
/* too large for our table, find the next prime the hard way */
} else {
FULL isqr; /* factor limit */
/*
* Our search for a prime may cause us to increment n over
* a perfect square, but never two perfect squares. The largest
* prime gap <= 2614941711251 is 651. Shanks conjectures that
* the largest gap below P is about ln(P)^2.
*
* The value fsqrt(n)^2 will always be the perfect square
* that is <= n. Given the smallness of prime gaps we will
* deal with, we know that n could carry us across the next
* perfect square (fsqrt(n)+1)^2 but not the following
* perfect square (fsqrt(n)+2)^2.
*
* Now the factor search limit for values < (fsqrt(n)+2)^2
* is the same limit for (fsqrt(n)+1)^2; namely fsqrt(n)+1.
* Therefore setting our limit at fsqrt(n)+1 and never
* bothering with it after that is safe.
*/
isqr = fsqrt(n)+1;
/*
* If our factor limit is even, then we can reduce it to
* the next lowest odd value. We already tested if n
* was even and all of our remaining potential factors
* are odd.
*/
if ((isqr & 0x1) == 0) {
--isqr;
}
/*
* Skip to next value not divisible by a trivial prime.
*/
n = firstjmp(n, tmp);
j = jmp + jmpptr(n);
/*
* Look for tiny prime factors of increasing n until we
* find a prime.
*/
do {
/* ignore Saber-C warning #530 - empty for statement */
/* OK to ignore in proc next_prime */
/* XXX - speed up test for large n by using GCDs */
/* find a factor, or give up if not found */
for (tp=JPRIME; (*tp <= isqr) && (n % *tp); ++tp) {
}
} while (*tp <= isqr && *tp != 1 && (n += nxtjmp(j)));
}
/* return the prime that we found */
return n;
}
/*
* Determine the previous small (32 bit) prime < a 32 bit value
*
* given:
* z search point
*
* Returns:
* 1 abs(z) >= 2^32
* 0 abs(z) <= 2
* greatest prime < abs(z) otherwise
*/
FULL
zpprime(ZVALUE z)
{
CONST unsigned short *tp; /* pointer to a prime factor */
FULL isqr; /* isqrt(z) */
FULL n; /* search point */
CONST unsigned char *j; /* current jump increment */
int tmp;
z.sign = 0;
/* ignore large values */
if (zge32b(z)) {
return (FULL)1;
}
/* deal with special case small values */
n = ztofull(z);
switch ((int)n) {
case 0:
case 1:
case 2:
/* ignore values <= 2 */
return (FULL)0;
case 3:
/* 3 returns the only even prime */
return (FULL)2;
}
/* deal with values above the bit map */
if (n > NXT_MAP_PRIME) {
/* find our search point */
n = ((n & 0x1) ? n-2 : n-1);
/* our factor limit - see next_prime for why this works */
isqr = fsqrt(n)+1;
if ((isqr & 0x1) == 0) {
--isqr;
}
/*
* Skip to previous value not divisible by a trivial prime.
*/
tmp = jmpindxval(n);
if (tmp >= 0) {
/* find next value not divisible by a trivial prime */
n += tmp;
/* find the previous jump index */
j = jmp + jmpptr(n);
/* jump back */
n -= prevjmp(j);
/* already not divisible by a trivial prime */
} else {
/* find the current jump index */
j = jmp + jmpptr(n);
}
/* factor values until we find a prime */
do {
/* ignore Saber-C warning #530 - empty for statement */
/* OK to ignore in proc zpprime */
/* XXX - speed up test for large n by using GCDs */
/* find a factor, or give up if not found */
for (tp=prime; (*tp <= isqr) && (n % *tp); ++tp) {
}
} while (*tp <= isqr && *tp != 1 && (n -= prevjmp(j)));
/* deal with values within the bit map */
} else if (n <= MAX_MAP_PRIME) {
/* find our search point */
n = ((n & 0x1) ? n-2 : n-1);
/* search until we find a 1 bit */
while (pr_map_bit(n) == 0) {
n -= (FULL)2;
}
/* deal with values that could cross into the bit map */
} else {
/* MAX_MAP_PRIME < n <= NXT_MAP_PRIME returns MAX_MAP_PRIME */
return MAX_MAP_PRIME;
}
/* return what we found */
return n;
}
/*
* Compute the number of primes <= a ZVALUE that can fit into a FULL
*
* given:
* z compute primes <= z
*
* Returns:
* -1 error
* >=0 number of primes <= x
*/
long
zpix(ZVALUE z)
{
/* pi(<0) is always 0 */
if (zisneg(z)) {
return (long)0;
}
/* firewall */
if (zge32b(z)) {
return (long)-1;
}
return pix(ztofull(z));
}
/*
* Compute the number of primes <= a ZVALUE
*
* given:
* x value of z
*
* Returns:
* -1 error
* >=0 number of primes <= x
*/
S_FUNC long
pix(FULL x)
{
long count; /* pi(x) */
FULL top; /* top of the range to test */
CONST unsigned short *tp; /* pointer to a tiny prime */
FULL i;
/* compute pi(x) using the 2^8 step table */
if (x <= MAX_PI10B) {
/* x within the prime table, so use it */
if (x < MAX_MAP_PRIME) {
/* firewall - pix(x) ==0 for x < 2 */
if (x < 2) {
count = 0;
} else {
/* determine how and where we will count */
if (x < 1024) {
count = 1;
tp = prime;
} else {
count = pi10b[x>>10];
tp = prime+count-1;
}
/* count primes in the table */
while (*tp++ <= x) {
++count;
}
}
/* x is larger than the prime table, so count the hard way */
} else {
/* case: count down from pi18b entry to x */
if (x & 0x200) {
top = (x | 0x3ff);
count = pi10b[(top+1)>>10];
for (i=next_prime(x); i <= top;
i=next_prime(i)) {
--count;
}
/* case: count up from pi10b entry to x */
} else {
count = pi10b[x>>10];
for (i=next_prime(x&(~0x3ff));
i <= x; i = next_prime(i)) {
++count;
}
}
}
/* compute pi(x) using the 2^18 interval table */
} else {
/* compute sum of intervals up to our interval */
for (count=0, i=0; i < (x>>18); ++i) {
count += pi18b[i];
}
/* case: count down from pi18b entry to x */
if (x & 0x20000) {
top = (x | 0x3ffff);
count += pi18b[i];
if (top > MAX_SM_PRIME) {
if (x < MAX_SM_PRIME) {
for (i=next_prime(x); i < MAX_SM_PRIME;
i=next_prime(i)) {
--count;
}
--count;
}
} else {
for (i=next_prime(x); i<=top; i=next_prime(i)) {
--count;
}
}
/* case: count up from pi18b entry to x */
} else {
for (i=next_prime(x&(~0x3ffff));
i <= x; i = next_prime(i)) {
++count;
}
}
}
return count;
}
/*
* Compute the smallest prime factor < limit
*
* given:
* n number to factor
* zlimit ending search point
* res factor, if found, or NULL
*
* Returns:
* -1 error, limit >= 2^32
* 0 no factor found, res is not changed
* 1 factor found, res (if non-NULL) is smallest prime factor
*
* NOTE: This routine will not return a factor == the test value
* except when the test value is 1 or -1.
*/
FLAG
zfactor(ZVALUE n, ZVALUE zlimit, ZVALUE *res)
{
FULL f; /* factor found, or 0 */
/* firewall */
if (res == NULL) {
math_error("%s: res NULL", __func__);
not_reached();
}
/*
* determine the limit
*/
if (zge32b(zlimit)) {
/* limit is too large to be reasonable */
return -1;
}
n.sign = 0; /* ignore sign of n */
/*
* find the smallest factor <= limit, if possible
*/
f = small_factor(n, ztofull(zlimit));
/*
* report the results
*/
if (f > 0) {
/* return factor if requested */
if (res) {
utoz(f, res);
}
/* report a factor was found */
return 1;
}
/* no factor was found */
return 0;
}
/*
* Find a smallest prime factor <= some small (32 bit) limit of a value
*
* given:
* z number to factor
* limit largest factor we will test
*
* Returns:
* 0 no prime <= the limit was found
* != 0 the smallest prime factor
*/
S_FUNC FULL
small_factor(ZVALUE z, FULL limit)
{
FULL top; /* current max factor level */
CONST unsigned short *tp; /* pointer to a tiny prime */
FULL factlim; /* highest factor to test */
CONST unsigned short *p; /* test factor */
FULL factor; /* test factor */
HALF tlim; /* limit on prime table use */
HALF divval[2]; /* divisor value */
ZVALUE div; /* test factor/divisor */
ZVALUE tmp;
CONST unsigned char *j;
/*
* catch impossible ranges
*/
if (limit < 2) {
/* range is too small */
return 0;
}
/*
* perform the even test
*/
if (ziseven(z)) {
if (zistwo(z)) {
/* z is 2, so don't return 2 as a factor */
return 0;
}
return 2;
/*
* value is odd
*/
} else if (limit == 2) {
/* limit is 2, value is odd, no factors will ever be found */
return 0;
}
/*
* force the factor limit to be odd
*/
if ((limit & 0x1) == 0) {
--limit;
}
/*
* case: number to factor fits into a FULL
*/
if (!zgtmaxufull(z)) {
FULL val = ztofull(z); /* find the smallest factor of val */
FULL isqr; /* sqrt of val */
/*
* special case: val is a prime <= MAX_MAP_PRIME
*/
if (val <= MAX_MAP_PRIME && pr_map_bit(val)) {
/* z is prime, so no factors will be found */
return 0;
}
/*
* we need not search above the sqrt of val
*/
isqr = fsqrt(val);
if (limit > isqr) {
/* limit is largest odd value <= sqrt of val */
limit = ((isqr & 0x1) ? isqr : isqr-1);
}
/*
* search for a small prime factor
*/
top = ((limit < MAX_MAP_VAL) ? limit : MAX_MAP_VAL);
for (tp = prime; *tp <= top && *tp != 1; ++tp) {
if (val%(*tp) == 0) {
return ((FULL)*tp);
}
}
#if FULL_BITS == 64
/*
* Our search will carry us beyond the prime table. We will
* continue to values until we reach our limit or until a
* factor is found.
*
* It is faster to simply test odd values and ignore non-prime
* factors because the work needed to find the next prime is
* more than the work one saves in not factor with non-prime
* values.
*
* We can improve on this method by skipping odd values that
* are a multiple of 3, 5, 7 and 11. We use a table of
* bytes that indicate the offsets between odd values that
* are not a multiple of 3,4,5,7 & 11.
*/
/* XXX - speed up test for large z by using GCDs */
j = jmp + jmpptr(NXT_MAP_PRIME);
for (top=NXT_MAP_PRIME; top <= limit; top += nxtjmp(j)) {
if ((val % top) == 0) {
return top;
}
}
#endif /* FULL_BITS == 64 */
/* no prime factors found */
return 0;
}
/*
* Find a factor of a value that is too large to fit into a FULL.
*
* determine if/what our sqrt factor limit will be
*/
if (zge64b(z)) {
/* we have no factor limit, avoid highest factor */
factlim = MAX_SM_PRIME-1;
} else if (zge32b(z)) {
/* determine if limit is too small to matter */
if (limit < BASE) {
factlim = limit;
} else {
/* find the isqrt(z) */
if (!zsqrt(z, &tmp, 0)) {
/* sqrt is exact */
factlim = ztofull(tmp);
} else {
/* sqrt is inexact */
factlim = ztofull(tmp)+1;
}
zfree(tmp);
/* avoid highest factor */
if (factlim >= MAX_SM_PRIME) {
factlim = MAX_SM_PRIME-1;
}
}
} else {
/* determine our factor limit */
factlim = fsqrt(ztofull(z));
if (factlim >= MAX_SM_PRIME) {
factlim = MAX_SM_PRIME-1;
}
}
if (factlim > limit) {
factlim = limit;
}
/*
* walk the prime table looking for factors
*
* XXX - consider using gcd of products of primes to speed this
* section up
*/
tlim = (HALF)((factlim >= MAX_MAP_PRIME) ? MAX_MAP_PRIME-1 : factlim);
div.sign = 0;
div.v = divval;
div.len = 1;
for (p=prime; (HALF)*p <= tlim; ++p) {
/* setup factor */
div.v[0] = (HALF)(*p);
if (zdivides(z, div))
return (FULL)(*p);
}
if ((FULL)*p > factlim) {
/* no factor found */
return (FULL)0;
}
/*
* test the highest factor possible
*/
div.v[0] = MAX_MAP_PRIME;
if (zdivides(z, div))
return (FULL)MAX_MAP_PRIME;
/*
* generate higher test factors as needed
*
* XXX - consider using gcd of products of primes to speed this
* section up
*/
#if BASEB == 16
div.len = 2;
#endif
factor = NXT_MAP_PRIME;
j = jmp + jmpptr(factor);
for(; factor <= factlim; factor += nxtjmp(j)) {
/* setup factor */
#if BASEB == 32
div.v[0] = (HALF)factor;
#else
div.v[0] = (HALF)(factor & BASE1);
div.v[1] = (HALF)(factor >> BASEB);
#endif
if (zdivides(z, div))
return (FULL)(factor);
}
if (factor >= factlim) {
/* no factor found */
return (FULL)0;
}
/*
* test the highest factor possible
*/
#if BASEB == 32
div.v[0] = MAX_SM_PRIME;
#else
div.v[0] = (MAX_SM_PRIME & BASE1);
div.v[1] = (MAX_SM_PRIME >> BASEB);
#endif
if (zdivides(z, div))
return (FULL)MAX_SM_PRIME;
/*
* no factor found
*/
return (FULL)0;
}
/*
* Compute the product of the primes up to the specified number.
*/
void
zpfact(ZVALUE z, ZVALUE *dest)
{
long n; /* limiting number to multiply by */
long p; /* current prime */
CONST unsigned short *tp; /* pointer to a tiny prime */
CONST unsigned char *j; /* current jump increment */
ZVALUE res, temp;
/* firewall */
if (dest == NULL) {
math_error("%s: dest NULL", __func__);
not_reached();
}
/* firewall */
if (zisneg(z)) {
math_error("Negative argument for factorial");
not_reached();
}
if (zge24b(z)) {
math_error("Very large factorial");
not_reached();
}
n = ztolong(z);
/*
* Deal with table lookup pfact values
*/
if (n <= MAX_PFACT_VAL) {
utoz(pfact_tbl[n], dest);
return;
}
/*
* Multiply by the primes in the static table
*/
utoz(pfact_tbl[MAX_PFACT_VAL], &res);
for (tp=(&prime[NXT_PFACT_VAL]); *tp != 1 && (long)(*tp) <= n; ++tp) {
zmuli(res, *tp, &temp);
zfree(res);
res = temp;
}
/*
* if needed, multiply by primes beyond the static table
*/
j = jmp + jmpptr(NXT_MAP_PRIME);
for (p = NXT_MAP_PRIME; p <= n; p += nxtjmp(j)) {
FULL isqr; /* isqrt(p) */
/* our factor limit - see next_prime for why this works */
isqr = fsqrt(p)+1;
if ((isqr & 0x1) == 0) {
--isqr;
}
/* ignore Saber-C warning #530 about empty for statement */
/* OK to ignore in proc zpfact */
/* find the next prime */
for (tp=prime; (*tp <= isqr) && (p % (long)(*tp)); ++tp) {
}
if (*tp <= isqr && *tp != 1) {
continue;
}
/* multiply by the next prime */
zmuli(res, p, &temp);
zfree(res);
res = temp;
}
*dest = res;
}
/*
* Perform a probabilistic primality test (algorithm P in Knuth vol2, 4.5.4).
* Returns false if definitely not prime, or true if probably prime.
* Count determines how many times to check for primality.
* The chance of a non-prime passing this test is less than (1/4)^count.
* For example, a count of 100 fails for only 1 in 10^60 numbers.
*
* It is interesting to note that ptest(a,1,x) (for any x >= 0) of this
* test will always return true for a prime, and rarely return true for
* a non-prime. The 1/4 is appears in practice to be a poor upper
* bound. Even so the only result that is EXACT and true is when
* this test returns false for a non-prime. When ptest returns true,
* one cannot determine if the value in question is prime, or the value
* is one of those rare non-primes that produces a false positive.
*
* The absolute value of count determines how many times to check
* for primality. If count < 0, then the trivial factor check is
* omitted.
* skip = 0 uses random bases
* skip = 1 uses prime bases 2, 3, 5, ...
* skip > 1 or < 0 uses bases skip, skip + 1, ...
*/
bool
zprimetest(ZVALUE z, long count, ZVALUE skip)
{
long limit = 0; /* test odd values from skip up to limit */
ZVALUE zbase; /* base as a ZVALUE */
long i, ij, ik;
ZVALUE zm1, z1, z2, z3;
int type; /* random, prime or consecutive integers */
CONST unsigned short *pr; /* pointer to small prime */
/*
* firewall - ignore sign of z, values 0 and 1 are not prime
*/
z.sign = 0;
if (zisleone(z)) {
return 0;
}
/*
* firewall - All even values, except 2, are not prime
*/
if (ziseven(z))
return zistwo(z);
if (z.len == 1 && *z.v == 3)
return 1; /* 3 is prime */
/*
* we know that z is an odd value > 1
*/
/*
* Perform trivial checks if count is not negative
*/
if (count >= 0) {
/*
* If the number is a small (32 bit) value, do a direct test
*/
if (!zge32b(z)) {
return zisprime(z);
}
/*
* See if the number has a tiny factor.
*/
if (small_factor(z, PTEST_PRECHECK) != 0) {
/* a tiny factor was found */
return false;
}
/*
* If our count is zero, do nothing more
*/
if (count == 0) {
/* no test was done, so no test failed! */
return true;
}
} else {
/* use the absolute value of count */
count = -count;
}
if (z.len < conf->redc2) {
return zredcprimetest(z, count, skip);
}
if (ziszero(skip)) {
type = 0;
zbase = _zero_;
} else if (zisone(skip)) {
type = 1;
itoz(2, &zbase);
limit = 1 << 16;
if (!zge16b(z))
limit = ztolong(z);
} else {
type = 2;
if (zrel(skip, z) >= 0 || zisneg(skip))
zmod(skip, z, &zbase, 0);
else
zcopy(skip, &zbase);
}
/*
* Loop over various bases, testing each one.
*/
zsub(z, _one_, &zm1);
ik = zlowbit(zm1);
zshift(zm1, -ik, &z1);
pr = prime;
for (i = 0; i < count; i++) {
switch (type) {
case 0:
zfree(zbase);
zrandrange(_two_, zm1, &zbase);
break;
case 1:
if (i == 0)
break;
zfree(zbase);
if (*pr == 1 || (long)*pr >= limit) {
zfree(z1);
zfree(zm1);
return true;
}
itoz((long) *pr++, &zbase);
break;
default:
if (i == 0)
break;
zadd(zbase, _one_, &z3);
zfree(zbase);
zbase = z3;
}
ij = 0;
zpowermod(zbase, z1, z, &z3);
for (;;) {
if (zisone(z3)) {
if (ij) {
/* number is definitely not prime */
zfree(z3);
zfree(zm1);
zfree(z1);
zfree(zbase);
return false;
}
break;
}
if (!zcmp(z3, zm1))
break;
if (++ij >= ik) {
/* number is definitely not prime */
zfree(z3);
zfree(zm1);
zfree(z1);
zfree(zbase);
return false;
}
zsquare(z3, &z2);
zfree(z3);
zmod(z2, z, &z3, 0);
zfree(z2);
}
zfree(z3);
}
zfree(zm1);
zfree(z1);
zfree(zbase);
/* number might be prime */
return true;
}
/*
* Called by zprimetest when simple cases have been eliminated
* and z.len < conf->redc2. Here count > 0, z is odd and > 3.
*/
bool
zredcprimetest(ZVALUE z, long count, ZVALUE skip)
{
long limit = 0; /* test odd values from skip up to limit */
ZVALUE zbase; /* base as a ZVALUE */
REDC *rp;
long i, ij, ik;
ZVALUE zm1, z1, z2, z3;
ZVALUE zredcm1;
int type; /* random, prime or consecutive integers */
CONST unsigned short *pr; /* pointer to small prime */
rp = zredcalloc(z);
zsub(z, rp->one, &zredcm1);
if (ziszero(skip)) {
zbase = _zero_;
type = 0;
} else if (zisone(skip)) {
itoz(2, &zbase);
type = 1;
limit = 1 << 16;
if (!zge16b(z))
limit = ztolong(z);
} else {
zredcencode(rp, skip, &zbase);
type = 2;
}
/*
* Loop over various "random" numbers, testing each one.
*/
zsub(z, _one_, &zm1);
ik = zlowbit(zm1);
zshift(zm1, -ik, &z1);
pr = prime;
for (i = 0; i < count; i++) {
switch (type) {
case 0:
do {
zfree(zbase);
zrandrange(_one_, z, &zbase);
}
while (!zcmp(zbase, rp->one) ||
!zcmp(zbase, zredcm1));
break;
case 1:
if (i == 0) {
break;
}
zfree(zbase);
if (*pr == 1 || (long)*pr >= limit) {
zfree(z1);
zfree(zm1);
if (z.len < conf->redc2) {
zredcfree(rp);
zfree(zredcm1);
}
return true;
}
itoz((long) *pr++, &z3);
zredcencode(rp, z3, &zbase);
zfree(z3);
break;
default:
if (i == 0)
break;
zadd(zbase, rp->one, &z3);
zfree(zbase);
zbase = z3;
if (zrel(zbase, z) >= 0) {
zsub(zbase, z, &z3);
zfree(zbase);
zbase = z3;
}
}
ij = 0;
zredcpower(rp, zbase, z1, &z3);
for (;;) {
if (!zcmp(z3, rp->one)) {
if (ij) {
/* number is definitely not prime */
zfree(z3);
zfree(zm1);
zfree(z1);
zfree(zbase);
zredcfree(rp);
zfree(zredcm1);
return false;
}
break;
}
if (!zcmp(z3, zredcm1))
break;
if (++ij >= ik) {
/* number is definitely not prime */
zfree(z3);
zfree(zm1);
zfree(z1);
zfree(zbase);
zredcfree(rp);
zfree(zredcm1);
return false;
}
zredcsquare(rp, z3, &z2);
zfree(z3);
z3 = z2;
}
zfree(z3);
}
zfree(zbase);
zredcfree(rp);
zfree(zredcm1);
zfree(zm1);
zfree(z1);
/* number might be prime */
return true;
}
/*
* znextcand - find the next integer that passes ptest().
* The signs of z and mod are ignored. Result is the least integer
* greater than abs(z) congruent to res modulo abs(mod), or if there
* is no such integer, zero.
*
* given:
* z search point > 2
* count ptests to perform per candidate
* skip ptests to skip
* res return congruent to res modulo abs(mod)
* mod congruent to res modulo abs(mod)
* cand candidate found
*/
bool
znextcand(ZVALUE z, long count, ZVALUE skip, ZVALUE res, ZVALUE mod,
ZVALUE *cand)
{
ZVALUE tmp1;
ZVALUE tmp2;
/* firewall */
if (cand == NULL) {
math_error("%s: cand NULL", __func__);
not_reached();
}
z.sign = 0;
mod.sign = 0;
if (ziszero(mod)) {
if (zrel(res, z) > 0 && zprimetest(res, count, skip)) {
zcopy(res, cand);
return true;
}
return false;
}
if (ziszero(z) && zisone(mod)) {
zcopy(_two_, cand);
return true;
}
zsub(res, z, &tmp1);
if (zmod(tmp1, mod, &tmp2, 0))
zadd(z, tmp2, cand);
else
zadd(z, mod, cand);
/*
* Now *cand is least integer greater than abs(z) and congruent
* to res modulo mod.
*/
zfree(tmp1);
zfree(tmp2);
if (zprimetest(*cand, count, skip))
return true;
zgcd(*cand, mod, &tmp1);
if (!zisone(tmp1)) {
zfree(tmp1);
zfree(*cand);
return false;
}
zfree(tmp1);
if (ziseven(*cand)) {
zadd(*cand, mod, &tmp1);
zfree(*cand);
*cand = tmp1;
if (zprimetest(*cand, count, skip))
return true;
}
/*
* *cand is now least odd integer > abs(z) and congruent to
* res modulo mod.
*/
if (zisodd(mod))
zshift(mod, 1, &tmp1);
else
zcopy(mod, &tmp1);
do {
zadd(*cand, tmp1, &tmp2);
zfree(*cand);
*cand = tmp2;
} while (!zprimetest(*cand, count, skip));
zfree(tmp1);
return true;
}
/*
* zprevcand - find the nearest previous integer that passes ptest().
* The signs of z and mod are ignored. Result is greatest positive integer
* less than abs(z) congruent to res modulo abs(mod), or if there
* is no such integer, zero.
*
* given:
* z search point > 2
* count ptests to perform per candidate
* skip ptests to skip
* res return congruent to res modulo abs(mod)
* mod congruent to res modulo abs(mod)
* cand candidate found
*/
bool
zprevcand(ZVALUE z, long count, ZVALUE skip, ZVALUE res, ZVALUE mod,
ZVALUE *cand)
{
ZVALUE tmp1;
ZVALUE tmp2;
/* firewall */
if (cand == NULL) {
math_error("%s: cand NULL", __func__);
not_reached();
}
z.sign = 0;
mod.sign = 0;
if (ziszero(mod)) {
if (zispos(res)&&zrel(res, z)<0 && zprimetest(res,count,skip)) {
zcopy(res, cand);
return true;
}
return false;
}
zsub(z, res, &tmp1);
if (zmod(tmp1, mod, &tmp2, 0))
zsub(z, tmp2, cand);
else
zsub(z, mod, cand);
/*
* *cand is now the greatest integer < z that is congruent to res
* modulo mod.
*/
zfree(tmp1);
zfree(tmp2);
if (zisneg(*cand)) {
zfree(*cand);
return false;
}
if (zprimetest(*cand, count, skip))
return true;
zgcd(*cand, mod, &tmp1);
if (!zisone(tmp1)) {
zfree(tmp1);
zmod(*cand, mod, &tmp1, 0);
zfree(*cand);
if (zprimetest(tmp1, count, skip)) {
*cand = tmp1;
return true;
}
if (ziszero(tmp1)) {
zfree(tmp1);
if (zprimetest(mod, count, skip)) {
zcopy(mod, cand);
return true;
}
return false;
}
zfree(tmp1);
return false;
}
zfree(tmp1);
if (ziseven(*cand)) {
zsub(*cand, mod, &tmp1);
zfree(*cand);
if (zisneg(tmp1)) {
zfree(tmp1);
return false;
}
*cand = tmp1;
if (zprimetest(*cand, count, skip))
return true;
}
/*
* *cand is now the greatest odd integer < z that is congruent to
* res modulo mod.
*/
if (zisodd(mod))
zshift(mod, 1, &tmp1);
else
zcopy(mod, &tmp1);
do {
zsub(*cand, tmp1, &tmp2);
zfree(*cand);
*cand = tmp2;
} while (!zprimetest(*cand, count, skip) && !zisneg(*cand));
zfree(tmp1);
if (zisneg(*cand)) {
zadd(*cand, mod, &tmp1);
zfree(*cand);
*cand = tmp1;
if (zistwo(*cand))
return true;
zfree(*cand);
return false;
}
return true;
}
/*
* Find the lowest prime factor of a number if one can be found.
* Search is conducted for the first count primes.
*
* Returns:
* 1 no factor found or z < 3
* >1 factor found
*/
FULL
zlowfactor(ZVALUE z, long count)
{
FULL factlim; /* highest factor to test */
CONST unsigned short *p; /* test factor */
FULL factor; /* test factor */
HALF tlim; /* limit on prime table use */
HALF divval[2]; /* divisor value */
ZVALUE div; /* test factor/divisor */
ZVALUE tmp;
z.sign = 0;
/*
* firewall
*/
if (count <= 0 || zisleone(z) || zistwo(z)) {
/* number is < 3 or count is <= 0 */
return (FULL)1;
}
/*
* test for the first factor
*/
if (ziseven(z)) {
return (FULL)2;
}
if (count <= 1) {
/* count was 1, tested the one and only factor */
return (FULL)1;
}
/*
* determine if/what our sqrt factor limit will be
*/
if (zge64b(z)) {
/* we have no factor limit, avoid highest factor */
factlim = MAX_SM_PRIME-1;
} else if (zge32b(z)) {
/* find the isqrt(z) */
if (!zsqrt(z, &tmp, 0)) {
/* sqrt is exact */
factlim = ztofull(tmp);
} else {
/* sqrt is inexact */
factlim = ztofull(tmp)+1;
}
zfree(tmp);
/* avoid highest factor */
if (factlim >= MAX_SM_PRIME) {
factlim = MAX_SM_PRIME-1;
}
} else {
/* determine our factor limit */
factlim = fsqrt(ztofull(z));
}
if (factlim >= MAX_SM_PRIME) {
factlim = MAX_SM_PRIME-1;
}
/*
* walk the prime table looking for factors
*/
tlim = (HALF)((factlim >= MAX_MAP_PRIME) ? MAX_MAP_PRIME-1 : factlim);
div.sign = 0;
div.v = divval;
div.len = 1;
for (p=prime, --count; count > 0 && (HALF)*p <= tlim; ++p, --count) {
/* setup factor */
div.v[0] = (HALF)(*p);
if (zdivides(z, div))
return (FULL)(*p);
}
if (count <= 0 || (FULL)*p > factlim) {
/* no factor found */
return (FULL)1;
}
/*
* test the highest factor possible
*/
div.v[0] = MAX_MAP_PRIME;
if (zdivides(z, div))
return (FULL)MAX_MAP_PRIME;
/*
* generate higher test factors as needed
*/
#if BASEB == 16
div.len = 2;
#endif
for(factor = NXT_MAP_PRIME;
count > 0 && factor <= factlim;
factor = next_prime(factor), --count) {
/* setup factor */
#if BASEB == 32
div.v[0] = (HALF)factor;
#else
div.v[0] = (HALF)(factor & BASE1);
div.v[1] = (HALF)(factor >> BASEB);
#endif
if (zdivides(z, div))
return (FULL)(factor);
}
if (count <= 0 || factor >= factlim) {
/* no factor found */
return (FULL)1;
}
/*
* test the highest factor possible
*/
#if BASEB == 32
div.v[0] = MAX_SM_PRIME;
#else
div.v[0] = (MAX_SM_PRIME & BASE1);
div.v[1] = (MAX_SM_PRIME >> BASEB);
#endif
if (zdivides(z, div))
return (FULL)MAX_SM_PRIME;
/*
* no factor found
*/
return (FULL)1;
}
/*
* Compute the least common multiple of all the numbers up to the
* specified number.
*/
void
zlcmfact(ZVALUE z, ZVALUE *dest)
{
long n; /* limiting number to multiply by */
long p; /* current prime */
long pp = 0; /* power of prime */
long i; /* test value */
CONST unsigned short *pr; /* pointer to a small prime */
ZVALUE res, temp;
/* firewall */
if (dest == NULL) {
math_error("%s: dest NULL", __func__);
not_reached();
}
if (zisneg(z) || ziszero(z)) {
math_error("Non-positive argument for lcmfact");
not_reached();
}
if (zge24b(z)) {
math_error("Very large lcmfact");
not_reached();
}
n = ztolong(z);
/*
* Multiply by powers of the necessary odd primes in order.
* The power for each prime is the highest one which is not
* more than the specified number.
*/
res = _one_;
for (pr=prime; (long)(*pr) <= n && *pr > 1; ++pr) {
i = p = *pr;
while (i <= n) {
pp = i;
i *= p;
}
zmuli(res, pp, &temp);
zfree(res);
res = temp;
}
for (p = NXT_MAP_PRIME; p <= n; p = (long)next_prime(p)) {
i = p;
while (i <= n) {
pp = i;
i *= p;
}
zmuli(res, pp, &temp);
zfree(res);
res = temp;
}
/*
* Finish by scaling by the necessary power of two.
*/
zshift(res, zhighbit(z), dest);
zfree(res);
}
/*
* fsqrt - fast square root of a FULL value
*
* We will determine the square root of a given value.
* Starting with the integer square root of the largest power of
* two <= the value, we will perform 3 Newton iterations to
* arrive at our guess.
*
* We have verified that fsqrt(x) == (FULL)sqrt((double)x), or
* fsqrt(x)-1 == (FULL)sqrt((double)x) for all 0 <= x < 2^32.
*
* given:
* x compute the integer square root of x
*/
S_FUNC FULL
fsqrt(FULL x)
{
FULL y; /* (FULL)temporary value */
int i;
/* firewall - deal with 0 */
if (x == 0) {
return 0;
}
/* ignore Saber-C warning #530 about empty for statement */
/* OK to ignore in proc fsqrt */
/* determine our initial guess */
for (i=0, y=x; y >= (FULL)256; i+=8, y>>=8) {
}
y = isqrt_pow2[i + topbit[y]];
/* perform 3 Newton interactions */
y = (y+x/y)>>1;
y = (y+x/y)>>1;
y = (y+x/y)>>1;
#if FULL_BITS == 64
y = (y+x/y)>>1;
#endif
/* return the result */
return y;
}