~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to sql/filesort.cc

Removed DBUG symbols and fixed TRUE/FALSE

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
  Sorts a database
22
22
*/
23
23
 
24
 
#include <drizzled/server_includes.h>
 
24
#include "mysql_priv.h"
 
25
#include <m_ctype.h>
25
26
#include "sql_sort.h"
26
 
#include <drizzled/drizzled_error_messages.h>
 
27
 
 
28
/// How to write record_ref.
 
29
#define WRITE_REF(file,from) \
 
30
if (my_b_write((file),(uchar*) (from),param->ref_length)) \
 
31
  return(1);
27
32
 
28
33
        /* functions defined in this file */
29
34
 
30
 
static char **make_char_array(char **old_pos, register uint32_t fields,
31
 
                              uint32_t length, myf my_flag);
32
 
static unsigned char *read_buffpek_from_file(IO_CACHE *buffer_file, uint32_t count,
33
 
                                     unsigned char *buf);
 
35
static char **make_char_array(char **old_pos, register uint fields,
 
36
                              uint length, myf my_flag);
 
37
static uchar *read_buffpek_from_file(IO_CACHE *buffer_file, uint count,
 
38
                                     uchar *buf);
34
39
static ha_rows find_all_keys(SORTPARAM *param,SQL_SELECT *select,
35
 
                             unsigned char * *sort_keys, IO_CACHE *buffer_file,
 
40
                             uchar * *sort_keys, IO_CACHE *buffer_file,
36
41
                             IO_CACHE *tempfile,IO_CACHE *indexfile);
37
 
static int write_keys(SORTPARAM *param,unsigned char * *sort_keys,
38
 
                      uint32_t count, IO_CACHE *buffer_file, IO_CACHE *tempfile);
39
 
static void make_sortkey(SORTPARAM *param,unsigned char *to, unsigned char *ref_pos);
 
42
static int write_keys(SORTPARAM *param,uchar * *sort_keys,
 
43
                      uint count, IO_CACHE *buffer_file, IO_CACHE *tempfile);
 
44
static void make_sortkey(SORTPARAM *param,uchar *to, uchar *ref_pos);
40
45
static void register_used_fields(SORTPARAM *param);
41
 
static int merge_index(SORTPARAM *param,unsigned char *sort_buffer,
 
46
static int merge_index(SORTPARAM *param,uchar *sort_buffer,
42
47
                       BUFFPEK *buffpek,
43
 
                       uint32_t maxbuffer,IO_CACHE *tempfile,
 
48
                       uint maxbuffer,IO_CACHE *tempfile,
44
49
                       IO_CACHE *outfile);
45
 
static bool save_index(SORTPARAM *param,unsigned char **sort_keys, uint32_t count, 
46
 
                       filesort_info_st *table_sort);
47
 
static uint32_t suffix_length(uint32_t string_length);
48
 
static uint32_t sortlength(THD *thd, SORT_FIELD *sortorder, uint32_t s_length,
 
50
static bool save_index(SORTPARAM *param,uchar **sort_keys, uint count, 
 
51
                       FILESORT_INFO *table_sort);
 
52
static uint suffix_length(ulong string_length);
 
53
static uint sortlength(THD *thd, SORT_FIELD *sortorder, uint s_length,
49
54
                       bool *multi_byte_charset);
50
55
static SORT_ADDON_FIELD *get_addon_fields(THD *thd, Field **ptabfield,
51
 
                                          uint32_t sortlength, uint32_t *plength);
 
56
                                          uint sortlength, uint *plength);
52
57
static void unpack_addon_fields(struct st_sort_addon_field *addon_field,
53
 
                                unsigned char *buff);
 
58
                                uchar *buff);
54
59
/**
55
60
  Sort a table.
56
61
  Creates a set of pointers that can be used to read the rows
70
75
  @param select         condition to apply to the rows
71
76
  @param max_rows       Return only this many rows
72
77
  @param sort_positions Set to 1 if we want to force sorting by position
73
 
                        (Needed by UPDATE/INSERT or ALTER Table)
 
78
                        (Needed by UPDATE/INSERT or ALTER TABLE)
74
79
  @param examined_rows  Store number of examined rows here
75
80
 
76
81
  @todo
87
92
    examined_rows       will be set to number of examined rows
88
93
*/
89
94
 
90
 
