<commit_before>
#include <assert.h>
#include <stdint.h>
#include <strings.h>
#include "bcv-svd-gabriel-rep.h"
#include "bcv-matrix-private.h"


struct _bcv_svd_grep
{
    bcv_matrix_t *x11; bcv_matrix_t *x12;
    bcv_matrix_t *x21; bcv_matrix_t *x22;
    double *d;
    void *work;
};


static void
bcv_svd_grep_init_storage (bcv_svd_grep_t *bcv, bcv_gabriel_holdin_t holdin, 
                           bcv_index_t M, bcv_index_t N);

static bcv_error_t
bcv_svd_grep_decompose (bcv_svd_grep_t *bcv);

static bcv_index_t
bcv_svd_grep_decompose_work_len (bcv_gabriel_holdin_t holdin, bcv_index_t M, 
                                 bcv_index_t N);


size_t
bcv_svd_grep_size (bcv_gabriel_holdin_t holdin, bcv_index_t M, bcv_index_t N)
{
    size_t total, decompose_e_tauq_taup, update_u, work_len, result = 0;
    bcv_index_t m, n, mn;
    bcv_index_t decompose_lwork;
    
    _bcv_assert_valid_gabriel_holdin (&holdin, M, N);
    m  = holdin.m;
    n  = holdin.n;
    mn = BCV_MIN (m,n);
    
    /* space for the bcv_svd_grep_t and x11, x12, x21, x22 */
    total = (sizeof (bcv_svd_grep_t) 
             + (__alignof__ (bcv_matrix_t) - 1) 
             + 4 * sizeof (bcv_matrix_t)
             + (__alignof__ (double) - 1));
        
    /* space for the M*N data matrix */
    if (M <= SIZE_MAX / N
        && M * N <= (SIZE_MAX - total) / sizeof (double))
    {
        total +=  M * N * sizeof (double);
        
        /* space for d */
        if (mn <= (SIZE_MAX - total) / sizeof (double))
        {
            total += mn * sizeof (double);
            
            /* work space; the memory used by decompose() can be
             * re-used by update()  
             */
            decompose_e_tauq_taup = 3 * mn * sizeof (double);
            decompose_lwork = bcv_svd_grep_decompose_work_len (holdin, M, N);
            update_u  = M * sizeof (double);
            
            if (mn <= SIZE_MAX / 3
                && 3 * mn <= SIZE_MAX / sizeof (double)
                && (decompose_lwork > 0 || mn == 0)<commit_msg>Fix potential divide-by-zero error<commit_after>
#include <assert.h>
#include <stdint.h>
#include <strings.h>
#include "bcv-svd-gabriel-rep.h"
#include "bcv-matrix-private.h"


struct _bcv_svd_grep
{
    bcv_matrix_t *x11; bcv_matrix_t *x12;
    bcv_matrix_t *x21; bcv_matrix_t *x22;
    double *d;
    void *work;
};


static void
bcv_svd_grep_init_storage (bcv_svd_grep_t *bcv, bcv_gabriel_holdin_t holdin, 
                           bcv_index_t M, bcv_index_t N);

static bcv_error_t
bcv_svd_grep_decompose (bcv_svd_grep_t *bcv);

static bcv_index_t
bcv_svd_grep_decompose_work_len (bcv_gabriel_holdin_t holdin, bcv_index_t M, 
                                 bcv_index_t N);


size_t
bcv_svd_grep_size (bcv_gabriel_holdin_t holdin, bcv_index_t M, bcv_index_t N)
{
    size_t total, decompose_e_tauq_taup, update_u, work_len, result = 0;
    bcv_index_t m, n, mn;
    bcv_index_t decompose_lwork;
    
    _bcv_assert_valid_gabriel_holdin (&holdin, M, N);
    m  = holdin.m;
    n  = holdin.n;
    mn = BCV_MIN (m,n);
    
    /* space for the bcv_svd_grep_t and x11, x12, x21, x22 */
    total = (sizeof (bcv_svd_grep_t) 
             + (__alignof__ (bcv_matrix_t) - 1) 
             + 4 * sizeof (bcv_matrix_t)
             + (__alignof__ (double) - 1));
        
    /* space for the M*N data matrix */
    if (N == 0 
        || (M <= SIZE_MAX / N
            && M * N <= (SIZE_MAX - total) / sizeof (double)))
    {
        total +=  M * N * sizeof (double);
        
        /* space for d */
        if (mn <= (SIZE_MAX - total) / sizeof (double))
        {
            total += mn * sizeof (double);
            
            /* work space; the memory used by decompose() can be
             * re-used by update()  
             */
            decompose_e_tauq_taup = 3 * mn * sizeof (double);
            decompose_lwork = bcv_svd_grep_decompose_work_len (holdin, M, N);
            update_u  = M * sizeof (double);
            
            if (mn <= SIZE_MAX / 3
                && 3 * mn <= SIZE_MAX / sizeof (double)
                && (decompose_lwork > 0 || mn == 0)
{'fraction_non_alphanumeric': 0.10819990295972828, 'fraction_numerical': 0.012372634643377001, 'mean_word_length': 2.0428044280442803, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7455166', 'n_tokens_mistral': 1695, 'n_tokens_neox': 1625, 'n_words': 356}