ha_rows filesort(THD *thd, Table *table, SORT_FIELD *sortorder, uint32_t s_length,
 
95
ha_rows filesort(THD *thd, TABLE *table, SORT_FIELD *sortorder, uint s_length,
91
96
                 SQL_SELECT *select, ha_rows max_rows,
92
97
                 bool sort_positions, ha_rows *examined_rows)
93
98
{
94
99
  int error;
95
 
  uint32_t memavl, min_sort_memory;
96
 
  uint32_t maxbuffer;
 
100
  ulong memavl, min_sort_memory;
 
101
  uint maxbuffer;
97
102
  BUFFPEK *buffpek;
98
103
  ha_rows records= HA_POS_ERROR;
99
 
  unsigned char **sort_keys= 0;
 
104
  uchar **sort_keys= 0;
100
105
  IO_CACHE tempfile, buffpek_pointers, *selected_records_file, *outfile; 
101
106
  SORTPARAM param;
102
107
  bool multi_byte_charset;
103
108
 
104
 
  filesort_info_st table_sort;
105
 
  TableList *tab= table->pos_in_table_list;
 
109
  FILESORT_INFO table_sort;
 
110
  TABLE_LIST *tab= table->pos_in_table_list;
106
111
  Item_subselect *subselect= tab ? tab->containing_subselect() : 0;
107
112
 
108
 
  DRIZZLE_FILESORT_START();
 
113
  MYSQL_FILESORT_START();
109
114
 
110
115
  /*
111
116
   Release InnoDB's adaptive hash index latch (if holding) before
118
123
    QUICK_INDEX_MERGE_SELECT. Work with a copy and put it back at the end 
119
124
    when index_merge select has finished with it.
120
125
  */
121
 
  memcpy(&table_sort, &table->sort, sizeof(filesort_info_st));
 
126
  memcpy(&table_sort, &table->sort, sizeof(FILESORT_INFO));
122
127
  table->sort.io_cache= NULL;
123
128
  
124
129
  outfile= table_sort.io_cache;
126
131
  my_b_clear(&buffpek_pointers);
127
132
  buffpek=0;
128
133
  error= 1;
129
 
  memset(&param, 0, sizeof(param));
 
134
  bzero((char*) &param,sizeof(param));
130
135
  param.sort_length= sortlength(thd, sortorder, s_length, &multi_byte_charset);
131
136
  param.ref_length= table->file->ref_length;
132
137
  param.addon_field= 0;
133
138
  param.addon_length= 0;
134
 
  if (!(table->file->ha_table_flags() & HA_FAST_KEY_READ) && !sort_positions)
 
139
  if (!(table->file->ha_table_flags() & HA_FAST_KEY_READ) &&
 
140
      !table->fulltext_searched && !sort_positions)
135
141
  {
136
142
    /* 
137
143
      Get the descriptors of all fields whose values are appended 
149
155
  if (param.addon_field)
150
156
  {
151
157
    param.res_length= param.addon_length;
152
 
    if (!(table_sort.addon_buf= (unsigned char *) my_malloc(param.addon_length,
 
158
    if (!(table_sort.addon_buf= (uchar *) my_malloc(param.addon_length,
153
159
                                                    MYF(MY_WME))))
154
160
      goto err;
155
161
  }
176
182
#ifdef CAN_TRUST_RANGE
177
183
  if (select && select->quick && select->quick->records > 0L)
178
184
  {
179
 
    records=cmin((ha_rows) (select->quick->records*2+EXTRA_RECORDS*2),
 
185
    records=min((ha_rows) (select->quick->records*2+EXTRA_RECORDS*2),
180
186
                table->file->stats.records)+EXTRA_RECORDS;
181
187
    selected_records_file=0;
182
188
  }
198
204
    goto err;
199
205
 
200
206
  memavl= thd->variables.sortbuff_size;
201
 
  min_sort_memory= cmax((uint32_t)MIN_SORT_MEMORY, param.sort_length*MERGEBUFF2);
 
207
  min_sort_memory= max(MIN_SORT_MEMORY, param.sort_length*MERGEBUFF2);
202
208
  while (memavl >= min_sort_memory)
203
209
  {
204
 
    uint32_t old_memavl;
205
 
    uint32_t keys= memavl/(param.rec_length+sizeof(char*));
206
 
    param.keys=(uint32_t) cmin(records+1, keys);
 
210
    ulong old_memavl;
 
211
    ulong keys= memavl/(param.rec_length+sizeof(char*));
 
212
    param.keys=(uint) min(records+1, keys);
207
213
    if ((table_sort.sort_keys=
208
 
         (unsigned char **) make_char_array((char **) table_sort.sort_keys,
 
214
         (uchar **) make_char_array((char **) table_sort.sort_keys,
209
215
                                    param.keys, param.rec_length, MYF(0))))
210
216
      break;
211
217
    old_memavl=memavl;
229
235
                             &tempfile, selected_records_file)) ==
230
236
      HA_POS_ERROR)
231
237
    goto err;
232
 
  maxbuffer= (uint32_t) (my_b_tell(&buffpek_pointers)/sizeof(*buffpek));
 
238
  maxbuffer= (uint) (my_b_tell(&buffpek_pointers)/sizeof(*buffpek));
233
239
 
234
240
  if (maxbuffer == 0)                   // The whole set is in memory
235
241
  {
236
 
    if (save_index(&param,sort_keys,(uint32_t) records, &table_sort))
 
242
    if (save_index(&param,sort_keys,(uint) records, &table_sort))
237
243
      goto err;
238
244
  }
239
245
  else
240
246
  {
241
247
    if (table_sort.buffpek && table_sort.buffpek_len < maxbuffer)
242
248
    {
243
 
      if (table_sort.buffpek)
244
 
        free(table_sort.buffpek);
 
249
      x_free(table_sort.buffpek);
245
250
      table_sort.buffpek= 0;
246
251
    }
247
252
    if (!(table_sort.buffpek=
248
 
          (unsigned char *) read_buffpek_from_file(&buffpek_pointers, maxbuffer,
 
253
          (uchar *) read_buffpek_from_file(&buffpek_pointers, maxbuffer,
249
254
                                 table_sort.buffpek)))
250
255
      goto err;
251
256
    buffpek= (BUFFPEK *) table_sort.buffpek;
266
271
    param.keys=((param.keys*(param.rec_length+sizeof(char*))) /
267
272
                param.rec_length-1);
268
273
    maxbuffer--;                                // Offset from 0
269
 
    if (merge_many_buff(&param,(unsigned char*) sort_keys,buffpek,&maxbuffer,
 
274
    if (merge_many_buff(&param,(uchar*) sort_keys,buffpek,&maxbuffer,
270
275
                        &tempfile))
271
276
      goto err;
272
277
    if (flush_io_cache(&tempfile) ||
273
278
        reinit_io_cache(&tempfile,READ_CACHE,0L,0,0))
274
279
      goto err;
275
 
    if (merge_index(&param,(unsigned char*) sort_keys,buffpek,maxbuffer,&tempfile,
 
280
    if (merge_index(&param,(uchar*) sort_keys,buffpek,maxbuffer,&tempfile,
276
281
                    outfile))
277
282
      goto err;
278
283
  }
282
287
 
283
288
 err:
284
289
  if (param.tmp_buffer)
285
 
    if (param.tmp_buffer)
286
 
      free(param.tmp_buffer);
 
290
    x_free(param.tmp_buffer);
287
291
  if (!subselect || !subselect->is_uncacheable())
288
292
  {
289
 
    if ((unsigned char*) sort_keys)
290
 
      free((unsigned char*) sort_keys);
 
293
    x_free((uchar*) sort_keys);
291
294
    table_sort.sort_keys= 0;
292
 
    if ((unsigned char*) buffpek)
293
 
      free((unsigned char*) buffpek);
 
295
    x_free((uchar*) buffpek);
294
296
    table_sort.buffpek= 0;
295
297
    table_sort.buffpek_len= 0;
296
298
  }
313
315
               MYF(ME_ERROR+ME_WAITTANG));
314
316
  else
315
317
    statistic_add(thd->status_var.filesort_rows,
316
 
                  (uint32_t) records, &LOCK_status);
 
318
                  (ulong) records, &LOCK_status);
317
319
  *examined_rows= param.examined_rows;
318
 
  memcpy(&table->sort, &table_sort, sizeof(filesort_info_st));
319
 
  DRIZZLE_FILESORT_END();
 
320
  memcpy(&table->sort, &table_sort, sizeof(FILESORT_INFO));
 
321
  MYSQL_FILESORT_END();
320
322
  return(error ? HA_POS_ERROR : records);
321
323
} /* filesort */
322
324
 
323
325
 
324
 
void filesort_free_buffers(Table *table, bool full)
 
326
void filesort_free_buffers(TABLE *table, bool full)
325
327
{
326
328
  if (table->sort.record_pointers)
327
329
  {
328
 
    free((unsigned char*) table->sort.record_pointers);
 
330
    my_free((uchar*) table->sort.record_pointers,MYF(0));
329
331
    table->sort.record_pointers=0;
330
332
  }
331
333
  if (full)
332
334
  {
333
335
    if (table->sort.sort_keys )
334
336
    {
335
 
      if ((unsigned char*) table->sort.sort_keys)
336
 
        free((unsigned char*) table->sort.sort_keys);
 
337
      x_free((uchar*) table->sort.sort_keys);
337
338
      table->sort.sort_keys= 0;
338
339
    }
339
340
    if (table->sort.buffpek)
340
341
    {
341
 
      if ((unsigned char*) table->sort.buffpek)
342
 
        free((unsigned char*) table->sort.buffpek);
 
342
      x_free((uchar*) table->sort.buffpek);
343
343
      table->sort.buffpek= 0;
344
344
      table->sort.buffpek_len= 0;
345
345
    }
346
346
  }
347
347
  if (table->sort.addon_buf)
348
348
  {
349
 
    free((char *) table->sort.addon_buf);
350
 
    free((char *) table->sort.addon_field);
 
349
    my_free((char *) table->sort.addon_buf, MYF(0));
 
350
    my_free((char *) table->sort.addon_field, MYF(MY_ALLOW_ZERO_PTR));
351
351
    table->sort.addon_buf=0;
352
352
    table->sort.addon_field=0;
353
353
  }
355
355
 
356
356
/** Make a array of string pointers. */
357
357
 
358
 
static char **make_char_array(char **old_pos, register uint32_t fields,
359
 
                              uint32_t length, myf my_flag)
 
358
static char **make_char_array(char **old_pos, register uint fields,
 
359
                              uint length, myf my_flag)
360
360
{
361
361
  register char **pos;
362
362
  char *char_pos;
363
363
 
364
364
  if (old_pos ||
365
 
      (old_pos= (char**) my_malloc((uint32_t) fields*(length+sizeof(char*)),
 
365
      (old_pos= (char**) my_malloc((uint) fields*(length+sizeof(char*)),
366
366
                                   my_flag)))
367
367
  {
368
368
    pos=old_pos; char_pos=((char*) (pos+fields)) -length;
375
375
 
376
376
/** Read 'count' number of buffer pointers into memory. */
377
377
 
378
 
static unsigned char *read_buffpek_from_file(IO_CACHE *buffpek_pointers, uint32_t count,
379
 
                                     unsigned char *buf)
 
378
static uchar *read_buffpek_from_file(IO_CACHE *buffpek_pointers, uint count,
 
379
                                     uchar *buf)
380
380
{
381
 
  uint32_t length= sizeof(BUFFPEK)*count;
382
 
  unsigned char *tmp= buf;
 
381
  ulong length= sizeof(BUFFPEK)*count;
 
382
  uchar *tmp= buf;
383
383
  if (count > UINT_MAX/sizeof(BUFFPEK))
384
384
    return 0; /* sizeof(BUFFPEK)*count will overflow */
385
385
  if (!tmp)
386
 
    tmp= (unsigned char *)my_malloc(length, MYF(MY_WME));
 
386
    tmp= (uchar *)my_malloc(length, MYF(MY_WME));
387
387
  if (tmp)
388
388
  {
389
389
    if (reinit_io_cache(buffpek_pointers,READ_CACHE,0L,0,0) ||
390
 
        my_b_read(buffpek_pointers, (unsigned char*) tmp, length))
 
390
        my_b_read(buffpek_pointers, (uchar*) tmp, length))
391
391
    {
392
 
      free((char*) tmp);
 
392
      my_free((char*) tmp, MYF(0));
393
393
      tmp=0;
394
394
    }
395
395
  }
435
435
*/
436
436
 
437
437
static ha_rows find_all_keys(SORTPARAM *param, SQL_SELECT *select,
438
 
                             unsigned char **sort_keys,
 
438
                             uchar **sort_keys,
439
439
                             IO_CACHE *buffpek_pointers,
440
440
                             IO_CACHE *tempfile, IO_CACHE *indexfile)
441
441
{
442
442
  int error,flag,quick_select;
443
 
  uint32_t idx,indexpos,ref_length;
444
 
  unsigned char *ref_pos,*next_pos,ref_buff[MAX_REFLENGTH];
 
443
  uint idx,indexpos,ref_length;
 
444
  uchar *ref_pos,*next_pos,ref_buff[MAX_REFLENGTH];
445
445
  my_off_t record;
446
 
  Table *sort_form;
 
446
  TABLE *sort_form;
447
447
  THD *thd= current_thd;
448
448
  volatile THD::killed_state *killed= &thd->killed;
449
449
  handler *file;
464
464
  next_pos=ref_pos;
465
465
  if (! indexfile && ! quick_select)
466
466
  {
467
 
    next_pos=(unsigned char*) 0;                        /* Find records in sequence */
 
467
    next_pos=(uchar*) 0;                        /* Find records in sequence */
468
468
    file->ha_rnd_init(1);
469
469
    file->extra_opt(HA_EXTRA_CACHE,
470
470
                    current_thd->variables.read_buff_size);
489
489
  register_used_fields(param);
490
490
  if (select && select->cond)
491
491
    select->cond->walk(&Item::register_field_in_read_map, 1,
492
 
                       (unsigned char*) sort_form);
 
492
                       (uchar*) sort_form);
493
493
  sort_form->column_bitmaps_set(&sort_form->tmp_set, &sort_form->tmp_set);
494
494
 
495
495
  for (;;)
507
507
    {
508
508
      if (indexfile)
509
509
      {
510
 
        if (my_b_read(indexfile,(unsigned char*) ref_pos,ref_length)) /* purecov: deadcode */
 
510
        if (my_b_read(indexfile,(uchar*) ref_pos,ref_length)) /* purecov: deadcode */
511
511
        {
512
512
          error= my_errno ? my_errno : -1;              /* Abort */
513
513
          break;
517
517
      else
518
518
      {
519
519
        error=file->rnd_next(sort_form->record[0]);
520
 
        if (!error)
521
 
          update_virtual_fields_marked_for_write(sort_form);
522
 
 
523
520
        if (!flag)
524
521
        {
525
522
          my_store_ptr(ref_pos,ref_length,record); // Position to row
618
615
*/
619
616
 
620
617
static int
621
 
write_keys(SORTPARAM *param, register unsigned char **sort_keys, uint32_t count,
 
618
write_keys(SORTPARAM *param, register uchar **sort_keys, uint count,
622
619
           IO_CACHE *buffpek_pointers, IO_CACHE *tempfile)
623
620
{
624
621
  size_t sort_length, rec_length;
625
 
  unsigned char **end;
 
622
  uchar **end;
626
623
  BUFFPEK buffpek;
627
624
 
628
625
  sort_length= param->sort_length;
629
626
  rec_length= param->rec_length;
630
 
  my_string_ptr_sort((unsigned char*) sort_keys, (uint32_t) count, sort_length);
 
627
  my_string_ptr_sort((uchar*) sort_keys, (uint) count, sort_length);
631
628
  if (!my_b_inited(tempfile) &&
632
629
      open_cached_file(tempfile, mysql_tmpdir, TEMP_PREFIX, DISK_BUFFER_SIZE,
633
630
                       MYF(MY_WME)))
634
631
    goto err;                                   /* purecov: inspected */
635
632
  /* check we won't have more buffpeks than we can possibly keep in memory */
636
 
  if (my_b_tell(buffpek_pointers) + sizeof(BUFFPEK) > (uint64_t)UINT_MAX)
 
633
  if (my_b_tell(buffpek_pointers) + sizeof(BUFFPEK) > (ulonglong)UINT_MAX)
637
634
    goto err;
638
635
  buffpek.file_pos= my_b_tell(tempfile);
639
636
  if ((ha_rows) count > param->max_rows)
640
 
    count=(uint32_t) param->max_rows;               /* purecov: inspected */
 
637
    count=(uint) param->max_rows;               /* purecov: inspected */
641
638
  buffpek.count=(ha_rows) count;
642
639
  for (end=sort_keys+count ; sort_keys != end ; sort_keys++)
643
 
    if (my_b_write(tempfile, (unsigned char*) *sort_keys, (uint32_t) rec_length))
 
640
    if (my_b_write(tempfile, (uchar*) *sort_keys, (uint) rec_length))
644
641
      goto err;
645
 
  if (my_b_write(buffpek_pointers, (unsigned char*) &buffpek, sizeof(buffpek)))
 
642
  if (my_b_write(buffpek_pointers, (uchar*) &buffpek, sizeof(buffpek)))
646
643
    goto err;
647
644
  return(0);
648
645
 
655
652
  Store length as suffix in high-byte-first order.
656
653
*/
657
654
 
658
 
static inline void store_length(unsigned char *to, uint32_t length, uint32_t pack_length)
 
655
static inline void store_length(uchar *to, uint length, uint pack_length)
659
656
{
660
657
  switch (pack_length) {
661
658
  case 1:
662
 
    *to= (unsigned char) length;
 
659
    *to= (uchar) length;
663
660
    break;
664
661
  case 2:
665
662
    mi_int2store(to, length);
677
674
/** Make a sort-key from record. */
678
675
 
679
676
static void make_sortkey(register SORTPARAM *param,
680
 
                         register unsigned char *to, unsigned char *ref_pos)
 
677
                         register uchar *to, uchar *ref_pos)
681
678
{
682
679
  register Field *field;
683
680
  register SORT_FIELD *sort_field;
684
 
  register uint32_t length;
 
681
  register uint length;
685
682
 
686
683
  for (sort_field=param->local_sortorder ;
687
684
       sort_field != param->end ;
695
692
        if (field->is_null())
696
693
        {
697
694
          if (sort_field->reverse)
698
 
            memset(to, 255, sort_field->length+1);
 
695
            bfill(to,sort_field->length+1,(char) 255);
699
696
          else
700
 
            memset(to, 0, sort_field->length+1);
 
697
            bzero((char*) to,sort_field->length+1);
701
698
          to+= sort_field->length+1;
702
699
          continue;
703
700
        }
713
710
      switch (sort_field->result_type) {
714
711
      case STRING_RESULT:
715
712
      {
716
 
        const CHARSET_INFO * const cs=item->collation.collation;
 
713
        CHARSET_INFO *cs=item->collation.collation;
717
714
        char fill_char= ((cs->state & MY_CS_BINSORT) ? (char) 0 : ' ');
718
715
        int diff;
719
 
        uint32_t sort_field_length;
 
716
        uint sort_field_length;
720
717
 
721
718
        if (maybe_null)
722
719
          *to++=1;
726
723
        if (!res)
727
724
        {
728
725
          if (maybe_null)
729
 
            memset(to-1, 0, sort_field->length+1);
 
726
            bzero((char*) to-1,sort_field->length+1);
730
727
          else
731
728
          {
732
729
            /* purecov: begin deadcode */
736
733
              This code is here mainly to avoid a hard crash in this case.
737
734
            */
738
735
            assert(0);
739
 
            memset(to, 0, sort_field->length);  // Avoid crash
 
736
            bzero((char*) to,sort_field->length);       // Avoid crash
740
737
            /* purecov: end */
741
738
          }
742
739
          break;
758
755
        if (sort_field->need_strxnfrm)
759
756
        {
760
757
          char *from=(char*) res->ptr();
761
 
          uint32_t tmp_length;
762
 
          if ((unsigned char*) from == to)
 
758
          uint tmp_length;
 
759
          if ((uchar*) from == to)
763
760
          {
764
761
            set_if_smaller(length,sort_field->length);
765
762
            memcpy(param->tmp_buffer,from,length);
766
763
            from=param->tmp_buffer;
767
764
          }
768
765
          tmp_length= my_strnxfrm(cs,to,sort_field->length,
769
 
                                  (unsigned char*) from, length);
 
766
                                  (uchar*) from, length);
770
767
          assert(tmp_length == sort_field->length);
771
768
        }
772
769
        else
773
770
        {
774
 
          my_strnxfrm(cs,(unsigned char*)to,length,(const unsigned char*)res->ptr(),length);
 
771
          my_strnxfrm(cs,(uchar*)to,length,(const uchar*)res->ptr(),length);
775
772
          cs->cset->fill(cs, (char *)to+length,diff,fill_char);
776
773
        }
777
774
        break;
778
775
      }
779
776
      case INT_RESULT:
780
777
        {
781
 
          int64_t value= item->val_int_result();
 
778
          longlong value= item->val_int_result();
782
779
          if (maybe_null)
783
780
          {
784
781
            *to++=1;                            /* purecov: inspected */
785
782
            if (item->null_value)
786
783
            {
787
784
              if (maybe_null)
788
 
                memset(to-1, 0, sort_field->length+1);
 
785
                bzero((char*) to-1,sort_field->length+1);
789
786
              else
790
787
              {
791
 
                memset(to, 0, sort_field->length);
 
788
                bzero((char*) to,sort_field->length);
792
789
              }
793
790
              break;
794
791
            }
795
792
          }
796
 
          to[7]= (unsigned char) value;
797
 
          to[6]= (unsigned char) (value >> 8);
798
 
          to[5]= (unsigned char) (value >> 16);
799
 
          to[4]= (unsigned char) (value >> 24);
800
 
          to[3]= (unsigned char) (value >> 32);
801
 
          to[2]= (unsigned char) (value >> 40);
802
 
          to[1]= (unsigned char) (value >> 48);
803
 
          if (item->unsigned_flag)                    /* Fix sign */
804
 
            to[0]= (unsigned char) (value >> 56);
805
 
          else
806
 
            to[0]= (unsigned char) (value >> 56) ^ 128; /* Reverse signbit */
 
793
#if SIZEOF_LONG_LONG > 4
 
794
          to[7]= (uchar) value;
 
795
          to[6]= (uchar) (value >> 8);
 
796
          to[5]= (uchar) (value >> 16);
 
797
          to[4]= (uchar) (value >> 24);
 
798
          to[3]= (uchar) (value >> 32);
 
799
          to[2]= (uchar) (value >> 40);
 
800
          to[1]= (uchar) (value >> 48);
 
801
          if (item->unsigned_flag)                    /* Fix sign */
 
802
            to[0]= (uchar) (value >> 56);
 
803
          else
 
804
            to[0]= (uchar) (value >> 56) ^ 128; /* Reverse signbit */
 
805
#else
 
806
          to[3]= (uchar) value;
 
807
          to[2]= (uchar) (value >> 8);
 
808
          to[1]= (uchar) (value >> 16);
 
809
          if (item->unsigned_flag)                    /* Fix sign */
 
810
            to[0]= (uchar) (value >> 24);
 
811
          else
 
812
            to[0]= (uchar) (value >> 24) ^ 128; /* Reverse signbit */
 
813
#endif
807
814
          break;
808
815
        }
809
816
      case DECIMAL_RESULT:
813
820
          {
814
821
            if (item->null_value)
815
822
            { 
816
 
              memset(to, 0, sort_field->length+1);
 
823
              bzero((char*)to, sort_field->length+1);
817
824
              to++;
818
825
              break;
819
826
            }
831
838
          {
832
839
            if (item->null_value)
833
840
            {
834
 
              memset(to, 0, sort_field->length+1);
 
841
              bzero((char*) to,sort_field->length+1);
835
842
              to++;
836
843
              break;
837
844
            }
838
845
            *to++=1;
839
846
          }
840
 
          change_double_for_sort(value,(unsigned char*) to);
 
847
          change_double_for_sort(value,(uchar*) to);
841
848
          break;
842
849
        }
843
850
      case ROW_RESULT:
854
861
      length=sort_field->length;
855
862
      while (length--)
856
863
      {
857
 
        *to = (unsigned char) (~ *to);
 
864
        *to = (uchar) (~ *to);
858
865
        to++;
859
866
      }
860
867
    }
871
878
      the same for all records.
872
879
    */
873
880
    SORT_ADDON_FIELD *addonf= param->addon_field;
874
 
    unsigned char *nulls= to;
 
881
    uchar *nulls= to;
875
882
    assert(addonf != 0);
876
 
    memset(nulls, 0, addonf->offset);
 
883
    bzero((char *) nulls, addonf->offset);
877
884
    to+= addonf->offset;
878
885
    for ( ; (field= addonf->field) ; addonf++)
879
886
    {
881
888
      {
882
889
        nulls[addonf->null_offset]|= addonf->null_bit;
883
890
#ifdef HAVE_purify
884
 
        memset(to, 0, addonf->length);
 
891
        bzero(to, addonf->length);
885
892
#endif
886
893
      }
887
894
      else
888
895
      {
889
896
#ifdef HAVE_purify
890
 
        unsigned char *end= field->pack(to, field->ptr);
891
 
        uint32_t length= (uint32_t) ((to + addonf->length) - end);
 
897
        uchar *end= field->pack(to, field->ptr);
 
898
        uint length= (uint) ((to + addonf->length) - end);
892
899
        assert((int) length >= 0);
893
900
        if (length)
894
 
          memset(end, 0, length);
 
901
          bzero(end, length);
895
902
#else
896
903
        (void) field->pack(to, field->ptr);
897
904
#endif
902
909
  else
903
910
  {
904
911
    /* Save filepos last */
905
 
    memcpy(to, ref_pos, (size_t) param->ref_length);
 
912
    memcpy((uchar*) to, ref_pos, (size_t) param->ref_length);
906
913
  }
907
914
  return;
908
915
}
915
922
static void register_used_fields(SORTPARAM *param)
916
923
{
917
924
  register SORT_FIELD *sort_field;
918
 
  Table *table=param->sort_form;
 
925
  TABLE *table=param->sort_form;
919
926
  MY_BITMAP *bitmap= table->read_set;
920
927
 
921
928
  for (sort_field= param->local_sortorder ;
931
938
    else
932
939
    {                                           // Item
933
940
      sort_field->item->walk(&Item::register_field_in_read_map, 1,
934
 
                             (unsigned char *) table);
 
941
                             (uchar *) table);
935
942
    }
936
943
  }
937
944
 
950
957
}
951
958
 
952
959
 
953
 
static bool save_index(SORTPARAM *param, unsigned char **sort_keys, uint32_t count, 
954
 
                       filesort_info_st *table_sort)
 
960
static bool save_index(SORTPARAM *param, uchar **sort_keys, uint count, 
 
961
                       FILESORT_INFO *table_sort)
955
962
{
956
 
  uint32_t offset,res_length;
957
 
  unsigned char *to;
 
963
  uint offset,res_length;
 
964
  uchar *to;
958
965
 
959
 
  my_string_ptr_sort((unsigned char*) sort_keys, (uint32_t) count, param->sort_length);
 
966
  my_string_ptr_sort((uchar*) sort_keys, (uint) count, param->sort_length);
960
967
  res_length= param->res_length;
961
968
  offset= param->rec_length-res_length;
962
969
  if ((ha_rows) count > param->max_rows)
963
 
    count=(uint32_t) param->max_rows;
 
970
    count=(uint) param->max_rows;
964
971
  if (!(to= table_sort->record_pointers= 
965
 
        (unsigned char*) my_malloc(res_length*count, MYF(MY_WME))))
 
972
        (uchar*) my_malloc(res_length*count, MYF(MY_WME))))
966
973
    return(1);                 /* purecov: inspected */
967
 
  for (unsigned char **end= sort_keys+count ; sort_keys != end ; sort_keys++)
 
974
  for (uchar **end= sort_keys+count ; sort_keys != end ; sort_keys++)
968
975
  {
969
976
    memcpy(to, *sort_keys+offset, res_length);
970
977
    to+= res_length;
975
982
 
976
983
/** Merge buffers to make < MERGEBUFF2 buffers. */
977
984
 
978
 
int merge_many_buff(SORTPARAM *param, unsigned char *sort_buffer,
979
 
                    BUFFPEK *buffpek, uint32_t *maxbuffer, IO_CACHE *t_file)
 
985
int merge_many_buff(SORTPARAM *param, uchar *sort_buffer,
 
986
                    BUFFPEK *buffpek, uint *maxbuffer, IO_CACHE *t_file)
980
987
{
981
 
  register uint32_t i;
 
988
  register uint i;
982
989
  IO_CACHE t_file2,*from_file,*to_file,*temp;
983
990
  BUFFPEK *lastbuff;
984
991
 
1011
1018
    temp=from_file; from_file=to_file; to_file=temp;
1012
1019
    setup_io_cache(from_file);
1013
1020
    setup_io_cache(to_file);
1014
 
    *maxbuffer= (uint32_t) (lastbuff-buffpek)-1;
 
1021
    *maxbuffer= (uint) (lastbuff-buffpek)-1;
1015
1022
  }
1016
1023
cleanup:
1017
1024
  close_cached_file(to_file);                   // This holds old result
1029
1036
  Read data to buffer.
1030
1037
 
1031
1038
  @retval
1032
 
    (uint32_t)-1 if something goes wrong
 
1039
    (uint)-1 if something goes wrong
1033
1040
*/
1034
1041
 
1035
 
uint32_t read_to_buffer(IO_CACHE *fromfile, BUFFPEK *buffpek,
1036
 
                    uint32_t rec_length)
 
1042
uint read_to_buffer(IO_CACHE *fromfile, BUFFPEK *buffpek,
 
1043
                    uint rec_length)
1037
1044
{
1038
 
  register uint32_t count;
1039
 
  uint32_t length;
 
1045
  register uint count;
 
1046
  uint length;
1040
1047
 
1041
 
  if ((count=(uint32_t) cmin((ha_rows) buffpek->max_keys,buffpek->count)))
 
1048
  if ((count=(uint) min((ha_rows) buffpek->max_keys,buffpek->count)))
1042
1049
  {
1043
 
    if (pread(fromfile->file,(unsigned char*) buffpek->base, (length= rec_length*count),buffpek->file_pos) == 0)
1044
 
      return((uint32_t) -1);                    /* purecov: inspected */
 
1050
    if (pread(fromfile->file,(uchar*) buffpek->base, (length= rec_length*count),buffpek->file_pos) == 0)
 
1051
      return((uint) -1);                        /* purecov: inspected */
1045
1052
    buffpek->key=buffpek->base;
1046
1053
    buffpek->file_pos+= length;                 /* New filepos */
1047
1054
    buffpek->count-=    count;
1062
1069
  @param[in] key_length key length
1063
1070
*/
1064
1071
 
1065
 
void reuse_freed_buff(QUEUE *queue, BUFFPEK *reuse, uint32_t key_length)
 
1072
void reuse_freed_buff(QUEUE *queue, BUFFPEK *reuse, uint key_length)
1066
1073
{
1067
 
  unsigned char *reuse_end= reuse->base + reuse->max_keys * key_length;
1068
 
  for (uint32_t i= 0; i < queue->elements; ++i)
 
1074
  uchar *reuse_end= reuse->base + reuse->max_keys * key_length;
 
1075
  for (uint i= 0; i < queue->elements; ++i)
1069
1076
  {
1070
1077
    BUFFPEK *bp= (BUFFPEK *) queue_element(queue, i);
1071
1078
    if (bp->base + bp->max_keys * key_length == reuse->base)
1103
1110
*/
1104
1111
 
1105
1112
int merge_buffers(SORTPARAM *param, IO_CACHE *from_file,
1106
 
                  IO_CACHE *to_file, unsigned char *sort_buffer,
 
1113
                  IO_CACHE *to_file, uchar *sort_buffer,
1107
1114
                  BUFFPEK *lastbuff, BUFFPEK *Fb, BUFFPEK *Tb,
1108
1115
                  int flag)
1109
1116
{
1110
1117
  int error;
1111
 
  uint32_t rec_length,res_length,offset;
 
1118
  uint rec_length,res_length,offset;
1112
1119
  size_t sort_length;
1113
 
  uint32_t maxcount;
 
1120
  ulong maxcount;
1114
1121
  ha_rows max_rows,org_max_rows;
1115
1122
  my_off_t to_start_filepos;
1116
 
  unsigned char *strpos;
 
1123
  uchar *strpos;
1117
1124
  BUFFPEK *buffpek;
1118
1125
  QUEUE queue;
1119
1126
  qsort2_cmp cmp;
1133
1140
  res_length= param->res_length;
1134
1141
  sort_length= param->sort_length;
1135
1142
  offset= rec_length-res_length;
1136
 
  maxcount= (uint32_t) (param->keys/((uint32_t) (Tb-Fb) +1));
 
1143
  maxcount= (ulong) (param->keys/((uint) (Tb-Fb) +1));
1137
1144
  to_start_filepos= my_b_tell(to_file);
1138
 
  strpos= (unsigned char*) sort_buffer;
 
1145
  strpos= (uchar*) sort_buffer;
1139
1146
  org_max_rows=max_rows= param->max_rows;
1140
1147
 
1141
1148
  /* The following will fire if there is not enough space in sort_buffer */
1151
1158
    cmp= get_ptr_compare(sort_length);
1152
1159
    first_cmp_arg= (void*) &sort_length;
1153
1160
  }
1154
 
  if (init_queue(&queue, (uint32_t) (Tb-Fb)+1, offsetof(BUFFPEK,key), 0,
 
1161
  if (init_queue(&queue, (uint) (Tb-Fb)+1, offsetof(BUFFPEK,key), 0,
1155
1162
                 (queue_compare) cmp, first_cmp_arg))
1156
1163
    return(1);                                /* purecov: inspected */
1157
1164
  for (buffpek= Fb ; buffpek <= Tb ; buffpek++)
1158
1165
  {
1159
1166
    buffpek->base= strpos;
1160
1167
    buffpek->max_keys= maxcount;
1161
 
    strpos+= (uint32_t) (error= (int) read_to_buffer(from_file, buffpek,
 
1168
    strpos+= (uint) (error= (int) read_to_buffer(from_file, buffpek,
1162
1169
                                                                         rec_length));
1163
1170
    if (error == -1)
1164
1171
      goto err;                                 /* purecov: inspected */
1165
1172
    buffpek->max_keys= buffpek->mem_count;      // If less data in buffers than expected
1166
 
    queue_insert(&queue, (unsigned char*) buffpek);
 
1173
    queue_insert(&queue, (uchar*) buffpek);
1167
1174
  }
1168
1175
 
1169
1176
  if (param->unique_buff)
1178
1185
    */
1179
1186
    buffpek= (BUFFPEK*) queue_top(&queue);
1180
1187
    memcpy(param->unique_buff, buffpek->key, rec_length);
1181
 
    if (my_b_write(to_file, (unsigned char*) buffpek->key, rec_length))
 
1188
    if (my_b_write(to_file, (uchar*) buffpek->key, rec_length))
1182
1189
    {
1183
1190
      error=1; goto err;                        /* purecov: inspected */
1184
1191
    }
1206
1213
      if (cmp)                                        // Remove duplicates
1207
1214
      {
1208
1215
        if (!(*cmp)(first_cmp_arg, &(param->unique_buff),
1209
 
                    (unsigned char**) &buffpek->key))
 
1216
                    (uchar**) &buffpek->key))
1210
1217
              goto skip_duplicate;
1211
 
            memcpy(param->unique_buff, buffpek->key, rec_length);
 
1218
            memcpy(param->unique_buff, (uchar*) buffpek->key, rec_length);
1212
1219
      }
1213
1220
      if (flag == 0)
1214
1221
      {
1215
 
        if (my_b_write(to_file,(unsigned char*) buffpek->key, rec_length))
 
1222
        if (my_b_write(to_file,(uchar*) buffpek->key, rec_length))
1216
1223
        {
1217
1224
          error=1; goto err;                        /* purecov: inspected */
1218
1225
        }
1219
1226
      }
1220
1227
      else
1221
1228
      {
1222
 
        if (my_b_write(to_file, (unsigned char*) buffpek->key+offset, res_length))
 
1229
        if (my_b_write(to_file, (uchar*) buffpek->key+offset, res_length))
1223
1230
        {
1224
1231
          error=1; goto err;                        /* purecov: inspected */
1225
1232
        }
1237
1244
        if (!(error= (int) read_to_buffer(from_file,buffpek,
1238
1245
                                          rec_length)))
1239
1246
        {
1240
 
          queue_remove(&queue,0);
 
1247
          VOID(queue_remove(&queue,0));
1241
1248
          reuse_freed_buff(&queue, buffpek, rec_length);
1242
1249
          break;                        /* One buffer have been removed */
1243
1250
        }
1257
1264
  */
1258
1265
  if (cmp)
1259
1266
  {
1260
 
    if (!(*cmp)(first_cmp_arg, &(param->unique_buff), (unsigned char**) &buffpek->key))
 
1267
    if (!(*cmp)(first_cmp_arg, &(param->unique_buff), (uchar**) &buffpek->key))
1261
1268
    {
1262
1269
      buffpek->key+= rec_length;         // Remove duplicate
1263
1270
      --buffpek->mem_count;
1268
1275
  {
1269
1276
    if ((ha_rows) buffpek->mem_count > max_rows)
1270
1277
    {                                        /* Don't write too many records */
1271
 
      buffpek->mem_count= (uint32_t) max_rows;
 
1278
      buffpek->mem_count= (uint) max_rows;
1272
1279
      buffpek->count= 0;                        /* Don't read more */
1273
1280
    }
1274
1281
    max_rows-= buffpek->mem_count;
1275
1282
    if (flag == 0)
1276
1283
    {
1277
 
      if (my_b_write(to_file,(unsigned char*) buffpek->key,
 
1284
      if (my_b_write(to_file,(uchar*) buffpek->key,
1278
1285
                     (rec_length*buffpek->mem_count)))
1279
1286
      {
1280
1287
        error= 1; goto err;                        /* purecov: inspected */
1282
1289
    }
1283
1290
    else
1284
1291
    {
1285
 
      register unsigned char *end;
 
1292
      register uchar *end;
1286
1293
      strpos= buffpek->key+offset;
1287
1294
      for (end= strpos+buffpek->mem_count*rec_length ;
1288
1295
           strpos != end ;
1289
1296
           strpos+= rec_length)
1290
1297
      {     
1291
 
        if (my_b_write(to_file, (unsigned char *) strpos, res_length))
 
1298
        if (my_b_write(to_file, (uchar *) strpos, res_length))
1292
1299
        {
1293
1300
          error=1; goto err;                        
1294
1301
        }
1299
1306
         != -1 && error != 0);
1300
1307
 
1301
1308
end:
1302
 
  lastbuff->count= cmin(org_max_rows-max_rows, param->max_rows);
 
1309
  lastbuff->count= min(org_max_rows-max_rows, param->max_rows);
1303
1310
  lastbuff->file_pos= to_start_filepos;
1304
1311
err:
1305
1312
  delete_queue(&queue);
1309
1316
 
1310
1317
        /* Do a merge to output-file (save only positions) */
1311
1318
 
1312
 
static int merge_index(SORTPARAM *param, unsigned char *sort_buffer,
1313
 
                       BUFFPEK *buffpek, uint32_t maxbuffer,
 
1319
static int merge_index(SORTPARAM *param, uchar *sort_buffer,
 
1320
                       BUFFPEK *buffpek, uint maxbuffer,
1314
1321
                       IO_CACHE *tempfile, IO_CACHE *outfile)
1315
1322
{
1316
1323
  if (merge_buffers(param,tempfile,outfile,sort_buffer,buffpek,buffpek,
1320
1327
} /* merge_index */
1321
1328
 
1322
1329
 
1323
 
static uint32_t suffix_length(uint32_t string_length)
 
1330
static uint suffix_length(ulong string_length)
1324
1331
{
1325
1332
  if (string_length < 256)
1326
1333
    return 1;
1351
1358
    Total length of sort buffer in bytes
1352
1359
*/
1353
1360
 
1354
 
static uint32_t
1355
 
sortlength(THD *thd, SORT_FIELD *sortorder, uint32_t s_length,
 
1361
static uint
 
1362
sortlength(THD *thd, SORT_FIELD *sortorder, uint s_length,
1356
1363
           bool *multi_byte_charset)
1357
1364
{
1358
 
  register uint32_t length;
1359
 
  const CHARSET_INFO *cs;
 
1365
  register uint length;
 
1366
  CHARSET_INFO *cs;
1360
1367
  *multi_byte_charset= 0;
1361
1368
 
1362
1369
  length=0;
1381
1388
    else
1382
1389
    {
1383
1390
      sortorder->result_type= sortorder->item->result_type();
1384
 
      if (sortorder->item->result_as_int64_t())
 
1391
      if (sortorder->item->result_as_longlong())
1385
1392
        sortorder->result_type= INT_RESULT;
1386
1393
      switch (sortorder->result_type) {
1387
1394
      case STRING_RESULT:
1401
1408
        }
1402
1409
        break;
1403
1410
      case INT_RESULT:
1404
 
        sortorder->length=8;                    // Size of intern int64_t
 
1411
#if SIZEOF_LONG_LONG > 4
 
1412
        sortorder->length=8;                    // Size of intern longlong
 
1413
#else
 
1414
        sortorder->length=4;
 
1415
#endif
1405
1416
        break;
1406
1417
      case DECIMAL_RESULT:
1407
1418
        sortorder->length=
1457
1468
*/
1458
1469
 
1459
1470
static SORT_ADDON_FIELD *
1460
 
get_addon_fields(THD *thd, Field **ptabfield, uint32_t sortlength, uint32_t *plength)
 
1471
get_addon_fields(THD *thd, Field **ptabfield, uint sortlength, uint *plength)
1461
1472
{
1462
1473
  Field **pfield;
1463
1474
  Field *field;
1464
1475
  SORT_ADDON_FIELD *addonf;
1465
 
  uint32_t length= 0;
1466
 
  uint32_t fields= 0;
1467
 
  uint32_t null_fields= 0;
 
1476
  uint length= 0;
 
1477
  uint fields= 0;
 
1478
  uint null_fields= 0;
1468
1479
  MY_BITMAP *read_set= (*ptabfield)->table->read_set;
1469
1480
 
1470
1481
  /*
1544
1555
*/
1545
1556
 
1546
1557
static void 
1547
 
unpack_addon_fields(struct st_sort_addon_field *addon_field, unsigned char *buff)
 
1558
unpack_addon_fields(struct st_sort_addon_field *addon_field, uchar *buff)
1548
1559
{
1549
1560
  Field *field;
1550
1561
  SORT_ADDON_FIELD *addonf= addon_field;
1568
1579
 
1569
1580
#define DBL_EXP_DIG (sizeof(double)*8-DBL_MANT_DIG)
1570
1581
 
1571
 
void change_double_for_sort(double nr,unsigned char *to)
 
1582
void change_double_for_sort(double nr,uchar *to)
1572
1583
{
1573
 
  unsigned char *tmp=(unsigned char*) to;
 
1584
  uchar *tmp=(uchar*) to;
1574
1585
  if (nr == 0.0)
1575
1586
  {                                             /* Change to zero string */
1576
 
    tmp[0]=(unsigned char) 128;
1577
 
    memset(tmp+1, 0, sizeof(nr)-1);
 
1587
    tmp[0]=(uchar) 128;
 
1588
    bzero((char*) tmp+1,sizeof(nr)-1);
1578
1589
  }
1579
1590
  else
1580
1591
  {
1581
1592
#ifdef WORDS_BIGENDIAN
1582
 
    memcpy(tmp,&nr,sizeof(nr));
 
1593
    memcpy_fixed(tmp,&nr,sizeof(nr));
1583
1594
#else
1584
1595
    {
1585
 
      unsigned char *ptr= (unsigned char*) &nr;
 
1596
      uchar *ptr= (uchar*) &nr;
1586
1597
#if defined(__FLOAT_WORD_ORDER) && (__FLOAT_WORD_ORDER == __BIG_ENDIAN)
1587
1598
      tmp[0]= ptr[3]; tmp[1]=ptr[2]; tmp[2]= ptr[1]; tmp[3]=ptr[0];
1588
1599
      tmp[4]= ptr[7]; tmp[5]=ptr[6]; tmp[6]= ptr[5]; tmp[7]=ptr[4];
1594
1605
#endif
1595
1606
    if (tmp[0] & 128)                           /* Negative */
1596
1607
    {                                           /* make complement */
1597
 
      uint32_t i;
 
1608
      uint i;
1598
1609
      for (i=0 ; i < sizeof(nr); i++)
1599
 
        tmp[i]=tmp[i] ^ (unsigned char) 255;
 
1610
        tmp[i]=tmp[i] ^ (uchar) 255;
1600
1611
    }
1601
1612
    else
1602
1613
    {                                   /* Set high and move exponent one up */
1603
 
      uint16_t exp_part=(((uint16_t) tmp[0] << 8) | (uint16_t) tmp[1] |
1604
 
                       (uint16_t) 32768);
1605
 
      exp_part+= (uint16_t) 1 << (16-1-DBL_EXP_DIG);
1606
 
      tmp[0]= (unsigned char) (exp_part >> 8);
1607
 
      tmp[1]= (unsigned char) exp_part;
 
1614
      ushort exp_part=(((ushort) tmp[0] << 8) | (ushort) tmp[1] |
 
1615
                       (ushort) 32768);
 
1616
      exp_part+= (ushort) 1 << (16-1-DBL_EXP_DIG);
 
1617
      tmp[0]= (uchar) (exp_part >> 8);
 
1618
      tmp[1]= (uchar) exp_part;
1608
1619
    }
1609
1620
  }
1610
1621
}